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

UsageToggleShortcut.browser.test.tsx

52 lines1.9 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]);
14​
15vi.mock(import("@effect/atom-react"), async (original) => ({
16 ...(await original()),
17 useAtomValue: (() => keybindings) as never,
18}));
19vi.mock(import("@tanstack/react-router"), async (original) => ({
20 ...(await original()),
21 useNavigate: (() => state.navigate) as never,
22}));
23​
24import { NavigationHistoryShortcuts } from "./AppSidebarLayout";
25import { CommandPalette } from "./CommandPalette";
26​
27// regression: #21357
28describe("usage toggle shortcut on Usage", () => {
29 it("goes back", async () => {
30 vi.clearAllMocks();
31 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
32 await using _screen = await render(<NavigationHistoryShortcuts />, { router: "/usage" });
33​
34 using back = vi.spyOn(window.history, "back").mockImplementation(() => undefined);
35 await userEvent.keyboard("{Control>}u{/Control}");
36​
37 expect(back).toHaveBeenCalledOnce();
38 });
39​
40 // Both listeners sit on window, so the palette must not claim the key whatever their order.
41 it("does not reopen Usage from the command palette", async () => {
42 vi.clearAllMocks();
43 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
44 await using _screen = await render(<CommandPalette>{null}</CommandPalette>, {
45 router: "/usage",
46 });
47​
48 await userEvent.keyboard("{Control>}u{/Control}");
49​
50 expect(state.navigate).not.toHaveBeenCalled();
51 });
52});