@rocapine/react-native-onboarding 1.73.0 → 1.74.1
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 +164 -3
- package/dist/OnboardingStudio.d.ts +82 -0
- package/dist/OnboardingStudio.d.ts.map +1 -0
- package/dist/OnboardingStudio.js +91 -0
- package/dist/OnboardingStudio.js.map +1 -0
- package/dist/OnboardingStudioClient.d.ts +14 -5
- package/dist/OnboardingStudioClient.d.ts.map +1 -1
- package/dist/OnboardingStudioClient.js +24 -9
- package/dist/OnboardingStudioClient.js.map +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +40 -4
- package/dist/index.js.map +1 -1
- package/dist/infra/provider/OnboardingProvider.d.ts +15 -2
- package/dist/infra/provider/OnboardingProvider.d.ts.map +1 -1
- package/dist/infra/provider/OnboardingProvider.js +66 -6
- package/dist/infra/provider/OnboardingProvider.js.map +1 -1
- package/dist/infra/queries/cacheKey.d.ts +41 -3
- package/dist/infra/queries/cacheKey.d.ts.map +1 -1
- package/dist/infra/queries/cacheKey.js +44 -8
- package/dist/infra/queries/cacheKey.js.map +1 -1
- package/dist/infra/queries/getOnboarding.query.d.ts.map +1 -1
- package/dist/infra/queries/getOnboarding.query.js +6 -1
- package/dist/infra/queries/getOnboarding.query.js.map +1 -1
- package/dist/paywalls/PaywallProvider.d.ts +52 -4
- package/dist/paywalls/PaywallProvider.d.ts.map +1 -1
- package/dist/paywalls/PaywallProvider.js +105 -7
- package/dist/paywalls/PaywallProvider.js.map +1 -1
- package/dist/paywalls/catalogSettleWaiter.d.ts +31 -0
- package/dist/paywalls/catalogSettleWaiter.d.ts.map +1 -0
- package/dist/paywalls/catalogSettleWaiter.js +58 -0
- package/dist/paywalls/catalogSettleWaiter.js.map +1 -0
- package/dist/paywalls/getPaywalls.query.d.ts.map +1 -1
- package/dist/paywalls/getPaywalls.query.js +6 -1
- package/dist/paywalls/getPaywalls.query.js.map +1 -1
- package/dist/paywalls/register.d.ts +105 -0
- package/dist/paywalls/register.d.ts.map +1 -0
- package/dist/paywalls/register.js +112 -0
- package/dist/paywalls/register.js.map +1 -0
- package/dist/paywalls/usePaywall.d.ts +29 -0
- package/dist/paywalls/usePaywall.d.ts.map +1 -1
- package/dist/paywalls/usePaywall.js +2 -1
- package/dist/paywalls/usePaywall.js.map +1 -1
- package/dist/userProperties/applyPatch.d.ts +18 -0
- package/dist/userProperties/applyPatch.d.ts.map +1 -0
- package/dist/userProperties/applyPatch.js +57 -0
- package/dist/userProperties/applyPatch.js.map +1 -0
- package/dist/userProperties/effectiveParams.d.ts +29 -0
- package/dist/userProperties/effectiveParams.d.ts.map +1 -0
- package/dist/userProperties/effectiveParams.js +56 -0
- package/dist/userProperties/effectiveParams.js.map +1 -0
- package/dist/userProperties/index.d.ts +9 -0
- package/dist/userProperties/index.d.ts.map +1 -0
- package/dist/userProperties/index.js +21 -0
- package/dist/userProperties/index.js.map +1 -0
- package/dist/userProperties/reserved.d.ts +33 -0
- package/dist/userProperties/reserved.d.ts.map +1 -0
- package/dist/userProperties/reserved.js +45 -0
- package/dist/userProperties/reserved.js.map +1 -0
- package/dist/userProperties/serialize.d.ts +38 -0
- package/dist/userProperties/serialize.d.ts.map +1 -0
- package/dist/userProperties/serialize.js +67 -0
- package/dist/userProperties/serialize.js.map +1 -0
- package/dist/userProperties/store.d.ts +65 -0
- package/dist/userProperties/store.d.ts.map +1 -0
- package/dist/userProperties/store.js +155 -0
- package/dist/userProperties/store.js.map +1 -0
- package/dist/userProperties/types.d.ts +21 -0
- package/dist/userProperties/types.d.ts.map +1 -0
- package/dist/userProperties/types.js +3 -0
- package/dist/userProperties/types.js.map +1 -0
- package/dist/userProperties/useUserProperties.d.ts +20 -0
- package/dist/userProperties/useUserProperties.d.ts.map +1 -0
- package/dist/userProperties/useUserProperties.js +30 -0
- package/dist/userProperties/useUserProperties.js.map +1 -0
- package/package.json +5 -1
- package/src/OnboardingStudio.ts +147 -0
- package/src/OnboardingStudioClient.ts +27 -11
- package/src/__tests__/OnboardingDataGate.test.tsx +445 -0
- package/src/__tests__/OnboardingStudio.test.ts +208 -0
- package/src/__tests__/cacheKey.test.ts +71 -0
- package/src/__tests__/getOnboarding.query.test.ts +47 -8
- package/src/index.ts +43 -0
- package/src/infra/provider/OnboardingProvider.tsx +98 -10
- package/src/infra/queries/cacheKey.ts +54 -9
- package/src/infra/queries/getOnboarding.query.ts +9 -1
- package/src/paywalls/PaywallProvider.tsx +162 -6
- package/src/paywalls/__tests__/catalogSettleWaiter.test.ts +134 -0
- package/src/paywalls/__tests__/getPaywalls.test.ts +29 -11
- package/src/paywalls/__tests__/register.test.ts +266 -0
- package/src/paywalls/catalogSettleWaiter.ts +72 -0
- package/src/paywalls/getPaywalls.query.ts +9 -1
- package/src/paywalls/register.ts +169 -0
- package/src/paywalls/usePaywall.ts +40 -2
- package/src/userProperties/__tests__/effectiveParams.test.ts +51 -0
- package/src/userProperties/__tests__/primitives.test.ts +96 -0
- package/src/userProperties/__tests__/store.test.ts +181 -0
- package/src/userProperties/applyPatch.ts +68 -0
- package/src/userProperties/effectiveParams.ts +60 -0
- package/src/userProperties/index.ts +11 -0
- package/src/userProperties/reserved.ts +44 -0
- package/src/userProperties/serialize.ts +61 -0
- package/src/userProperties/store.ts +179 -0
- package/src/userProperties/types.ts +22 -0
- package/src/userProperties/useUserProperties.ts +26 -0
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from "vitest";
|
|
3
|
+
import React, { StrictMode, Suspense, useEffect } from "react";
|
|
4
|
+
import { act } from "react";
|
|
5
|
+
import { createRoot, type Root } from "react-dom/client";
|
|
6
|
+
import { useQueryClient } from "@tanstack/react-query";
|
|
7
|
+
import { USER_PROPERTIES_STORAGE_KEY } from "../userProperties/store";
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* The serve-time rule, end to end through a real render:
|
|
11
|
+
*
|
|
12
|
+
* Audience resolution happens at serve time, and a served payload is frozen
|
|
13
|
+
* for that presentation. User-property changes apply to the NEXT serve.
|
|
14
|
+
*
|
|
15
|
+
* These render the real `OnboardingProvider` (react-dom under jsdom — the gate
|
|
16
|
+
* has no RN-only code path; `react-native` is stubbed the way the other suites
|
|
17
|
+
* stub it) against a fake client, so the assertions are about what a host
|
|
18
|
+
* observes: how many times `getSteps` ran, with which audience params, and
|
|
19
|
+
* whether the subtree under the provider ever unmounted.
|
|
20
|
+
*
|
|
21
|
+
* Every test loads a FRESH module graph (`vi.resetModules` + dynamic import):
|
|
22
|
+
* the user-property store, the `OnboardingStudio` facade and the provider's
|
|
23
|
+
* `QueryClient` are all module singletons, and a "next launch" is exactly a
|
|
24
|
+
* fresh graph over the same persisted storage.
|
|
25
|
+
*/
|
|
26
|
+
|
|
27
|
+
// In-memory AsyncStorage shared across module resets, so a property persisted
|
|
28
|
+
// during one "launch" is there to hydrate on the next. `holdHydration` makes
|
|
29
|
+
// the user-property read (only that one — a production client's payload cache
|
|
30
|
+
// read must not deadlock on it) block until released: the window in which a
|
|
31
|
+
// host writes a property before the store is ready.
|
|
32
|
+
const storage = vi.hoisted(() => {
|
|
33
|
+
const map = new Map<string, string>();
|
|
34
|
+
let gate: { promise: Promise<void>; release: () => void } | null = null;
|
|
35
|
+
// Literal, not the imported constant: `vi.hoisted` runs before imports.
|
|
36
|
+
// `beforeEach` checks it still matches `USER_PROPERTIES_STORAGE_KEY`.
|
|
37
|
+
const heldKey = "rocapine-user-properties";
|
|
38
|
+
return {
|
|
39
|
+
map,
|
|
40
|
+
heldKey,
|
|
41
|
+
holdHydration() {
|
|
42
|
+
let release!: () => void;
|
|
43
|
+
const promise = new Promise<void>((r) => (release = r));
|
|
44
|
+
gate = { promise, release };
|
|
45
|
+
},
|
|
46
|
+
releaseHydration() {
|
|
47
|
+
gate?.release();
|
|
48
|
+
gate = null;
|
|
49
|
+
},
|
|
50
|
+
getItem: vi.fn(async (key: string) => {
|
|
51
|
+
if (gate && key === heldKey) await gate.promise;
|
|
52
|
+
return map.get(key) ?? null;
|
|
53
|
+
}),
|
|
54
|
+
setItem: vi.fn(async (key: string, value: string) => void map.set(key, value)),
|
|
55
|
+
removeItem: vi.fn(async (key: string) => void map.delete(key)),
|
|
56
|
+
getAllKeys: vi.fn(async () => [...map.keys()]),
|
|
57
|
+
multiRemove: vi.fn(async (keys: string[]) => keys.forEach((k) => map.delete(k))),
|
|
58
|
+
};
|
|
59
|
+
});
|
|
60
|
+
vi.mock("@react-native-async-storage/async-storage", () => ({ default: storage }));
|
|
61
|
+
// `OnboardingStudioClient` reads `Platform.OS`; `preloadAssets` reaches for
|
|
62
|
+
// `Image.prefetch`. Neither is exercised here beyond loading.
|
|
63
|
+
vi.mock("react-native", () => ({
|
|
64
|
+
Platform: { OS: "ios" },
|
|
65
|
+
Image: { prefetch: vi.fn(async () => true) },
|
|
66
|
+
}));
|
|
67
|
+
|
|
68
|
+
(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true;
|
|
69
|
+
|
|
70
|
+
type Modules = {
|
|
71
|
+
OnboardingProvider: typeof import("../infra/provider/OnboardingProvider").OnboardingProvider;
|
|
72
|
+
OnboardingStudio: typeof import("../OnboardingStudio").OnboardingStudio;
|
|
73
|
+
useOnboardingStep: typeof import("../infra/hooks/useOnboardingStep").useOnboardingStep;
|
|
74
|
+
useOnboardingStart: typeof import("../infra/hooks/useOnboardingStart").useOnboardingStart;
|
|
75
|
+
};
|
|
76
|
+
|
|
77
|
+
/** A fresh module graph — a fresh store, facade and QueryClient — i.e. one launch. */
|
|
78
|
+
const launch = async (): Promise<Modules> => {
|
|
79
|
+
vi.resetModules();
|
|
80
|
+
const [provider, studio, step, start] = await Promise.all([
|
|
81
|
+
import("../infra/provider/OnboardingProvider"),
|
|
82
|
+
import("../OnboardingStudio"),
|
|
83
|
+
import("../infra/hooks/useOnboardingStep"),
|
|
84
|
+
import("../infra/hooks/useOnboardingStart"),
|
|
85
|
+
]);
|
|
86
|
+
return {
|
|
87
|
+
OnboardingProvider: provider.OnboardingProvider,
|
|
88
|
+
OnboardingStudio: studio.OnboardingStudio,
|
|
89
|
+
useOnboardingStep: step.useOnboardingStep,
|
|
90
|
+
useOnboardingStart: start.useOnboardingStart,
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
type Fonts = Record<string, Record<string, string>>;
|
|
95
|
+
|
|
96
|
+
const onboarding = (id: string, fonts: Fonts = {}) =>
|
|
97
|
+
({
|
|
98
|
+
metadata: { id, name: id },
|
|
99
|
+
steps: [{ id: `${id}-step-1`, name: "one", type: "MediaContent", displayProgressHeader: true, payload: {} }],
|
|
100
|
+
fonts,
|
|
101
|
+
}) as any;
|
|
102
|
+
|
|
103
|
+
// A real fonts manifest. With an empty one `FontLoaderGate` never passes through
|
|
104
|
+
// `null`, which would make every "did not unmount" assertion vacuous.
|
|
105
|
+
const FONTS: Fonts = { Inter: { regular: "https://cdn.test/inter.ttf" } };
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* A sandbox client (no disk cache, so every serve is a network call and the
|
|
109
|
+
* `getSteps` spy is the whole story). Answers with an onboarding named after
|
|
110
|
+
* the `plan` param it was asked for, so the assertions can tell WHICH audience
|
|
111
|
+
* a payload was resolved against.
|
|
112
|
+
*/
|
|
113
|
+
const makeClient = () => {
|
|
114
|
+
const getSteps = vi.fn(async (_opts: unknown, params: Record<string, string>) => ({
|
|
115
|
+
data: onboarding(`for-${params.plan ?? "nobody"}`, FONTS),
|
|
116
|
+
headers: {
|
|
117
|
+
"ONBS-Onboarding-Id": "real",
|
|
118
|
+
"ONBS-Onboarding-Name": "real",
|
|
119
|
+
"ONBS-Audience-Id": "real",
|
|
120
|
+
},
|
|
121
|
+
}));
|
|
122
|
+
return { projectId: "p1", options: { isSandbox: true }, getSteps } as any;
|
|
123
|
+
};
|
|
124
|
+
|
|
125
|
+
// Counts mounts/unmounts of the subtree the gate guards. A `null` render by the
|
|
126
|
+
// gate — the defect under test — shows up here as an unmount.
|
|
127
|
+
const probe = { mounts: 0, unmounts: 0 };
|
|
128
|
+
const Probe = ({ label }: { label: string }) => {
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
probe.mounts += 1;
|
|
131
|
+
return () => {
|
|
132
|
+
probe.unmounts += 1;
|
|
133
|
+
};
|
|
134
|
+
}, []);
|
|
135
|
+
return <div data-testid="probe">{label}</div>;
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
let root: Root | null = null;
|
|
139
|
+
let container: HTMLDivElement | null = null;
|
|
140
|
+
|
|
141
|
+
const flush = () => new Promise<void>((r) => setTimeout(r, 0));
|
|
142
|
+
/** Lets pending promises, effects and the store's coalesced write all land. */
|
|
143
|
+
const settle = async () => {
|
|
144
|
+
await act(async () => {
|
|
145
|
+
await flush();
|
|
146
|
+
await flush();
|
|
147
|
+
});
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const render = async (element: React.ReactElement) => {
|
|
151
|
+
container = document.createElement("div");
|
|
152
|
+
document.body.appendChild(container);
|
|
153
|
+
root = createRoot(container);
|
|
154
|
+
await act(async () => {
|
|
155
|
+
root!.render(element);
|
|
156
|
+
});
|
|
157
|
+
await settle();
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
const rerender = async (element: React.ReactElement) => {
|
|
161
|
+
await act(async () => {
|
|
162
|
+
root!.render(element);
|
|
163
|
+
});
|
|
164
|
+
await settle();
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
const unmount = async () => {
|
|
168
|
+
if (root) await act(async () => root!.unmount());
|
|
169
|
+
container?.remove();
|
|
170
|
+
root = null;
|
|
171
|
+
container = null;
|
|
172
|
+
};
|
|
173
|
+
|
|
174
|
+
const text = () => container?.textContent ?? "";
|
|
175
|
+
|
|
176
|
+
beforeEach(() => {
|
|
177
|
+
expect(storage.heldKey).toBe(USER_PROPERTIES_STORAGE_KEY);
|
|
178
|
+
storage.map.clear();
|
|
179
|
+
storage.releaseHydration();
|
|
180
|
+
// `vi.restoreAllMocks` only restores `spyOn` spies; these `vi.fn`s would
|
|
181
|
+
// otherwise carry call history from one test into the next.
|
|
182
|
+
storage.getItem.mockClear();
|
|
183
|
+
storage.setItem.mockClear();
|
|
184
|
+
storage.removeItem.mockClear();
|
|
185
|
+
storage.getAllKeys.mockClear();
|
|
186
|
+
storage.multiRemove.mockClear();
|
|
187
|
+
probe.mounts = 0;
|
|
188
|
+
probe.unmounts = 0;
|
|
189
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
190
|
+
vi.spyOn(console, "info").mockImplementation(() => {});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
afterEach(async () => {
|
|
194
|
+
await unmount();
|
|
195
|
+
vi.restoreAllMocks();
|
|
196
|
+
});
|
|
197
|
+
|
|
198
|
+
describe("OnboardingDataGate — audience params are pinned at serve time", () => {
|
|
199
|
+
it("a user-property write after the first serve does not re-key, refetch, or unmount the subtree", async () => {
|
|
200
|
+
const { OnboardingProvider, OnboardingStudio } = await launch();
|
|
201
|
+
const client = makeClient();
|
|
202
|
+
|
|
203
|
+
await render(
|
|
204
|
+
<OnboardingProvider client={client} fontsFallback={<div>fallback</div>}>
|
|
205
|
+
<Probe label="served" />
|
|
206
|
+
</OnboardingProvider>
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
expect(client.getSteps).toHaveBeenCalledTimes(1);
|
|
210
|
+
expect(text()).toBe("served");
|
|
211
|
+
expect(probe.mounts).toBe(1);
|
|
212
|
+
|
|
213
|
+
// The write that used to blank the whole app: the merged params changed, the
|
|
214
|
+
// query key followed them, react-query answered `undefined` for the unseen
|
|
215
|
+
// key, and the gate rendered `null`.
|
|
216
|
+
await act(async () => {
|
|
217
|
+
OnboardingStudio.setUserProperty("plan", "pro");
|
|
218
|
+
});
|
|
219
|
+
await settle();
|
|
220
|
+
await act(async () => {
|
|
221
|
+
OnboardingStudio.setUserProperties({ demandScore: 87 });
|
|
222
|
+
});
|
|
223
|
+
await settle();
|
|
224
|
+
|
|
225
|
+
expect(client.getSteps).toHaveBeenCalledTimes(1);
|
|
226
|
+
expect(text()).toBe("served");
|
|
227
|
+
expect(probe.unmounts).toBe(0);
|
|
228
|
+
expect(probe.mounts).toBe(1);
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
it("pins once under StrictMode's double render: one fetch, with the merged params", async () => {
|
|
232
|
+
const { OnboardingProvider, OnboardingStudio } = await launch();
|
|
233
|
+
OnboardingStudio.setUserProperty("plan", "pro");
|
|
234
|
+
const client = makeClient();
|
|
235
|
+
|
|
236
|
+
await render(
|
|
237
|
+
<StrictMode>
|
|
238
|
+
<OnboardingProvider client={client} customAudienceParams={{ channel: "a" }}>
|
|
239
|
+
<Probe label="served" />
|
|
240
|
+
</OnboardingProvider>
|
|
241
|
+
</StrictMode>
|
|
242
|
+
);
|
|
243
|
+
|
|
244
|
+
expect(client.getSteps).toHaveBeenCalledTimes(1);
|
|
245
|
+
expect(client.getSteps.mock.calls[0][1]).toEqual({ channel: "a", plan: "pro" });
|
|
246
|
+
expect(text()).toBe("served");
|
|
247
|
+
});
|
|
248
|
+
|
|
249
|
+
it("a `customAudienceParams` prop change after the first serve is frozen too", async () => {
|
|
250
|
+
const { OnboardingProvider } = await launch();
|
|
251
|
+
const client = makeClient();
|
|
252
|
+
const tree = (params: Record<string, string>) => (
|
|
253
|
+
<OnboardingProvider client={client} customAudienceParams={params}>
|
|
254
|
+
<Probe label="served" />
|
|
255
|
+
</OnboardingProvider>
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
await render(tree({ channel: "a" }));
|
|
259
|
+
expect(client.getSteps).toHaveBeenCalledTimes(1);
|
|
260
|
+
expect(client.getSteps.mock.calls[0][1]).toEqual({ channel: "a" });
|
|
261
|
+
|
|
262
|
+
await rerender(tree({ channel: "b" }));
|
|
263
|
+
|
|
264
|
+
expect(client.getSteps).toHaveBeenCalledTimes(1);
|
|
265
|
+
expect(text()).toBe("served");
|
|
266
|
+
expect(probe.unmounts).toBe(0);
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
it("a write BEFORE the first serve (during hydration) is in the first fetch's params", async () => {
|
|
270
|
+
storage.holdHydration();
|
|
271
|
+
const { OnboardingProvider, OnboardingStudio } = await launch();
|
|
272
|
+
const client = makeClient();
|
|
273
|
+
|
|
274
|
+
await render(
|
|
275
|
+
<OnboardingProvider client={client} customAudienceParams={{ plan: "prop", channel: "a" }}>
|
|
276
|
+
<Probe label="served" />
|
|
277
|
+
</OnboardingProvider>
|
|
278
|
+
);
|
|
279
|
+
|
|
280
|
+
// Still hydrating: nothing fetched, nothing pinned.
|
|
281
|
+
expect(client.getSteps).not.toHaveBeenCalled();
|
|
282
|
+
|
|
283
|
+
await act(async () => {
|
|
284
|
+
OnboardingStudio.setUserProperty("plan", "pro");
|
|
285
|
+
});
|
|
286
|
+
await act(async () => {
|
|
287
|
+
storage.releaseHydration();
|
|
288
|
+
});
|
|
289
|
+
await settle();
|
|
290
|
+
|
|
291
|
+
expect(client.getSteps).toHaveBeenCalledTimes(1);
|
|
292
|
+
// Store wins over the prop, per key; the pin was taken from the READY
|
|
293
|
+
// snapshot, not from the empty pre-hydration map.
|
|
294
|
+
expect(client.getSteps.mock.calls[0][1]).toEqual({ plan: "pro", channel: "a" });
|
|
295
|
+
expect(text()).toBe("served");
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
it("the next serve (a fresh launch) picks up properties written during the previous one", async () => {
|
|
299
|
+
// Launch 1: serve, then write a property mid-flow.
|
|
300
|
+
const first = await launch();
|
|
301
|
+
const client1 = makeClient();
|
|
302
|
+
await render(
|
|
303
|
+
<first.OnboardingProvider client={client1}>
|
|
304
|
+
<Probe label="served" />
|
|
305
|
+
</first.OnboardingProvider>
|
|
306
|
+
);
|
|
307
|
+
expect(client1.getSteps.mock.calls[0][1]).toEqual({});
|
|
308
|
+
|
|
309
|
+
await act(async () => {
|
|
310
|
+
first.OnboardingStudio.setUserProperty("plan", "pro");
|
|
311
|
+
});
|
|
312
|
+
await settle(); // the store's coalesced write reaches "disk"
|
|
313
|
+
expect(client1.getSteps).toHaveBeenCalledTimes(1);
|
|
314
|
+
await unmount();
|
|
315
|
+
|
|
316
|
+
// Launch 2: a fresh module graph hydrates from the same storage.
|
|
317
|
+
const second = await launch();
|
|
318
|
+
const client2 = makeClient();
|
|
319
|
+
await render(
|
|
320
|
+
<second.OnboardingProvider client={client2}>
|
|
321
|
+
<Probe label="served again" />
|
|
322
|
+
</second.OnboardingProvider>
|
|
323
|
+
);
|
|
324
|
+
|
|
325
|
+
expect(client2.getSteps).toHaveBeenCalledTimes(1);
|
|
326
|
+
expect(client2.getSteps.mock.calls[0][1]).toEqual({ plan: "pro" });
|
|
327
|
+
expect(text()).toBe("served again");
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
it("a remount of the provider is a new serve and re-resolves with the current properties", async () => {
|
|
331
|
+
const { OnboardingProvider, OnboardingStudio } = await launch();
|
|
332
|
+
const client = makeClient();
|
|
333
|
+
const tree = (key: number) => (
|
|
334
|
+
<OnboardingProvider key={key} client={client}>
|
|
335
|
+
<Probe label={`serve ${key}`} />
|
|
336
|
+
</OnboardingProvider>
|
|
337
|
+
);
|
|
338
|
+
|
|
339
|
+
await render(tree(1));
|
|
340
|
+
await act(async () => {
|
|
341
|
+
OnboardingStudio.setUserProperty("plan", "pro");
|
|
342
|
+
});
|
|
343
|
+
await settle();
|
|
344
|
+
expect(client.getSteps).toHaveBeenCalledTimes(1);
|
|
345
|
+
|
|
346
|
+
await rerender(tree(2));
|
|
347
|
+
|
|
348
|
+
expect(client.getSteps).toHaveBeenCalledTimes(2);
|
|
349
|
+
expect(client.getSteps.mock.calls[1][1]).toEqual({ plan: "pro" });
|
|
350
|
+
expect(text()).toBe("serve 2");
|
|
351
|
+
// The price of re-targeting: a remount IS the full teardown the pin exists
|
|
352
|
+
// to prevent mid-flow, so it belongs at a flow boundary, never mid-onboarding.
|
|
353
|
+
expect(probe.unmounts).toBe(1);
|
|
354
|
+
expect(probe.mounts).toBe(2);
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
it("the forced-refetch escape hatch still works, under the pinned audience, without unmounting", async () => {
|
|
358
|
+
const { OnboardingProvider, OnboardingStudio } = await launch();
|
|
359
|
+
const client = makeClient();
|
|
360
|
+
|
|
361
|
+
let invalidate: (() => Promise<void>) | null = null;
|
|
362
|
+
const Escape = () => {
|
|
363
|
+
const queryClient = useQueryClient();
|
|
364
|
+
invalidate = () => queryClient.invalidateQueries({ queryKey: ["onboardingQuestions"] });
|
|
365
|
+
return null;
|
|
366
|
+
};
|
|
367
|
+
|
|
368
|
+
await render(
|
|
369
|
+
<OnboardingProvider client={client} customAudienceParams={{ plan: "prop" }}>
|
|
370
|
+
<Escape />
|
|
371
|
+
<Probe label="served" />
|
|
372
|
+
</OnboardingProvider>
|
|
373
|
+
);
|
|
374
|
+
expect(client.getSteps).toHaveBeenCalledTimes(1);
|
|
375
|
+
|
|
376
|
+
// A property written mid-flow does not re-target the presentation, even
|
|
377
|
+
// when the host then forces a content refresh: the refetch re-serves the
|
|
378
|
+
// same pinned audience. Re-targeting is a new serve, i.e. a remount.
|
|
379
|
+
await act(async () => {
|
|
380
|
+
OnboardingStudio.setUserProperty("plan", "pro");
|
|
381
|
+
});
|
|
382
|
+
await settle();
|
|
383
|
+
await act(async () => {
|
|
384
|
+
await invalidate!();
|
|
385
|
+
});
|
|
386
|
+
await settle();
|
|
387
|
+
|
|
388
|
+
expect(client.getSteps).toHaveBeenCalledTimes(2);
|
|
389
|
+
expect(client.getSteps.mock.calls[1][1]).toEqual({ plan: "prop" });
|
|
390
|
+
expect(text()).toBe("served");
|
|
391
|
+
expect(probe.unmounts).toBe(0);
|
|
392
|
+
});
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
describe("the step hooks read the served audience params", () => {
|
|
396
|
+
it("shares the gate's query when the store is non-empty: one fetch, the store-targeted payload", async () => {
|
|
397
|
+
const { OnboardingProvider, OnboardingStudio, useOnboardingStep } = await launch();
|
|
398
|
+
OnboardingStudio.setUserProperty("plan", "pro");
|
|
399
|
+
const client = makeClient();
|
|
400
|
+
|
|
401
|
+
const Step = () => {
|
|
402
|
+
const { onboardingMetadata } = useOnboardingStep({ stepNumber: 1 });
|
|
403
|
+
return <div>{onboardingMetadata.id}</div>;
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
await render(
|
|
407
|
+
<OnboardingProvider client={client} customAudienceParams={{ channel: "a" }}>
|
|
408
|
+
<Suspense fallback={<div>suspended</div>}>
|
|
409
|
+
<Step />
|
|
410
|
+
</Suspense>
|
|
411
|
+
</OnboardingProvider>
|
|
412
|
+
);
|
|
413
|
+
|
|
414
|
+
// Before this change the hook built its own query from the RAW prop
|
|
415
|
+
// (`{ channel: "a" }`), so a non-empty store meant a second fetch under a
|
|
416
|
+
// second key — and the screens rendered the payload resolved WITHOUT the
|
|
417
|
+
// user's properties.
|
|
418
|
+
expect(client.getSteps).toHaveBeenCalledTimes(1);
|
|
419
|
+
expect(client.getSteps.mock.calls[0][1]).toEqual({ channel: "a", plan: "pro" });
|
|
420
|
+
expect(text()).toBe("for-pro");
|
|
421
|
+
});
|
|
422
|
+
|
|
423
|
+
it("useOnboardingStart shares it too", async () => {
|
|
424
|
+
const { OnboardingProvider, OnboardingStudio, useOnboardingStart } = await launch();
|
|
425
|
+
OnboardingStudio.setUserProperty("plan", "pro");
|
|
426
|
+
const client = makeClient();
|
|
427
|
+
|
|
428
|
+
const Start = () => {
|
|
429
|
+
const { startStepNumber } = useOnboardingStart();
|
|
430
|
+
return <div>start {startStepNumber}</div>;
|
|
431
|
+
};
|
|
432
|
+
|
|
433
|
+
await render(
|
|
434
|
+
<OnboardingProvider client={client}>
|
|
435
|
+
<Suspense fallback={<div>suspended</div>}>
|
|
436
|
+
<Start />
|
|
437
|
+
</Suspense>
|
|
438
|
+
</OnboardingProvider>
|
|
439
|
+
);
|
|
440
|
+
|
|
441
|
+
expect(client.getSteps).toHaveBeenCalledTimes(1);
|
|
442
|
+
expect(client.getSteps.mock.calls[0][1]).toEqual({ plan: "pro" });
|
|
443
|
+
expect(text()).toBe("start 1");
|
|
444
|
+
});
|
|
445
|
+
});
|
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
|
+
|
|
3
|
+
// `OnboardingStudioClient` reads `Platform.OS`; stub it so the class loads under
|
|
4
|
+
// Node — the same stub the other client-touching suites use.
|
|
5
|
+
vi.mock("react-native", () => ({ Platform: { OS: "ios" } }));
|
|
6
|
+
// `userProperties/store.ts` constructs the real singleton at module scope from
|
|
7
|
+
// AsyncStorage. The facade under test is built over an INJECTED store, so this
|
|
8
|
+
// mock only has to let the import resolve.
|
|
9
|
+
const { getItem, setItem, removeItem } = vi.hoisted(() => ({
|
|
10
|
+
getItem: vi.fn(async () => null),
|
|
11
|
+
setItem: vi.fn(async () => {}),
|
|
12
|
+
removeItem: vi.fn(async () => {}),
|
|
13
|
+
}));
|
|
14
|
+
vi.mock("@react-native-async-storage/async-storage", () => ({
|
|
15
|
+
default: { getItem, setItem, removeItem },
|
|
16
|
+
}));
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
createOnboardingStudio,
|
|
20
|
+
resolveProviderClient,
|
|
21
|
+
MISSING_CLIENT_MESSAGE,
|
|
22
|
+
} from "../OnboardingStudio";
|
|
23
|
+
import { createUserPropertyStore } from "../userProperties/store";
|
|
24
|
+
import { OnboardingStudioClient } from "../OnboardingStudioClient";
|
|
25
|
+
|
|
26
|
+
const makeStorage = () => {
|
|
27
|
+
const map = new Map<string, string>();
|
|
28
|
+
return {
|
|
29
|
+
getItem: vi.fn(async (k: string) => map.get(k) ?? null),
|
|
30
|
+
setItem: vi.fn(async (k: string, v: string) => void map.set(k, v)),
|
|
31
|
+
removeItem: vi.fn(async (k: string) => void map.delete(k)),
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
/** A fresh facade over a fresh store, so no test can leak into another. */
|
|
36
|
+
const makeStudio = () => {
|
|
37
|
+
const storage = makeStorage();
|
|
38
|
+
const store = createUserPropertyStore(storage);
|
|
39
|
+
return { studio: createOnboardingStudio(store), store, storage };
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
beforeEach(() => {
|
|
43
|
+
vi.restoreAllMocks();
|
|
44
|
+
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
describe("OnboardingStudio.init", () => {
|
|
48
|
+
it("is not initialized before init", () => {
|
|
49
|
+
const { studio } = makeStudio();
|
|
50
|
+
expect(studio.isInitialized()).toBe(false);
|
|
51
|
+
expect(studio.getClient()).toBeNull();
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("builds a client from the config and returns it", () => {
|
|
55
|
+
const { studio } = makeStudio();
|
|
56
|
+
const client = studio.init({ projectId: "abc", appVersion: "1.0.0" });
|
|
57
|
+
|
|
58
|
+
expect(client).toBeInstanceOf(OnboardingStudioClient);
|
|
59
|
+
expect(client.projectId).toBe("abc");
|
|
60
|
+
expect(client.options.appVersion).toBe("1.0.0");
|
|
61
|
+
expect(studio.isInitialized()).toBe(true);
|
|
62
|
+
expect(studio.getClient()).toBe(client);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it("does not leak `userProperties` into the client options", () => {
|
|
66
|
+
const { studio } = makeStudio();
|
|
67
|
+
const client = studio.init({ projectId: "abc", userProperties: { plan: "free" } });
|
|
68
|
+
expect("userProperties" in client.options).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("seeds initial user properties, so targeting is right on a first-ever launch", () => {
|
|
72
|
+
const { studio } = makeStudio();
|
|
73
|
+
studio.init({ projectId: "abc", userProperties: { plan: "free", days: 3 } });
|
|
74
|
+
expect(studio.getUserProperties()).toEqual({ plan: "free", days: 3 });
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it("is a silent no-op when called twice with the same config", () => {
|
|
78
|
+
const { studio } = makeStudio();
|
|
79
|
+
const first = studio.init({ projectId: "abc", appVersion: "1.0.0" });
|
|
80
|
+
const second = studio.init({ projectId: "abc", appVersion: "1.0.0" });
|
|
81
|
+
|
|
82
|
+
// Same client instance: Fast Refresh re-running module scope must not
|
|
83
|
+
// rebuild the client and orphan the one the providers already hold.
|
|
84
|
+
expect(second).toBe(first);
|
|
85
|
+
expect(console.warn).not.toHaveBeenCalled();
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it("replaces the client and warns when called again with a different config", () => {
|
|
89
|
+
const { studio } = makeStudio();
|
|
90
|
+
const first = studio.init({ projectId: "abc" });
|
|
91
|
+
const second = studio.init({ projectId: "different" });
|
|
92
|
+
|
|
93
|
+
expect(second).not.toBe(first);
|
|
94
|
+
expect(studio.getClient()).toBe(second);
|
|
95
|
+
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining("different"));
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("starts hydrating persisted properties without waiting for a provider", () => {
|
|
99
|
+
const { studio, storage } = makeStudio();
|
|
100
|
+
studio.init({ projectId: "abc" });
|
|
101
|
+
expect(storage.getItem).toHaveBeenCalled();
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("lets an init-time property win over a persisted one", async () => {
|
|
105
|
+
const storage = makeStorage();
|
|
106
|
+
await storage.setItem("rocapine-user-properties", JSON.stringify({ plan: "pro", days: 9 }));
|
|
107
|
+
const store = createUserPropertyStore(storage);
|
|
108
|
+
const studio = createOnboardingStudio(store);
|
|
109
|
+
|
|
110
|
+
studio.init({ projectId: "abc", userProperties: { plan: "free" } });
|
|
111
|
+
await store.ensureHydrated();
|
|
112
|
+
|
|
113
|
+
// The host's current knowledge beats last launch's; disk fills the gap.
|
|
114
|
+
expect(studio.getUserProperties()).toEqual({ plan: "free", days: 9 });
|
|
115
|
+
});
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
describe("OnboardingStudio user properties", () => {
|
|
119
|
+
it("sets a single property", () => {
|
|
120
|
+
const { studio } = makeStudio();
|
|
121
|
+
studio.setUserProperty("plan", "free");
|
|
122
|
+
expect(studio.getUserProperties()).toEqual({ plan: "free" });
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("deletes a property when the value is null", () => {
|
|
126
|
+
const { studio } = makeStudio();
|
|
127
|
+
studio.setUserProperty("plan", "free");
|
|
128
|
+
studio.setUserProperty("plan", null);
|
|
129
|
+
expect(studio.getUserProperties()).toEqual({});
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("merges a batch rather than replacing", () => {
|
|
133
|
+
const { studio } = makeStudio();
|
|
134
|
+
studio.setUserProperty("plan", "free");
|
|
135
|
+
studio.setUserProperties({ days: 3 });
|
|
136
|
+
expect(studio.getUserProperties()).toEqual({ plan: "free", days: 3 });
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
it("removes a property by name", () => {
|
|
140
|
+
const { studio } = makeStudio();
|
|
141
|
+
studio.setUserProperties({ plan: "free", days: 3 });
|
|
142
|
+
studio.removeUserProperty("plan");
|
|
143
|
+
expect(studio.getUserProperties()).toEqual({ days: 3 });
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it("refuses a reserved name and warns", () => {
|
|
147
|
+
const { studio } = makeStudio();
|
|
148
|
+
studio.setUserProperty("projectId", "sneaky");
|
|
149
|
+
expect(studio.getUserProperties()).toEqual({});
|
|
150
|
+
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining("projectId"));
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("works before init — properties are not gated on configuration", () => {
|
|
154
|
+
const { studio } = makeStudio();
|
|
155
|
+
studio.setUserProperty("plan", "free");
|
|
156
|
+
expect(studio.getUserProperties()).toEqual({ plan: "free" });
|
|
157
|
+
expect(studio.isInitialized()).toBe(false);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe("OnboardingStudio.reset", () => {
|
|
162
|
+
it("clears user properties", () => {
|
|
163
|
+
const { studio } = makeStudio();
|
|
164
|
+
studio.setUserProperties({ plan: "free", days: 3 });
|
|
165
|
+
studio.reset();
|
|
166
|
+
expect(studio.getUserProperties()).toEqual({});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("keeps the client — reset forgets the user, not the configuration", () => {
|
|
170
|
+
const { studio } = makeStudio();
|
|
171
|
+
const client = studio.init({ projectId: "abc" });
|
|
172
|
+
studio.reset();
|
|
173
|
+
expect(studio.getClient()).toBe(client);
|
|
174
|
+
expect(studio.isInitialized()).toBe(true);
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
it("does not clear the payload cache", () => {
|
|
178
|
+
// Logging out should forget who you are, not force a refetch of content that
|
|
179
|
+
// has not changed. `getClient()?.clearCache()` is there if both are wanted.
|
|
180
|
+
const { studio } = makeStudio();
|
|
181
|
+
const client = studio.init({ projectId: "abc" });
|
|
182
|
+
const clearCache = vi.spyOn(client, "clearCache");
|
|
183
|
+
studio.reset();
|
|
184
|
+
expect(clearCache).not.toHaveBeenCalled();
|
|
185
|
+
});
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
describe("resolveProviderClient", () => {
|
|
189
|
+
const a = new OnboardingStudioClient("prop", {});
|
|
190
|
+
const b = new OnboardingStudioClient("studio", {});
|
|
191
|
+
|
|
192
|
+
it("prefers the prop, so an existing host is unaffected", () => {
|
|
193
|
+
expect(resolveProviderClient(a, b)).toBe(a);
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
it("falls back to the studio's client", () => {
|
|
197
|
+
expect(resolveProviderClient(undefined, b)).toBe(b);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it("is null when neither exists", () => {
|
|
201
|
+
expect(resolveProviderClient(undefined, null)).toBeNull();
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
it("names both fixes in the missing-client message", () => {
|
|
205
|
+
expect(MISSING_CLIENT_MESSAGE).toContain("OnboardingStudio.init");
|
|
206
|
+
expect(MISSING_CLIENT_MESSAGE).toContain("client");
|
|
207
|
+
});
|
|
208
|
+
});
|