mirror of
https://github.com/block/goose.git
synced 2026-07-17 12:56:20 +02:00
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:
@@ -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);
|
||||
});
|
||||
});
|
||||
@@ -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 />;
|
||||
|
||||
Reference in New Issue
Block a user