Mai*_*man 8 javascript reactjs react-testing-library jest-mock-axios
使用 Reactjs 框架版本 16
用于 api 请求的 axios-hooks
用于测试的反应测试库
UnhandledPromiseRejectionWarning: TypeError: 在测试环境被拆除后捕获错误
无法读取 null 的属性“nodeType”(节点:171) UnhandledPromiseRejectionWarning:未处理的承诺拒绝。这个错误要么是因为在没有 catch 块的情况下抛出了异步函数,要么是因为拒绝了一个没有用 .catch() 处理过的承诺。(拒绝编号:4)
测试代码
test('If user enters an invalid email or password', async () => {
const { getByText, getByLabelText, getByTestId } = render(
<Provider store={mockStore}>
<Login />
</Provider>
)
fireEvent.change(getByLabelText('Email'), {
target: { value: 'test@emailing.com' }
})
fireEvent.change(getByLabelText('Password'), {
target: { value: 'password' }
})
getByText('Sign in').click()
await waitForDomChange()
await act(async () => {
try {
await MockAxios.mockError({
code: 400,
data: {
response: {
data: {
message: "This password is incorrect. Please re-enter your password."
}
}
}
})
} catch (error) {
console.log(error)
}
})
await waitForDomChange()
expect(MockAxios).toHaveBeenCalledTimes(1)
await expect(getByTestId('error-msg')).toBeInTheDocument()
})
Run Code Online (Sandbox Code Playgroud)
(代码已测试)使用 axios 钩子发出请求,然后将错误发送到表单
const [{ data, loading, error }, exec] = useAxios(
{
method: 'POST',
url: 'oauth/token/',
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
},
{ manual: true }
)
return (
<>
{(loading || userDataLoading) && <Loader />}
<Form
onSubmit={onSubmit}
error={error}
/>
</>
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7344 次 |
| 最近记录: |