反应测试图中的模拟功能误差

发布于 2025-01-27 21:50:24 字数 1982 浏览 3 评论 0原文

我是我新手进行反应测试的每个人,我正在尝试做一些练习示例,我遇到了一个错误,需要您的帮助,这是我的应用程序组件,

  const[firstName,setFirstName]=useState("")
  const[lastName,setLastName]=useState("")
  const [data,setData] = useState({})

  const handleFirstName = (e) =>{
    setFirstName(e.target.value)
  }
  const handleLastName = (e) =>{
    setLastName(e.target.value)
  }
  
  const handleSubmit = (e) =>{
    e.preventDefault();
    setData({firstName,lastName})
    console.log(firstName,lastName)
  }

  return (
    <div className="App">
     <form onSubmit={handleSubmit} data-testid="form" >
       <div>
         <label>FirstName
         <input type="text" name="firstName" id="firstName" value={firstName} onChange={handleFirstName}/>
         </label>
       </div>
       {firstName && firstName.length > 10 && <p data-testid="error-msg" >FirstName is not valid</p>}
       <div>
         <label>lastName
         <input type="text" name="lastName" id="lastName" value={lastName} onChange={handleLastName}/>
         </label>
       </div>
       <button type="submit" name="submit" disabled={firstName === ""} >submit</button>
     </form>
    </div>
  );
}

这是我的测试逻辑,

  const mockFunction = jest.fn();
  const {getByText}=render(<App onSubmit={mockFunction}/>)
  const firstNameLabel = screen.getByRole("textbox",{'name':'FirstName'})
  fireEvent.change(firstNameLabel,{"target":{'value':"dhffssß"}})
  const lastNameLabel = screen.getByRole("textbox",{"name":"lastName"})
  fireEvent.change(lastNameLabel,{"target":{'value':"dhfffsß"}})
  const btn = screen.getByRole('button',{'name':'submit'})
  fireEvent.click(btn)
  expect(mockFunction).toHaveBeenCalledTimes(1)
 })

我正在测试简单的表单,但会得到此错误的


期望( jest.fn())。

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

Hii Everyone I am new to react testing ,I am trying to do some example for practise , I am getting a Error,Need your help , this is my App Component

  const[firstName,setFirstName]=useState("")
  const[lastName,setLastName]=useState("")
  const [data,setData] = useState({})

  const handleFirstName = (e) =>{
    setFirstName(e.target.value)
  }
  const handleLastName = (e) =>{
    setLastName(e.target.value)
  }
  
  const handleSubmit = (e) =>{
    e.preventDefault();
    setData({firstName,lastName})
    console.log(firstName,lastName)
  }

  return (
    <div className="App">
     <form onSubmit={handleSubmit} data-testid="form" >
       <div>
         <label>FirstName
         <input type="text" name="firstName" id="firstName" value={firstName} onChange={handleFirstName}/>
         </label>
       </div>
       {firstName && firstName.length > 10 && <p data-testid="error-msg" >FirstName is not valid</p>}
       <div>
         <label>lastName
         <input type="text" name="lastName" id="lastName" value={lastName} onChange={handleLastName}/>
         </label>
       </div>
       <button type="submit" name="submit" disabled={firstName === ""} >submit</button>
     </form>
    </div>
  );
}

this is my testing logic

  const mockFunction = jest.fn();
  const {getByText}=render(<App onSubmit={mockFunction}/>)
  const firstNameLabel = screen.getByRole("textbox",{'name':'FirstName'})
  fireEvent.change(firstNameLabel,{"target":{'value':"dhffssß"}})
  const lastNameLabel = screen.getByRole("textbox",{"name":"lastName"})
  fireEvent.change(lastNameLabel,{"target":{'value':"dhfffsß"}})
  const btn = screen.getByRole('button',{'name':'submit'})
  fireEvent.click(btn)
  expect(mockFunction).toHaveBeenCalledTimes(1)
 })

I am testing simple form but getting this error


expect(jest.fn()).toHaveBeenCalledTimes(expected)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼性发作 2025-02-03 21:50:24

尝试调用onsubmit函数,您在handlesabmit处理程序中传递。在这种情况下,您应该能够在测试中跟踪onsubmit回调。

