дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-03 16:51:16 +02:00
Search Everywhere opened at a fixed 700 px width with its height capped at 600 px, clipping long paths, containers and previews on wide monitors. Dialog's resizable="both" now resizes from all eight edges and corners (the opposite edge stays put), not only the bottom-right grip, and Search Everywhere uses it with a remembered size (openidea.search.everywhereSize.v1, clamped to its minimum). The other resizable dialogs get the edge handles too. Covered by Dialog and Search Everywhere tests. Co-authored-by: elfadl <[email protected]>
704 рядки
24 KiB
TypeScript
704 рядки
24 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { act, fireEvent, render, screen, within } from "@testing-library/react";
|
|
import userEvent from "@testing-library/user-event";
|
|
import { useLayoutEffect, useRef, type ReactNode } from "react";
|
|
import { Popup } from "./Popup";
|
|
import { isPopupOpen, openLayer } from "./popupState";
|
|
import { usePlatform } from "@/platform/platformInfo";
|
|
import { Dialog, dialogPlatformOf, orderDialogButtons, type DialogButton, type DialogPlatform } from "./Dialog";
|
|
|
|
const labels = (bs: readonly DialogButton[]) => bs.map((b) => b.label);
|
|
|
|
function safeDelete(handlers: { view?: () => void; anyway?: () => void; cancel?: () => void } = {}): DialogButton[] {
|
|
return [
|
|
{ label: "View Usages", role: "default", onClick: handlers.view ?? (() => {}) },
|
|
{ label: "Delete Anyway", onClick: handlers.anyway ?? (() => {}) },
|
|
{ label: "Cancel", role: "cancel", onClick: handlers.cancel },
|
|
];
|
|
}
|
|
|
|
function footerLabels() {
|
|
const footer = document.querySelector("[data-dialog-footer]") as HTMLElement;
|
|
return within(footer)
|
|
.getAllByRole("button")
|
|
.map((b) => b.textContent);
|
|
}
|
|
|
|
describe("orderDialogButtons", () => {
|
|
it("macOS: Cancel first, the default button last (IDEA sortActionsOnMac)", () => {
|
|
expect(labels(orderDialogButtons(safeDelete(), "mac"))).toEqual(["Cancel", "Delete Anyway", "View Usages"]);
|
|
const settings: DialogButton[] = [
|
|
{ label: "OK", role: "default" },
|
|
{ label: "Cancel", role: "cancel" },
|
|
{ label: "Apply" },
|
|
];
|
|
expect(labels(orderDialogButtons(settings, "mac"))).toEqual(["Cancel", "Apply", "OK"]);
|
|
});
|
|
|
|
it("Windows / Linux: the default button first, then declaration order", () => {
|
|
expect(labels(orderDialogButtons(safeDelete(), "other"))).toEqual(["View Usages", "Delete Anyway", "Cancel"]);
|
|
const settings: DialogButton[] = [
|
|
{ label: "Cancel", role: "cancel" },
|
|
{ label: "Apply" },
|
|
{ label: "OK", role: "default" },
|
|
];
|
|
expect(labels(orderDialogButtons(settings, "other"))).toEqual(["OK", "Cancel", "Apply"]);
|
|
});
|
|
|
|
it("maps host OS to chrome", () => {
|
|
expect(dialogPlatformOf("macos")).toBe("mac");
|
|
expect(dialogPlatformOf("web")).toBe("mac");
|
|
expect(dialogPlatformOf("windows")).toBe("other");
|
|
expect(dialogPlatformOf("linux")).toBe("other");
|
|
});
|
|
});
|
|
|
|
describe("Dialog chrome", () => {
|
|
const initialOs = usePlatform.getState().os;
|
|
afterEach(() => usePlatform.setState({ os: initialOs }));
|
|
|
|
const show = (platform?: DialogPlatform, onClose = vi.fn()) =>
|
|
render(
|
|
<Dialog open onClose={onClose} title="Usages Detected" buttons={safeDelete()} platform={platform}>
|
|
<p>Usages found</p>
|
|
</Dialog>,
|
|
);
|
|
|
|
it("macOS: a sheet titled in its content, no window buttons, Cancel … default rightmost", () => {
|
|
const onClose = vi.fn();
|
|
show("mac", onClose);
|
|
const bar = document.querySelector("[data-dialog-titlebar]") as HTMLElement;
|
|
expect(within(bar).queryAllByRole("button")).toEqual([]);
|
|
expect(bar.textContent).toBe("Usages Detected");
|
|
expect(footerLabels()).toEqual(["Cancel", "Delete Anyway", "View Usages"]);
|
|
fireEvent.keyDown(document.activeElement ?? document.body, { key: "Escape" });
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("Windows / Linux: title then a close button, default leftmost", () => {
|
|
const onClose = vi.fn();
|
|
show("other", onClose);
|
|
const bar = document.querySelector("[data-dialog-titlebar]") as HTMLElement;
|
|
expect(
|
|
within(bar)
|
|
.getAllByRole("button")
|
|
.map((b) => b.getAttribute("aria-label")),
|
|
).toEqual(["Close"]);
|
|
expect(footerLabels()).toEqual(["View Usages", "Delete Anyway", "Cancel"]);
|
|
fireEvent.click(within(bar).getByRole("button", { name: "Close" }));
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("follows the host platform by default", () => {
|
|
usePlatform.setState({ os: "windows" });
|
|
show();
|
|
expect(footerLabels()).toEqual(["View Usages", "Delete Anyway", "Cancel"]);
|
|
});
|
|
|
|
it("popups (top / untitled) get no window title bar", () => {
|
|
render(
|
|
<Dialog open onClose={() => {}} aria-label="Recent Files">
|
|
<button>row</button>
|
|
</Dialog>,
|
|
);
|
|
expect(document.querySelector("[data-dialog-titlebar]")).toBeNull();
|
|
});
|
|
|
|
it("drags a popup by a [data-dialog-drag] handle, but not by its controls or body", () => {
|
|
render(
|
|
<Dialog open onClose={() => {}} position="top" aria-label="Search Everywhere">
|
|
<div data-dialog-drag="">
|
|
<span>header</span>
|
|
<button>tab</button>
|
|
</div>
|
|
<div>rows</div>
|
|
</Dialog>,
|
|
);
|
|
const dialog = screen.getByRole("dialog");
|
|
const handle = dialog.querySelector("[data-dialog-drag]")!;
|
|
const down = (el: Element, x: number) => fireEvent.pointerDown(el, { button: 0, clientX: x, clientY: 10 });
|
|
down(handle.querySelector("button")!, 10);
|
|
down(dialog.querySelector("div:last-child")!, 10);
|
|
expect(dialog.style.translate).toBe("");
|
|
down(handle, 10);
|
|
fireEvent.pointerMove(window, { buttons: 1, clientX: 200, clientY: 40 });
|
|
fireEvent.pointerUp(window);
|
|
expect(dialog.style.translate).toBe("190px 30px");
|
|
});
|
|
|
|
it("resizes a top popup by its corner, keeping its top edge, and reports the size", () => {
|
|
const onResized = vi.fn();
|
|
render(
|
|
<Dialog
|
|
open
|
|
onClose={() => {}}
|
|
position="top"
|
|
aria-label="Find in Files"
|
|
width={400}
|
|
resizable="both"
|
|
minWidth={200}
|
|
minHeight={100}
|
|
onResized={onResized}
|
|
>
|
|
<div>rows</div>
|
|
</Dialog>,
|
|
);
|
|
const dialog = screen.getByRole("dialog");
|
|
fireEvent.pointerDown(screen.getByRole("separator", { name: "Resize" }), { button: 0, clientX: 0, clientY: 0 });
|
|
fireEvent.pointerMove(window, { buttons: 1, clientX: 500, clientY: 300 });
|
|
fireEvent.pointerUp(window);
|
|
expect([dialog.style.width, dialog.style.height]).toEqual(["500px", "300px"]);
|
|
expect(dialog.style.translate).toBe("250px 0px");
|
|
expect(onResized).toHaveBeenCalledExactlyOnceWith({ w: 500, h: 300 });
|
|
});
|
|
|
|
it("opens a resizable popup at a remembered size", () => {
|
|
render(
|
|
<Dialog
|
|
open
|
|
onClose={() => {}}
|
|
position="top"
|
|
aria-label="Find in Files"
|
|
resizable="both"
|
|
initialSize={{ w: 600, h: 400 }}
|
|
>
|
|
<div>rows</div>
|
|
</Dialog>,
|
|
);
|
|
const dialog = screen.getByRole("dialog");
|
|
expect([dialog.style.width, dialog.style.height]).toEqual(["600px", "400px"]);
|
|
});
|
|
|
|
it("caps a content-sized dialog at maxHeight until it is resized", () => {
|
|
render(
|
|
<Dialog open onClose={() => {}} title="Push" resizable="both" maxHeight={720}>
|
|
<div>rows</div>
|
|
</Dialog>,
|
|
);
|
|
const dialog = screen.getByRole("dialog");
|
|
expect(dialog).not.toHaveAttribute("data-resized");
|
|
expect(dialog.style.maxHeight).toBe("min(720px, 100%)");
|
|
fireEvent.pointerDown(screen.getByRole("separator", { name: "Resize" }), { button: 0, clientX: 0, clientY: 0 });
|
|
fireEvent.pointerMove(window, { buttons: 1, clientX: 500, clientY: 800 });
|
|
fireEvent.pointerUp(window);
|
|
expect(dialog).toHaveAttribute("data-resized");
|
|
expect(dialog.style.maxHeight).toBe("");
|
|
expect(dialog.style.height).not.toBe("");
|
|
});
|
|
|
|
it("resizes from the left edge without shifting the right edge or changing height", () => {
|
|
const onResized = vi.fn();
|
|
render(
|
|
<Dialog
|
|
open
|
|
onClose={() => {}}
|
|
position="top"
|
|
aria-label="Search Everywhere"
|
|
width={400}
|
|
resizable="both"
|
|
minWidth={200}
|
|
minHeight={100}
|
|
onResized={onResized}
|
|
>
|
|
<div>rows</div>
|
|
</Dialog>,
|
|
);
|
|
const dialog = screen.getByRole("dialog");
|
|
vi.spyOn(dialog, "getBoundingClientRect").mockReturnValue({
|
|
x: 200,
|
|
y: 50,
|
|
left: 200,
|
|
top: 50,
|
|
right: 600,
|
|
bottom: 350,
|
|
width: 400,
|
|
height: 300,
|
|
toJSON: () => ({}),
|
|
});
|
|
const west = dialog.querySelector('[data-resize="w"]') as HTMLElement;
|
|
fireEvent.pointerDown(west, { button: 0, clientX: 200, clientY: 150 });
|
|
fireEvent.pointerMove(window, { buttons: 1, clientX: 120, clientY: 150 });
|
|
fireEvent.pointerUp(window);
|
|
expect([dialog.style.width, dialog.style.height]).toEqual(["480px", "300px"]);
|
|
expect(dialog.style.translate).toBe("-40px 0px");
|
|
expect(onResized).toHaveBeenCalledExactlyOnceWith({ w: 480, h: 300 });
|
|
});
|
|
|
|
it("resizes from the top edge for both top popups and centered dialogs without dragging", () => {
|
|
const onResizedTop = vi.fn();
|
|
const { unmount } = render(
|
|
<Dialog
|
|
open
|
|
onClose={() => {}}
|
|
position="top"
|
|
aria-label="Top Popup"
|
|
width={400}
|
|
resizable="both"
|
|
minWidth={200}
|
|
minHeight={100}
|
|
onResized={onResizedTop}
|
|
>
|
|
<div data-dialog-drag="">header</div>
|
|
</Dialog>,
|
|
);
|
|
const topDialog = screen.getByRole("dialog");
|
|
vi.spyOn(topDialog, "getBoundingClientRect").mockReturnValue({
|
|
x: 200,
|
|
y: 120,
|
|
left: 200,
|
|
top: 120,
|
|
right: 600,
|
|
bottom: 420,
|
|
width: 400,
|
|
height: 300,
|
|
toJSON: () => ({}),
|
|
});
|
|
const topNorth = topDialog.querySelector('[data-resize="n"]') as HTMLElement;
|
|
fireEvent.pointerDown(topNorth, { button: 0, clientX: 300, clientY: 120 });
|
|
fireEvent.pointerMove(window, { buttons: 1, clientX: 350, clientY: 70 });
|
|
fireEvent.pointerUp(window);
|
|
expect([topDialog.style.width, topDialog.style.height]).toEqual(["400px", "350px"]);
|
|
expect(topDialog.style.translate).toBe("0px -50px");
|
|
expect(onResizedTop).toHaveBeenCalledExactlyOnceWith({ w: 400, h: 350 });
|
|
unmount();
|
|
|
|
const onResizedCenter = vi.fn();
|
|
render(
|
|
<Dialog
|
|
open
|
|
onClose={() => {}}
|
|
title="Centered"
|
|
width={400}
|
|
resizable="both"
|
|
minWidth={200}
|
|
minHeight={100}
|
|
onResized={onResizedCenter}
|
|
>
|
|
<div>body</div>
|
|
</Dialog>,
|
|
);
|
|
const centerDialog = screen.getByRole("dialog");
|
|
vi.spyOn(centerDialog, "getBoundingClientRect").mockReturnValue({
|
|
x: 200,
|
|
y: 150,
|
|
left: 200,
|
|
top: 150,
|
|
right: 600,
|
|
bottom: 450,
|
|
width: 400,
|
|
height: 300,
|
|
toJSON: () => ({}),
|
|
});
|
|
const centerNorth = centerDialog.querySelector('[data-resize="n"]') as HTMLElement;
|
|
fireEvent.pointerDown(centerNorth, { button: 0, clientX: 300, clientY: 150 });
|
|
fireEvent.pointerMove(window, { buttons: 1, clientX: 350, clientY: 90 });
|
|
fireEvent.pointerUp(window);
|
|
expect([centerDialog.style.width, centerDialog.style.height]).toEqual(["400px", "360px"]);
|
|
expect(centerDialog.style.translate).toBe("0px -30px");
|
|
expect(onResizedCenter).toHaveBeenCalledExactlyOnceWith({ w: 400, h: 360 });
|
|
});
|
|
|
|
it("marks data-resized when opened with initialSize", () => {
|
|
render(
|
|
<Dialog
|
|
open
|
|
onClose={() => {}}
|
|
position="top"
|
|
aria-label="Remembered"
|
|
resizable="both"
|
|
initialSize={{ w: 600, h: 400 }}
|
|
>
|
|
<div>rows</div>
|
|
</Dialog>,
|
|
);
|
|
expect(screen.getByRole("dialog")).toHaveAttribute("data-resized");
|
|
});
|
|
|
|
it("still drags a window dialog by its title bar", () => {
|
|
render(
|
|
<Dialog open onClose={() => {}} title="Usages Detected" buttons={safeDelete()} platform="other">
|
|
<p>Usages found</p>
|
|
</Dialog>,
|
|
);
|
|
const dialog = screen.getByRole("dialog");
|
|
const bar = document.querySelector("[data-dialog-titlebar]") as HTMLElement;
|
|
fireEvent.pointerDown(bar, { button: 0, clientX: 10, clientY: 10 });
|
|
fireEvent.pointerMove(window, { buttons: 1, clientX: 60, clientY: 40 });
|
|
fireEvent.pointerUp(window);
|
|
expect(dialog.style.translate).toBe("80px 30px");
|
|
// The close button never starts a drag.
|
|
fireEvent.pointerDown(within(bar).getByRole("button", { name: "Close" }), { button: 0, clientX: 10, clientY: 10 });
|
|
fireEvent.pointerMove(window, { buttons: 1, clientX: 200, clientY: 40 });
|
|
fireEvent.pointerUp(window);
|
|
expect(dialog.style.translate).toBe("80px 30px");
|
|
});
|
|
|
|
it("renders the help button and footer start on the left", () => {
|
|
const onHelp = vi.fn();
|
|
render(
|
|
<Dialog
|
|
open
|
|
onClose={() => {}}
|
|
title="T"
|
|
onHelp={onHelp}
|
|
footerStart={<span>status</span>}
|
|
buttons={safeDelete()}
|
|
platform="mac"
|
|
>
|
|
<p>x</p>
|
|
</Dialog>,
|
|
);
|
|
expect(footerLabels()[0]).toBe("?");
|
|
fireEvent.click(screen.getByRole("button", { name: "Help" }));
|
|
expect(onHelp).toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe("Dialog keyboard", () => {
|
|
it("Enter runs the default button, Escape cancels", () => {
|
|
const view = vi.fn();
|
|
const onClose = vi.fn();
|
|
render(
|
|
<Dialog open onClose={onClose} title="Usages Detected" buttons={safeDelete({ view })} platform="mac">
|
|
<input aria-label="Name" />
|
|
</Dialog>,
|
|
);
|
|
fireEvent.keyDown(screen.getByLabelText("Name"), { key: "Enter" });
|
|
expect(view).toHaveBeenCalledTimes(1);
|
|
fireEvent.keyDown(document.body, { key: "Escape" });
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("Enter is left to text areas, buttons and handlers that consumed it; disabled default does nothing", () => {
|
|
const ok = vi.fn();
|
|
const { rerender } = render(
|
|
<Dialog
|
|
open
|
|
onClose={() => {}}
|
|
title="T"
|
|
buttons={[
|
|
{ label: "OK", role: "default", onClick: ok },
|
|
{ label: "Cancel", role: "cancel" },
|
|
]}
|
|
>
|
|
<textarea aria-label="Message" />
|
|
<input aria-label="Own" onKeyDown={(e) => e.key === "Enter" && e.preventDefault()} />
|
|
</Dialog>,
|
|
);
|
|
fireEvent.keyDown(screen.getByLabelText("Message"), { key: "Enter" });
|
|
fireEvent.keyDown(screen.getByLabelText("Own"), { key: "Enter" });
|
|
fireEvent.keyDown(screen.getByRole("button", { name: "Cancel" }), { key: "Enter" });
|
|
expect(ok).not.toHaveBeenCalled();
|
|
// ⌘Enter submits from a text area.
|
|
fireEvent.keyDown(screen.getByLabelText("Message"), { key: "Enter", metaKey: true });
|
|
expect(ok).toHaveBeenCalledTimes(1);
|
|
rerender(
|
|
<Dialog
|
|
open
|
|
onClose={() => {}}
|
|
title="T"
|
|
buttons={[{ label: "OK", role: "default", onClick: ok, disabled: true }]}
|
|
>
|
|
<input aria-label="Field" />
|
|
</Dialog>,
|
|
);
|
|
fireEvent.keyDown(screen.getByLabelText("Field"), { key: "Enter" });
|
|
expect(ok).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("a cancel button without a handler closes the dialog", () => {
|
|
const onClose = vi.fn();
|
|
render(
|
|
<Dialog
|
|
open
|
|
onClose={onClose}
|
|
title="T"
|
|
buttons={[
|
|
{ label: "OK", role: "default", onClick: () => {} },
|
|
{ label: "Cancel", role: "cancel" },
|
|
]}
|
|
>
|
|
<p>x</p>
|
|
</Dialog>,
|
|
);
|
|
fireEvent.click(screen.getByRole("button", { name: "Cancel" }));
|
|
expect(onClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("focuses the default button when the content has no controls; Tab skips the title bar", () => {
|
|
render(
|
|
<Dialog open onClose={() => {}} title="Usages Detected" buttons={safeDelete()} platform="mac">
|
|
<p>Usages found</p>
|
|
</Dialog>,
|
|
);
|
|
expect(document.activeElement).toBe(screen.getByRole("button", { name: "View Usages" }));
|
|
fireEvent.keyDown(document.activeElement!, { key: "Tab" });
|
|
expect(document.activeElement).toBe(screen.getByRole("button", { name: "Cancel" }));
|
|
});
|
|
});
|
|
|
|
describe("Dialog focus containment", () => {
|
|
it.each(["Enter", "Escape"])("keeps deferred editor focus out and restores it after %s", async (key) => {
|
|
const editor = document.createElement("textarea");
|
|
document.body.append(editor);
|
|
editor.focus();
|
|
const accepted = vi.fn();
|
|
const cancelled = vi.fn();
|
|
const { rerender, unmount } = render(
|
|
<Dialog
|
|
open
|
|
title="Add File to Git"
|
|
icon="question"
|
|
onClose={cancelled}
|
|
buttons={[
|
|
{ label: "Add", role: "default", onClick: accepted },
|
|
{ label: "Cancel", role: "cancel" },
|
|
]}
|
|
>
|
|
<p>Do you want to add the following file to Git?</p>
|
|
</Dialog>,
|
|
);
|
|
await act(async () => {
|
|
await Promise.resolve();
|
|
editor.focus();
|
|
});
|
|
expect(document.activeElement).toBe(screen.getByRole("button", { name: "Add" }));
|
|
await userEvent.keyboard(`{${key}}`);
|
|
expect(accepted).toHaveBeenCalledTimes(key === "Enter" ? 1 : 0);
|
|
expect(cancelled).toHaveBeenCalledTimes(key === "Escape" ? 1 : 0);
|
|
rerender(
|
|
<Dialog open={false} onClose={cancelled}>
|
|
<p>closed</p>
|
|
</Dialog>,
|
|
);
|
|
expect(document.activeElement).toBe(editor);
|
|
unmount();
|
|
editor.remove();
|
|
});
|
|
|
|
it("lets nested dialogs own focus, then restores the last field in the parent", () => {
|
|
const { rerender } = render(
|
|
<Dialog open title="Parent" onClose={() => {}}>
|
|
<input aria-label="First" />
|
|
<input aria-label="Last" />
|
|
</Dialog>,
|
|
);
|
|
screen.getByLabelText("Last").focus();
|
|
const content = (child: boolean) => (
|
|
<>
|
|
<Dialog open title="Parent" onClose={() => {}}>
|
|
<input aria-label="First" />
|
|
<input aria-label="Last" />
|
|
</Dialog>
|
|
{child && (
|
|
<Dialog open title="Child" onClose={() => {}}>
|
|
<input aria-label="Child field" />
|
|
</Dialog>
|
|
)}
|
|
</>
|
|
);
|
|
rerender(content(true));
|
|
expect(document.activeElement).toBe(screen.getByLabelText("Child field"));
|
|
rerender(content(false));
|
|
expect(document.activeElement).toBe(screen.getByLabelText("Last"));
|
|
});
|
|
});
|
|
|
|
it("allows a menu above a dialog and remembers its last focused control", () => {
|
|
const { unmount } = render(
|
|
<Dialog open title="Parent" onClose={() => {}}>
|
|
<input aria-label="First" />
|
|
<input aria-label="Last" />
|
|
</Dialog>,
|
|
);
|
|
const last = screen.getByLabelText("Last");
|
|
last.focus();
|
|
const menu = document.createElement("button");
|
|
document.body.append(menu);
|
|
const layer = openLayer(() => menu);
|
|
menu.focus();
|
|
expect(document.activeElement).toBe(menu);
|
|
layer.release();
|
|
last.focus();
|
|
document.body.focus();
|
|
const outside = document.createElement("input");
|
|
document.body.append(outside);
|
|
outside.focus();
|
|
expect(document.activeElement).toBe(last);
|
|
unmount();
|
|
menu.remove();
|
|
outside.remove();
|
|
});
|
|
|
|
it("allows a newly mounted menu to focus before its popup layer registers", () => {
|
|
function MenuItem() {
|
|
const ref = useRef<HTMLButtonElement>(null);
|
|
useLayoutEffect(() => {
|
|
ref.current?.focus();
|
|
}, []);
|
|
return <button ref={ref}>Menu item</button>;
|
|
}
|
|
const show = (menu: boolean) => (
|
|
<>
|
|
<Dialog open title="Parent" onClose={() => {}}>
|
|
<input aria-label="Field" />
|
|
</Dialog>
|
|
{menu && (
|
|
<Popup open anchor={{ x: 0, y: 0 }} onClose={() => {}} autoFocus={false}>
|
|
<MenuItem />
|
|
</Popup>
|
|
)}
|
|
</>
|
|
);
|
|
const { rerender } = render(show(false));
|
|
rerender(show(true));
|
|
expect(document.activeElement).toBe(screen.getByRole("button", { name: "Menu item" }));
|
|
});
|
|
|
|
describe("Dialog over popups", () => {
|
|
it("closes the popups open when it opens, but not one it is rendered inside", () => {
|
|
const anchor = document.createElement("button");
|
|
document.body.append(anchor);
|
|
const sessions = vi.fn();
|
|
const pinned = vi.fn();
|
|
const { rerender } = render(
|
|
<>
|
|
<Popup open anchor={anchor} onClose={sessions} aria-label="Sessions">
|
|
<span>tasks</span>
|
|
</Popup>
|
|
<Popup open anchor={anchor} onClose={pinned} closeOnDeactivate={false} aria-label="Pinned">
|
|
<span>pinned</span>
|
|
</Popup>
|
|
</>,
|
|
);
|
|
rerender(
|
|
<>
|
|
<Popup open anchor={anchor} onClose={sessions} aria-label="Sessions">
|
|
<span>tasks</span>
|
|
</Popup>
|
|
<Popup open anchor={anchor} onClose={pinned} closeOnDeactivate={false} aria-label="Pinned">
|
|
<span>pinned</span>
|
|
</Popup>
|
|
<Dialog open onClose={() => {}} title="Delete Agent Task">
|
|
<span>delete?</span>
|
|
</Dialog>
|
|
</>,
|
|
);
|
|
expect(sessions).toHaveBeenCalledExactlyOnceWith("outside");
|
|
// Popups that stay open while the window is inactive stay open here too.
|
|
expect(pinned).not.toHaveBeenCalled();
|
|
|
|
const outer = vi.fn();
|
|
const Breakpoint = ({ editing }: { editing: boolean }) => (
|
|
<Popup open anchor={anchor} onClose={outer} aria-label="Breakpoint">
|
|
<Dialog open={editing} onClose={() => {}} title="Edit">
|
|
<span>inside</span>
|
|
</Dialog>
|
|
</Popup>
|
|
);
|
|
const inner = render(<Breakpoint editing={false} />);
|
|
inner.rerender(<Breakpoint editing />);
|
|
expect(screen.getByRole("dialog", { name: "Edit" })).toBeTruthy();
|
|
expect(outer).not.toHaveBeenCalled();
|
|
anchor.remove();
|
|
});
|
|
});
|
|
|
|
describe("Dialog that takes no focus", () => {
|
|
const teardown: (() => void)[] = [];
|
|
afterEach(() => {
|
|
for (const undo of teardown.splice(0)) undo();
|
|
});
|
|
|
|
function setup(extra?: ReactNode) {
|
|
const editor = document.createElement("input");
|
|
editor.setAttribute("aria-label", "Editor");
|
|
document.body.append(editor);
|
|
editor.focus();
|
|
const allow = vi.fn();
|
|
const deny = vi.fn();
|
|
const dialog = (open: boolean) => (
|
|
<>
|
|
{extra}
|
|
<Dialog
|
|
open={open}
|
|
takesFocus={false}
|
|
icon="warning"
|
|
title="test client wants to run Build"
|
|
onClose={deny}
|
|
buttons={[
|
|
{ label: "Allow", onClick: allow },
|
|
{ label: "Deny", role: "default", autoFocus: true, onClick: deny },
|
|
]}
|
|
>
|
|
<p>Background project</p>
|
|
</Dialog>
|
|
</>
|
|
);
|
|
const view = render(dialog(true));
|
|
teardown.push(() => {
|
|
view.unmount();
|
|
editor.remove();
|
|
});
|
|
const close = () => view.rerender(dialog(false));
|
|
return { editor, allow, deny, close };
|
|
}
|
|
|
|
it("leaves the focus outside and answers no key typed there", () => {
|
|
const { editor, allow, deny } = setup();
|
|
expect(document.activeElement).toBe(editor);
|
|
expect(isPopupOpen()).toBe(false);
|
|
fireEvent.keyDown(editor, { key: "Enter" });
|
|
fireEvent.keyDown(editor, { key: "Escape" });
|
|
fireEvent.keyDown(document.body, { key: "Enter" });
|
|
expect(allow).not.toHaveBeenCalled();
|
|
expect(deny).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("leaves a popup open underneath with its keys", () => {
|
|
const closed = vi.fn();
|
|
const { editor, deny } = setup(
|
|
<Popup open anchor={{ x: 0, y: 0 }} onClose={closed} autoFocus={false} aria-label="Completion">
|
|
<span>item</span>
|
|
</Popup>,
|
|
);
|
|
expect(closed).not.toHaveBeenCalled();
|
|
fireEvent.keyDown(editor, { key: "Escape" });
|
|
expect(closed).toHaveBeenCalledExactlyOnceWith("escape");
|
|
expect(deny).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("answers a click on a button", () => {
|
|
const { allow } = setup();
|
|
fireEvent.click(screen.getByRole("button", { name: "Allow" }));
|
|
expect(allow).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it("takes the keyboard once clicked into: Enter runs the default button, Escape cancels", async () => {
|
|
const { editor, deny } = setup();
|
|
await userEvent.click(screen.getByText("Background project"));
|
|
expect(document.activeElement).not.toBe(editor);
|
|
expect(isPopupOpen()).toBe(true);
|
|
await userEvent.keyboard("{Enter}");
|
|
expect(deny).toHaveBeenCalledTimes(1);
|
|
await userEvent.keyboard("{Escape}");
|
|
expect(deny).toHaveBeenCalledTimes(2);
|
|
// Focus stays in the dialog once it has it.
|
|
editor.focus();
|
|
expect(document.activeElement).not.toBe(editor);
|
|
});
|
|
|
|
it("gives the focus back only when it took it", async () => {
|
|
const first = setup();
|
|
first.editor.blur();
|
|
first.close();
|
|
expect(document.activeElement).toBe(document.body);
|
|
|
|
const second = setup();
|
|
await userEvent.click(screen.getByText("Background project"));
|
|
second.close();
|
|
expect(document.activeElement).toBe(second.editor);
|
|
expect(isPopupOpen()).toBe(false);
|
|
});
|
|
});
|