UNPKG

redux-logic-test

Version:

redux-logic test utilities to facilitate the testing of logic. Create mock store

195 lines (157 loc) 6.29 kB
(function webpackUniversalModuleDefinition(root, factory) { if(typeof exports === 'object' && typeof module === 'object') module.exports = factory(require("Redux"), require("ReduxLogic")); else if(typeof define === 'function' && define.amd) define(["Redux", "ReduxLogic"], factory); else if(typeof exports === 'object') exports["ReduxLogicTest"] = factory(require("Redux"), require("ReduxLogic")); else root["ReduxLogicTest"] = factory(root["Redux"], root["ReduxLogic"]); })(this, function(__WEBPACK_EXTERNAL_MODULE_2__, __WEBPACK_EXTERNAL_MODULE_3__) { return /******/ (function(modules) { // webpackBootstrap /******/ // The module cache /******/ var installedModules = {}; /******/ // The require function /******/ function __webpack_require__(moduleId) { /******/ // Check if module is in cache /******/ if(installedModules[moduleId]) /******/ return installedModules[moduleId].exports; /******/ // Create a new module (and put it into the cache) /******/ var module = installedModules[moduleId] = { /******/ exports: {}, /******/ id: moduleId, /******/ loaded: false /******/ }; /******/ // Execute the module function /******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__); /******/ // Flag the module as loaded /******/ module.loaded = true; /******/ // Return the exports of the module /******/ return module.exports; /******/ } /******/ // expose the modules object (__webpack_modules__) /******/ __webpack_require__.m = modules; /******/ // expose the module cache /******/ __webpack_require__.c = installedModules; /******/ // __webpack_public_path__ /******/ __webpack_require__.p = ""; /******/ // Load entry module and return exports /******/ return __webpack_require__(0); /******/ }) /************************************************************************/ /******/ ([ /* 0 */ /***/ (function(module, exports, __webpack_require__) { 'use strict'; exports.__esModule = true; exports.createMockStore = undefined; var _createMockStore = __webpack_require__(1); var _createMockStore2 = _interopRequireDefault(_createMockStore); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { 'default': obj }; } exports.createMockStore = _createMockStore2['default']; exports['default'] = { createMockStore: _createMockStore2['default'] }; /***/ }), /* 1 */ /***/ (function(module, exports, __webpack_require__) { 'use strict'; exports.__esModule = true; var _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; exports['default'] = createMockStore; var _redux = __webpack_require__(2); var _reduxLogic = __webpack_require__(3); /* // specify as much as necessary for your particular test const store = createMockStore({ initialState: optionalObject, reducer: optionalFn, // default: identity reducer logic: optionalLogic, // default: [] injectedDeps: optionalObject, // default {} middleware: optionalArr // other mw, exclude logicMiddleware }); store.dispatch(...) // use as necessary for your test // when all inflight logic has all completed calls fn + returns promise store.whenComplete(fn) - shorthand for store.logicMiddleware.whenComplete(fn) store.actions - the actions dispatched, use store.resetActions() to clear store.resetActions() - clear store.actions // access the logicMiddleware created for logic/injectedDeps props // use addLogic, mergeNewLogic, replaceLogic, whenComplete, monitor$ store.logicMiddleware */ var defaultMockStoreOptions = { initialState: undefined, reducer: function reducer(state /* , action */) { return state; }, // identity reducer logic: [], // used for the logicMiddleware that is created injectedDeps: {}, // used for the logicMiddleware that is created middleware: [] // other mw, exclude logicMiddleware from this array }; var ALLOWED_OPTIONS = Object.keys(defaultMockStoreOptions); function checkOptions(obj) { Object.keys(obj).forEach(function (k) { // check keys if (ALLOWED_OPTIONS.indexOf(k) === -1) { // no match throw new Error('invalid option: ' + k); } }); if (obj.reducer && typeof obj.reducer !== 'function') { throw new Error('reducer should be a function'); } if (obj.middleware && !Array.isArray(obj.middleware)) { throw new Error('middleware should be an array'); } } function createMockStore() { var options = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {}; checkOptions(options); // throws if any problems var opts = _extends({}, defaultMockStoreOptions, options); var initialState = opts.initialState, reducer = opts.reducer, logic = opts.logic, injectedDeps = opts.injectedDeps, middleware = opts.middleware; // track the actions dispatched using a custom mw added last var actions = []; var trackActionsMW = function trackActionsMW() { return (/* store */function (next) { return function (action) { actions.push(action); return next(action); }; } ); }; var logicMiddleware = (0, _reduxLogic.createLogicMiddleware)(logic, injectedDeps); var enhancer = _redux.applyMiddleware.apply(undefined, [logicMiddleware].concat(middleware, [trackActionsMW])); var store = (0, _redux.createStore)(reducer, initialState, enhancer); Object.defineProperty(store, 'actions', { // create store.actions getter enumerable: true, get: function get() { return actions; } }); store.resetActions = function () { actions.length = 0; }; // truncate store.logicMiddleware = logicMiddleware; store.whenComplete = function (fn) { return logicMiddleware.whenComplete(fn); }; return store; } /***/ }), /* 2 */ /***/ (function(module, exports) { module.exports = __WEBPACK_EXTERNAL_MODULE_2__; /***/ }), /* 3 */ /***/ (function(module, exports) { module.exports = __WEBPACK_EXTERNAL_MODULE_3__; /***/ }) /******/ ]) }); ;