효소를 사용하여 모카가 기대하고 주장합니다.Redux 단위 테스트에서 상점을 업데이트하는 방법은 무엇입니까?
내 단위 테스트의 목적은 일시 중지되었을 때 올바른 인수로 디스패치가 호출되고 mergeProps에서 일시 중지되지 않았는지 확인하는 것입니다. 동적으로 내 저장소의 상태를 변경해야합니다 : paused: true
.
나는 일시 중지 된 값을 디스 패칭하여 업데이트하려고하지만 모의이며 실제로 감속기를 통과하지 않기 때문에 올바른 것으로 생각하지 않습니다.
패키지 redux-mock-store을 사용하고 있습니다.
어떻게하면됩니까?
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));
});
});
용기 :
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),
);
행동 :
updateOption: (key, value, id) => ({
type: actionTypes.player.UPDATE_OPTION,
key,
value,
id,
}),
이 링크를 본 적이 있습니까? http://redux.js.org/docs/recipes/WritingTests.html – denvaar
그래도 위의 문서를 읽는 것이 좋습니다. 행동, 감속기, 구성 요소 등 세 가지 다른 것들을 별도로 테스트해야합니다. 액션 (실제로 액션 크리에이터)을 테스트 할 때, 전달 된 실제 액션이 올바르게 형성되고 있는지 확인하기 만하면됩니다. 감속기를 테스트 할 때 이미 수행중인 것처럼 상점을 조롱하고 일부 조치를 파견하고 새로운 상태가 반환되는지 확인하십시오. 구성 요소를 테스트 할 때 실제로 비헤이비어를 테스트하고 싶습니다 (예 : 여기를 클릭하면 디스패치가 호출 됨). 디스패치를 물론 조롱합니다. –