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

settingsScopeRetention.browser.test.tsx

139 lines5.0 KB

1import { act, useState, type ReactNode } from "react";
2import { createRoot, type Root } from "react-dom/client";
3import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
4import type { SettingsScopeSearch } from "./settingsScope";
5​
6interface ScopeState {
7 readonly search: SettingsScopeSearch;
8 readonly environmentId: string | null;
9}
10​
11const state = vi.hoisted(() => ({
12 pathname: "/settings/diagnostics",
13 scope: { search: {}, environmentId: null } as ScopeState,
14}));
15​
16vi.mock(import("@tanstack/react-router"), async (original) => ({
17 ...(await original()),
18 createFileRoute: (() => (options: { component: () => ReactNode }) => ({
19 options,
20 useSearch: () => state.scope.search,
21 useNavigate: () => vi.fn(),
22 })) as never,
23 useLocation: ((options?: { select: (location: unknown) => unknown }) => {
24 const location = { pathname: state.pathname, hash: "", state: {} };
25 return options ? options.select(location) : location;
26 }) as never,
27 Outlet: (() => <StatefulContent />) as never,
28}));
29vi.mock(import("./SettingsScopeContext"), async (original) => {
30 const { createContext, useContext } = await import("react");
31 const Scope = createContext<ScopeState>(state.scope);
32 return {
33 ...(await original()),
34 SettingsScopeProvider: (({ children }: { children: ReactNode }) => (
35 <Scope value={state.scope}>{children}</Scope>
36 )) as never,
37 useSettingsScope: (() => {
38 const { search, environmentId } = useContext(Scope);
39 return {
40 search,
41 scope: { kind: "all" },
42 environment: environmentId ? { environmentId } : null,
43 connectedEnvironments: environmentId ? [{}] : [],
44 };
45 }) as never,
46 };
47});
48vi.mock(import("../../state/environments"), async (original) => ({
49 ...(await original()),
50 useEnvironments: (() => ({ environments: [] })) as never,
51}));
52vi.mock(import("../ui/sidebar"), async (original) => ({
53 ...(await original()),
54 SidebarInset: ({ children }: { children?: ReactNode }) => <div>{children}</div>,
55}));
56vi.mock(import("../WorkspacePageHeader"), async (original) => ({
57 ...(await original()),
58 WorkspacePageHeader: ({ children }: { children?: ReactNode }) => <div>{children}</div>,
59}));
60vi.mock(import("./SettingsBreadcrumb"), async (original) => ({
61 ...(await original()),
62 SettingsBreadcrumb: (() => null) as never,
63}));
64vi.mock(import("../sidebar/mainAppLocation"), async (original) => ({
65 ...(await original()),
66 useNavigateToMainApp: () => vi.fn(),
67}));
68vi.mock(import("../../hooks/useNavigateBack"), async (original) => ({
69 ...(await original()),
70 useEscapeToGoBack: () => undefined,
71}));
72vi.mock(import("./SettingsPanels"), async (original) => ({
73 ...(await original()),
74 useSettingsRestore: (() => ({ changedSettingLabels: [] })) as never,
75}));
76​
77import { Route } from "../../routes/settings";
78​
79function StatefulContent() {
80 const [value, setValue] = useState(0);
81 return (
82 <button aria-label="Retained value" onClick={() => setValue(value + 1)}>
83 {value}
84 </button>
85 );
87​
88let root: Root;
89let container: HTMLDivElement;
90const Layout = Route.options.component!;
91await Layout.preload?.();
92​
93async function render(scope: ScopeState) {
94 state.scope = scope;
95 await act(async () => root.render(<Layout />));
97function retainedValue() {
98 return container.querySelector('button[aria-label="Retained value"]')?.textContent ?? null;
100​
101beforeEach(() => {
102 container = document.createElement("div");
103 document.body.append(container);
104 root = createRoot(container);
105});
106afterEach(async () => {
107 await act(async () => root.unmount());
108 container.remove();
109});
110​
111const first = { search: { machine: "first", project: "one" }, environmentId: "first" };
112const otherProject = { search: { machine: "first", project: "two" }, environmentId: "first" };
113const otherMachine = { search: { machine: "second", project: "one" }, environmentId: "second" };
114const implicitFirst = { search: { project: "one" }, environmentId: "first" };
115const implicitSecond = { search: { project: "one" }, environmentId: "second" };
116​
117const disconnected = { search: first.search, environmentId: null };
118​
119it.each([
120 ["/settings/diagnostics", "another project", "1", first, otherProject],
121 ["/settings/providers", "another project", "1", first, otherProject],
122 ["/settings/diagnostics", "another environment", "0", first, otherMachine],
123 ["/settings/providers", "another environment", "0", first, otherMachine],
124 ["/settings/diagnostics", "an implicit environment change", "0", implicitFirst, implicitSecond],
125 ["/settings/providers", "an implicit environment change", "0", implicitFirst, implicitSecond],
126 ["/settings/diagnostics", "a disconnect", "0", first, disconnected],
127 ["/settings/keybindings", "another project", "0", first, otherProject],
128])("%s after %s shows %s", async (pathname, _change, expected, before, after) => {
129 state.pathname = pathname;
130 await render(before);
131 await act(async () =>
132 container.querySelector<HTMLButtonElement>('button[aria-label="Retained value"]')?.click(),
133 );
134 expect(retainedValue()).toBe("1");
135​
136 await render(after);
137​
138 expect(retainedValue()).toBe(expected);
139});