reactjs - 使用 fireEvent 发送值
问题描述
我正在使用fireEvent
from @testing-library/react
。我声明了一个反应组件如下:
const CustomParamTest = () => {
onButtonClick(customParam) {
console.log(customParam);
}
return (
<>
<button
data-testid="custom-param-button"
onClick={(customParam) => onButtonClick(customParam) }
>
Custom param
</button>
</>
);
}
我有这个测试:
describe('basic test', () => {
test('items should be empty', () => {
const { getByTestId } = render(<CustomParamTest />);
const customButton = getByTestId('custom-param-button');
fireEvent.click(customButton);
});
});
我想要实现的是使用点击事件发送自定义参数,如下所示:
fireEvent.click(customButton, myCustomParameter);
我想访问myCustomParameter
我的onButtonClick
函数内部。我可以使其工作如下,但我想要一个更好的解决方案:
我创建了一个这样的自定义点击事件:
const customEvent = createEvent.click(customButton, { button: 2 });
customEvent.myCustomProp = 'customValue';
fireEvent.click(customButton, customEvent);
在我的组件中,我可以像这样访问这个值:
onClick={(e) => console.log(e.nativeEvent.myCustomProp)}
我觉得应该有更好的方法来做到这一点。
解决方案
推荐阅读
- python - 使用python中的xml树将嵌套的XML内容转换为CSV
- amazon-web-services - 在 AWS 上使用 Calico 的 Kubernetes -- 无法 ping 不同节点上的 pod
- azure-devops - 部署时无法加载 API 定义获取错误,但在 localhost 上很好
- unit-testing - 如何在堆栈中重新运行测试
- javascript - 在 JavaScript 中为每个用户同步 API 中的关键部分
- excel - 如何让数据透视表与第二个表中定义的类别层次结构一起使用
- mongodb - 将具有相似数据的对象分组并使用 mongoose 重新格式化
- ruby-on-rails - 控制器的未定义方法“get”
- image - 如何双击图像/或链接来启动安装?APPLESCRIPT [请查看图片]
- java - 当 Java Web 项目中的大小达到近 4.6MB 时,文件下载失败并显示一个空文件