【发布时间】:2021-06-08 18:53:45
【问题描述】:
我在组件中有按钮:
<button
type="button"
title="Logout"
data-testid="logout"
onClick={logout}
>
和方法logout:
const logout = async () => {
try {
await authLogout()
} catch (error) {
console.error(error)
} finally {
props.history.push('/login')
}
}
现在我尝试通过单击按钮来测试注销方法:
describe('UserMenuComponent', () => {
it('go to login page after click on logout', async () => {
const history = createMemoryHistory()
const { getByTestId } = render(
<Router history={history}>
<UserMenuComponent />,
</Router>,
{
initialState: { reducersAuth: initialState },
},
)
fireEvent.click(getByTestId('logout'))
expect(history.location.pathname).toBe('/login')
})
})
finally 块中的内部方法 logout 我有 props.history.push('/login'),我在测试中检查了这个:expect(history.location.pathname).toBe('/login') 但测试返回错误:
expect(received).toBe(expected) // Object.is equality
Expected: "/login"
Received: "/"
【问题讨论】:
标签: reactjs react-router jestjs react-testing-library