Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix: call onError if FS access API fails in secure ctx
  • Loading branch information
rolandjitsu committed May 7, 2022
1 parent 2556af7 commit a2039fd
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/index.js
Expand Up @@ -787,6 +787,12 @@ export function useDropzone(props = {}) {
if (inputRef.current) {
inputRef.current.value = null;
inputRef.current.click();
} else {
onErrCb(
new Error(
"Cannot open the file picker because the https://developer.mozilla.org/en-US/docs/Web/API/File_System_Access_API is not supported and no <input> was provided."
)
);
}
} else {
onErrCb(e);
Expand Down
49 changes: 49 additions & 0 deletions src/index.spec.js
Expand Up @@ -1833,6 +1833,55 @@ describe("useDropzone() hook", () => {
expect(dropzone).toContainElement(activeRef.current);
expect(onErrorSpy).toHaveBeenCalledWith(err);
});

test("showOpenFilePicker() should call {onError} when a security error occurs and no <input> was provided", async () => {
const activeRef = createRef();
const active = <span ref={activeRef}>I am active</span>;

const thenable = createThenable();
const showOpenFilePickerMock = jest
.fn()
.mockReturnValue(thenable.promise);

window.showOpenFilePicker = showOpenFilePickerMock;

const onErrorSpy = jest.fn();
const onDropSpy = jest.fn();
const onFileDialogOpenSpy = jest.fn();

const ui = (
<Dropzone
onError={onErrorSpy}
onDrop={onDropSpy}
onFileDialogOpen={onFileDialogOpenSpy}
accept={{
"application/pdf": [],
}}
multiple
useFsAccessApi
>
{({ getRootProps, isFileDialogActive }) => (
<div {...getRootProps()}>{isFileDialogActive && active}</div>
)}
</Dropzone>
);

const { container } = render(ui);

const dropzone = container.querySelector("div");

fireEvent.click(dropzone);

expect(activeRef.current).not.toBeNull();
expect(dropzone).toContainElement(activeRef.current);
expect(onFileDialogOpenSpy).toHaveBeenCalled();

const err = new DOMException("oops :(", "SecurityError");
await act(() => thenable.cancel(err));
expect(activeRef.current).not.toBeNull();
expect(dropzone).toContainElement(activeRef.current);
expect(onErrorSpy).toHaveBeenCalled();
});
});

describe("onKeyDown", () => {
Expand Down

0 comments on commit a2039fd

Please sign in to comment.