@rocapine/react-native-onboarding 1.75.0 → 1.77.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +30 -4
- package/dist/OnboardingStudioClient.d.ts +18 -0
- package/dist/OnboardingStudioClient.d.ts.map +1 -1
- package/dist/OnboardingStudioClient.js +44 -17
- package/dist/OnboardingStudioClient.js.map +1 -1
- package/dist/index.d.ts +5 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +16 -2
- package/dist/index.js.map +1 -1
- package/dist/onboarding-example.d.ts +140 -0
- package/dist/onboarding-example.d.ts.map +1 -1
- package/dist/onboarding-example.js +41 -1
- package/dist/onboarding-example.js.map +1 -1
- package/dist/paywalls/PaywallProvider.d.ts +71 -5
- package/dist/paywalls/PaywallProvider.d.ts.map +1 -1
- package/dist/paywalls/PaywallProvider.js +237 -57
- package/dist/paywalls/PaywallProvider.js.map +1 -1
- package/dist/paywalls/customScreens.d.ts +31 -0
- package/dist/paywalls/customScreens.d.ts.map +1 -1
- package/dist/paywalls/observer.d.ts +129 -0
- package/dist/paywalls/observer.d.ts.map +1 -0
- package/dist/paywalls/observer.js +211 -0
- package/dist/paywalls/observer.js.map +1 -0
- package/dist/paywalls/present.d.ts +19 -15
- package/dist/paywalls/present.d.ts.map +1 -1
- package/dist/paywalls/present.js +21 -17
- package/dist/paywalls/present.js.map +1 -1
- package/dist/paywalls/types.d.ts +3 -1
- package/dist/paywalls/types.d.ts.map +1 -1
- package/dist/products/adapters/expoIap.d.ts +38 -1
- package/dist/products/adapters/expoIap.d.ts.map +1 -1
- package/dist/products/adapters/expoIap.js +509 -75
- package/dist/products/adapters/expoIap.js.map +1 -1
- package/dist/products/index.d.ts +1 -0
- package/dist/products/index.d.ts.map +1 -1
- package/dist/products/index.js.map +1 -1
- package/dist/products/types.d.ts +13 -0
- package/dist/products/types.d.ts.map +1 -1
- package/dist/requestIdentity.d.ts +41 -0
- package/dist/requestIdentity.d.ts.map +1 -0
- package/dist/requestIdentity.js +70 -0
- package/dist/requestIdentity.js.map +1 -0
- package/dist/screens/completingActions.d.ts +55 -3
- package/dist/screens/completingActions.d.ts.map +1 -1
- package/dist/screens/completingActions.js +209 -15
- package/dist/screens/completingActions.js.map +1 -1
- package/dist/screens/elementTypeRegistry.d.ts +14 -0
- package/dist/screens/elementTypeRegistry.d.ts.map +1 -1
- package/dist/screens/elementTypeRegistry.js +38 -1
- package/dist/screens/elementTypeRegistry.js.map +1 -1
- package/dist/screens/elements/ButtonElement.d.ts +3 -2
- package/dist/screens/elements/ButtonElement.d.ts.map +1 -1
- package/dist/screens/elements/ButtonElement.js +5 -1
- package/dist/screens/elements/ButtonElement.js.map +1 -1
- package/dist/screens/types.d.ts +2 -2
- package/dist/screens/types.d.ts.map +1 -1
- package/dist/screens/types.js +5 -1
- package/dist/screens/types.js.map +1 -1
- package/dist/screens/unknownKeys.d.ts +25 -4
- package/dist/screens/unknownKeys.d.ts.map +1 -1
- package/dist/screens/unknownKeys.js +81 -2
- package/dist/screens/unknownKeys.js.map +1 -1
- package/dist/steps/common.types.d.ts +178 -6
- package/dist/steps/common.types.d.ts.map +1 -1
- package/dist/steps/common.types.js +64 -3
- package/dist/steps/common.types.js.map +1 -1
- package/dist/types.d.ts +17 -0
- package/dist/types.d.ts.map +1 -1
- package/dist/userProperties/reserved.d.ts +16 -15
- package/dist/userProperties/reserved.d.ts.map +1 -1
- package/dist/userProperties/reserved.js +18 -14
- package/dist/userProperties/reserved.js.map +1 -1
- package/dist/version.d.ts +10 -0
- package/dist/version.d.ts.map +1 -0
- package/dist/version.js +13 -0
- package/dist/version.js.map +1 -0
- package/package.json +5 -1
- package/src/OnboardingStudioClient.ts +47 -19
- package/src/__tests__/authoringDocEscapes.test.ts +59 -0
- package/src/__tests__/completingActions.test.ts +371 -1
- package/src/__tests__/customActionHooks.test.ts +224 -0
- package/src/__tests__/getOnboarding.query.test.ts +30 -0
- package/src/__tests__/onboardingExampleElements.test.ts +47 -0
- package/src/__tests__/requestIdentity.client.test.ts +115 -0
- package/src/__tests__/requestIdentity.test.ts +112 -0
- package/src/__tests__/requestPermissionAction.test.ts +138 -0
- package/src/__tests__/retryIdempotencyDocs.test.ts +57 -0
- package/src/__tests__/unknownKeys.test.ts +110 -0
- package/src/index.ts +33 -1
- package/src/onboarding-example.ts +41 -1
- package/src/paywalls/PaywallProvider.tsx +370 -66
- package/src/paywalls/__tests__/PaywallProvider.observer.test.tsx +866 -0
- package/src/paywalls/__tests__/getPaywalls.test.ts +30 -0
- package/src/paywalls/__tests__/observer.test.ts +401 -0
- package/src/paywalls/__tests__/present.test.ts +0 -18
- package/src/paywalls/customScreens.ts +34 -1
- package/src/paywalls/observer.ts +308 -0
- package/src/paywalls/present.ts +24 -18
- package/src/paywalls/types.ts +3 -1
- package/src/products/adapters/__tests__/expoIap.contract.test.ts +648 -0
- package/src/products/adapters/__tests__/expoIap.test.ts +25 -11
- package/src/products/adapters/__tests__/expoIap5Fake.ts +288 -0
- package/src/products/adapters/expoIap.ts +609 -78
- package/src/products/index.ts +1 -0
- package/src/products/types.ts +13 -0
- package/src/requestIdentity.ts +101 -0
- package/src/screens/completingActions.ts +223 -15
- package/src/screens/elementTypeRegistry.ts +34 -0
- package/src/screens/elements/ButtonElement.ts +9 -1
- package/src/screens/types.ts +2 -2
- package/src/screens/unknownKeys.ts +113 -7
- package/src/steps/common.types.ts +213 -6
- package/src/types.ts +17 -0
- package/src/userProperties/__tests__/effectiveParams.test.ts +2 -2
- package/src/userProperties/__tests__/primitives.test.ts +1 -1
- package/src/userProperties/reserved.ts +18 -14
- package/src/version.ts +9 -0
|
@@ -0,0 +1,866 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
3
|
+
import React, { useContext } from "react";
|
|
4
|
+
import { act } from "react";
|
|
5
|
+
import { createRoot, type Root } from "react-dom/client";
|
|
6
|
+
import type { PaywallObserver } from "../observer";
|
|
7
|
+
import type { PaywallContextValue } from "../PaywallProvider";
|
|
8
|
+
import type { ProductRuntime, ProductProvider, PurchaseResult, ResolvedProduct } from "../../products/types";
|
|
9
|
+
import { LATE_PURCHASE_TIMEOUT_MS } from "../present";
|
|
10
|
+
import type { QueryClient } from "@tanstack/react-query";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* `PaywallProvider`'s observer wiring, end to end through a real render
|
|
14
|
+
* (react-dom under jsdom, `react-native` stubbed — the same harness as
|
|
15
|
+
* `__tests__/OnboardingDataGate.test.tsx`). Every test loads a fresh module
|
|
16
|
+
* graph: the provider's `QueryClient` and the user-property store are module
|
|
17
|
+
* singletons.
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
vi.mock("@react-native-async-storage/async-storage", () => {
|
|
21
|
+
const map = new Map<string, string>();
|
|
22
|
+
return {
|
|
23
|
+
default: {
|
|
24
|
+
getItem: async (k: string) => map.get(k) ?? null,
|
|
25
|
+
setItem: async (k: string, v: string) => void map.set(k, v),
|
|
26
|
+
removeItem: async (k: string) => void map.delete(k),
|
|
27
|
+
getAllKeys: async () => [...map.keys()],
|
|
28
|
+
multiRemove: async (ks: string[]) => ks.forEach((k) => map.delete(k)),
|
|
29
|
+
},
|
|
30
|
+
};
|
|
31
|
+
});
|
|
32
|
+
vi.mock("react-native", () => ({
|
|
33
|
+
Platform: { OS: "ios" },
|
|
34
|
+
Image: { prefetch: async () => true },
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
|
|
38
|
+
|
|
39
|
+
const paywall = (moment: string, id = `pw-${moment}`) => ({
|
|
40
|
+
id,
|
|
41
|
+
name: moment,
|
|
42
|
+
moment,
|
|
43
|
+
audienceId: 7,
|
|
44
|
+
audienceName: "everyone",
|
|
45
|
+
elements: [],
|
|
46
|
+
billing: "store" as const,
|
|
47
|
+
products: [{ key: "annual", ios: "com.app.annual.ios", android: "com.app.annual.android" }],
|
|
48
|
+
configuration: null,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const catalogOf = (...moments: string[]) => ({
|
|
52
|
+
metadata: { locale: "en", draft: false },
|
|
53
|
+
paywalls: Object.fromEntries(moments.map((m) => [m, paywall(m)])),
|
|
54
|
+
fonts: null,
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const makeClient = (catalog: ReturnType<typeof catalogOf>) =>
|
|
58
|
+
({
|
|
59
|
+
projectId: "p1",
|
|
60
|
+
options: { isSandbox: true },
|
|
61
|
+
getPaywalls: vi.fn(async () => ({ data: catalog, headers: {} })),
|
|
62
|
+
}) as any;
|
|
63
|
+
|
|
64
|
+
const resolved = (key: string): ResolvedProduct => ({
|
|
65
|
+
key,
|
|
66
|
+
productId: `store-${key}`,
|
|
67
|
+
store: "app_store",
|
|
68
|
+
title: key,
|
|
69
|
+
description: "",
|
|
70
|
+
price: "$1",
|
|
71
|
+
priceAmount: 1,
|
|
72
|
+
currencyCode: "USD",
|
|
73
|
+
period: "year",
|
|
74
|
+
periodCount: 1,
|
|
75
|
+
periodIso: "P1Y",
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
const productProvider: ProductProvider = {
|
|
79
|
+
getProducts: async (refs) => refs.map((r) => resolved(r.key)),
|
|
80
|
+
purchase: async (p) => ({ status: "purchased", productKey: p.key }),
|
|
81
|
+
restore: async () => ({ status: "nothing_to_restore" }),
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
const recordingObserver = () => {
|
|
85
|
+
const calls: Array<[string, unknown?]> = [];
|
|
86
|
+
const observer: PaywallObserver = {
|
|
87
|
+
start: (info) => {
|
|
88
|
+
calls.push(["start", info]);
|
|
89
|
+
return {
|
|
90
|
+
shown: () => void calls.push(["shown"]),
|
|
91
|
+
end: (outcome) => void calls.push(["end", outcome]),
|
|
92
|
+
};
|
|
93
|
+
},
|
|
94
|
+
};
|
|
95
|
+
return { observer, calls };
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
let root: Root | null = null;
|
|
99
|
+
let container: HTMLDivElement | null = null;
|
|
100
|
+
const flush = () => new Promise<void>((r) => setTimeout(r, 0));
|
|
101
|
+
const settle = async () => {
|
|
102
|
+
await act(async () => {
|
|
103
|
+
await flush();
|
|
104
|
+
await flush();
|
|
105
|
+
});
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
type Captured = { ctx: PaywallContextValue; runtime: ProductRuntime | null; queryClient: QueryClient };
|
|
109
|
+
|
|
110
|
+
/** Mounts a real provider and returns live accessors into its context. */
|
|
111
|
+
const mount = async (props: {
|
|
112
|
+
catalog?: ReturnType<typeof catalogOf>;
|
|
113
|
+
observer?: unknown;
|
|
114
|
+
presentAckTimeoutMs?: number | null;
|
|
115
|
+
productProvider?: ProductProvider;
|
|
116
|
+
}) => {
|
|
117
|
+
vi.resetModules();
|
|
118
|
+
const [{ PaywallProvider, PaywallContext }, { ProductRuntimeContext }, { useQueryClient }] = await Promise.all([
|
|
119
|
+
import("../PaywallProvider"),
|
|
120
|
+
import("../../products/ProductRuntimeContext"),
|
|
121
|
+
// After the reset, so it is the instance the provider's QueryClientProvider uses.
|
|
122
|
+
import("@tanstack/react-query"),
|
|
123
|
+
]);
|
|
124
|
+
const captured = {} as Captured;
|
|
125
|
+
const Capture = () => {
|
|
126
|
+
captured.ctx = useContext(PaywallContext);
|
|
127
|
+
captured.runtime = useContext(ProductRuntimeContext);
|
|
128
|
+
captured.queryClient = useQueryClient();
|
|
129
|
+
return null;
|
|
130
|
+
};
|
|
131
|
+
const client = makeClient(props.catalog ?? catalogOf("upgrade", "downsell"));
|
|
132
|
+
const element = (observer: unknown, catalogClient = client) => (
|
|
133
|
+
<PaywallProvider
|
|
134
|
+
client={catalogClient}
|
|
135
|
+
productProvider={props.productProvider ?? productProvider}
|
|
136
|
+
observer={observer as PaywallObserver}
|
|
137
|
+
presentAckTimeoutMs={props.presentAckTimeoutMs === undefined ? null : props.presentAckTimeoutMs}
|
|
138
|
+
>
|
|
139
|
+
<Capture />
|
|
140
|
+
</PaywallProvider>
|
|
141
|
+
);
|
|
142
|
+
container = document.createElement("div");
|
|
143
|
+
document.body.appendChild(container);
|
|
144
|
+
root = createRoot(container);
|
|
145
|
+
await act(async () => {
|
|
146
|
+
root!.render(element(props.observer));
|
|
147
|
+
});
|
|
148
|
+
await settle();
|
|
149
|
+
return {
|
|
150
|
+
captured,
|
|
151
|
+
/** `{ settle: false }` under fake timers: `settle()` waits on a real `setTimeout`. */
|
|
152
|
+
rerender: async (observer: unknown, catalogClient?: any, options?: { settle?: boolean }) => {
|
|
153
|
+
await act(async () => {
|
|
154
|
+
root!.render(element(observer, catalogClient));
|
|
155
|
+
});
|
|
156
|
+
if (options?.settle !== false) await settle();
|
|
157
|
+
},
|
|
158
|
+
};
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
/** For `present()` calls that resolve IMMEDIATELY (a refusal): returns the result. */
|
|
162
|
+
const presentImmediate = async (captured: Captured, moment: string) => {
|
|
163
|
+
let result: unknown;
|
|
164
|
+
await act(async () => {
|
|
165
|
+
result = await captured.ctx.present(moment);
|
|
166
|
+
});
|
|
167
|
+
return result;
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* A background revalidation that swaps a moment's variant: same query key, a
|
|
172
|
+
* fresh catalog (what `getPaywalls.query.ts`'s detached `fetchAndCache` pushes).
|
|
173
|
+
*/
|
|
174
|
+
const revalidate = async (captured: Captured, catalog: unknown) => {
|
|
175
|
+
await act(async () => {
|
|
176
|
+
captured.queryClient.setQueriesData({ queryKey: ["paywallCatalog"] }, catalog);
|
|
177
|
+
});
|
|
178
|
+
await settle();
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
const act_ = async (fn: () => void | Promise<unknown>) => {
|
|
182
|
+
await act(async () => {
|
|
183
|
+
await fn();
|
|
184
|
+
});
|
|
185
|
+
await settle();
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
beforeEach(() => {
|
|
189
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
190
|
+
vi.spyOn(console, "info").mockImplementation(() => {});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
afterEach(async () => {
|
|
194
|
+
if (root) await act(async () => root!.unmount());
|
|
195
|
+
container?.remove();
|
|
196
|
+
root = null;
|
|
197
|
+
container = null;
|
|
198
|
+
vi.restoreAllMocks();
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
describe("PaywallProvider observer — present()", () => {
|
|
202
|
+
it("reports start, shown and end for a presentation, with the contract's info", async () => {
|
|
203
|
+
const { observer, calls } = recordingObserver();
|
|
204
|
+
const { captured } = await mount({ observer });
|
|
205
|
+
await act(async () => void captured.ctx.present("upgrade"));
|
|
206
|
+
await settle();
|
|
207
|
+
await act_(() => captured.ctx.acknowledgePresentation());
|
|
208
|
+
await act_(() => captured.ctx.complete({ status: "dismissed" }));
|
|
209
|
+
|
|
210
|
+
expect(calls).toEqual([
|
|
211
|
+
[
|
|
212
|
+
"start",
|
|
213
|
+
{
|
|
214
|
+
moment: "upgrade",
|
|
215
|
+
paywallId: "pw-upgrade",
|
|
216
|
+
audienceId: "7",
|
|
217
|
+
renderMode: "elements",
|
|
218
|
+
billing: "store",
|
|
219
|
+
surface: "present",
|
|
220
|
+
},
|
|
221
|
+
],
|
|
222
|
+
["shown"],
|
|
223
|
+
["end", { status: "dismissed" }],
|
|
224
|
+
]);
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
it("calls nothing for unknown-moment and already-presenting refusals", async () => {
|
|
228
|
+
const { observer, calls } = recordingObserver();
|
|
229
|
+
const { captured } = await mount({ observer });
|
|
230
|
+
expect(await presentImmediate(captured, "nope")).toEqual({ status: "error", reason: "unknown-moment" });
|
|
231
|
+
expect(calls).toEqual([]);
|
|
232
|
+
|
|
233
|
+
await act(async () => void captured.ctx.present("upgrade"));
|
|
234
|
+
await settle();
|
|
235
|
+
expect(await presentImmediate(captured, "downsell")).toMatchObject({ reason: "already-presenting" });
|
|
236
|
+
expect(calls.map(([n]) => n)).toEqual(["start"]);
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it("ends a never-acknowledged presentation with error host-never-presented", async () => {
|
|
240
|
+
const { observer, calls } = recordingObserver();
|
|
241
|
+
const { captured } = await mount({ observer, presentAckTimeoutMs: 20 });
|
|
242
|
+
let settled: unknown;
|
|
243
|
+
await act(async () => {
|
|
244
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
245
|
+
});
|
|
246
|
+
await act(async () => {
|
|
247
|
+
await new Promise((r) => setTimeout(r, 60));
|
|
248
|
+
});
|
|
249
|
+
await settle();
|
|
250
|
+
expect(settled).toEqual({ status: "error", reason: "host-never-presented" });
|
|
251
|
+
expect(calls.map(([n]) => n)).toEqual(["start", "end"]);
|
|
252
|
+
expect(calls[1]).toEqual(["end", { status: "error", reason: "host-never-presented" }]);
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it("ends with error paywall-disappeared when the moment leaves the catalog mid-presentation", async () => {
|
|
256
|
+
const { observer, calls } = recordingObserver();
|
|
257
|
+
const { captured, rerender } = await mount({ observer });
|
|
258
|
+
let settled: unknown;
|
|
259
|
+
await act(async () => {
|
|
260
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
261
|
+
});
|
|
262
|
+
await settle();
|
|
263
|
+
await act_(() => captured.ctx.acknowledgePresentation());
|
|
264
|
+
// A different client = a different query key, whose catalog lacks the moment.
|
|
265
|
+
const next = makeClient(catalogOf("downsell"));
|
|
266
|
+
next.projectId = "p2";
|
|
267
|
+
await rerender(observer, next);
|
|
268
|
+
expect(settled).toEqual({ status: "error", reason: "paywall-disappeared" });
|
|
269
|
+
expect(calls.map(([n]) => n)).toEqual(["start", "shown", "end"]);
|
|
270
|
+
expect(calls[2]).toEqual(["end", { status: "error", reason: "paywall-disappeared" }]);
|
|
271
|
+
// The END carries the paywall that was started, not whatever is in the catalog now.
|
|
272
|
+
expect((calls[0][1] as { paywallId: string }).paywallId).toBe("pw-upgrade");
|
|
273
|
+
});
|
|
274
|
+
|
|
275
|
+
it("reports a dismiss upgraded to purchased as purchased, with the slot's productId for this platform", async () => {
|
|
276
|
+
const { observer, calls } = recordingObserver();
|
|
277
|
+
const { captured } = await mount({ observer });
|
|
278
|
+
let settled: unknown;
|
|
279
|
+
await act(async () => {
|
|
280
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
281
|
+
});
|
|
282
|
+
await settle();
|
|
283
|
+
await act_(() => captured.runtime!.purchase("annual"));
|
|
284
|
+
await act_(() => captured.ctx.complete({ status: "dismissed" }));
|
|
285
|
+
expect(settled).toEqual({ status: "purchased" });
|
|
286
|
+
expect(calls[calls.length - 1]).toEqual([
|
|
287
|
+
"end",
|
|
288
|
+
{ status: "purchased", transaction: { productId: "com.app.annual.ios" } },
|
|
289
|
+
]);
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
it("does not credit a purchase from a previous presentation to the next one", async () => {
|
|
293
|
+
const { observer, calls } = recordingObserver();
|
|
294
|
+
const { captured } = await mount({ observer });
|
|
295
|
+
await act(async () => void captured.ctx.present("upgrade"));
|
|
296
|
+
await settle();
|
|
297
|
+
await act_(() => captured.runtime!.purchase("annual"));
|
|
298
|
+
await act_(() => captured.ctx.complete({ status: "dismissed" }));
|
|
299
|
+
await act(async () => void captured.ctx.present("downsell"));
|
|
300
|
+
await settle();
|
|
301
|
+
await act_(() => captured.ctx.complete({ status: "purchased" }));
|
|
302
|
+
// Purchased reported by the closing action itself, with no SDK purchase in
|
|
303
|
+
// THIS presentation: no transaction rather than the previous one's.
|
|
304
|
+
expect(calls[calls.length - 1]).toEqual(["end", { status: "purchased" }]);
|
|
305
|
+
});
|
|
306
|
+
|
|
307
|
+
it("ends each presentation exactly once even when complete() is called twice, and starts the downsell clean", async () => {
|
|
308
|
+
const { observer, calls } = recordingObserver();
|
|
309
|
+
const { captured } = await mount({ observer });
|
|
310
|
+
await act(async () => void captured.ctx.present("upgrade"));
|
|
311
|
+
await settle();
|
|
312
|
+
await act_(() => {
|
|
313
|
+
captured.ctx.complete({ status: "dismissed" });
|
|
314
|
+
captured.ctx.complete({ status: "dismissed" });
|
|
315
|
+
});
|
|
316
|
+
expect(calls.filter(([n]) => n === "end")).toHaveLength(1);
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
it("never changes present()'s result when the observer throws or is malformed", async () => {
|
|
320
|
+
const boom = () => {
|
|
321
|
+
throw new Error("observer bug");
|
|
322
|
+
};
|
|
323
|
+
for (const observer of [
|
|
324
|
+
{ start: boom },
|
|
325
|
+
{ start: () => ({ shown: boom, end: boom }) },
|
|
326
|
+
{ start: () => 42 },
|
|
327
|
+
{ start: () => undefined },
|
|
328
|
+
42,
|
|
329
|
+
]) {
|
|
330
|
+
const { captured } = await mount({ observer });
|
|
331
|
+
let settled: unknown;
|
|
332
|
+
await act(async () => {
|
|
333
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
334
|
+
});
|
|
335
|
+
await settle();
|
|
336
|
+
await act_(() => captured.ctx.acknowledgePresentation());
|
|
337
|
+
await act_(() => captured.ctx.complete({ status: "cancelled" }));
|
|
338
|
+
expect(settled).toEqual({ status: "cancelled" });
|
|
339
|
+
// The surface is free again: the next present() starts normally.
|
|
340
|
+
expect(captured.ctx.activePaywall).toBeNull();
|
|
341
|
+
await act(async () => root!.unmount());
|
|
342
|
+
root = null;
|
|
343
|
+
}
|
|
344
|
+
});
|
|
345
|
+
|
|
346
|
+
it("with no observer, behaves exactly as before", async () => {
|
|
347
|
+
const { captured } = await mount({});
|
|
348
|
+
let settled: unknown;
|
|
349
|
+
await act(async () => {
|
|
350
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
351
|
+
});
|
|
352
|
+
await settle();
|
|
353
|
+
await act_(() => captured.ctx.complete({ status: "dismissed" }));
|
|
354
|
+
expect(settled).toEqual({ status: "dismissed" });
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it("reads the CURRENT observer prop, not the one captured at mount", async () => {
|
|
358
|
+
const first = recordingObserver();
|
|
359
|
+
const second = recordingObserver();
|
|
360
|
+
const { captured, rerender } = await mount({ observer: first.observer });
|
|
361
|
+
await rerender(second.observer);
|
|
362
|
+
await act(async () => void captured.ctx.present("upgrade"));
|
|
363
|
+
await settle();
|
|
364
|
+
expect(first.calls).toEqual([]);
|
|
365
|
+
expect(second.calls.map(([n]) => n)).toEqual(["start"]);
|
|
366
|
+
});
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
describe("PaywallProvider observer — present() after a variant swap", () => {
|
|
370
|
+
const variantB = (products: Array<{ key: string; ios: string; android: string }>) => {
|
|
371
|
+
const catalog = catalogOf("upgrade", "downsell");
|
|
372
|
+
catalog.paywalls.upgrade = { ...paywall("upgrade", "pw-upgrade-b"), products };
|
|
373
|
+
return catalog;
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
it("reports the store id of the variant the purchase was made on, for the same slot key", async () => {
|
|
377
|
+
const { observer, calls } = recordingObserver();
|
|
378
|
+
const { captured } = await mount({ observer });
|
|
379
|
+
let settled: unknown;
|
|
380
|
+
await act(async () => {
|
|
381
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
382
|
+
});
|
|
383
|
+
await settle();
|
|
384
|
+
await revalidate(captured, variantB([{ key: "annual", ios: "com.app.b.annual.ios", android: "b.android" }]));
|
|
385
|
+
expect(captured.ctx.activePaywall?.id).toBe("pw-upgrade-b");
|
|
386
|
+
await act_(() => captured.runtime!.purchase("annual"));
|
|
387
|
+
await act_(() => captured.ctx.complete({ status: "dismissed" }));
|
|
388
|
+
expect(settled).toEqual({ status: "purchased" });
|
|
389
|
+
// Credited to the presentation of the FIRST variant...
|
|
390
|
+
expect((calls[0][1] as { paywallId: string }).paywallId).toBe("pw-upgrade");
|
|
391
|
+
// ...with the id of the slot actually bought.
|
|
392
|
+
expect(calls[calls.length - 1]).toEqual([
|
|
393
|
+
"end",
|
|
394
|
+
{ status: "purchased", transaction: { productId: "com.app.b.annual.ios" } },
|
|
395
|
+
]);
|
|
396
|
+
});
|
|
397
|
+
|
|
398
|
+
it("carries a transaction for a slot only the swapped-in variant has", async () => {
|
|
399
|
+
const { observer, calls } = recordingObserver();
|
|
400
|
+
const { captured } = await mount({ observer });
|
|
401
|
+
await act(async () => void captured.ctx.present("upgrade"));
|
|
402
|
+
await settle();
|
|
403
|
+
await revalidate(captured, variantB([{ key: "monthly", ios: "com.app.b.monthly.ios", android: "b.m.android" }]));
|
|
404
|
+
await act_(() => captured.runtime!.purchase("monthly"));
|
|
405
|
+
await act_(() => captured.ctx.complete({ status: "dismissed" }));
|
|
406
|
+
expect(calls[calls.length - 1]).toEqual([
|
|
407
|
+
"end",
|
|
408
|
+
{ status: "purchased", transaction: { productId: "com.app.b.monthly.ios" } },
|
|
409
|
+
]);
|
|
410
|
+
});
|
|
411
|
+
});
|
|
412
|
+
|
|
413
|
+
describe("PaywallProvider observer — a purchase wins over the paywall disappearing (owner decision)", () => {
|
|
414
|
+
/** A store whose purchases settle only when the test says so. */
|
|
415
|
+
const deferredStore = () => {
|
|
416
|
+
const waiting: Array<(r: PurchaseResult) => void> = [];
|
|
417
|
+
const provider: ProductProvider = {
|
|
418
|
+
...productProvider,
|
|
419
|
+
purchase: () => new Promise<PurchaseResult>((resolve) => void waiting.push(resolve)),
|
|
420
|
+
};
|
|
421
|
+
const settleNext = async (result: PurchaseResult) => {
|
|
422
|
+
await act(async () => {
|
|
423
|
+
waiting.shift()!(result);
|
|
424
|
+
});
|
|
425
|
+
await settle();
|
|
426
|
+
};
|
|
427
|
+
return { provider, settleNext, waiting };
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
/** A different query key whose catalog lacks "upgrade": `data` goes undefined, then lacks it. */
|
|
431
|
+
const withoutUpgrade = () => {
|
|
432
|
+
const next = makeClient(catalogOf("downsell"));
|
|
433
|
+
next.projectId = "p2";
|
|
434
|
+
return next;
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
afterEach(() => {
|
|
438
|
+
vi.useRealTimers();
|
|
439
|
+
});
|
|
440
|
+
|
|
441
|
+
it("ends purchased with its key when a purchase SETTLED before the paywall disappeared", async () => {
|
|
442
|
+
const { observer, calls } = recordingObserver();
|
|
443
|
+
const { captured, rerender } = await mount({ observer });
|
|
444
|
+
let settled: unknown;
|
|
445
|
+
await act(async () => {
|
|
446
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
447
|
+
});
|
|
448
|
+
await settle();
|
|
449
|
+
await act_(() => captured.ctx.acknowledgePresentation());
|
|
450
|
+
await act_(() => captured.runtime!.purchase("annual"));
|
|
451
|
+
await rerender(observer, withoutUpgrade());
|
|
452
|
+
expect(settled).toEqual({ status: "purchased" });
|
|
453
|
+
expect(calls.map(([n]) => n)).toEqual(["start", "shown", "end"]);
|
|
454
|
+
expect(calls[2]).toEqual([
|
|
455
|
+
"end",
|
|
456
|
+
{ status: "purchased", transaction: { productId: "com.app.annual.ios" } },
|
|
457
|
+
]);
|
|
458
|
+
});
|
|
459
|
+
|
|
460
|
+
it("waits for a purchase in flight, then ends and resolves with the store outcome", async () => {
|
|
461
|
+
const store = deferredStore();
|
|
462
|
+
const { observer, calls } = recordingObserver();
|
|
463
|
+
const { captured, rerender } = await mount({ observer, productProvider: store.provider });
|
|
464
|
+
let settled: unknown;
|
|
465
|
+
await act(async () => {
|
|
466
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
467
|
+
});
|
|
468
|
+
await settle();
|
|
469
|
+
await act(async () => void captured.runtime!.purchase("annual"));
|
|
470
|
+
await settle();
|
|
471
|
+
expect(store.waiting).toHaveLength(1);
|
|
472
|
+
await rerender(observer, withoutUpgrade());
|
|
473
|
+
expect(settled).toBeUndefined();
|
|
474
|
+
expect(calls.map(([n]) => n)).toEqual(["start"]);
|
|
475
|
+
// The surface is free meanwhile: nothing is left holding `activeMoment`.
|
|
476
|
+
expect(captured.ctx.activePaywall).toBeNull();
|
|
477
|
+
await store.settleNext({ status: "purchased", productKey: "annual" });
|
|
478
|
+
expect(settled).toEqual({ status: "purchased" });
|
|
479
|
+
expect(calls).toEqual([
|
|
480
|
+
["start", expect.objectContaining({ paywallId: "pw-upgrade" })],
|
|
481
|
+
["end", { status: "purchased", transaction: { productId: "com.app.annual.ios" } }],
|
|
482
|
+
]);
|
|
483
|
+
});
|
|
484
|
+
|
|
485
|
+
it("ends dismissed after LATE_PURCHASE_TIMEOUT_MS when the purchase in flight never settles", async () => {
|
|
486
|
+
const store = deferredStore();
|
|
487
|
+
const { observer, calls } = recordingObserver();
|
|
488
|
+
const { captured, rerender } = await mount({ observer, productProvider: store.provider });
|
|
489
|
+
let settled: unknown;
|
|
490
|
+
await act(async () => {
|
|
491
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
492
|
+
});
|
|
493
|
+
await settle();
|
|
494
|
+
await act(async () => void captured.runtime!.purchase("annual"));
|
|
495
|
+
await settle();
|
|
496
|
+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
|
497
|
+
await rerender(observer, withoutUpgrade(), { settle: false });
|
|
498
|
+
await act(async () => {
|
|
499
|
+
vi.advanceTimersByTime(LATE_PURCHASE_TIMEOUT_MS - 1);
|
|
500
|
+
});
|
|
501
|
+
expect(settled).toBeUndefined();
|
|
502
|
+
expect(calls.map(([n]) => n)).toEqual(["start"]);
|
|
503
|
+
await act(async () => {
|
|
504
|
+
vi.advanceTimersByTime(1);
|
|
505
|
+
});
|
|
506
|
+
expect(settled).toEqual({ status: "dismissed" });
|
|
507
|
+
expect(calls[1]).toEqual(["end", { status: "dismissed" }]);
|
|
508
|
+
vi.useRealTimers();
|
|
509
|
+
// A purchase settling after the bound changes nothing.
|
|
510
|
+
await store.settleNext({ status: "purchased", productKey: "annual" });
|
|
511
|
+
expect(calls).toHaveLength(2);
|
|
512
|
+
expect(settled).toEqual({ status: "dismissed" });
|
|
513
|
+
});
|
|
514
|
+
|
|
515
|
+
it("waits for a purchase in flight when the Modal is CLOSED, then resolves with the store outcome (owner ruling, round 5)", async () => {
|
|
516
|
+
// Reverses the round-4 test that asserted the late purchase was dropped.
|
|
517
|
+
const store = deferredStore();
|
|
518
|
+
const { observer, calls } = recordingObserver();
|
|
519
|
+
const { captured } = await mount({ observer, productProvider: store.provider });
|
|
520
|
+
let first: unknown;
|
|
521
|
+
await act(async () => {
|
|
522
|
+
captured.ctx.present("upgrade").then((r) => (first = r));
|
|
523
|
+
});
|
|
524
|
+
await settle();
|
|
525
|
+
await act(async () => void captured.runtime!.purchase("annual"));
|
|
526
|
+
await settle();
|
|
527
|
+
await act_(() => captured.ctx.complete({ status: "dismissed" }));
|
|
528
|
+
expect(first).toBeUndefined();
|
|
529
|
+
expect(calls.map(([n]) => n)).toEqual(["start"]);
|
|
530
|
+
// The surface is freed meanwhile.
|
|
531
|
+
expect(captured.ctx.activePaywall).toBeNull();
|
|
532
|
+
await store.settleNext({ status: "purchased", productKey: "annual" });
|
|
533
|
+
expect(first).toEqual({ status: "purchased" });
|
|
534
|
+
expect(calls[1]).toEqual([
|
|
535
|
+
"end",
|
|
536
|
+
{ status: "purchased", transaction: { productId: "com.app.annual.ios" } },
|
|
537
|
+
]);
|
|
538
|
+
});
|
|
539
|
+
|
|
540
|
+
it("ends a closed presentation dismissed after LATE_PURCHASE_TIMEOUT_MS when its purchase never settles", async () => {
|
|
541
|
+
const store = deferredStore();
|
|
542
|
+
const { observer, calls } = recordingObserver();
|
|
543
|
+
const { captured } = await mount({ observer, productProvider: store.provider });
|
|
544
|
+
let first: unknown;
|
|
545
|
+
await act(async () => {
|
|
546
|
+
captured.ctx.present("upgrade").then((r) => (first = r));
|
|
547
|
+
});
|
|
548
|
+
await settle();
|
|
549
|
+
await act(async () => void captured.runtime!.purchase("annual"));
|
|
550
|
+
await settle();
|
|
551
|
+
vi.useFakeTimers({ toFake: ["setTimeout", "clearTimeout"] });
|
|
552
|
+
await act(async () => captured.ctx.complete({ status: "dismissed" }));
|
|
553
|
+
await act(async () => {
|
|
554
|
+
vi.advanceTimersByTime(LATE_PURCHASE_TIMEOUT_MS - 1);
|
|
555
|
+
});
|
|
556
|
+
expect(first).toBeUndefined();
|
|
557
|
+
await act(async () => {
|
|
558
|
+
vi.advanceTimersByTime(1);
|
|
559
|
+
});
|
|
560
|
+
expect(first).toEqual({ status: "dismissed" });
|
|
561
|
+
expect(calls[1]).toEqual(["end", { status: "dismissed" }]);
|
|
562
|
+
});
|
|
563
|
+
|
|
564
|
+
it("drops a purchase that settles after its presentation ENDED, and never credits it to the next", async () => {
|
|
565
|
+
// The race the old generation counter guarded. A non-dismissal end (here a
|
|
566
|
+
// host render error) does not wait, so A has ended when its purchase settles.
|
|
567
|
+
const store = deferredStore();
|
|
568
|
+
const { observer, calls } = recordingObserver();
|
|
569
|
+
const { captured } = await mount({ observer, productProvider: store.provider });
|
|
570
|
+
let first: unknown;
|
|
571
|
+
let second: unknown;
|
|
572
|
+
await act(async () => {
|
|
573
|
+
captured.ctx.present("upgrade").then((r) => (first = r));
|
|
574
|
+
});
|
|
575
|
+
await settle();
|
|
576
|
+
await act(async () => void captured.runtime!.purchase("annual"));
|
|
577
|
+
await settle();
|
|
578
|
+
await act_(() => captured.ctx.complete({ status: "error", reason: "render-error" }));
|
|
579
|
+
expect(first).toEqual({ status: "error", reason: "render-error" });
|
|
580
|
+
await act(async () => {
|
|
581
|
+
captured.ctx.present("downsell").then((r) => (second = r));
|
|
582
|
+
});
|
|
583
|
+
await settle();
|
|
584
|
+
await store.settleNext({ status: "purchased", productKey: "annual" });
|
|
585
|
+
await act_(() => captured.ctx.complete({ status: "dismissed" }));
|
|
586
|
+
expect(second).toEqual({ status: "dismissed" });
|
|
587
|
+
expect(calls.filter(([n]) => n === "end")).toEqual([
|
|
588
|
+
["end", { status: "error", reason: "render-error" }],
|
|
589
|
+
["end", { status: "dismissed" }],
|
|
590
|
+
]);
|
|
591
|
+
});
|
|
592
|
+
|
|
593
|
+
it("lets a downsell present() run while it waits, and never crosses the two presentations", async () => {
|
|
594
|
+
const store = deferredStore();
|
|
595
|
+
const { observer, calls } = recordingObserver();
|
|
596
|
+
const { captured, rerender } = await mount({ observer, productProvider: store.provider });
|
|
597
|
+
let first: unknown;
|
|
598
|
+
let second: unknown;
|
|
599
|
+
await act(async () => {
|
|
600
|
+
captured.ctx.present("upgrade").then((r) => (first = r));
|
|
601
|
+
});
|
|
602
|
+
await settle();
|
|
603
|
+
await act(async () => void captured.runtime!.purchase("annual"));
|
|
604
|
+
await settle();
|
|
605
|
+
await rerender(observer, withoutUpgrade());
|
|
606
|
+
await act(async () => {
|
|
607
|
+
captured.ctx.present("downsell").then((r) => (second = r));
|
|
608
|
+
});
|
|
609
|
+
await settle();
|
|
610
|
+
expect(captured.ctx.activePaywall?.moment).toBe("downsell");
|
|
611
|
+
expect(calls.map(([n]) => n)).toEqual(["start", "start"]);
|
|
612
|
+
|
|
613
|
+
await store.settleNext({ status: "purchased", productKey: "annual" });
|
|
614
|
+
expect(first).toEqual({ status: "purchased" });
|
|
615
|
+
expect(second).toBeUndefined();
|
|
616
|
+
expect(calls[2]).toEqual([
|
|
617
|
+
"end",
|
|
618
|
+
{ status: "purchased", transaction: { productId: "com.app.annual.ios" } },
|
|
619
|
+
]);
|
|
620
|
+
// The downsell is still showing, and its end owes nothing to the old purchase.
|
|
621
|
+
expect(captured.ctx.activePaywall?.moment).toBe("downsell");
|
|
622
|
+
await act_(() => captured.ctx.complete({ status: "dismissed" }));
|
|
623
|
+
expect(second).toEqual({ status: "dismissed" });
|
|
624
|
+
expect(calls[3]).toEqual(["end", { status: "dismissed" }]);
|
|
625
|
+
});
|
|
626
|
+
});
|
|
627
|
+
|
|
628
|
+
describe("PaywallProvider observer — a settled purchase sticks (owner ruling, round 5)", () => {
|
|
629
|
+
const deferredStore = () => {
|
|
630
|
+
const waiting: Array<(r: PurchaseResult) => void> = [];
|
|
631
|
+
const provider: ProductProvider = {
|
|
632
|
+
...productProvider,
|
|
633
|
+
purchase: () => new Promise<PurchaseResult>((resolve) => void waiting.push(resolve)),
|
|
634
|
+
};
|
|
635
|
+
const settleNext = async (result: PurchaseResult) => {
|
|
636
|
+
await act(async () => {
|
|
637
|
+
waiting.shift()!(result);
|
|
638
|
+
});
|
|
639
|
+
await settle();
|
|
640
|
+
};
|
|
641
|
+
return { provider, settleNext };
|
|
642
|
+
};
|
|
643
|
+
const withoutUpgrade = () => {
|
|
644
|
+
const next = makeClient(catalogOf("downsell"));
|
|
645
|
+
next.projectId = "p2";
|
|
646
|
+
return next;
|
|
647
|
+
};
|
|
648
|
+
|
|
649
|
+
it("paid, then a second purchase cancelled, then the paywall disappears: register() still sees purchased", async () => {
|
|
650
|
+
const store = deferredStore();
|
|
651
|
+
const { observer, calls } = recordingObserver();
|
|
652
|
+
const { captured, rerender } = await mount({ observer, productProvider: store.provider });
|
|
653
|
+
let registered: any;
|
|
654
|
+
await act(async () => {
|
|
655
|
+
captured.ctx.register("upgrade").then((r) => (registered = r));
|
|
656
|
+
});
|
|
657
|
+
await settle();
|
|
658
|
+
await act(async () => void captured.runtime!.purchase("annual"));
|
|
659
|
+
await settle();
|
|
660
|
+
await store.settleNext({ status: "purchased", productKey: "annual" });
|
|
661
|
+
await act(async () => void captured.runtime!.purchase("annual"));
|
|
662
|
+
await settle();
|
|
663
|
+
await store.settleNext({ status: "cancelled" });
|
|
664
|
+
await rerender(observer, withoutUpgrade());
|
|
665
|
+
expect(registered).toMatchObject({ ran: true, reason: "purchased", outcome: { status: "purchased" } });
|
|
666
|
+
expect(calls[calls.length - 1]).toEqual([
|
|
667
|
+
"end",
|
|
668
|
+
{ status: "purchased", transaction: { productId: "com.app.annual.ios" } },
|
|
669
|
+
]);
|
|
670
|
+
});
|
|
671
|
+
|
|
672
|
+
it("a close with a purchase in flight ends at once when one already settled purchased (round 6)", async () => {
|
|
673
|
+
const store = deferredStore();
|
|
674
|
+
const { observer, calls } = recordingObserver();
|
|
675
|
+
const { captured } = await mount({ observer, productProvider: store.provider });
|
|
676
|
+
let settled: unknown;
|
|
677
|
+
await act(async () => {
|
|
678
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
679
|
+
});
|
|
680
|
+
await settle();
|
|
681
|
+
await act(async () => void captured.runtime!.purchase("annual"));
|
|
682
|
+
await settle();
|
|
683
|
+
await store.settleNext({ status: "purchased", productKey: "annual" });
|
|
684
|
+
await act(async () => void captured.runtime!.purchase("annual"));
|
|
685
|
+
await settle();
|
|
686
|
+
await act_(() => captured.ctx.complete({ status: "dismissed" }));
|
|
687
|
+
expect(settled).toEqual({ status: "purchased" });
|
|
688
|
+
expect(calls[calls.length - 1]).toEqual([
|
|
689
|
+
"end",
|
|
690
|
+
{ status: "purchased", transaction: { productId: "com.app.annual.ios" } },
|
|
691
|
+
]);
|
|
692
|
+
});
|
|
693
|
+
|
|
694
|
+
it("a disappearance with a purchase in flight ends at once when one already settled purchased (round 6)", async () => {
|
|
695
|
+
const store = deferredStore();
|
|
696
|
+
const { observer } = recordingObserver();
|
|
697
|
+
const { captured, rerender } = await mount({ observer, productProvider: store.provider });
|
|
698
|
+
let settled: unknown;
|
|
699
|
+
await act(async () => {
|
|
700
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
701
|
+
});
|
|
702
|
+
await settle();
|
|
703
|
+
await act(async () => void captured.runtime!.purchase("annual"));
|
|
704
|
+
await settle();
|
|
705
|
+
await store.settleNext({ status: "purchased", productKey: "annual" });
|
|
706
|
+
await act(async () => void captured.runtime!.purchase("annual"));
|
|
707
|
+
await settle();
|
|
708
|
+
await rerender(observer, withoutUpgrade());
|
|
709
|
+
expect(settled).toEqual({ status: "purchased" });
|
|
710
|
+
});
|
|
711
|
+
|
|
712
|
+
it("a host-reported error after a settled purchase still ends purchased, with its transaction", async () => {
|
|
713
|
+
const { observer, calls } = recordingObserver();
|
|
714
|
+
const { captured } = await mount({ observer });
|
|
715
|
+
let settled: unknown;
|
|
716
|
+
await act(async () => {
|
|
717
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
718
|
+
});
|
|
719
|
+
await settle();
|
|
720
|
+
await act_(() => captured.runtime!.purchase("annual"));
|
|
721
|
+
await act_(() => captured.ctx.complete({ status: "error", reason: "render-error" }));
|
|
722
|
+
expect(settled).toEqual({ status: "purchased" });
|
|
723
|
+
expect(calls[calls.length - 1]).toEqual([
|
|
724
|
+
"end",
|
|
725
|
+
{ status: "purchased", transaction: { productId: "com.app.annual.ios" } },
|
|
726
|
+
]);
|
|
727
|
+
});
|
|
728
|
+
});
|
|
729
|
+
|
|
730
|
+
describe("PaywallProvider observer — observePresentation (the inline step's seam)", () => {
|
|
731
|
+
it("opens a paywall_step presentation through the same observer", async () => {
|
|
732
|
+
const { observer, calls } = recordingObserver();
|
|
733
|
+
const { captured } = await mount({ observer });
|
|
734
|
+
const p = captured.ctx.observePresentation(paywall("upgrade"));
|
|
735
|
+
p.shown();
|
|
736
|
+
p.end({ status: "purchased" }, "annual");
|
|
737
|
+
expect(calls).toEqual([
|
|
738
|
+
["start", expect.objectContaining({ surface: "paywall_step", paywallId: "pw-upgrade" })],
|
|
739
|
+
["shown"],
|
|
740
|
+
["end", { status: "purchased", transaction: { productId: "com.app.annual.ios" } }],
|
|
741
|
+
]);
|
|
742
|
+
});
|
|
743
|
+
});
|
|
744
|
+
|
|
745
|
+
describe("PaywallProvider observer — complete() carries a custom screen's transaction (#287)", () => {
|
|
746
|
+
const tx = { originalTransactionIdentifier: "2000000123", productId: "com.app.annual.ios" };
|
|
747
|
+
|
|
748
|
+
const presentUpgrade = async (captured: Captured) => {
|
|
749
|
+
let settled: unknown;
|
|
750
|
+
await act(async () => {
|
|
751
|
+
captured.ctx.present("upgrade").then((r) => (settled = r));
|
|
752
|
+
});
|
|
753
|
+
await settle();
|
|
754
|
+
return () => settled;
|
|
755
|
+
};
|
|
756
|
+
|
|
757
|
+
it("forwards it, sanitized, to end() and leaves present()'s result unchanged", async () => {
|
|
758
|
+
const { observer, calls } = recordingObserver();
|
|
759
|
+
const { captured } = await mount({ observer });
|
|
760
|
+
const settled = await presentUpgrade(captured);
|
|
761
|
+
await act_(() => captured.ctx.complete({ status: "purchased" }, { ...tx, restored: "no", extra: 1 } as any));
|
|
762
|
+
expect(settled()).toEqual({ status: "purchased" });
|
|
763
|
+
expect(calls[calls.length - 1]).toEqual(["end", { status: "purchased", transaction: tx }]);
|
|
764
|
+
});
|
|
765
|
+
|
|
766
|
+
it("drops it when the screen reports anything other than purchased", async () => {
|
|
767
|
+
for (const status of ["dismissed", "cancelled"] as const) {
|
|
768
|
+
const { observer, calls } = recordingObserver();
|
|
769
|
+
const { captured } = await mount({ observer });
|
|
770
|
+
const settled = await presentUpgrade(captured);
|
|
771
|
+
await act_(() => captured.ctx.complete({ status }, tx));
|
|
772
|
+
expect(settled()).toEqual({ status });
|
|
773
|
+
expect(calls[calls.length - 1]).toEqual(["end", { status }]);
|
|
774
|
+
await act(async () => root!.unmount());
|
|
775
|
+
root = null;
|
|
776
|
+
}
|
|
777
|
+
});
|
|
778
|
+
|
|
779
|
+
it("drops it with a cancelled report even when a recorded purchase makes the end purchased", async () => {
|
|
780
|
+
const { observer, calls } = recordingObserver();
|
|
781
|
+
const { captured } = await mount({ observer });
|
|
782
|
+
const settled = await presentUpgrade(captured);
|
|
783
|
+
await act_(() => captured.runtime!.purchase("annual"));
|
|
784
|
+
await act_(() => captured.ctx.complete({ status: "cancelled" }, { originalTransactionIdentifier: "2000000123" }));
|
|
785
|
+
expect(settled()).toEqual({ status: "purchased" });
|
|
786
|
+
expect(calls[calls.length - 1]).toEqual([
|
|
787
|
+
"end",
|
|
788
|
+
{ status: "purchased", transaction: { productId: "com.app.annual.ios" } },
|
|
789
|
+
]);
|
|
790
|
+
});
|
|
791
|
+
|
|
792
|
+
it("adds the screen's ids to a recorded purchase only when it names the same product", async () => {
|
|
793
|
+
const { observer, calls } = recordingObserver();
|
|
794
|
+
const { captured } = await mount({ observer });
|
|
795
|
+
await presentUpgrade(captured);
|
|
796
|
+
await act_(() => captured.runtime!.purchase("annual"));
|
|
797
|
+
await act_(() => captured.ctx.complete({ status: "purchased" }, tx));
|
|
798
|
+
expect(calls[calls.length - 1]).toEqual(["end", { status: "purchased", transaction: tx }]);
|
|
799
|
+
});
|
|
800
|
+
|
|
801
|
+
it("drops the screen's ids, with a warning, when they name no product and a purchase is recorded", async () => {
|
|
802
|
+
const { observer, calls } = recordingObserver();
|
|
803
|
+
const { captured } = await mount({ observer });
|
|
804
|
+
await presentUpgrade(captured);
|
|
805
|
+
await act_(() => captured.runtime!.purchase("annual"));
|
|
806
|
+
vi.mocked(console.warn).mockClear();
|
|
807
|
+
await act_(() => captured.ctx.complete({ status: "purchased" }, { originalTransactionIdentifier: "2000000123" }));
|
|
808
|
+
expect(calls[calls.length - 1]).toEqual([
|
|
809
|
+
"end",
|
|
810
|
+
{ status: "purchased", transaction: { productId: "com.app.annual.ios" } },
|
|
811
|
+
]);
|
|
812
|
+
expect(console.warn).toHaveBeenCalledTimes(1);
|
|
813
|
+
});
|
|
814
|
+
|
|
815
|
+
it("warns once when it drops a transaction reported with a status other than purchased", async () => {
|
|
816
|
+
const { observer } = recordingObserver();
|
|
817
|
+
const { captured } = await mount({ observer });
|
|
818
|
+
await presentUpgrade(captured);
|
|
819
|
+
vi.mocked(console.warn).mockClear();
|
|
820
|
+
await act_(() => captured.ctx.complete({ status: "dismissed" }, tx));
|
|
821
|
+
expect(console.warn).toHaveBeenCalledTimes(1);
|
|
822
|
+
expect(vi.mocked(console.warn).mock.calls[0][0]).toContain("dismissed");
|
|
823
|
+
});
|
|
824
|
+
|
|
825
|
+
it("still resolves present() and ends once when the transaction throws when read", async () => {
|
|
826
|
+
const revocable = Proxy.revocable({}, {});
|
|
827
|
+
revocable.revoke();
|
|
828
|
+
const throwing = Object.defineProperty({}, "productId", {
|
|
829
|
+
enumerable: true,
|
|
830
|
+
get() {
|
|
831
|
+
throw new Error("getter bug");
|
|
832
|
+
},
|
|
833
|
+
});
|
|
834
|
+
for (const hostile of [revocable.proxy, throwing]) {
|
|
835
|
+
const { observer, calls } = recordingObserver();
|
|
836
|
+
const { captured } = await mount({ observer });
|
|
837
|
+
const settled = await presentUpgrade(captured);
|
|
838
|
+
await act_(() => captured.ctx.complete({ status: "purchased" }, hostile as any));
|
|
839
|
+
expect(settled()).toEqual({ status: "purchased" });
|
|
840
|
+
expect(calls.filter(([n]) => n === "end")).toEqual([["end", { status: "purchased" }]]);
|
|
841
|
+
expect(captured.ctx.activePaywall).toBeNull();
|
|
842
|
+
await act(async () => root!.unmount());
|
|
843
|
+
root = null;
|
|
844
|
+
}
|
|
845
|
+
});
|
|
846
|
+
|
|
847
|
+
it("is a no-op from a stale complete, transaction included", async () => {
|
|
848
|
+
const { observer, calls } = recordingObserver();
|
|
849
|
+
const { captured } = await mount({ observer });
|
|
850
|
+
await presentUpgrade(captured);
|
|
851
|
+
const staleComplete = captured.ctx.complete;
|
|
852
|
+
await act_(() => staleComplete({ status: "dismissed" }));
|
|
853
|
+
let downsell: unknown;
|
|
854
|
+
await act(async () => {
|
|
855
|
+
captured.ctx.present("downsell").then((r) => (downsell = r));
|
|
856
|
+
});
|
|
857
|
+
await settle();
|
|
858
|
+
await act_(() => staleComplete({ status: "purchased" }, tx));
|
|
859
|
+
expect(downsell).toBeUndefined();
|
|
860
|
+
expect(captured.ctx.activePaywall?.moment).toBe("downsell");
|
|
861
|
+
expect(calls.filter(([n]) => n === "end")).toEqual([["end", { status: "dismissed" }]]);
|
|
862
|
+
await act_(() => captured.ctx.complete({ status: "dismissed" }));
|
|
863
|
+
expect(downsell).toEqual({ status: "dismissed" });
|
|
864
|
+
expect(calls[calls.length - 1]).toEqual(["end", { status: "dismissed" }]);
|
|
865
|
+
});
|
|
866
|
+
});
|