Skip to content

Commit a2039fd

Browse files
committed
fix: call onError if FS access API fails in secure ctx
1 parent 2556af7 commit a2039fd

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

src/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,12 @@ export function useDropzone(props = {}) {
787787
if (inputRef.current) {
788788
inputRef.current.value = null;
789789
inputRef.current.click();
790+
} else {
791+
onErrCb(
792+
new Error(
793+
"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."
794+
)
795+
);
790796
}
791797
} else {
792798
onErrCb(e);

src/index.spec.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,6 +1833,55 @@ describe("useDropzone() hook", () => {
18331833
expect(dropzone).toContainElement(activeRef.current);
18341834
expect(onErrorSpy).toHaveBeenCalledWith(err);
18351835
});
1836+
1837+
test("showOpenFilePicker() should call {onError} when a security error occurs and no <input> was provided", async () => {
1838+
const activeRef = createRef();
1839+
const active = <span ref={activeRef}>I am active</span>;
1840+
1841+
const thenable = createThenable();
1842+
const showOpenFilePickerMock = jest
1843+
.fn()
1844+
.mockReturnValue(thenable.promise);
1845+
1846+
window.showOpenFilePicker = showOpenFilePickerMock;
1847+
1848+
const onErrorSpy = jest.fn();
1849+
const onDropSpy = jest.fn();
1850+
const onFileDialogOpenSpy = jest.fn();
1851+
1852+
const ui = (
1853+
<Dropzone
1854+
onError={onErrorSpy}
1855+
onDrop={onDropSpy}
1856+
onFileDialogOpen={onFileDialogOpenSpy}
1857+
accept={{
1858+
"application/pdf": [],
1859+
}}
1860+
multiple
1861+
useFsAccessApi
1862+
>
1863+
{({ getRootProps, isFileDialogActive }) => (
1864+
<div {...getRootProps()}>{isFileDialogActive && active}</div>
1865+
)}
1866+
</Dropzone>
1867+
);
1868+
1869+
const { container } = render(ui);
1870+
1871+
const dropzone = container.querySelector("div");
1872+
1873+
fireEvent.click(dropzone);
1874+
1875+
expect(activeRef.current).not.toBeNull();
1876+
expect(dropzone).toContainElement(activeRef.current);
1877+
expect(onFileDialogOpenSpy).toHaveBeenCalled();
1878+
1879+
const err = new DOMException("oops :(", "SecurityError");
1880+
await act(() => thenable.cancel(err));
1881+
expect(activeRef.current).not.toBeNull();
1882+
expect(dropzone).toContainElement(activeRef.current);
1883+
expect(onErrorSpy).toHaveBeenCalled();
1884+
});
18361885
});
18371886

18381887
describe("onKeyDown", () => {

0 commit comments

Comments
 (0)