I have a mock module like this in my component test file
jest.mock('../../../magic/index', () => ({
navigationEnabled: () => true,
guidanceEnabled: () => true
}));
these functions will be called in render function of my component to hide and show some specific feature.
I want to take a snapshot on different combinations of the return value of those mock functions.
for suppose I have a test case like this
it('RowListItem should not render navigation and guidance options', () => {
const wrapper = shallow(
<RowListItem type="regularList" {...props} />
);
expect(enzymeToJson(wrapper)).toMatchSnapshot();
});
to run this test case I want to change the mock module functions return values to false
like this dynamically
jest.mock('../../../magic/index', () => ({
navigationEnabled: () => false,
guidanceEnabled: () => false
}));
because i am importing RowListItem
component already once so my mock module wont re import again. so it wont change. how can i solve this ?
You can mock the module so it returns spies and import it into your test:
Then later on you can change the actual implementation using
mockImplementation
and in the next test
I had a hard time getting the accepted answers to work - my equivalents of
navigationEnabled
andguidanceEnabled
were undefined when I tried to callmockReturnValueOnce
on them.Here's what I had to do:
In
../../../magic/__mocks__/index.js
:in my
index.test.js
file:what you want to do is
you can look more about these functions here =>https://facebook.github.io/jest/docs/mock-functions.html#mock-return-values