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

app-updates.test.ts

291 lines9.0 KB

1import { describe, expect, it, vi } from "vite-plus/test";
2​
3import {
4 createAppUpdateLaunchCheck,
5 registerHiddenUpdateTap,
6 runAppUpdateCheck,
7 type AppUpdateCheckState,
8 type AppUpdateClient,
9} from "./app-updates";
10​
11vi.mock("expo-updates", () => ({
12 isEnabled: true,
13 checkForUpdateAsync: vi.fn(),
14 fetchUpdateAsync: vi.fn(),
15 reloadAsync: vi.fn(),
16}));
17​
18function makeUpdateClient(overrides: Partial<AppUpdateClient> = {}): AppUpdateClient {
19 return {
20 isEnabled: true,
21 checkForUpdateAsync: vi.fn(async () => ({
22 isAvailable: false,
23 isRollBackToEmbedded: false,
24 })),
25 fetchUpdateAsync: vi.fn(async () => ({
26 isNew: true,
27 isRollBackToEmbedded: false,
28 })),
29 reloadAsync: vi.fn(async () => {}),
30 ...overrides,
31 };
33​
34describe("runAppUpdateCheck", () => {
35 it("does nothing while running from the Metro development server", async () => {
36 vi.stubGlobal("__DEV__", true);
37 const client = makeUpdateClient();
38​
39 try {
40 await runAppUpdateCheck({ client });
41 } finally {
42 vi.unstubAllGlobals();
43 }
44​
45 expect(client.checkForUpdateAsync).not.toHaveBeenCalled();
46 });
47​
48 it("downloads and restarts when a new update is available", async () => {
49 const client = makeUpdateClient({
50 checkForUpdateAsync: vi.fn(async () => ({
51 isAvailable: true,
52 isRollBackToEmbedded: false,
53 })),
54 });
55 const states: AppUpdateCheckState[] = [];
56​
57 await runAppUpdateCheck({ client, onStateChange: (state) => states.push(state) });
58​
59 expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
60 expect(client.fetchUpdateAsync).toHaveBeenCalledOnce();
61 expect(client.reloadAsync).toHaveBeenCalledOnce();
62 expect(states).toEqual(["checking", "downloading", "restarting"]);
63 });
64​
65 it("restarts into the embedded bundle for a rollback directive", async () => {
66 const client = makeUpdateClient({
67 checkForUpdateAsync: vi.fn(async () => ({
68 isAvailable: false,
69 isRollBackToEmbedded: true,
70 })),
71 fetchUpdateAsync: vi.fn(async () => ({
72 isNew: false,
73 isRollBackToEmbedded: true,
74 })),
75 });
76​
77 await runAppUpdateCheck({ client });
78​
79 expect(client.fetchUpdateAsync).toHaveBeenCalledOnce();
80 expect(client.reloadAsync).toHaveBeenCalledOnce();
81 });
82​
83 it("stops quietly when the running bundle is current", async () => {
84 const client = makeUpdateClient();
85 const states: AppUpdateCheckState[] = [];
86​
87 await runAppUpdateCheck({ client, onStateChange: (state) => states.push(state) });
88​
89 expect(client.fetchUpdateAsync).not.toHaveBeenCalled();
90 expect(client.reloadAsync).not.toHaveBeenCalled();
91 expect(states).toEqual(["checking", "current"]);
92 });
93​
94 it("reports manual failures without continuing the update", async () => {
95 const reportError = vi.spyOn(console, "error").mockImplementation(() => {});
96 const client = makeUpdateClient({
97 checkForUpdateAsync: vi.fn(async () => {
98 throw new Error("offline");
99 }),
100 });
101 const failures: string[] = [];
102 const states: AppUpdateCheckState[] = [];
103​
104 await runAppUpdateCheck({
105 client,
106 onFailure: (message) => failures.push(message),
107 onStateChange: (state) => states.push(state),
108 });
109​
110 expect(client.fetchUpdateAsync).not.toHaveBeenCalled();
111 expect(failures).toEqual(["offline"]);
112 expect(states).toEqual(["checking", "idle"]);
113 reportError.mockRestore();
114 });
115​
116 it.each(["ERR_NOT_AVAILABLE_IN_DEV_CLIENT", "ERR_UPDATES_DISABLED"])(
117 "treats Expo's %s failure as an unavailable update check",
118 async (code) => {
119 const reportError = vi.spyOn(console, "error").mockImplementation(() => {});
120 const error = Object.assign(new Error("Updates are unavailable"), { code });
121 const client = makeUpdateClient({
122 checkForUpdateAsync: vi.fn(async () => {
123 throw error;
124 }),
125 });
126 const failures: string[] = [];
127 const states: AppUpdateCheckState[] = [];
128​
129 await runAppUpdateCheck({
130 client,
131 onFailure: (message) => failures.push(message),
132 onStateChange: (state) => states.push(state),
133 });
134​
135 expect(reportError).not.toHaveBeenCalled();
136 expect(failures).toEqual([]);
137 expect(states).toEqual(["checking", "idle"]);
138 reportError.mockRestore();
139 },
140 );
141​
142 it("coalesces overlapping launch and manual checks", async () => {
143 let resolveCheck!: (result: {
144 readonly isAvailable: boolean;
145 readonly isRollBackToEmbedded: boolean;
146 }) => void;
147 const checkResult = new Promise<{
148 readonly isAvailable: boolean;
149 readonly isRollBackToEmbedded: boolean;
150 }>((resolve) => {
151 resolveCheck = resolve;
152 });
153 const client = makeUpdateClient({
154 checkForUpdateAsync: vi.fn(() => checkResult),
155 });
156 const checkOnLaunch = createAppUpdateLaunchCheck(client);
157 const manualStates: AppUpdateCheckState[] = [];
158​
159 const launchCheck = checkOnLaunch();
160 const manualCheck = runAppUpdateCheck({
161 client,
162 onStateChange: (state) => manualStates.push(state),
163 });
164​
165 expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
166 expect(manualStates).toEqual(["checking"]);
167​
168 resolveCheck({
169 isAvailable: false,
170 isRollBackToEmbedded: false,
171 });
172 await Promise.all([launchCheck, manualCheck]);
173​
174 expect(manualStates).toEqual(["checking", "current"]);
175​
176 await runAppUpdateCheck({ client });
177 expect(client.checkForUpdateAsync).toHaveBeenCalledTimes(2);
178 });
179​
180 it("forwards failures to a manual check coalesced with the launch check", async () => {
181 const reportError = vi.spyOn(console, "error").mockImplementation(() => {});
182 let rejectCheck!: (error: Error) => void;
183 const checkResult = new Promise<{
184 readonly isAvailable: boolean;
185 readonly isRollBackToEmbedded: boolean;
186 }>((_resolve, reject) => {
187 rejectCheck = reject;
188 });
189 const client = makeUpdateClient({
190 checkForUpdateAsync: vi.fn(() => checkResult),
191 });
192 const checkOnLaunch = createAppUpdateLaunchCheck(client);
193 const failures: string[] = [];
194 const manualStates: AppUpdateCheckState[] = [];
195​
196 const launchCheck = checkOnLaunch();
197 const manualCheck = runAppUpdateCheck({
198 client,
199 onFailure: (message) => failures.push(message),
200 onStateChange: (state) => manualStates.push(state),
201 });
202​
203 rejectCheck(new Error("offline"));
204 await Promise.all([launchCheck, manualCheck]);
205​
206 expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
207 expect(failures).toEqual(["offline"]);
208 expect(manualStates).toEqual(["checking", "idle"]);
209 reportError.mockRestore();
210 });
211​
212 it("publishes the in-flight check before a state callback can re-enter", async () => {
213 let resolveCheck!: (result: {
214 readonly isAvailable: boolean;
215 readonly isRollBackToEmbedded: boolean;
216 }) => void;
217 const checkResult = new Promise<{
218 readonly isAvailable: boolean;
219 readonly isRollBackToEmbedded: boolean;
220 }>((resolve) => {
221 resolveCheck = resolve;
222 });
223 const client = makeUpdateClient({
224 checkForUpdateAsync: vi.fn(() => checkResult),
225 });
226 const reentrantStates: AppUpdateCheckState[] = [];
227 let reentrantCheck: Promise<void> | undefined;
228 let didReenter = false;
229​
230 const initialCheck = runAppUpdateCheck({
231 client,
232 onStateChange: (state) => {
233 if (state !== "checking" || didReenter) return;
234 didReenter = true;
235 reentrantCheck = runAppUpdateCheck({
236 client,
237 onStateChange: (reentrantState) => reentrantStates.push(reentrantState),
238 });
239 },
240 });
241​
242 expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
243 expect(reentrantCheck).toBeDefined();
244 expect(reentrantStates).toEqual(["checking"]);
245​
246 resolveCheck({
247 isAvailable: false,
248 isRollBackToEmbedded: false,
249 });
250 await Promise.all([initialCheck, reentrantCheck]);
251​
252 expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
253 expect(reentrantStates).toEqual(["checking", "current"]);
254 });
255});
256​
257describe("createAppUpdateLaunchCheck", () => {
258 it("checks at most once for each JavaScript launch", async () => {
259 const client = makeUpdateClient();
260 const checkOnLaunch = createAppUpdateLaunchCheck(client);
261​
262 const first = checkOnLaunch();
263 const second = checkOnLaunch();
264 await first;
265​
266 expect(second).toBeUndefined();
267 expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
268 });
269​
270 it("does nothing when Expo updates are disabled", () => {
271 const client = makeUpdateClient({ isEnabled: false });
272 const checkOnLaunch = createAppUpdateLaunchCheck(client);
273​
274 expect(checkOnLaunch()).toBeUndefined();
275 expect(client.checkForUpdateAsync).not.toHaveBeenCalled();
276 });
277});
278​
279describe("registerHiddenUpdateTap", () => {
280 it("unlocks the manual check on the fifth tap", () => {
281 let count = 0;
282​
283 for (let tap = 1; tap <= 5; tap += 1) {
284 const result = registerHiddenUpdateTap(count);
285 expect(result.shouldCheck).toBe(tap === 5);
286 count = result.nextCount;
287 }
288​
289 expect(count).toBe(0);
290 });
291});