Thunk Middleware with Custom API Utils
// And now testing is *super* easy - just inject away! | |
it ('should hit correct endpoint', () => { | |
const api = createMockApi([ | |
{test: 'users/1', resp: ({data: {id: 1, type: 'user', name: 'Chris'}})} | |
]); | |
const store = createMockAppStore({api}); | |
store.dispatch(fetchUser(1)); | |
expect(api.fetch.firstCall.args[0]).toEqual('users/1'); | |
expect(store.dispatch.firstCall.args[0]).toEqual( | |
{action: FETCH_USER, user: {id: 1, type: 'user', name: 'Chris'}} | |
); | |
}) |
// Where the magic happens | |
export function createThunkMiddleware(args) { | |
return function createDispatch(next) { | |
return function doDispatch(action) { | |
if (isFunction(action)) { | |
return doDispatch(action(args)); | |
} | |
return next(action); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment