node.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. /*
  6. * Helpers for testing Redux.
  7. */
  8. var FINISHED = "@@test/SPYSHOULDBEFINISHED";
  9. /**
  10. * Creates the middleware that checks if specified actions are dispatched.
  11. * Use it like `const spy = spyInTheMiddle(fn1, fn2, ...), store = applyMiddleware(..., spy)(reducer);`.
  12. * The fn1, ... get action in the parameter and should return truthy value if it is the expected one,
  13. * in which case the expectation will be removed; and shoudl return falsy value if it can be ignored.
  14. * For the moment, only first expectation in the list is checked until it returns true, then it is removed and the next one will be used,
  15. * In case the action is invalid (like, it has right type but wrong payload), the expectation fn can (and should) throw,
  16. * for example using the test assertion (in chai/expect, assertions are truthy, so your expectation fn can look like below)
  17. *
  18. * ({type, payload: {foo}}) => type == "FOOIFY" && expect(foo).to.equal("bar")
  19. *
  20. * All actions are passed downstream, unless you threw.
  21. *
  22. * @param expectations
  23. */
  24. var spyInTheMiddle = exports.spyInTheMiddle = function spyInTheMiddle() {
  25. for (var _len = arguments.length, expectations = Array(_len), _key = 0; _key < _len; _key++) {
  26. expectations[_key] = arguments[_key];
  27. }
  28. return function (store) {
  29. return function (next) {
  30. return function (action) {
  31. if (action.type == FINISHED && expectations.length > 0) throw new Error("Expectations left unfulfilled: " + expectations.length);
  32. if (expectations[0] && expectations[0](action)) {
  33. expectations.shift();
  34. }
  35. return next(action);
  36. };
  37. };
  38. };
  39. };
  40. /**
  41. * Call `store.dispatch(SPY_VERIFY)` at the end of the test.
  42. * It will pass if all expectations were fulfilled and fail if there are still some hanging there.
  43. * @type {{type: string}}
  44. */
  45. var SPY_VERIFY = exports.SPY_VERIFY = { type: FINISHED };
  46. /**
  47. * Reducer that always returns unchanged state.
  48. * @param state
  49. * @param action
  50. */
  51. var dumbReducer = exports.dumbReducer = function dumbReducer(state, action) {
  52. return state;
  53. };