【发布时间】:2020-07-21 02:38:22
【问题描述】:
我正在使用user-event 尝试进行更“真实”的用户交互。但是,在我单击输入后,它不会触发 onChange 函数,因为默认情况下它只会打开文件资源管理器供用户上传文件。如何模拟用户上传文件?
我的代码:
// Component
const FileInputComponent = ({ handleFileUpload }) => (
<div>
<input type="file" id="testing" accept=".png,.jpg" onChange={handleFileUpload} />
<label htmlFor="testing">Input File Here</label>
</div>
);
// Test file
test("Clicking on the label button calls the `handleFileUpload` function", () => {
const handleFileUploadMockFn = jest.fn();
const { getByLabelText } = render(<FileInputComponent handleFileUpload={handleFileUploadMockFn} />
userEvent.click(getByLabelText("Input File Here"));
expect(handleFileUploadMockFn).toHaveBeenCalledTimes(1);
});
【问题讨论】: