javascript - Mocking the same function in two different test blocks in Jest -
i've got question creating mocks of functions in jest. i've got 2 elements on page call different functions in react component. both of functions call same function, passed in props, onfieldchanged. want simulate change on these 2 elements, , confirm props.onfieldchanged called.
when write first test (the first test below), passes flying colors. when write second test, second test passes first test fails. basically, cannot both tests pass @ same time. need reset mock somehow? know how this?
what gives?
describe('user selects checkbox', () => { props.onfieldchanged = jest.fn(); const wrapper = shallow(<dateoptions {...props} />); it('calls 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 onfieldchanged function', () => { const element = wrapper.find('#lowertimelimit'); element.simulate('change'); expect(props.onfieldchanged).tohavebeencalled(); }); });
try pass jest.fn() different variable:
describe('user selects checkbox', () => { it('calls onfieldchanged function', () => { const onfieldchanged = jest.fn(); const wrapper = shallow(<dateoptions {...props, onfieldchanged} />); const element = wrapper.find('#addonetostudydays'); element.simulate('change'); expect(props.onfieldchanged).tohavebeencalled(); }); }); describe('user types in input', () => { it('calls onfieldchanged function', () => { const onfieldchanged = jest.fn(); const wrapper = shallow(<dateoptions {...props, onfieldchanged} />); const element = wrapper.find('#lowertimelimit'); element.simulate('change'); expect(props.onfieldchanged).tohavebeencalled(); }); });
Comments
Post a Comment