reactjs - how to change jest mock function return value in each test? -


i have mock module in component test file

  jest.mock('../../../magic/index', () => ({     navigationenabled: () => true,     guidanceenabled: () => true   })); 

these functions called in render function of component hide , show specific feature.

i want take snapshot on different combinations of return value of mock functions.

for suppose have test case this

 it('rowlistitem should not render navigation , guidance options', () => {     const wrapper = shallow(       <rowlistitem type="regularlist" {...props} />     );     expect(enzymetojson(wrapper)).tomatchsnapshot();   }); 

to run test case want change mock module functions return values false dynamically

jest.mock('../../../magic/index', () => ({     navigationenabled: () => false,     guidanceenabled: () => false   })); 

because importing rowlistitem component once mock module wont re import again. wont change. how can solve ?

you can mock module returns spies , import test:

import {navigationenabled, guidanceenabled} '../../../magic/index'  jest.mock('../../../magic/index', () => ({     navigationenabled: jest.fn(),     guidanceenabled: jest.fn() })); 

then later on can change actual implementation using mockimplementation

navigationenabled.mockimplementation(()=> true) //or navigationenabled.mockreturnvalueonce(true); 

and in next test

navigationenabled.mockimplementation(()=> false) //or navigationenabled.mockreturnvalueonce(false); 

Comments

Popular posts from this blog

Is there a better way to structure post methods in Class Based Views -

performance - Why is XCHG reg, reg a 3 micro-op instruction on modern Intel architectures? -

jquery - Responsive Navbar with Sub Navbar -