test(a11y): cover the overlay state and the Ctrl/Cmd+K stand-down
SonarCloud's gate counted 6 new lines no test had loaded: the overlay state and focusOnMount (lib/overlays.svelte.ts), and the palette shortcut's new early return. overlays.test.ts drives them in Node: overlays.open follows either overlay, focusOnMount focuses without scrolling, and Ctrl+K is left alone under an overlay (palette shut, default not prevented) but toggles the palette otherwise. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
24fa8554e2
commit
91f49d2cef
1 file changed
+68
@@ -0,0 +1,68 @@
|
||||
import { afterEach, describe, expect, it, vi } from "vitest";
|
||||
import { focusOnMount, overlays } from "./overlays.svelte";
|
||||
import { installPaletteShortcut, palette } from "./palette.svelte";
|
||||
|
||||
afterEach(() => {
|
||||
overlays.consent = false;
|
||||
overlays.humanCheck = false;
|
||||
palette.hide();
|
||||
vi.unstubAllGlobals();
|
||||
});
|
||||
|
||||
describe("first-run overlays", () => {
|
||||
it("count as open while either the consent gate or the human check is", () => {
|
||||
expect(overlays.open).toBe(false);
|
||||
overlays.consent = true;
|
||||
expect(overlays.open).toBe(true);
|
||||
overlays.consent = false;
|
||||
overlays.humanCheck = true;
|
||||
expect(overlays.open).toBe(true);
|
||||
overlays.humanCheck = false;
|
||||
expect(overlays.open).toBe(false);
|
||||
});
|
||||
|
||||
it("take focus as they mount, without scrolling to it", () => {
|
||||
const focus = vi.fn();
|
||||
focusOnMount({ focus } as unknown as HTMLElement);
|
||||
expect(focus).toHaveBeenCalledWith({ preventScroll: true });
|
||||
});
|
||||
});
|
||||
|
||||
describe("the Ctrl/Cmd+K shortcut", () => {
|
||||
/** A keydown with only what the handler reads; this suite runs in Node. */
|
||||
function press(target: EventTarget, init: { key: string; ctrlKey?: boolean }): Event {
|
||||
const event = Object.assign(new Event("keydown", { cancelable: true }), {
|
||||
isComposing: false,
|
||||
keyCode: 0,
|
||||
metaKey: false,
|
||||
ctrlKey: false,
|
||||
...init,
|
||||
});
|
||||
target.dispatchEvent(event);
|
||||
return event;
|
||||
}
|
||||
|
||||
it("stands down under a first-run overlay, and toggles the palette otherwise", () => {
|
||||
const win = new EventTarget();
|
||||
vi.stubGlobal("window", win);
|
||||
const teardown = installPaletteShortcut();
|
||||
try {
|
||||
// Under the gate the palette would open beneath it and hold focus out
|
||||
// of sight, so the key is left alone entirely.
|
||||
overlays.consent = true;
|
||||
const ignored = press(win, { key: "k", ctrlKey: true });
|
||||
expect(palette.open).toBe(false);
|
||||
expect(ignored.defaultPrevented).toBe(false);
|
||||
|
||||
overlays.consent = false;
|
||||
const handled = press(win, { key: "k", ctrlKey: true });
|
||||
expect(palette.open).toBe(true);
|
||||
expect(handled.defaultPrevented).toBe(true);
|
||||
|
||||
press(win, { key: "Escape" });
|
||||
expect(palette.open).toBe(false);
|
||||
} finally {
|
||||
teardown();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user