Open History to see the last commit
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]);
14
15vi.mock(import("@effect/atom-react"), async (original) => ({
16 ...(await original()),
17 useAtomValue: (() => keybindings) as never,
18}));
19vi.mock(import("../state/server"), async (original) => {
20 const actual = await original();
21 const { Atom } = await import("effect/reactivity");
22 const { compileResolvedKeybindingsConfig: compile } = await import("@t3tools/shared/keybindings");
23 // Factories are hoisted above the module's own constants.
24 const compiled = compile([
25 { key: "mod+u", command: "navigation.back", when: "usagePageOpen" },
26 { key: "mod+u", command: "usage.open", when: "!usagePageOpen" },
27 ]);
28 return { ...actual, primaryServerKeybindingsAtom: Atom.make(compiled) };
29});
30vi.mock(import("@tanstack/react-router"), async (original) => ({
31 ...(await original()),
32 useNavigate: (() => state.navigate) as never,
33}));
34
35import { useCommandPaletteMount } from "../commandPaletteStore";
36import { installCommandPaletteCommands } from "../shortcuts/commandPaletteCommands";
37import { registerNavigationHistoryCommands } from "./AppSidebarLayout";
38
39function MountedPalette() {
40 useCommandPaletteMount();
41 return null;
42}
43
44// regression: #21357
45describe("usage toggle shortcut on Usage", () => {
46 it("goes back", async () => {
47 vi.clearAllMocks();
48 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
49 await using screen = await render(null, { router: "/usage" });
50 using _commands = { [Symbol.dispose]: registerNavigationHistoryCommands(screen.router!) };
51
52 using back = vi.spyOn(window.history, "back").mockImplementation(() => undefined);
53 await userEvent.keyboard("{Control>}u{/Control}");
54
55 expect(back).toHaveBeenCalledOnce();
56 });
57
58 // Both listeners sit on window, so the palette must not claim the key whatever their order.
59 it("does not reopen Usage from the command palette", async () => {
60 vi.clearAllMocks();
61 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
62 await using screen = await render(<MountedPalette />, { router: "/usage" });
63 using navigate = vi.spyOn(screen.router!, "navigate");
64 using commands = new DisposableStack();
65 commands.defer(
66 installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),
67 );
68
69 await userEvent.keyboard("{Control>}u{/Control}");
70
71 expect(navigate).not.toHaveBeenCalled();
72 });
73});