I have a utils file that looks something like this
// utils.js
const getNextDate = (startDate) => moment(startDate, 'MMM Do YYYY').startOf('day').add(10, 'days').format('MMM Do YYYY');
const getDisplayName = (user) => {
if (user.type === 'admin') {
return 'Admin';
} else if (user.type === 'guest') {
return 'Guest'
} else if(user.type === 'user') {
return `${user.firstname} ${user.lastname}`
} else {
return 'No user'
}
}
export {
getNextDate,
getDisplayName
}
I also have a mock of my utils file in my mocks folder where I implement the mock return values for testing. It looks something like this
// mock/utils.js
export const getNextDate = () => 'Oct 20th 2020';
export const getDisplayName = (user) => user
In my component and test, I'm doing something like this
//Component.js
import React from 'react';
import { getNextDate, getDisplayName } from './utils'
export const Component = () => {
const user = {
type: 'admin',
firstname: 'john',
lastname: 'doe',
}
return (
<div>
<div>{getDisplayName(user)}</div>
<div>{getNextDate(moment())}</div>
</div>
)
}
// Component.test.js
import { Component } from '../Component'
jest.mock('./utils', () => require('./mock/utils'));
describe('Component', () => {
beforeEach(() => {
wrapper = shallow(
<Component />
);
});
it('renders next date', () => {
// At this point, I want to change the mock return value of getNextDate to Dec 25th 2020 without changing it in mock/utils.js as other tests are using that value
expect(wrapper.find('.date').text()).toEqual('Dec 25th 2020');
});
});
However, in one of the test cases, I'm trying to change the mock implementation of getNextDate
. How do I get this done since I can't just call getNextDate.mockImplementation()
directly?