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

workspace-tab-navigation.test.ts

162 lines5.5 KB

1import { describe, expect, it } from "vite-plus/test";
2​
3import { restoreSettingsLinkState } from "../settings/settings-navigation";
4import { workspaceSelectionsIn, workspaceTabNavigationState } from "./workspace-tab-navigation";
5​
6const home = { key: "home", name: "Home" };
7const thread = {
8 key: "thread",
9 name: "Thread",
10 params: { environmentId: "env", threadId: "thread-a" },
11};
12const pullRequest = {
13 key: "pr",
14 name: "PullRequest",
15 params: { environmentId: "env", projectId: "project", repository: "o/r", number: 7 },
16};
17const settings = { key: "settings", name: "Settings" };
18const available = { thread: () => true, pullRequest: () => true };
19const names = (state: { routes: readonly { name: string }[] }) =>
20 state.routes.map((route) => route.name);
21​
22describe("workspace tab selections", () => {
23 it("remembers the chat, pull request and Settings page each tab showed", () => {
24 const afterThread = workspaceSelectionsIn([home, thread], {});
25 const afterPullRequest = workspaceSelectionsIn([home, pullRequest], afterThread);
26 expect(afterPullRequest).toMatchObject({
27 threads: thread.params,
28 pullRequests: pullRequest.params,
29 });
30 const afterSettings = workspaceSelectionsIn(
31 [home, settings, { key: "appearance", name: "SettingsAppearance" }],
32 afterPullRequest,
33 );
34 expect(afterSettings.settings).toBe("SettingsAppearance");
35 expect(workspaceSelectionsIn([home, thread], afterSettings)).toBe(afterSettings);
36 });
37​
38 it("doesn't remember pages a Settings row doesn't open", () => {
39 for (const name of ["SettingsEnvironmentNew", "SettingsProjectOverview", "SettingsAuth"]) {
40 expect(workspaceSelectionsIn([home, settings, { key: name, name }], {}).settings).toBe(
41 undefined,
42 );
43 }
44 });
45});
46​
47describe("opening a workspace tab", () => {
48 const columns = { columns: true, check: available };
49​
50 it("opens the remembered page beside the list with columns, and the list without", () => {
51 const state = { routes: [home, thread] };
52 const selections = { threads: thread.params, pullRequests: pullRequest.params };
53 expect(names(workspaceTabNavigationState(state, "pullRequests", selections, columns))).toEqual([
54 "Home",
55 "PullRequest",
56 ]);
57 expect(
58 names(
59 workspaceTabNavigationState(state, "pullRequests", selections, {
60 columns: false,
61 check: available,
62 }),
63 ),
64 ).toEqual(["Home"]);
65 expect(names(workspaceTabNavigationState(state, "settings", {}, columns))).toEqual([
66 "Home",
67 "Settings",
68 "SettingsEnvironments",
69 ]);
70 });
71​
72 it("keeps the route that is already open, so re-selecting a tab doesn't rebuild it", () => {
73 const next = workspaceTabNavigationState(
74 { routes: [home, thread] },
75 "threads",
76 { threads: thread.params },
77 columns,
78 );
79 expect(next.routes.map((route) => route.key)).toEqual(["home", "thread"]);
80 });
81​
82 it("reopens a tab's page under the key it had, so its kept screen is reused", () => {
83 const columnsAvailable = { columns: true, check: available };
84 let selections = workspaceSelectionsIn([home, thread], {});
85 const away = workspaceTabNavigationState(
86 { routes: [home, thread] },
87 "pullRequests",
88 selections,
89 {
90 columns: false,
91 check: available,
92 },
93 );
94 selections = workspaceSelectionsIn(away.routes, selections);
95 const back = workspaceTabNavigationState(away, "threads", selections, columnsAvailable);
96 expect(back.routes.at(-1)).toMatchObject({ key: "thread", params: thread.params });
97 });
98​
99 it("falls back to the list for a chat that no longer exists, or an unknown Settings page", () => {
100 const missing = { thread: () => false, pullRequest: () => true };
101 expect(
102 names(
103 workspaceTabNavigationState(
104 { routes: [home] },
105 "threads",
106 { threads: thread.params },
107 {
108 columns: true,
109 check: missing,
110 },
111 ),
112 ),
113 ).toEqual(["Home"]);
114 expect(
115 names(
116 workspaceTabNavigationState(
117 { routes: [home] },
118 "settings",
119 { settings: "SettingsGone" },
120 columns,
121 ),
122 ),
123 ).toEqual(["Home", "Settings", "SettingsEnvironments"]);
124 });
125​
126 it("keeps an open sheet on top", () => {
127 const sheet = { key: "sheet", name: "ConnectOnboarding" };
128 const next = workspaceTabNavigationState(
129 { routes: [home] },
130 "threads",
131 { threads: thread.params },
132 {
133 ...columns,
134 overlays: [sheet],
135 },
136 );
137 expect(names(next)).toEqual(["Home", "Thread", "ConnectOnboarding"]);
138 expect(next.index).toBe(2);
139 });
140});
141​
142describe("Settings links", () => {
143 it("puts the Settings list under a linked page, and a page beside a bare link with columns", () => {
144 const linked = restoreSettingsLinkState(
145 { routes: [{ name: "Home" }, { name: "SettingsKeyboard" }] },
146 undefined,
147 );
148 expect(names(linked!)).toEqual(["Home", "Settings", "SettingsKeyboard"]);
149 const bare = { routes: [{ name: "Home" }, { name: "Settings" }] };
150 expect(names(restoreSettingsLinkState(bare, "SettingsAppearance")!)).toEqual([
151 "Home",
152 "Settings",
153 "SettingsAppearance",
154 ]);
155 expect(restoreSettingsLinkState(bare, undefined)).toBe(bare);
156 });
157​
158 it("opens the sign-in link as a modal over the workspace", () => {
159 const state = { routes: [{ name: "Home" }, { name: "SettingsAuth" }] };
160 expect(restoreSettingsLinkState(state, "SettingsAppearance")).toBe(state);
161 });
162});