如何使用 useDropZone 在 enzyme 和 jest 中为组件编写测试用例?
如何为这样的组件编写测试用例?模拟onChange或onClick函数不添加任何文件。
import React from 'react';
import {useDropzone} from 'react-dropzone';
function Basic(props) {
const {acceptedFiles, getRootProps, getInputProps} = useDropzone();
const files = acceptedFiles.map(file => (
<li key={file.path}>
{file.path} - {file.size} bytes
</li>
));
return (
<section className="container">
<div data-testid="dropDiv" {...getRootProps({className: 'dropzone'})}>
<input data-testid="dropInput" {...getInputProps()} />
<p>Drag 'n' drop some files here, or click to select files</p>
</div>
<aside>
<h4>Files</h4>
<ul>{files}</ul>
</aside>
</section>
);
}
How to write test case for a component like this? Simulation of onChange or onClick function does not add any files.
import React from 'react';
import {useDropzone} from 'react-dropzone';
function Basic(props) {
const {acceptedFiles, getRootProps, getInputProps} = useDropzone();
const files = acceptedFiles.map(file => (
<li key={file.path}>
{file.path} - {file.size} bytes
</li>
));
return (
<section className="container">
<div data-testid="dropDiv" {...getRootProps({className: 'dropzone'})}>
<input data-testid="dropInput" {...getInputProps()} />
<p>Drag 'n' drop some files here, or click to select files</p>
</div>
<aside>
<h4>Files</h4>
<ul>{files}</ul>
</aside>
</section>
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
测试组件的行为。 官方文档底部有一个关于测试的示例。建议将 react-testing-library 用于
react-拖放区
。您可以创建模拟文件并模拟放置区域上的
drop
事件。然后断言组件渲染什么。例如
index.tsx
:index.test.tsx
:测试结果:
Testing the behavior of the component. There is an example about testing at the bottom of the official doc. The react-testing-library is recommended to use for
react-dropzone
.You can create mock files and simulate the
drop
event on the drop zone. Then assert what does the component render.E.g.
index.tsx
:index.test.tsx
:Test result: