Skip to content

Instantly share code, notes, and snippets.

@yanick
Created March 29, 2019 19:43
Show Gist options
  • Save yanick/cd4ba393772342c519e07ff7641c087d to your computer and use it in GitHub Desktop.
Save yanick/cd4ba393772342c519e07ff7641c087d to your computer and use it in GitHub Desktop.
Less boilerplate to test Redux middleware (in Jest)
type MWFixtures = Partial<{
dispatch: Function;
getState: Function;
next: Function;
action: object;
}>;
function mw_fixtures(fixtures: MWFixtures) {
return fp.defaults({
dispatch: jest.fn(),
getState: jest.fn(),
next: jest.fn(),
action: { type: 'NOOP' },
})(fixtures);
}
function test_mw(mw: Middleware, fixtures: MWFixtures) {
fixtures = mw_fixtures(fixtures);
fixtures.returned = mw(fixtures as any)(fixtures.next as any)(fixtures.action);
return fixtures;
}
test('forced try_play_turn', () => {
const { dispatch } = test_mw(mw_try_play_turn, {
action: try_play_turn(true),
});
expect(dispatch).toHaveBeenCalledWith(play_turn());
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment