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 { registerNavigationHistoryCommands } from "./AppSidebarLayout";
36import { CommandPalette } from "./CommandPalette";
37
38// regression: #21357
39describe("usage toggle shortcut on Usage", () => {
40 it("goes back", async () => {
41 vi.clearAllMocks();
42 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
43 await using screen = await render(null, { router: "/usage" });
44 using _commands = { [Symbol.dispose]: registerNavigationHistoryCommands(screen.router!) };
45
46 using back = vi.spyOn(window.history, "back").mockImplementation(() => undefined);
47 await userEvent.keyboard("{Control>}u{/Control}");
48
49 expect(back).toHaveBeenCalledOnce();
50 });
51
52 // Both listeners sit on window, so the palette must not claim the key whatever their order.
53 it("does not reopen Usage from the command palette", async () => {
54 vi.clearAllMocks();
55 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
56 await using _screen = await render(<CommandPalette>{null}</CommandPalette>, {
57 router: "/usage",
58 });
59
60 await userEvent.keyboard("{Control>}u{/Control}");
61
62 expect(state.navigate).not.toHaveBeenCalled();
63 });
64});