const App = ({ onSubmit }) => {
  const[firstName,setFirstName]=useState("")
  const[lastName,setLastName]=useState("")
  const [data,setData] = useState({})

  const handleFirstName = (e) =>{
    setFirstName(e.target.value)
  }
  const handleLastName = (e) =>{
    setLastName(e.target.value)
  }
  
  const handleSubmit = (e) =>{
    e.preventDefault();
    setData({firstName,lastName});
    console.log(firstName,lastName);
    onSubmit();
  }

  return (
    <div className="App">
     <form onSubmit={handleSubmit} data-testid="form" >
       <div>
         <label>FirstName
         <input type="text" name="firstName" id="firstName" value={firstName} onChange={handleFirstName}/>
         </label>
       </div>
       {firstName && firstName.length > 10 && <p data-testid="error-msg" >FirstName is not valid</p>}
       <div>
         <label>lastName
         <input type="text" name="lastName" id="lastName" value={lastName} onChange={handleLastName}/>
         </label>
       </div>
       <button type="submit" name="submit" disabled={firstName === ""} >submit</button>
     </form>
    </div>
  );
}
}

Try to call onSubmit function which you pass in props in your handleSubmit handler. In this case you should be able to track if the onSubmit callback was called in your tests.

const App = ({ onSubmit }) => {
  const[firstName,setFirstName]=useState("")
  const[lastName,setLastName]=useState("")
  const [data,setData] = useState({})

  const handleFirstName = (e) =>{
    setFirstName(e.target.value)
  }
  const handleLastName = (e) =>{
    setLastName(e.target.value)
  }
  
  const handleSubmit = (e) =>{
    e.preventDefault();
    setData({firstName,lastName});
    console.log(firstName,lastName);
    onSubmit();
  }

  return (
    <div className="App">
     <form onSubmit={handleSubmit} data-testid="form" >
       <div>
         <label>FirstName
         <input type="text" name="firstName" id="firstName" value={firstName} onChange={handleFirstName}/>
         </label>
       </div>
       {firstName && firstName.length > 10 && <p data-testid="error-msg" >FirstName is not valid</p>}
       <div>
         <label>lastName
         <input type="text" name="lastName" id="lastName" value={lastName} onChange={handleLastName}/>
         </label>
       </div>
       <button type="submit" name="submit" disabled={firstName === ""} >submit</button>
     </form>
    </div>
  );
}
}
写下不归期 2025-02-03 21:50:24

更改firstName输入后,您可能需要等到启用提交按钮之前,然后等待回调调用(不确定第二个等待很有用):

import { render, waitFor } from '@testing-library/react';

const mockFunction = jest.fn();
const {getByText}=render(<App onSubmit={mockFunction}/>)
const firstNameLabel = screen.getByRole("textbox",{'name':'FirstName'})
fireEvent.change(firstNameLabel,{"target":{'value':"dhffssß"}})
const lastNameLabel = screen.getByRole("textbox",{"name":"lastName"})
fireEvent.change(lastNameLabel,{"target":{'value':"dhfffsß"}})

// wait until button is enabled
await waitFor(() => expect(getByText('submit').toBeEnabled());

fireEvent.click(getByText('submit'));
await waitFor(() => expect(mockFunction).toHaveBeenCalled();

You may need to wait until the submit button is enabled before trying to interact with it, after you changed the firstName input, and wait for the callback call (not sure the second wait is usefull) :

import { render, waitFor } from '@testing-library/react';

const mockFunction = jest.fn();
const {getByText}=render(<App onSubmit={mockFunction}/>)
const firstNameLabel = screen.getByRole("textbox",{'name':'FirstName'})
fireEvent.change(firstNameLabel,{"target":{'value':"dhffssß"}})
const lastNameLabel = screen.getByRole("textbox",{"name":"lastName"})
fireEvent.change(lastNameLabel,{"target":{'value':"dhfffsß"}})

// wait until button is enabled
await waitFor(() => expect(getByText('submit').toBeEnabled());

fireEvent.click(getByText('submit'));
await waitFor(() => expect(mockFunction).toHaveBeenCalled();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文