Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
3.4 kB ยท 105 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { getModifierKey, isKeyboardOwned, isResetShortcut } from "./keyboard";
// No DOM under deno test: a minimal HTMLElement that answers matches/closest.class FakeElement { constructor( private tag: string, private role?: string, public isContentEditable = false, ) {} matches(selector: string) { return selector.split(", ").includes(this.tag); } closest(selector: string) { return this.role && selector.includes(`[role="${this.role}"]`) ? this : null; }}
const original = Object.getOwnPropertyDescriptor(globalThis, "HTMLElement");beforeAll(() => { Object.defineProperty(globalThis, "HTMLElement", { value: FakeElement, configurable: true, });});afterAll(() => { if (original) Object.defineProperty(globalThis, "HTMLElement", original); else delete (globalThis as { HTMLElement?: unknown }).HTMLElement;});
const el = (...args: ConstructorParameters<typeof FakeElement>) => new FakeElement(...args) as unknown as EventTarget;
describe("isKeyboardOwned", () => { it("is owned by form fields and contenteditable", () => { expect(isKeyboardOwned(el("input"))).toBe(true); expect(isKeyboardOwned(el("textarea"))).toBe(true); expect(isKeyboardOwned(el("select"))).toBe(true); expect(isKeyboardOwned(el("div", undefined, true))).toBe(true); });
it("is owned inside an open overlay", () => { expect(isKeyboardOwned(el("button", "dialog"))).toBe(true); expect(isKeyboardOwned(el("div", "menu"))).toBe(true); expect(isKeyboardOwned(el("div", "listbox"))).toBe(true); });
it("is free on the page and for non-elements", () => { expect(isKeyboardOwned(el("button"))).toBe(false); expect(isKeyboardOwned(el("div", "row"))).toBe(false); expect(isKeyboardOwned(null)).toBe(false); expect(isKeyboardOwned({} as EventTarget)).toBe(false); });});
describe("isResetShortcut", () => { const event = ( overrides: Partial<Parameters<typeof isResetShortcut>[0]>, ) => ({ key: "Escape", metaKey: false, ctrlKey: false, repeat: false, target: el("body"), ...overrides, });
it("fires on โ+Esc and Ctrl+Esc", () => { expect(isResetShortcut(event({ metaKey: true }))).toBe(true); expect(isResetShortcut(event({ ctrlKey: true }))).toBe(true); });
it("ignores bare Esc, other keys and repeats", () => { expect(isResetShortcut(event({}))).toBe(false); expect(isResetShortcut(event({ metaKey: true, key: "k" }))).toBe(false); expect(isResetShortcut(event({ metaKey: true, repeat: true }))).toBe(false); });
it("leaves Esc to a focused field or open overlay", () => { expect(isResetShortcut(event({ metaKey: true, target: el("input") }))).toBe( false, ); expect( isResetShortcut(event({ ctrlKey: true, target: el("div", "dialog") })), ).toBe(false); });});
describe("getModifierKey", () => { it("shows โ on Apple platforms and Ctrl elsewhere", () => { expect( getModifierKey("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7)"), ).toBe("โ"); expect(getModifierKey("Mozilla/5.0 (iPhone; CPU iPhone OS 17_0)")).toBe( "โ", ); expect(getModifierKey("Mozilla/5.0 (Windows NT 10.0; Win64; x64)")).toBe( "Ctrl", ); expect(getModifierKey("Mozilla/5.0 (X11; Linux x86_64)")).toBe("Ctrl"); });});