【发布时间】:2017-07-28 18:10:51
【问题描述】:
我有一个关于在 Jest 中创建函数模拟的问题。我的页面上有两个元素在我的 React 组件中调用不同的函数。这两个函数都调用同一个函数,该函数是从 props 传入的,onFieldChanged。我想模拟这两个元素的变化,并确认props.onFieldChanged被调用了。
当我编写第一个测试(下面的第一个测试)时,它以优异的成绩通过。当我编写第二个测试时,第二个测试通过了,但现在第一个测试失败了。基本上,我不能同时通过两个测试。我需要以某种方式重置模拟吗?有谁知道怎么做?
什么给了?
describe('user selects checkbox', () => {
props.onFieldChanged = jest.fn();
const wrapper = shallow(<DateOptions {...props} />);
it('calls the onFieldChanged function', () => {
const element = wrapper.find('#addOneToStudyDays');
element.simulate('change');
expect(props.onFieldChanged).toHaveBeenCalled();
});
});
describe('user types in input', () => {
props.onFieldChanged = jest.fn();
const wrapper = shallow(<DateOptions {...props} />);
it('calls the onFieldChanged function', () => {
const element = wrapper.find('#lowerTimeLimit');
element.simulate('change');
expect(props.onFieldChanged).toHaveBeenCalled();
});
});
【问题讨论】:
标签: javascript unit-testing reactjs jestjs