Skip to content
Source
Sign inSign up

KeybindingsUpdateToast.test.ts

65 lines2.2 KB

1import type { ServerConfigStreamEvent } from "@t3tools/contracts";
2import { describe, expect, it, vi } from "vite-plus/test";
3​
4const mocks = vi.hoisted(() => ({
5 state: null as unknown as import("effect/reactivity").Atom.Writable<{
6 readonly latestEvent: ServerConfigStreamEvent | null;
7 }>,
8 addToast: vi.fn<(toast: { readonly title?: unknown }) => void>(),
9}));
10​
11// The event atom is derived from server state, as in production; nothing else reads it.
12vi.mock(import("../state/server"), async () => {
13 const { Atom } = await import("effect/reactivity");
14 mocks.state = Atom.make<{ readonly latestEvent: ServerConfigStreamEvent | null }>({
15 latestEvent: null,
16 });
17 return {
18 primaryServerConfigAtom: Atom.make(null),
19 primaryServerConfigEventAtom: Atom.make((get) => get(mocks.state).latestEvent),
20 } as never;
21});
22vi.mock(import("../state/primaryEnvironment"), async () => {
23 const { Atom } = await import("effect/reactivity");
24 return { primaryEnvironmentIdAtom: Atom.make(null) } as never;
25});
26vi.mock(
27 import("./KeybindingsConfigWarning"),
28 () => ({ KeybindingsConfigWarning: () => null }) as never,
29);
30vi.mock(
31 import("./ui/toast"),
32 () =>
33 ({
34 toastManager: { add: mocks.addToast },
35 stackedThreadToast: (toast: unknown) => toast,
36 }) as never,
37);
38​
39import { appAtomRegistry } from "../rpc/atomRegistry";
40import { startKeybindingsUpdateToasts } from "./KeybindingsUpdateToast";
41​
42function keybindingsUpdated(issues: ReadonlyArray<unknown>): ServerConfigStreamEvent {
43 return { type: "keybindingsUpdated", payload: { keybindings: [], issues } } as never;
45​
46describe("startKeybindingsUpdateToasts", () => {
47 it("toasts reloads the server reports after start", () => {
48 const stop = startKeybindingsUpdateToasts();
49 try {
50 appAtomRegistry.set(mocks.state, { latestEvent: keybindingsUpdated([]) });
51 appAtomRegistry.set(mocks.state, {
52 latestEvent: keybindingsUpdated([
53 { kind: "keybindings.malformed-config", message: "Unexpected token" },
54 ]),
55 });
56​
57 expect(mocks.addToast.mock.calls.map(([toast]) => toast.title)).toEqual([
58 "Keybindings updated",
59 "Invalid keybindings configuration",
60 ]);
61 } finally {
62 stop();
63 }
64 });
65});