@commercetools-frontend/sdk
Version:
Tools for declarative fetching
67 lines (56 loc) • 3.29 kB
JavaScript
;
Object.defineProperty(exports, '__esModule', { value: true });
var _JSON$stringify = require('@babel/runtime-corejs3/core-js-stable/json/stringify');
var _Array$isArray = require('@babel/runtime-corejs3/core-js-stable/array/is-array');
var _findIndexInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/find-index');
var _concatInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/concat');
var _spliceInstanceProperty = require('@babel/runtime-corejs3/core-js-stable/instance/splice');
var _Promise = require('@babel/runtime-corejs3/core-js-stable/promise');
var fastEquals = require('fast-equals');
var uuid = require('uuid');
function _interopDefault (e) { return e && e.__esModule ? e : { 'default': e }; }
var _JSON$stringify__default = /*#__PURE__*/_interopDefault(_JSON$stringify);
var _Array$isArray__default = /*#__PURE__*/_interopDefault(_Array$isArray);
var _findIndexInstanceProperty__default = /*#__PURE__*/_interopDefault(_findIndexInstanceProperty);
var _concatInstanceProperty__default = /*#__PURE__*/_interopDefault(_concatInstanceProperty);
var _spliceInstanceProperty__default = /*#__PURE__*/_interopDefault(_spliceInstanceProperty);
var _Promise__default = /*#__PURE__*/_interopDefault(_Promise);
/**
* START -->
* Remove once `@commercetools/sdk-client` exposes proper types.
*/
/* <-- END */
const serialize = data => {
const undefinedPlaceholder = uuid.v4();
const placeholderRegexp = new RegExp("\"".concat(undefinedPlaceholder, "\""), 'g');
const mapUndefinedValues = (_k, v) => v === undefined ? undefinedPlaceholder : v;
const withPlaceholders = _JSON$stringify__default["default"](data, mapUndefinedValues, 2);
return withPlaceholders.replace(placeholderRegexp, 'undefined');
};
const throwIfNoMocksArePassed = mocks => {
if (!mocks || !_Array$isArray__default["default"](mocks) || mocks.length === 0) {
throw new Error('Missing or invalid argument for `mocks`. Expected an array of mocked actions.');
}
};
const isSdkAction = action => action.type === 'SDK';
const isSdkMockSuccess = mock => mock.response !== undefined;
const createTestMiddleware = mocks => {
throwIfNoMocksArePassed(mocks);
// We clone the mocks so we can keep the user-provided mocks around for
// the debugging message. The mocksStack gets mutated, while mocks
// should never be mutated.
const mocksStack = [...mocks];
return () => next => action => {
var _context;
if (!isSdkAction(action)) {
return next(action);
}
const index = _findIndexInstanceProperty__default["default"](mocksStack).call(mocksStack, item => fastEquals.deepEqual(item.action, action));
if (index === -1) throw new Error(_concatInstanceProperty__default["default"](_context = "Could not find any more mocks for action ".concat(serialize(action), " in ")).call(_context, serialize(mocks)));
const mock = mocksStack[index];
// Mocks should only be used once, so we remove it from the stack.
_spliceInstanceProperty__default["default"](mocksStack).call(mocksStack, index, 1);
return isSdkMockSuccess(mock) ? _Promise__default["default"].resolve(mock.response) : _Promise__default["default"].reject(mock.error);
};
};
exports.createTestMiddleware = createTestMiddleware;