fix: prevent file drags from replacing the app view (#71)

* fix: prevent dropped files from replacing the app view

* test: use drag events in app navigation test
This commit is contained in:
Matt Toohey
2026-04-02 13:42:32 +11:00
committed by GitHub
parent 25710dc534
commit d15244758a
2 changed files with 51 additions and 0 deletions
+39
View File
@@ -0,0 +1,39 @@
import { render } from "@testing-library/react";
import { afterEach, describe, expect, it, vi } from "vitest";
import { App } from "./App";
vi.mock("@/app/AppShell", () => ({
AppShell: () => <div data-testid="app-shell" />,
}));
describe("App", () => {
afterEach(() => {
vi.unstubAllGlobals();
});
it("prevents default window navigation when files are dragged into the app", () => {
vi.stubGlobal("__TAURI_INTERNALS__", undefined);
vi.stubGlobal(
"DragEvent",
window.DragEvent ?? class DragEvent extends Event {},
);
render(<App />);
const dragOverEvent = new DragEvent("dragover", {
bubbles: true,
cancelable: true,
});
const dropEvent = new DragEvent("drop", {
bubbles: true,
cancelable: true,
});
window.dispatchEvent(dragOverEvent);
window.dispatchEvent(dropEvent);
expect(dragOverEvent.defaultPrevented).toBe(true);
expect(dropEvent.defaultPrevented).toBe(true);
});
});
+12
View File
@@ -4,6 +4,13 @@ import { AppShell } from "@/app/AppShell";
export function App() {
useEffect(() => {
const preventWindowFileNavigation = (event: DragEvent) => {
event.preventDefault();
};
window.addEventListener("dragover", preventWindowFileNavigation);
window.addEventListener("drop", preventWindowFileNavigation);
// Dynamic import to avoid crash in non-Tauri environments (e.g., Playwright E2E)
if (window.__TAURI_INTERNALS__) {
import("@tauri-apps/api/window").then(({ getCurrentWindow }) => {
@@ -12,6 +19,11 @@ export function App() {
.catch(() => {});
});
}
return () => {
window.removeEventListener("dragover", preventWindowFileNavigation);
window.removeEventListener("drop", preventWindowFileNavigation);
};
}, []);
return <AppShell />;