[英]react-testing-library material-ui slider
I am trying to create a test for material-ui slider.我正在尝试为 material-ui slider 创建一个测试。 Able to find slider options but
fireEvent.click()
does not trigger onChange
event.能够找到 slider 选项但
fireEvent.click()
不会触发onChange
事件。 Example is in here: https://codesandbox.io/s/material-demo-y62ks示例在这里: https://codesandbox.io/s/material-demo-y62ks
I'm late to the party, but for some people.我参加聚会迟到了,但对某些人来说。
Use fireEvent.mouseDown(option1);
使用
fireEvent.mouseDown(option1);
it should work, I tested it in codesanbox link.它应该可以工作,我在codesanbox链接中对其进行了测试。
For future people also want to test slider please see my answer: Testing a material ui slider with @testing-library/react对于未来的人也想测试 slider 请看我的回答: Testing a material ui slider with @testing-library/react
Whenever you are testing MUI components, you must use userEvent, instead of fireEvent, because most MUI components do not use "standard" functions like onClick
to trigger their events.每当您测试 MUI 组件时,您必须使用 userEvent,而不是 fireEvent,因为大多数 MUI 组件不使用像
onClick
这样的“标准”函数来触发它们的事件。
userEvent best simulates the user's action. userEvent 最能模拟用户的动作。 You can import it from "@ testing-library / user-event".
您可以从“@ testing-library / user-event”导入它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.