| 1 | import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings"; |
| 2 | import { afterEach, describe, expect, it, vi } from "vite-plus/test"; |
| 3 | import { resolveUsageShortcut } from "./usageShortcuts"; |
| 4 | |
| 5 | class TestElement extends EventTarget { |
| 6 | constructor( |
| 7 | readonly tagName: string, |
| 8 | readonly isContentEditable = false, |
| 9 | ) { |
| 10 | super(); |
| 11 | } |
| 12 | |
| 13 | closest() { |
| 14 | return null; |
| 15 | } |
| 16 | } |
| 17 | |
| 18 | afterEach(() => vi.unstubAllGlobals()); |
| 19 | |
| 20 | it("does not reserve the former Limits shortcut", () => { |
| 21 | vi.stubGlobal("HTMLElement", TestElement); |
| 22 | vi.stubGlobal("navigator", { platform: "Linux" }); |
| 23 | expect( |
| 24 | resolveUsageShortcut( |
| 25 | { |
| 26 | key: "l", |
| 27 | metaKey: false, |
| 28 | ctrlKey: false, |
| 29 | shiftKey: false, |
| 30 | altKey: false, |
| 31 | target: new TestElement("BODY"), |
| 32 | }, |
| 33 | DEFAULT_RESOLVED_KEYBINDINGS, |
| 34 | ), |
| 35 | ).toBeNull(); |
| 36 | }); |
| 37 | |
| 38 | describe("Usage shortcuts while typing", () => { |
| 39 | it.each([new TestElement("INPUT"), new TestElement("TEXTAREA"), new TestElement("DIV", true)])( |
| 40 | "leaves letters to the focused $tagName field", |
| 41 | (target) => { |
| 42 | vi.stubGlobal("HTMLElement", TestElement); |
| 43 | vi.stubGlobal("navigator", { platform: "Linux" }); |
| 44 | const event = { |
| 45 | key: "t", |
| 46 | metaKey: false, |
| 47 | ctrlKey: false, |
| 48 | shiftKey: false, |
| 49 | altKey: false, |
| 50 | target, |
| 51 | }; |
| 52 | |
| 53 | expect(resolveUsageShortcut(event, DEFAULT_RESOLVED_KEYBINDINGS)).toBeNull(); |
| 54 | expect( |
| 55 | resolveUsageShortcut( |
| 56 | { ...event, target: new TestElement("BODY") }, |
| 57 | DEFAULT_RESOLVED_KEYBINDINGS, |
| 58 | ), |
| 59 | ).toBe("usage.tokens"); |
| 60 | }, |
| 61 | ); |
| 62 | }); |