当我测试用户事件时,我使用react-testing-library的fireevent函数。
・src/example.js
import counter from "./components/counter";const example = () => { const origincount = 0; return ;};export default example;
登录后复制
・src/commponets/counter.jsx
import { usestate } from "react";const counter = (props) => { const { origincount } = props; const [count, setcount] = usestate(origincount); const countup = () => { setcount(count + 1); }; return ();};export default counter;a test of counter
current count:{count}
登录后复制
・src/commponets/counter.test.jsx
import { render, screen, fireEvent } from "@testing-library/react";import Counter from "./Counter";test("Whether the current count counts up or not, in case the countUp button is clicked ", () => { const { debug } = render();//test the initial state. const spanElBeforUpdate = screen.getByText("Current count:0"); expect(spanElBeforUpdate).toBeInTheDocument();//test the user event. In this case, a click event. const btn = screen.getByRole("button", { name: "Countup" }); fireEvent.click(btn);//test the state which is after clicked button. const spanEl = screen.getByText("Current count:1"); expect(spanEl).toBeInTheDocument();});
登录后复制
以上就是React 基础知识~单元测试/用户事件的详细内容,更多请关注【创想鸟】其它相关文章!
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2663993.html