Because react-dropzone uses asynchronous callbacks for drag-and-drop events, you should use @testing-library/react and wrap event triggers in act() to ensure tests are reliable. Note that Enzyme is not supported.
import React from "react";
import Dropzone from "react-dropzone";
import {act, fireEvent, render} from "@testing-library/react";
test("invoke onDragEnter when dragenter event occurs", async () => {
const file = new File([JSON.stringify({ping: true})], "ping.json", {type: "application/json"});
const data = mockData([file]);
const onDragEnter = jest.fn();
const ui = (
<Dropzone onDragEnter={onDragEnter}>
{({getRootProps, getInputProps}) => (
<div {...getRootProps()}>
<input {...getInputProps()} />
</div>
)}
</Dropzone>
);
const {container} = render(ui);
await act(() => fireEvent.dragEnter(container.querySelector("div"), data));
expect(onDragEnter).toHaveBeenCalled();
});
function mockData(files) {
return {
dataTransfer: {
files,
items: files.map(file => ({
kind: "file",
type: file.type,
getAsFile: () => file
})),
types: ["Files"]
}
};
}