Use of the enzyme, mocha and expectation claims.
The purpose of my unit test is to verify that the send will be called with the correct arguments when paused and not paused in mergeProps. I need to dynamically change the state of my store: paused: true .
At the moment I am trying to update a paused value by sending, but I do not think this is correct, because it is just a layout and never passes through a reducer.
I am using the redux-mock-store package.
How to do it?
describe('Play Container', () => { const id = 'audio-player-1'; const store = configureMockStore()({ players: { 'audio-player-1': { paused: false } } }); let dispatchSpy; let wrapper; beforeEach(() => { dispatchSpy = expect.spyOn(store, 'dispatch'); wrapper = shallow( <PlayContainer className={attributes.className}> {children} </PlayContainer>, { context: { id } }, ).shallow({ context: { store } }); }); it('onClick toggles play if paused', () => { //Not Working store.dispatch(updateOption('paused', true, id)); wrapper.simulate('click'); expect(dispatchSpy).toHaveBeenCalledWith(play(id)); }); it('onClick toggles pause if playing', () => { wrapper.simulate('click'); expect(dispatchSpy).toHaveBeenCalledWith(pause(id)); }); });
Container:
const mapStateToProps = ({ players }, { id }) => ({ paused: players[id].paused }); const mergeProps = (stateProps, { dispatch }, { id }) => ({ onClick: () => (stateProps.paused ? dispatch(play(id)) : dispatch(pause(id))) }); export default connectWithId(mapStateToProps, null, mergeProps)(Play);
connectWithId:
//getContext() is from recompose library and just injects id into props export const connectWithId = (...args) => compose( getContext({ id: React.PropTypes.string }), connect(...args), );
actions:
updateOption: (key, value, id) => ({ type: actionTypes.player.UPDATE_OPTION, key, value, id, }),