Я использую user-event , чтобы попытаться сделать больше 'realisti c' пользовательских взаимодействий. Однако после того, как я нажму на вход, он не будет запускать функцию 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);
});