【发布时间】:2018-07-26 13:11:12
【问题描述】:
我有一个订阅者,它根据提供给 pub 事件的参数调度一个操作
// subscriptions.js
import store from '../store';
import { action } from '../actions';
export const subscribeToToggle = () => {
window.$.subscribe('action/toggle', (_e, isToggleOn) => {
if (isToggleOn=== true){
store.dispatch(action());
}
});
};
在我的测试文件中,我编写了 2 个测试来测试仅在提供 true 时才发送操作。
// subscriptions.test.js
import { subscribeToToggle } from './subscriptions';
import jQuery from 'jquery';
import { actionTypes } from '../constants';
import store from '../store';
jest.mock('../store');
beforeEach(() => {
window.$ = jQuery;
window.$.unsubscribe('action/toggle');
});
test('Action not sent when false', () => {
subscribeToToggleOpen();
window.$.publish('action/toggle', false);
expect(store.getActions().length).toBe(0);
});
test('Action sent when true', () => {
subscribeToToggleOpen();
window.$.publish('action/toggle', true);
expect(store.getActions().length).toBe(1);
expect(store.getActions()[0].type).toBe(actionTypes.ACTION);
});
我有以下使用 redux-test-utils 模拟的商店
import { createMockStore } from 'redux-test-utils';
let store = null;
store = createMockStore('');
export default store;
我面临的问题是,我的测试只有在错误测试首先出现时才能通过。如果它们是“假时未发送的动作”测试失败的另一种方式,因为它看到“真时发送的动作”测试提供的动作。
我有什么办法可以使用 beforeEach 方法来重置模拟的商店对象?
【问题讨论】:
标签: javascript redux jestjs