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

app-updates.test.ts

252 lines7.8 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("downloads and restarts when a new update is available", async () => {
36 const client = makeUpdateClient({
37 checkForUpdateAsync: vi.fn(async () => ({
38 isAvailable: true,
39 isRollBackToEmbedded: false,
40 })),
41 });
42 const states: AppUpdateCheckState[] = [];
43​
44 await runAppUpdateCheck({ client, onStateChange: (state) => states.push(state) });
45​
46 expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
47 expect(client.fetchUpdateAsync).toHaveBeenCalledOnce();
48 expect(client.reloadAsync).toHaveBeenCalledOnce();
49 expect(states).toEqual(["checking", "downloading", "restarting"]);
50 });
51​
52 it("restarts into the embedded bundle for a rollback directive", async () => {
53 const client = makeUpdateClient({
54 checkForUpdateAsync: vi.fn(async () => ({
55 isAvailable: false,
56 isRollBackToEmbedded: true,
57 })),
58 fetchUpdateAsync: vi.fn(async () => ({
59 isNew: false,
60 isRollBackToEmbedded: true,
61 })),
62 });
63​
64 await runAppUpdateCheck({ client });
65​
66 expect(client.fetchUpdateAsync).toHaveBeenCalledOnce();
67 expect(client.reloadAsync).toHaveBeenCalledOnce();
68 });
69​
70 it("stops quietly when the running bundle is current", async () => {
71 const client = makeUpdateClient();
72 const states: AppUpdateCheckState[] = [];
73​
74 await runAppUpdateCheck({ client, onStateChange: (state) => states.push(state) });
75​
76 expect(client.fetchUpdateAsync).not.toHaveBeenCalled();
77 expect(client.reloadAsync).not.toHaveBeenCalled();
78 expect(states).toEqual(["checking", "current"]);
79 });
80​
81 it("reports manual failures without continuing the update", async () => {
82 const reportError = vi.spyOn(console, "error").mockImplementation(() => {});
83 const client = makeUpdateClient({
84 checkForUpdateAsync: vi.fn(async () => {
85 throw new Error("offline");
86 }),
87 });
88 const failures: string[] = [];
89 const states: AppUpdateCheckState[] = [];
90​
91 await runAppUpdateCheck({
92 client,
93 onFailure: (message) => failures.push(message),
94 onStateChange: (state) => states.push(state),
95 });
96​
97 expect(client.fetchUpdateAsync).not.toHaveBeenCalled();
98 expect(failures).toEqual(["offline"]);
99 expect(states).toEqual(["checking", "idle"]);
100 reportError.mockRestore();
101 });
102​
103 it("coalesces overlapping launch and manual checks", async () => {
104 let resolveCheck!: (result: {
105 readonly isAvailable: boolean;
106 readonly isRollBackToEmbedded: boolean;
107 }) => void;
108 const checkResult = new Promise<{
109 readonly isAvailable: boolean;
110 readonly isRollBackToEmbedded: boolean;
111 }>((resolve) => {
112 resolveCheck = resolve;
113 });
114 const client = makeUpdateClient({
115 checkForUpdateAsync: vi.fn(() => checkResult),
116 });
117 const checkOnLaunch = createAppUpdateLaunchCheck(client);
118 const manualStates: AppUpdateCheckState[] = [];
119​
120 const launchCheck = checkOnLaunch();
121 const manualCheck = runAppUpdateCheck({
122 client,
123 onStateChange: (state) => manualStates.push(state),
124 });
125​
126 expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
127 expect(manualStates).toEqual(["checking"]);
128​
129 resolveCheck({
130 isAvailable: false,
131 isRollBackToEmbedded: false,
132 });
133 await Promise.all([launchCheck, manualCheck]);
134​
135 expect(manualStates).toEqual(["checking", "current"]);
136​
137 await runAppUpdateCheck({ client });
138 expect(client.checkForUpdateAsync).toHaveBeenCalledTimes(2);
139 });
140​
141 it("forwards failures to a manual check coalesced with the launch check", async () => {
142 const reportError = vi.spyOn(console, "error").mockImplementation(() => {});
143 let rejectCheck!: (error: Error) => void;
144 const checkResult = new Promise<{
145 readonly isAvailable: boolean;
146 readonly isRollBackToEmbedded: boolean;
147 }>((_resolve, reject) => {
148 rejectCheck = reject;
149 });
150 const client = makeUpdateClient({
151 checkForUpdateAsync: vi.fn(() => checkResult),
152 });
153 const checkOnLaunch = createAppUpdateLaunchCheck(client);
154 const failures: string[] = [];
155 const manualStates: AppUpdateCheckState[] = [];
156​
157 const launchCheck = checkOnLaunch();
158 const manualCheck = runAppUpdateCheck({
159 client,
160 onFailure: (message) => failures.push(message),
161 onStateChange: (state) => manualStates.push(state),
162 });
163​
164 rejectCheck(new Error("offline"));
165 await Promise.all([launchCheck, manualCheck]);
166​
167 expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
168 expect(failures).toEqual(["offline"]);
169 expect(manualStates).toEqual(["checking", "idle"]);
170 reportError.mockRestore();
171 });
172​
173 it("publishes the in-flight check before a state callback can re-enter", async () => {
174 let resolveCheck!: (result: {
175 readonly isAvailable: boolean;
176 readonly isRollBackToEmbedded: boolean;
177 }) => void;
178 const checkResult = new Promise<{
179 readonly isAvailable: boolean;
180 readonly isRollBackToEmbedded: boolean;
181 }>((resolve) => {
182 resolveCheck = resolve;
183 });
184 const client = makeUpdateClient({
185 checkForUpdateAsync: vi.fn(() => checkResult),
186 });
187 const reentrantStates: AppUpdateCheckState[] = [];
188 let reentrantCheck: Promise<void> | undefined;
189 let didReenter = false;
190​
191 const initialCheck = runAppUpdateCheck({
192 client,
193 onStateChange: (state) => {
194 if (state !== "checking" || didReenter) return;
195 didReenter = true;
196 reentrantCheck = runAppUpdateCheck({
197 client,
198 onStateChange: (reentrantState) => reentrantStates.push(reentrantState),
199 });
200 },
201 });
202​
203 expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
204 expect(reentrantCheck).toBeDefined();
205 expect(reentrantStates).toEqual(["checking"]);
206​
207 resolveCheck({
208 isAvailable: false,
209 isRollBackToEmbedded: false,
210 });
211 await Promise.all([initialCheck, reentrantCheck]);
212​
213 expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
214 expect(reentrantStates).toEqual(["checking", "current"]);
215 });
216});
217​
218describe("createAppUpdateLaunchCheck", () => {
219 it("checks at most once for each JavaScript launch", async () => {
220 const client = makeUpdateClient();
221 const checkOnLaunch = createAppUpdateLaunchCheck(client);
222​
223 const first = checkOnLaunch();
224 const second = checkOnLaunch();
225 await first;
226​
227 expect(second).toBeUndefined();
228 expect(client.checkForUpdateAsync).toHaveBeenCalledOnce();
229 });
230​
231 it("does nothing when Expo updates are disabled", () => {
232 const client = makeUpdateClient({ isEnabled: false });
233 const checkOnLaunch = createAppUpdateLaunchCheck(client);
234​
235 expect(checkOnLaunch()).toBeUndefined();
236 expect(client.checkForUpdateAsync).not.toHaveBeenCalled();
237 });
238});
239​
240describe("registerHiddenUpdateTap", () => {
241 it("unlocks the manual check on the fifth tap", () => {
242 let count = 0;
243​
244 for (let tap = 1; tap <= 5; tap += 1) {
245 const result = registerHiddenUpdateTap(count);
246 expect(result.shouldCheck).toBe(tap === 5);
247 count = result.nextCount;
248 }
249​
250 expect(count).toBe(0);
251 });
252});