小编Vel*_*ack的帖子

匹配器错误:接收的值必须是模拟或间谍函数

我正在为表单 React 组件编写测试(使用 Jest 和 React 测试库)。我有一个在表单提交上运行的方法:

const onSubmit = (data) => {
  // ...
  setIsPopupActive(true);
  // ...
};
Run Code Online (Sandbox Code Playgroud)

并且useEffectisPopupActive更改后运行,所以也在提交时:

useEffect(() => {
  if (isPopupActive) {
    setTimeout(() => {
      setIsPopupActive(false);
    }, 3000);
  }
}, [isPopupActive]);
Run Code Online (Sandbox Code Playgroud)

在测试中,我想检查一下,弹出窗口是否在 3 秒后消失。所以这是我的测试:

it('Closes popup after 3 seconds', async () => {
    const nameInput = screen.getByPlaceholderText('Imi?');
    const emailInput = screen.getByPlaceholderText('Email');
    const messageInput = screen.getByPlaceholderText('Wiadomo??');
    const submitButton = screen.getByText('Wy?lij');

    jest.useFakeTimers();

    fireEvent.change(nameInput, { target: { value: 'Test name' } });
    fireEvent.change(emailInput, { target: …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs jestjs react-testing-library

5
推荐指数
2
解决办法
652
查看次数