reactjs - 模拟单击我的组件中的另一个组件不起作用
问题描述
下面是我的组件,
interface Props{
onValueChange:(selectedvalue: string, data: string) => void;
}
const MyComp = (props: Props): JSX.Element => {
return(
<AnotherComponent
onValueChange={(value: string, data: string): void => {
props.onValueChange(value, data);
}}/>
);
}
下面是另一个组件
const AnotherComponent = (props:Props):JSX.Element =>{
return(
<div
id={"boxItem" + i.toString()}
onClick={(): void => {
props.onValueChange(value, data);
}}
/>
);
}
下面是我的 MyComp.test 文件,
it("calls onValuechange", () => {
const changeEvent = jest.fn();
wrapper.setProps({ onValueChange: changeEvent });
wrapper
.find(AnotherComponent)
.simulate("click",{val:"val",data:"val"});
expect(changeEvent).toHaveBeenCalledWith("val", "val");
});
运行测试时出现以下错误,
FAIL src/components/MyComp/MyComp.test.tsx (5.445 s)
× calls onValueChange (66 ms)
MyComp
√ test component initialization with props (3 ms)
● calls onValueChange
expect(jest.fn()).toHaveBeenCalledWith(...expected)
Expected: "val", "val"
Number of calls: 0
53 |
54 | .simulate("click", { data: "val", value: "val" });
> 55 | expect(changeEvent).toHaveBeenCalledWith("val", "val");
at Object.<anonymous>
不模拟单击 AnotherComponent。我在这里做错了什么吗?提前致谢
解决方案
推荐阅读
- java - pom.xml 文件在尝试添加依赖项时显示错误
- node.js - 仅安装 devDependencies - `swagger-express-middleware` 会阻止 `glob` 安装
- c# - C# 以某种方式将排名添加到列表元素
- javascript - 如何最小化 AWS Chime SDK 浏览器客户端使用的 JavaScript 文件的大小
- r - 2-Sample Cramer-Von Mises 统计的演示 - 如何显示总和以使 CYM 统计的线
- javascript - Reactjs复选框在组件的更新状态下不起作用
- oracle-sqldeveloper - 无法在 SQL Developer for Oracle 的单独目录中导出数据
- javascript - 如何批量提交()用于firestore大量文档?
- python - 在 ListView django 中进行分页
- java - 设置用于验证从服务器接收到的证书的最小 RSA 密钥长度