Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

UsageToggleShortcut.browser.test.tsx

98 lines4.1 KB

1import { compileResolvedKeybindingsConfig } from "@t3tools/shared/keybindings";
2import { describe, expect, it, vi } from "vite-plus/test";
3import { userEvent } from "vite-plus/test/browser";
4​
5import { render } from "../../test/render";
6​
7const state = vi.hoisted(() => ({ navigate: vi.fn() }));
8​
9// One key toggles Usage: it opens Usage elsewhere and goes back from it.
10const keybindings = compileResolvedKeybindingsConfig([
11 { key: "mod+u", command: "navigation.back", when: "usagePageOpen" },
12 { key: "mod+u", command: "usage.open", when: "!usagePageOpen" },
13 { key: "ctrl+a", command: "navigation.back", when: "!terminalFocus" },
14 { key: "ctrl+d", command: "navigation.forward", when: "!terminalFocus" },
15]);
16​
17vi.mock(import("@effect/atom-react"), async (original) => ({
18 ...(await original()),
19 useAtomValue: (() => keybindings) as never,
20}));
21vi.mock(import("../state/server"), async (original) => {
22 const actual = await original();
23 const { Atom } = await import("effect/reactivity");
24 const { compileResolvedKeybindingsConfig: compile } = await import("@t3tools/shared/keybindings");
25 // Factories are hoisted above the module's own constants.
26 const compiled = compile([
27 { key: "mod+u", command: "navigation.back", when: "usagePageOpen" },
28 { key: "mod+u", command: "usage.open", when: "!usagePageOpen" },
29 { key: "ctrl+a", command: "navigation.back", when: "!terminalFocus" },
30 { key: "ctrl+d", command: "navigation.forward", when: "!terminalFocus" },
31 ]);
32 return { ...actual, primaryServerKeybindingsAtom: Atom.make(compiled) };
33});
34vi.mock(import("@tanstack/react-router"), async (original) => ({
35 ...(await original()),
36 useNavigate: (() => state.navigate) as never,
37}));
38​
39import { useCommandPaletteMount } from "../commandPaletteStore";
40import { installCommandPaletteCommands } from "../shortcuts/commandPaletteCommands";
41import { registerNavigationHistoryCommands } from "./AppSidebarLayout";
42​
43function MountedPalette() {
44 useCommandPaletteMount();
45 return null;
47​
48// regression: #21357
49describe("usage toggle shortcut on Usage", () => {
50 it("goes back", async () => {
51 vi.clearAllMocks();
52 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
53 await using screen = await render(null, { router: "/usage" });
54 using _commands = { [Symbol.dispose]: registerNavigationHistoryCommands(screen.router!) };
55​
56 using back = vi.spyOn(window.history, "back").mockImplementation(() => undefined);
57 await userEvent.keyboard("{Control>}u{/Control}");
58​
59 expect(back).toHaveBeenCalledOnce();
60 });
61​
62 it("handles custom history shortcuts before a focused editor consumes them", async () => {
63 vi.clearAllMocks();
64 await using screen = await render(
65 <textarea
66 aria-label="Editor"
67 onKeyDown={(event) => {
68 event.preventDefault();
69 event.stopPropagation();
70 }}
71 />,
72 { router: "/usage" },
73 );
74 using _commands = { [Symbol.dispose]: registerNavigationHistoryCommands(screen.router!) };
75 using back = vi.spyOn(window.history, "back").mockImplementation(() => undefined);
76 using forward = vi.spyOn(window.history, "forward").mockImplementation(() => undefined);
77 await userEvent.click(screen.getByRole("textbox", { name: "Editor" }));
78 await userEvent.keyboard("{Control>}a{/Control}{Control>}d{/Control}");
79 expect(back).toHaveBeenCalledOnce();
80 expect(forward).toHaveBeenCalledOnce();
81 });
82​
83 // Both listeners sit on window, so the palette must not claim the key whatever their order.
84 it("does not reopen Usage from the command palette", async () => {
85 vi.clearAllMocks();
86 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
87 await using screen = await render(<MountedPalette />, { router: "/usage" });
88 using navigate = vi.spyOn(screen.router!, "navigate");
89 using commands = new DisposableStack();
90 commands.defer(
91 installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),
92 );
93​
94 await userEvent.keyboard("{Control>}u{/Control}");
95​
96 expect(navigate).not.toHaveBeenCalled();
97 });
98});