Skip to content

Instantly share code, notes, and snippets.

@jeffbski
Last active June 30, 2017 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeffbski/91d358d322f6a59f53f2be858db03863 to your computer and use it in GitHub Desktop.
Save jeffbski/91d358d322f6a59f53f2be858db03863 to your computer and use it in GitHub Desktop.
redux-logic-test helper example
import { createMockStore } from 'redux-logic-test';
// specify as much as necessary for your particular test
const store = createMockStore({
initialState: optionalObject,
reducer: optionalFn, // default: identity reducer fn(state, action) { return state; }
logic: optionalLogic, // default: [] - used for the logicMiddleware that is created
injectedDeps: optionalObject, // default {} - used for the logicMiddleware that is created
middleware: optionalArr // other mw, exclude logicMiddleware from this array
});
store.dispatch(...) // use as necessary for your test
store.whenComplete(fn) - shorthand for store.logicMiddleware.whenComplete(fn) - when all inflight logic has all completed calls fn and returns promise
store.actions - the actions dispatched, use store.resetActions() to clear
store.resetActions() - clear store.actions
store.logicMiddlware // access logicMiddleware automatically created from logic/injectedDeps props
// allows you to use store.logicMiddleware.addLogic, mergeNewLogic, replaceLogic, whenComplete, monitor$
// So a test might look something like
const store = createMockStore({
initialState: { foo: 42 },
logic: [ fooLogic, barLogic ], // logicMiddleware will be created for this logic and injectedDeps
injectedDeps: { api: myAPI }
});
store.dispatch({ type: FOO });
store.dispatch({ type: BAR });
store.whenComplete(() => { // when logic is finished
expect(store.actions).toEqual([ // check the actions that were dispatched
{ type: FOO },
{ type: BAR },
{ type: FOO_SUCCESS, payload: [] },
{ type: BAR_SUCCESS, payload: {} }
]);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment