Created
March 29, 2019 19:43
-
-
Save yanick/cd4ba393772342c519e07ff7641c087d to your computer and use it in GitHub Desktop.
Less boilerplate to test Redux middleware (in Jest)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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