Open History to see the last commit
1import { describe, expect, it } from "vite-plus/test";
2
3import type { NavigationState } from "@react-navigation/native";
4import {
5 findLinkedRoute,
6 findStackRoute,
7 restoreDraftLinkState,
8 shouldHandleAppLink,
9 updateLinkedRoute,
10} from "./appLinking";
11
12describe("shouldHandleAppLink", () => {
13 it.each(["t3code://", "t3code:///", "t3code-dev://", "t3code-preview://"])(
14 "ignores scheme-only URL %s",
15 (url) => {
16 expect(shouldHandleAppLink(url, true)).toBe(false);
17 },
18 );
19
20 it.each([
21 "t3code://threads/env-1/thread-1",
22 "t3code://pair?pairingUrl=x",
23 "t3code-dev://settings/usage?tab=limits",
24 ])("handles path-bearing URL %s", (url) => {
25 expect(shouldHandleAppLink(url, true)).toBe(true);
26 });
27
28 it.each(["t3code://expo-development-client/?url=x", "t3code://expo-sharing/anything"])(
29 "ignores lifecycle URL %s",
30 (url) => {
31 expect(shouldHandleAppLink(url, true)).toBe(false);
32 },
33 );
34});
35
36describe("cloud-only deep links", () => {
37 it.each([
38 "t3code://connect-onboarding",
39 "t3code-dev://connect-onboarding/?source=x",
40 "t3code://settings/waitlist",
41 "t3code://settings/auth",
42 "exp://192.168.1.2:8081/--/settings/auth",
43 ])("ignores %s without cloud config", (url) => {
44 expect(shouldHandleAppLink(url, false)).toBe(false);
45 expect(shouldHandleAppLink(url, true)).toBe(true);
46 });
47
48 it.each([
49 "t3code://settings",
50 "t3code://settings/authors",
51 "t3code://pair?next=settings/auth",
52 "t3code://threads/env/thread/files/scripts/connect-onboarding",
53 "t3code://new/draft/files/settings/auth",
54 "exp://192.168.1.2:8081/--/threads/env/thread/files/settings/waitlist",
55 ])("still handles %s without cloud config", (url) => {
56 expect(shouldHandleAppLink(url, false)).toBe(true);
57 });
58});
59
60describe("draft deep links", () => {
61 it("opens the old draft URL on the thread screen with its draft identity", () => {
62 const draft = { projectId: "project-1", environmentId: "env-1", draftId: "draft-1" };
63 expect(
64 restoreDraftLinkState(
65 { routes: [{ name: "Home" }, { name: "Thread", params: draft }] },
66 "/new/draft?draftId=draft-1",
67 ),
68 ).toEqual({
69 index: 1,
70 routes: [{ name: "Home" }, { name: "Thread", params: { draft } }],
71 });
72 });
73
74 it.each([
75 ["environment", "NewTaskEnvironment"],
76 ["branch", "NewTaskBranch"],
77 ["files", "NewTaskFiles"],
78 ["files/src/index.ts", "NewTaskFile"],
79 ["attachments/attachment-1", "NewTaskAttachment"],
80 ["settings", "NewTaskThreadSettings"],
81 ])("restores a draft beneath its %s screen", (path, name) => {
82 const params = { environmentId: "env-1", projectId: "project-1" };
83 const target = { name, params };
84 expect(
85 restoreDraftLinkState({ routes: [{ name: "Home" }, target] }, `/new/draft/${path}`),
86 ).toEqual({
87 index: 2,
88 routes: [{ name: "Home" }, { name: "Thread", params: { draft: params } }, target],
89 });
90 });
91
92 it("leaves live-thread and project-selection links unchanged", () => {
93 const state = { routes: [{ name: "Thread", params: { threadId: "thread-1" } }] };
94 expect(restoreDraftLinkState(state, "/threads/env-1/thread-1")).toBe(state);
95 expect(restoreDraftLinkState(state, "/new")).toBe(state);
96 expect(restoreDraftLinkState(undefined, "/new/draft")).toBeUndefined();
97 });
98});
99
100describe("nested link routes", () => {
101 const settingsLink = {
102 routes: [
103 { name: "Home" },
104 {
105 name: "SettingsSheet",
106 state: {
107 routes: [
108 { name: "Settings" },
109 { name: "SettingsEnvironmentNew", params: { pairingUrl: "x", autoConnect: "1" } },
110 ],
111 },
112 },
113 ],
114 };
115
116 it("finds a route the link opens inside a nested navigator", () => {
117 expect(findLinkedRoute(settingsLink, ["SettingsEnvironmentNew"])?.params).toEqual({
118 pairingUrl: "x",
119 autoConnect: "1",
120 });
121 expect(findLinkedRoute(settingsLink, ["ConnectionsNew"])).toBeUndefined();
122 });
123
124 it("rewrites a nested route without touching the rest of the state", () => {
125 const next = updateLinkedRoute(settingsLink, ["SettingsEnvironmentNew"], (route) => ({
126 ...route,
127 params: { pairingUrl: "x" },
128 }));
129 expect(findLinkedRoute(next, ["SettingsEnvironmentNew"])?.params).toEqual({ pairingUrl: "x" });
130 expect(next?.routes[0]).toBe(settingsLink.routes[0]);
131 expect(updateLinkedRoute(settingsLink, ["ConnectionsNew"], (route) => route)).toBe(
132 settingsLink,
133 );
134 });
135});
136
137describe("findStackRoute", () => {
138 const stack = (key: string, routes: NavigationState["routes"]) =>
139 ({
140 key,
141 index: routes.length - 1,
142 routeNames: routes.map((route) => route.name),
143 routes,
144 stale: false,
145 type: "stack",
146 }) as NavigationState;
147
148 it("finds a nested screen with the navigator that holds it", () => {
149 const root = stack("root", [
150 { key: "home", name: "Home" },
151 {
152 key: "settings",
153 name: "Settings",
154 state: stack("settings-stack", [{ key: "new-task", name: "NewTaskProject" }]),
155 },
156 ]);
157 expect(findStackRoute(root, "NewTaskProject")).toEqual({
158 navigatorKey: "settings-stack",
159 routeKey: "new-task",
160 focused: true,
161 });
162 });
163
164 it("reports whether the screen is its navigator's focused route", () => {
165 const covered = stack("root", [
166 { key: "new-task", name: "NewTaskProject" },
167 { key: "add-project", name: "AddProject" },
168 ]);
169 expect(findStackRoute(covered, "NewTaskProject")?.focused).toBe(false);
170 expect(findStackRoute(covered, "AddProject")?.focused).toBe(true);
171 });
172
173 it("prefers the top-most screen and reports a missing one", () => {
174 const root = stack("root", [
175 { key: "first", name: "NewTaskProject" },
176 { key: "second", name: "NewTaskProject" },
177 ]);
178 expect(findStackRoute(root, "NewTaskProject")?.routeKey).toBe("second");
179 expect(findStackRoute(root, "Thread")).toBeUndefined();
180 });
181});