gpt4 book ai didi

javascript - React Hook 表单无法在 React 测试库测试中提交

转载 作者:行者123 更新时间:2023-12-05 03:45:12 24 4
gpt4 key购买 nike

我正在编写一个测试以确保我的表单正在使用 React 测试库提交,并且我也在使用 React Hook 表单。我的提交方法无法在我的测试中调用。运行此测试时出现以下错误:

● reset password should send

expect(jest.fn()).toHaveBeenCalled()

Expected number of calls: >= 1
Received number of calls: 0

谁能解释一下我做错了什么?

我的组件

const ResetPassword = () => {
const { handleSubmit } = useForm();

const onSubmit = (resetFormData: { email: string }) => {
const { email } = resetFormData;
// sends email using external API
};

return (
<form onSubmit={handleSubmit(onSubmit)}>
<input
name="email"
type="text"
placeholder="Email Address"
/>
<button type="submit">
Send Email
</button>
</form>
);
};

export default ResetPassword;

我的测试文件

import userEvent from '@testing-library/user-event';
import { render, cleanup, screen, act } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

afterEach(cleanup);

it('reset password should send', async () => {
render(<ResetPassword />);

const handleSubmit = jest.fn();
const onSubmit = jest.fn();
const value = 'user@email.com';
const input = screen.getByPlaceholderText(/Email Address/i);
await userEvent.type(input, value);

await act(async () => {
userEvent.click(screen.getByRole('button', { name: /Send Email/i }));
});

expect(onSubmit).toHaveBeenCalled();
});

最佳答案

对于将来偶然发现此问题的任何人,我阅读了 article由 Trixin 评论并重写了我的单元测试。现在是它的精简版。基本上,我正在测试用户而不是开发人员的体验,以避免误报/否定

组件

const ResetPassword = () => {
const { handleSubmit } = useForm();
const [message, setMessage] = useState(''); // I alert the
// user of a successful sent message
const onSubmit = (resetFormData: { email: string }) => {
const { email } = resetFormData;
// sends email using external API
};

return (
<form onSubmit={handleSubmit(onSubmit)}>
<input
name="email"
type="text"
placeholder="Email Address"
/>
<button type="submit">
Send Email
</button>
{message}
</form>
);
};

export default ResetPassword;

测试文件

it('reset password should send', async () => {
render(<ResetPassword />);

const value = 'user@test.com';
const input = screen.getByPlaceholderText(/Email Address/i);
await userEvent.type(input, value);

await act(async () => {
fireEvent.click(screen.getByText(/Send Email/i));
});
expect(screen.getByText('Message Sent!')).toBeInTheDocument();
});

it('reset password should not send', async () => {
render(<ResetPassword />);

const input = screen.getByPlaceholderText(/Email Address/i);
const inValidEmail = 'user.com';

await userEvent.type(input, inValidEmail);
await act(async () => {
fireEvent.click(screen.getByText(/Send Email/i));
});
expect(screen.getByText('Invalid email address')).toBeInTheDocument();

// reset input value
fireEvent.change(input, { target: { value: '' } });
await userEvent.type(input, '');
// user hits spacebar and tries to submit
await act(async () => {
fireEvent.keyDown(input, {
charCode: 62,
code: 62,
key: 'Space Bar',
keyCode: 62,
});
});

await act(async () => {
fireEvent.click(screen.getByText(/Send Email/i));
});
expect(screen.getByText('Email is required')).toBeInTheDocument();
});

关于javascript - React Hook 表单无法在 React 测试库测试中提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65980481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com