【问题标题】:I'm not Getting any number of calls in my React Test using React Testing Library我在使用 React 测试库的 React 测试中没有收到任何数量的调用
【发布时间】:2020-08-31 20:28:55
【问题描述】:

我使用反应测试库模拟了我的登录组件,执行了必要的“getBy”并使用 fireEvent 提交或单击提交按钮。我使用 onSubmit 函数渲染了组件,但它仍然没有被调用。

it("Calls onSubmit with username and Password when submitted", () => {
   const onSubmit = jest.fn();
   const { getByTestId, getByText } = render(
      <MemoryRouter>
           <Login onSubmit = {onSubmit} />
      </MemoryRouter>
      );
      const button = getByText("Login");
      const email = getByTestId("email");
      const password = getByTestId("password");
      const form = getByTestId("form-element");

    fireEvent.change(email, { target: { value: "Tolulope@gmail.com" } });
    fireEvent.change(password, { target: { value: "YummyPizza" } });
    fireEvent.click(button);

   expect(onSubmit).toHaveBeenCalledTimes(1);
   expect(onSubmit).toHaveBeenCalledWith({
      email: email.value,
      password: password.value,
});

 });

我试过了,但 fireEvent.submit 和 fireEvent.click 分别在表单和按钮上,但似乎都没有触发该功能。

这是我的登录组件的设置。

 <form data-testid="form-element" style={{width: "360px"}}>
    <Input data-testid= "email"    onChange={handleChange} value={email}   name='email' type='text' label='Email Address' icon = 'at' required/>
    <Input data-testid= "password"    onChange={handleChange} value={password} name='password' type='password' label='Password' icon = 'lock' required/>

    <ButtonAuth  className='text-xl text-white uppercase cursor-pointer  mt-1 block w-full h-12 outline-none border-none bg-green-500 mt-3' value='Login' type="submit" onSubmit={onSubmit} disabled={!enabled} />

</form>

Input 和 Button 都是组件,但可以重复使用 input 和 button 标签。

【问题讨论】:

    标签: javascript reactjs testing jestjs react-testing-library


    【解决方案1】:

    您必须在表单上触发提交事件。

    it("Calls onSubmit with username and Password when submitted", () => {
      const onSubmit = jest.fn();
      const { getByTestId } = render(
        <MemoryRouter>
          <Login onSubmit={onSubmit} />
        </MemoryRouter>
      );
    
      const form = getByTestId("form-element");
    
      expect(onSubmit).not.toHaveBeenCalled();
    
      fireEvent.submit(form, {
        target: {
          values: {
            email: "Tolulope@gmail.com",
            password: "YummyPizza",
          },
        },
      });
    
      expect(onSubmit).toHaveBeenCalledTimes(1);
      expect(onSubmit).toHaveBeenCalledWith({
        email: "Tolulope@gmail.com",
        password: "YummyPizza",
      });
    });
    

    【讨论】:

    • 感谢帮助,我之前也是这样做的,还是不行
    • @TOLULOPEADETULA 以上是我测试表单的方式,但有趣的是,kentcdodds here 建议在提交按钮上触发点击事件。您介意更新和分享您尝试测试的表单组件的最小示例,以便我们更好地了解您正在使用什么吗?
    • 我已编辑问题以添加表单。我现在去看看博文
    • @TOLULOPEADETULA Bruh,onSubmit 回调出现在表单上,​​而不是按钮上。
    • 我的错,它最初就在那里,它在我的调试过程中为什么它不起作用,我向下移动到按钮。
    猜你喜欢
    • 1970-01-01
    • 2021-11-09
    • 2021-07-03
    • 2021-06-14
    • 2022-01-19
    • 2020-03-24
    • 2023-02-09
    • 1970-01-01
    • 2021-06-01
    相关资源
    最近更新 更多