test('user is logged out when the server doesnt resond with a new access token', async () => {
const spy = jest.spyOn(storageUtils, "getItemAndCheckExpiry")
spy.mockImplementation(() => {return JSON.stringify({access: 'efrijreoireor', refresh: 'rufrijfreijriej'})})
const history = createMemoryHistory()
history.push('/auth')
render(
<Router history={history}>
<AuthProvider >
<App />
</AuthProvider>
</Router>
)
await waitFor(() => expect(history.location.pathname).toBe('/'))
expect(screen.getByText(/Something went wrong/i)).toBeInTheDocument()
expect(spy).toHaveBeenCalledTimes(2)
jest.useFakeTimers()
jest.advanceTimersByTime(6000)
expect(screen.queryByText(/Something went wrong/i)).not.toBeInTheDocument()
// expect(timeOut).toHaveBeenCalledTimes(1)
// expect(timeOut).toHaveBeenLastCalledWith(expect.any(Function), 4000);
spy.mockRestore()
What this test should do is make sure that the message "something went wrong" disappears after 5 seconds. However, it doesn't work. I believe it is because I use fake timers inside an async function, but don't know how to solve this problem.
}catch(err){
logout();
setUnexpectedLogoutError('Something went wrong and you were logged out.')
setTimeout(() => {
setUnexpectedLogoutError(null)
}, 5000)
}
}
That's how this functionality is implemented. Any help will be appreciated.