0
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()

该测试应该做的是确保“出现问题”的消息在 5 秒后消失。但是,它不起作用。我相信这是因为我在异步函数中使用了假计时器,但不知道如何解决这个问题。

    }catch(err){
        logout(); 
        setUnexpectedLogoutError('Something went wrong and you were logged out.')
        setTimeout(() => {
            setUnexpectedLogoutError(null)
        }, 5000)
    }
}

这就是这个功能的实现方式。任何帮助将不胜感激。

4

0 回答 0