We use
TZ=UTC
environment variable so every serialized date are in UTC to prevent environment conflict.
You can mock the result of new Date()
as follows:
const Today = new Date(2020, 10, 1)
describe('Favorites reducer', () => {
beforeAll(() => {
mockdate.set(Today)
})
})
When a component uses AuthContext, you first need to mock it at the top of the file:
import { useAuthContext } from 'features/auth/context/AuthContext'
jest.mock('features/auth/context/AuthContext')
Then you can use 2 different utils depending on your need.
If you want a disconnected user:
mockAuthContextWithoutUser()
If you want a logged in specific user:
mockAuthContextWithUser(anUserFixture)
If you want to persist your mock (using mockReturnValue
instead of mockReturnValueOnce
) you can pass the persist option as follows:
mockAuthContextWithoutUser({ persist: true })
mockAuthContextWithUser(anUserFixture, { persist: true })
When the tested component use route params through useRoute
hook, params
can be mocked as follows:
import { useRoute } from '__mocks__/@react-navigation/native'
// later
useRoute.mockReturnValue({
params: {
shouldDisplayLoginModal: withModal,
},
})
- Mock the snackbar context at the beginning of the file
const mockShowErrorSnackBar = jest.fn()
const mockShowInfoSnackBar = jest.fn()
jest.mock('ui/components/snackBar/SnackBarContext', () => ({
useSnackBarContext: () => ({
showErrorSnackBar: mockShowErrorSnackBar,
showInfoSnackBar: mockShowInfoSnackBar,
}),
}))
- In your test, assert that the mock has been called with the message that is supposed to be displayed in the snackBar, and with the correct timeout.
expect(mockShowErrorSnackBar).toHaveBeenCalledWith({
message:
'Ton compte Google semble ne pas être valide. Pour pouvoir te connecter, confirme d’abord ton adresse e-mail Google.',
timeout: SNACK_BAR_TIME_OUT_LONG,
})