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

commandPaletteCommands.browser.test.tsx

70 lines2.5 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";
6import { openCommandPalette } from "../commandPaletteBus";
7import { useCommandPaletteMount, useCommandPaletteStore } from "../commandPaletteStore";
8import { installCommandPaletteCommands } from "./commandPaletteCommands";
9​
10const keybindings = compileResolvedKeybindingsConfig([
11 { key: "mod+k", command: "commandPalette.toggle" },
12]);
13​
14function MountedPalette() {
15 useCommandPaletteMount();
16 return null;
18​
19function setup() {
20 useCommandPaletteStore.setState(useCommandPaletteStore.getInitialState(), true);
22​
23describe("command palette shortcuts", () => {
24 it("stay inert on routes without the palette", async () => {
25 setup();
26 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
27 await using screen = await render(null, { router: "/pair" });
28 using commands = new DisposableStack();
29 commands.defer(
30 installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),
31 );
32​
33 await userEvent.keyboard("{Control>}k{/Control}");
34​
35 expect(useCommandPaletteStore.getState().open).toBe(false);
36 });
37​
38 it("open the palette while it is mounted, and it closes when it unmounts", async () => {
39 setup();
40 using _platform = vi.spyOn(navigator, "platform", "get").mockReturnValue("Linux x86_64");
41 await using screen = await render(<MountedPalette />, { router: "/" });
42 using commands = new DisposableStack();
43 commands.defer(
44 installCommandPaletteCommands(screen.router!, { keybindings: () => keybindings }),
45 );
46​
47 await userEvent.keyboard("{Control>}k{/Control}");
48 expect(useCommandPaletteStore.getState().open).toBe(true);
49​
50 await screen.rerender(<div />);
51 expect(useCommandPaletteStore.getState().open).toBe(false);
52 });
53});
54​
55describe("openCommandPalette", () => {
56 it("drops a request made while no palette is mounted", async () => {
57 setup();
58 await using screen = await render(null, { router: "/pair" });
59​
60 openCommandPalette({ open: "new-thread-in" });
61 await screen.rerender(<MountedPalette />);
62 expect(useCommandPaletteStore.getState()).toMatchObject({ open: false, openIntent: null });
63​
64 openCommandPalette({ open: "new-thread-in" });
65 expect(useCommandPaletteStore.getState()).toMatchObject({
66 open: true,
67 openIntent: { kind: "new-thread-in" },
68 });
69 });
70});