@rocapine/react-native-onboarding 1.73.0 → 1.74.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 +136 -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 +11 -2
- package/dist/infra/provider/OnboardingProvider.d.ts.map +1 -1
- package/dist/infra/provider/OnboardingProvider.js +26 -2
- 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 +63 -0
- package/dist/userProperties/store.d.ts.map +1 -0
- package/dist/userProperties/store.js +153 -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 +1 -1
- package/src/OnboardingStudio.ts +147 -0
- package/src/OnboardingStudioClient.ts +27 -11
- 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 +45 -4
- 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 +177 -0
- package/src/userProperties/types.ts +22 -0
- package/src/userProperties/useUserProperties.ts +26 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rocapine/react-native-onboarding",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.74.0",
|
|
4
4
|
"description": "Headless React Native SDK for Rocapine Onboarding Studio - Data fetching, state management, and hooks",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
import { OnboardingStudioClient } from "./OnboardingStudioClient";
|
|
2
|
+
import type { OnboardingStudioClientOptions } from "./types";
|
|
3
|
+
import { userPropertyStore } from "./userProperties/store";
|
|
4
|
+
import type { UserPropertyStore } from "./userProperties/store";
|
|
5
|
+
import type {
|
|
6
|
+
UserProperties,
|
|
7
|
+
UserPropertyPatch,
|
|
8
|
+
UserPropertyValue,
|
|
9
|
+
} from "./userProperties/types";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* What `OnboardingStudio.init` takes: everything `new OnboardingStudioClient()`
|
|
13
|
+
* takes, plus the project id it takes positionally, plus optional initial user
|
|
14
|
+
* properties.
|
|
15
|
+
*
|
|
16
|
+
* `userProperties` here is a genuine convenience rather than sugar. Audience
|
|
17
|
+
* targeting is evaluated against the property map, and persisted properties are
|
|
18
|
+
* only hydrated from disk — so on a first-EVER install there is nothing to
|
|
19
|
+
* hydrate and the first catalog fetch matches the catch-all audience. Seeding at
|
|
20
|
+
* `init`, which runs before any provider mounts, is the only way to be targeted
|
|
21
|
+
* correctly on that first launch.
|
|
22
|
+
*/
|
|
23
|
+
export type OnboardingStudioConfig = { projectId: string } & OnboardingStudioClientOptions & {
|
|
24
|
+
userProperties?: UserPropertyPatch;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type OnboardingStudioFacade = {
|
|
28
|
+
/**
|
|
29
|
+
* Configure the SDK. Returns the client it built, so a host that needs
|
|
30
|
+
* `clearCache()` (or wants to pass it explicitly) can keep hold of it.
|
|
31
|
+
*
|
|
32
|
+
* Idempotent for an unchanged config — Fast Refresh re-runs module scope, and
|
|
33
|
+
* rebuilding the client there would orphan the one the providers already hold.
|
|
34
|
+
* A genuinely CHANGED config replaces the client and warns, because silently
|
|
35
|
+
* ignoring it would be worse than either alternative.
|
|
36
|
+
*/
|
|
37
|
+
init: (config: OnboardingStudioConfig) => OnboardingStudioClient;
|
|
38
|
+
/** The configured client, or `null` before `init`. */
|
|
39
|
+
getClient: () => OnboardingStudioClient | null;
|
|
40
|
+
isInitialized: () => boolean;
|
|
41
|
+
/** Set one property. `null` deletes it. */
|
|
42
|
+
setUserProperty: (key: string, value: UserPropertyValue | null) => void;
|
|
43
|
+
/** Set several at once. Merges; a `null` value deletes that key. */
|
|
44
|
+
setUserProperties: (patch: UserPropertyPatch) => void;
|
|
45
|
+
removeUserProperty: (key: string) => void;
|
|
46
|
+
getUserProperties: () => UserProperties;
|
|
47
|
+
/**
|
|
48
|
+
* Forget the user: clears every user property, in memory and on disk. Call it
|
|
49
|
+
* on logout.
|
|
50
|
+
*
|
|
51
|
+
* Deliberately does NOT clear the payload cache or the configuration. Logging
|
|
52
|
+
* out should forget who someone is, not force a refetch of content that has
|
|
53
|
+
* not changed — `getClient()?.clearCache()` is right there if both are wanted.
|
|
54
|
+
*/
|
|
55
|
+
reset: () => void;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
/** Stable signature of a config, for the idempotency check in `init`. */
|
|
59
|
+
const configSignature = (projectId: string, options: Record<string, unknown>): string =>
|
|
60
|
+
`${projectId}::${JSON.stringify(options)}`;
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* The SDK's front door, in the shape the SDKs it sits alongside use
|
|
64
|
+
* (`Superwall.configure` / `Purchases.configure` / `amplitude.init`): one
|
|
65
|
+
* module-level object that owns configuration and user identity.
|
|
66
|
+
*
|
|
67
|
+
* A factory with the store injected, so its tests need a fake store rather than
|
|
68
|
+
* a module mock — the same reason `createUserPropertyStore` is a factory.
|
|
69
|
+
*
|
|
70
|
+
* `register`/`present` deliberately do NOT live here. Presenting needs the
|
|
71
|
+
* mounted provider's catalog and presentation state, so they stay on
|
|
72
|
+
* `usePaywall()`, where that state is reachable and where a call cannot be made
|
|
73
|
+
* before a provider exists.
|
|
74
|
+
*/
|
|
75
|
+
export const createOnboardingStudio = (store: UserPropertyStore): OnboardingStudioFacade => {
|
|
76
|
+
let client: OnboardingStudioClient | null = null;
|
|
77
|
+
let signature: string | null = null;
|
|
78
|
+
|
|
79
|
+
return {
|
|
80
|
+
init: (config) => {
|
|
81
|
+
const { projectId, userProperties: initialProperties, ...options } = config;
|
|
82
|
+
const next = configSignature(projectId, options);
|
|
83
|
+
|
|
84
|
+
if (client && signature === next) {
|
|
85
|
+
// Same config: hand back the same client. Seeding the same properties
|
|
86
|
+
// again would be a no-op anyway (`applyUserPropertyPatch` returns the
|
|
87
|
+
// same object when nothing changed), so there is nothing else to do.
|
|
88
|
+
return client;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (client) {
|
|
92
|
+
console.warn(
|
|
93
|
+
`[onboarding-studio] init() was called again with a DIFFERENT config ` +
|
|
94
|
+
`(projectId "${projectId}"). The previous client has been replaced. ` +
|
|
95
|
+
"Providers already holding the old client keep using it until they " +
|
|
96
|
+
"re-render — call init() once, at module scope."
|
|
97
|
+
);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
client = new OnboardingStudioClient(projectId, options);
|
|
101
|
+
signature = next;
|
|
102
|
+
|
|
103
|
+
// Before hydration on purpose: the store merges disk UNDER memory, so a
|
|
104
|
+
// property the host states here beats a stale one from a previous launch.
|
|
105
|
+
if (initialProperties) store.set(initialProperties);
|
|
106
|
+
// Start reading the persisted map now rather than at the first provider
|
|
107
|
+
// mount, so it is ready before anything queries.
|
|
108
|
+
void store.ensureHydrated();
|
|
109
|
+
|
|
110
|
+
return client;
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
getClient: () => client,
|
|
114
|
+
isInitialized: () => client !== null,
|
|
115
|
+
|
|
116
|
+
setUserProperty: (key, value) => store.set({ [key]: value }),
|
|
117
|
+
setUserProperties: (patch) => store.set(patch),
|
|
118
|
+
removeUserProperty: (key) => store.remove(key),
|
|
119
|
+
getUserProperties: () => store.get(),
|
|
120
|
+
reset: () => store.reset(),
|
|
121
|
+
};
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
/** The facade every host uses. */
|
|
125
|
+
export const OnboardingStudio = createOnboardingStudio(userPropertyStore);
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Which client a provider should use: an explicit `client` prop wins, otherwise
|
|
129
|
+
* the one `OnboardingStudio.init()` built.
|
|
130
|
+
*
|
|
131
|
+
* The prop keeps winning so every host that already passes one is untouched by
|
|
132
|
+
* `init` existing.
|
|
133
|
+
*/
|
|
134
|
+
export const resolveProviderClient = (
|
|
135
|
+
prop: OnboardingStudioClient | undefined,
|
|
136
|
+
fromStudio: OnboardingStudioClient | null
|
|
137
|
+
): OnboardingStudioClient | null => prop ?? fromStudio;
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Shared by both providers, because a host hitting this has exactly two fixes
|
|
141
|
+
* and should be told both rather than left to guess which one this provider
|
|
142
|
+
* wanted.
|
|
143
|
+
*/
|
|
144
|
+
export const MISSING_CLIENT_MESSAGE =
|
|
145
|
+
"[onboarding-studio] No client. Either call OnboardingStudio.init({ projectId }) " +
|
|
146
|
+
"once at module scope, or pass a `client` prop built with " +
|
|
147
|
+
"`new OnboardingStudioClient(projectId, options)`.";
|
|
@@ -9,8 +9,8 @@ import {
|
|
|
9
9
|
BaseStepType,
|
|
10
10
|
} from "./types";
|
|
11
11
|
import {
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
ONBOARDING_CACHE_KEY_PREFIX,
|
|
13
|
+
PAYWALLS_CACHE_KEY_PREFIX,
|
|
14
14
|
} from "./infra/queries/cacheKey";
|
|
15
15
|
import {
|
|
16
16
|
PaywallCatalog,
|
|
@@ -35,18 +35,34 @@ export class OnboardingStudioClient {
|
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
38
|
-
* Removes
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
* invalidate the React Query keys `["onboardingQuestions", ...]` and
|
|
38
|
+
* Removes EVERY cached onboarding payload and paywall catalog this SDK wrote,
|
|
39
|
+
* across all audience-param variants. The next query mount or app launch is
|
|
40
|
+
* then a cache miss and refetches. To force an in-session refetch as well,
|
|
41
|
+
* also invalidate the React Query keys `["onboardingQuestions", ...]` and
|
|
43
42
|
* `["paywallCatalog", ...]`.
|
|
43
|
+
*
|
|
44
|
+
* Scans `getAllKeys()` by prefix rather than naming two keys, because the keys
|
|
45
|
+
* are now scoped by a hash of the resolved audience params (see
|
|
46
|
+
* `infra/queries/cacheKey.ts`) — so there is no longer one key per domain to
|
|
47
|
+
* name. This also picks up entries written before that scoping existed, which
|
|
48
|
+
* would otherwise be orphaned forever.
|
|
49
|
+
*
|
|
50
|
+
* It does NOT clear user properties: `USER_PROPERTIES_STORAGE_KEY` matches
|
|
51
|
+
* neither prefix, deliberately. Clearing a payload cache must not forget who
|
|
52
|
+
* the user is — that is what `OnboardingStudio.reset()` is for.
|
|
44
53
|
*/
|
|
45
54
|
async clearCache(): Promise<void> {
|
|
46
|
-
|
|
47
|
-
AsyncStorage.
|
|
48
|
-
|
|
49
|
-
|
|
55
|
+
try {
|
|
56
|
+
const keys = await AsyncStorage.getAllKeys();
|
|
57
|
+
const ours = keys.filter(
|
|
58
|
+
(key) =>
|
|
59
|
+
key.startsWith(ONBOARDING_CACHE_KEY_PREFIX) ||
|
|
60
|
+
key.startsWith(PAYWALLS_CACHE_KEY_PREFIX),
|
|
61
|
+
);
|
|
62
|
+
if (ours.length > 0) await AsyncStorage.multiRemove(ours);
|
|
63
|
+
} catch (error) {
|
|
64
|
+
console.warn("Failed to clear SDK caches:", error);
|
|
65
|
+
}
|
|
50
66
|
}
|
|
51
67
|
|
|
52
68
|
async getSteps<StepType extends BaseStepType = OnboardingStepType>(
|
|
@@ -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
|
+
});
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
getOnboardingCacheKey,
|
|
4
|
+
getPaywallsCacheKey,
|
|
5
|
+
DEFAULT_ONBOARDING_CACHE_KEY,
|
|
6
|
+
DEFAULT_PAYWALLS_CACHE_KEY,
|
|
7
|
+
ONBOARDING_CACHE_KEY_PREFIX,
|
|
8
|
+
PAYWALLS_CACHE_KEY_PREFIX,
|
|
9
|
+
} from "../infra/queries/cacheKey";
|
|
10
|
+
import { paramsHash } from "../userProperties/serialize";
|
|
11
|
+
import { USER_PROPERTIES_STORAGE_KEY } from "../userProperties/store";
|
|
12
|
+
|
|
13
|
+
describe("cache keys", () => {
|
|
14
|
+
it("is byte-identical to the legacy key when there are no params", () => {
|
|
15
|
+
expect(getOnboardingCacheKey()).toBe(DEFAULT_ONBOARDING_CACHE_KEY);
|
|
16
|
+
expect(getOnboardingCacheKey(undefined, "")).toBe(DEFAULT_ONBOARDING_CACHE_KEY);
|
|
17
|
+
expect(getPaywallsCacheKey()).toBe(DEFAULT_PAYWALLS_CACHE_KEY);
|
|
18
|
+
expect(getPaywallsCacheKey(undefined, "")).toBe(DEFAULT_PAYWALLS_CACHE_KEY);
|
|
19
|
+
expect(getPaywallsCacheKey("v2")).toBe("rocapine-paywalls-sdk-v2");
|
|
20
|
+
expect(getOnboardingCacheKey("v2")).toBe("rocapine-onboarding-sdk-v2");
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it("appends the hash when params are present", () => {
|
|
24
|
+
const hash = paramsHash({ plan: "free" });
|
|
25
|
+
expect(getPaywallsCacheKey(undefined, hash)).toBe(`${DEFAULT_PAYWALLS_CACHE_KEY}-${hash}`);
|
|
26
|
+
expect(getPaywallsCacheKey("v2", hash)).toBe(`rocapine-paywalls-sdk-v2-${hash}`);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("gives different params different keys", () => {
|
|
30
|
+
const a = getPaywallsCacheKey(undefined, paramsHash({ plan: "free" }));
|
|
31
|
+
const b = getPaywallsCacheKey(undefined, paramsHash({ plan: "pro" }));
|
|
32
|
+
expect(a).not.toBe(b);
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("gives the same params the same key regardless of insertion order", () => {
|
|
36
|
+
const a = getPaywallsCacheKey(undefined, paramsHash({ plan: "free", days: "3" }));
|
|
37
|
+
const b = getPaywallsCacheKey(undefined, paramsHash({ days: "3", plan: "free" }));
|
|
38
|
+
expect(a).toBe(b);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("keeps the two domains on separate prefixes", () => {
|
|
42
|
+
const hash = paramsHash({ plan: "free" });
|
|
43
|
+
expect(getOnboardingCacheKey(undefined, hash).startsWith(ONBOARDING_CACHE_KEY_PREFIX)).toBe(true);
|
|
44
|
+
expect(getPaywallsCacheKey(undefined, hash).startsWith(PAYWALLS_CACHE_KEY_PREFIX)).toBe(true);
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("every key a helper can produce starts with its prefix, so clearCache finds it", () => {
|
|
48
|
+
const hash = paramsHash({ plan: "free" });
|
|
49
|
+
for (const key of [
|
|
50
|
+
getPaywallsCacheKey(),
|
|
51
|
+
getPaywallsCacheKey("v2"),
|
|
52
|
+
getPaywallsCacheKey(undefined, hash),
|
|
53
|
+
getPaywallsCacheKey("v2", hash),
|
|
54
|
+
]) {
|
|
55
|
+
expect(key.startsWith(PAYWALLS_CACHE_KEY_PREFIX)).toBe(true);
|
|
56
|
+
}
|
|
57
|
+
for (const key of [
|
|
58
|
+
getOnboardingCacheKey(),
|
|
59
|
+
getOnboardingCacheKey("v2"),
|
|
60
|
+
getOnboardingCacheKey(undefined, hash),
|
|
61
|
+
getOnboardingCacheKey("v2", hash),
|
|
62
|
+
]) {
|
|
63
|
+
expect(key.startsWith(ONBOARDING_CACHE_KEY_PREFIX)).toBe(true);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it("does not match the user-property key, which clearCache must never wipe", () => {
|
|
68
|
+
expect(USER_PROPERTIES_STORAGE_KEY.startsWith(ONBOARDING_CACHE_KEY_PREFIX)).toBe(false);
|
|
69
|
+
expect(USER_PROPERTIES_STORAGE_KEY.startsWith(PAYWALLS_CACHE_KEY_PREFIX)).toBe(false);
|
|
70
|
+
});
|
|
71
|
+
});
|
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
2
2
|
|
|
3
3
|
// AsyncStorage is mocked — the query reads/writes the production cache through it.
|
|
4
|
-
const { getItem, setItem, removeItem } = vi.hoisted(() => ({
|
|
4
|
+
const { getItem, setItem, removeItem, getAllKeys, multiRemove } = vi.hoisted(() => ({
|
|
5
5
|
getItem: vi.fn<(key: string) => Promise<string | null>>(),
|
|
6
6
|
setItem: vi.fn<(key: string, value: string) => Promise<void>>(),
|
|
7
7
|
removeItem: vi.fn<(key: string) => Promise<void>>(),
|
|
8
|
+
// `clearCache` scans by prefix now, because the keys are scoped by a hash of
|
|
9
|
+
// the resolved audience params and are no longer predictable one-by-one.
|
|
10
|
+
getAllKeys: vi.fn<() => Promise<string[]>>(),
|
|
11
|
+
multiRemove: vi.fn<(keys: string[]) => Promise<void>>(),
|
|
8
12
|
}));
|
|
9
13
|
vi.mock("@react-native-async-storage/async-storage", () => ({
|
|
10
|
-
default: { getItem, setItem, removeItem },
|
|
14
|
+
default: { getItem, setItem, removeItem, getAllKeys, multiRemove },
|
|
11
15
|
}));
|
|
12
16
|
// OnboardingStudioClient (used by the clearCache test) imports `react-native`
|
|
13
17
|
// for `Platform`; stub it so the class loads under Node.
|
|
@@ -156,17 +160,52 @@ describe("getOnboardingCacheKey", () => {
|
|
|
156
160
|
});
|
|
157
161
|
|
|
158
162
|
describe("OnboardingStudioClient.clearCache", () => {
|
|
159
|
-
beforeEach(() =>
|
|
163
|
+
beforeEach(() => {
|
|
164
|
+
multiRemove.mockReset();
|
|
165
|
+
multiRemove.mockResolvedValue(undefined);
|
|
166
|
+
getAllKeys.mockReset();
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
it("clears every param variant of both domains, plus any legacy unscoped key", async () => {
|
|
170
|
+
getAllKeys.mockResolvedValue([
|
|
171
|
+
CACHE_KEY,
|
|
172
|
+
`${CACHE_KEY}-7f3a1c92`,
|
|
173
|
+
CUSTOM_CACHE_KEY,
|
|
174
|
+
"rocapine-paywalls-studio",
|
|
175
|
+
"rocapine-paywalls-sdk-v2-deadbeef",
|
|
176
|
+
"unrelated-app-key",
|
|
177
|
+
]);
|
|
178
|
+
const client = new OnboardingStudioClient("p1", {});
|
|
179
|
+
await client.clearCache();
|
|
180
|
+
expect(multiRemove).toHaveBeenCalledWith([
|
|
181
|
+
CACHE_KEY,
|
|
182
|
+
`${CACHE_KEY}-7f3a1c92`,
|
|
183
|
+
CUSTOM_CACHE_KEY,
|
|
184
|
+
"rocapine-paywalls-studio",
|
|
185
|
+
"rocapine-paywalls-sdk-v2-deadbeef",
|
|
186
|
+
]);
|
|
187
|
+
});
|
|
160
188
|
|
|
161
|
-
it("
|
|
189
|
+
it("leaves user properties alone — clearing a cache must not forget who the user is", async () => {
|
|
190
|
+
getAllKeys.mockResolvedValue(["rocapine-user-properties"]);
|
|
162
191
|
const client = new OnboardingStudioClient("p1", {});
|
|
163
192
|
await client.clearCache();
|
|
164
|
-
expect(
|
|
193
|
+
expect(multiRemove).not.toHaveBeenCalled();
|
|
165
194
|
});
|
|
166
195
|
|
|
167
|
-
it("
|
|
168
|
-
|
|
196
|
+
it("does nothing when there is nothing of ours to clear", async () => {
|
|
197
|
+
getAllKeys.mockResolvedValue(["unrelated-app-key"]);
|
|
198
|
+
const client = new OnboardingStudioClient("p1", {});
|
|
169
199
|
await client.clearCache();
|
|
170
|
-
expect(
|
|
200
|
+
expect(multiRemove).not.toHaveBeenCalled();
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it("does not throw when storage enumeration fails", async () => {
|
|
204
|
+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
205
|
+
getAllKeys.mockRejectedValue(new Error("storage unavailable"));
|
|
206
|
+
const client = new OnboardingStudioClient("p1", {});
|
|
207
|
+
await expect(client.clearCache()).resolves.toBeUndefined();
|
|
208
|
+
expect(warn).toHaveBeenCalled();
|
|
209
|
+
warn.mockRestore();
|
|
171
210
|
});
|
|
172
211
|
});
|
package/src/index.ts
CHANGED
|
@@ -47,6 +47,49 @@ export {
|
|
|
47
47
|
generateWheelPickerRangeItems,
|
|
48
48
|
resolveWheelPickerItems,
|
|
49
49
|
} from "./steps/ComposableScreen/types";
|
|
50
|
+
// The SDK's front door: configuration (`init`) and user identity
|
|
51
|
+
// (`setUserProperty`, `reset`), in the shape of the SDKs it sits alongside —
|
|
52
|
+
// `Superwall.configure`, `Purchases.configure`, `amplitude.init`.
|
|
53
|
+
//
|
|
54
|
+
// User properties feed audience resolution for BOTH onboardings and paywalls.
|
|
55
|
+
// This is a module-level object rather than a provider so non-React code — a
|
|
56
|
+
// login handler, an analytics service — can write to it; see
|
|
57
|
+
// `userProperties/store.ts` for why that one departure from the package's
|
|
58
|
+
// provider+context pattern is the point rather than an oversight.
|
|
59
|
+
//
|
|
60
|
+
// `register`/`present` are NOT here: presenting needs the mounted provider's
|
|
61
|
+
// state, so they stay on `usePaywall()`.
|
|
62
|
+
export { OnboardingStudio } from "./OnboardingStudio";
|
|
63
|
+
export type { OnboardingStudioConfig, OnboardingStudioFacade } from "./OnboardingStudio";
|
|
64
|
+
// `useUserProperties()` is the React READ path; the write path is the facade.
|
|
65
|
+
//
|
|
66
|
+
// The internal helpers (`toQueryParams`, `paramsHash`, `applyUserPropertyPatch`,
|
|
67
|
+
// `resolveEffectiveParams`, `userPropertyStore`) are deliberately NOT exported:
|
|
68
|
+
// they are implementation detail, and `paramsHash` in particular is a cache-key
|
|
69
|
+
// concern a host reproducing it would only drift from.
|
|
70
|
+
export {
|
|
71
|
+
useUserProperties,
|
|
72
|
+
createUserPropertyStore,
|
|
73
|
+
USER_PROPERTIES_STORAGE_KEY,
|
|
74
|
+
RESERVED_USER_PROPERTY_KEYS,
|
|
75
|
+
isReservedUserPropertyKey,
|
|
76
|
+
} from "./userProperties";
|
|
77
|
+
export type {
|
|
78
|
+
UserProperties,
|
|
79
|
+
UserPropertyPatch,
|
|
80
|
+
UserPropertyValue,
|
|
81
|
+
UserPropertySnapshot,
|
|
82
|
+
UserPropertyStorage,
|
|
83
|
+
UserPropertyStore,
|
|
84
|
+
} from "./userProperties";
|
|
85
|
+
// `register(moment, feature)` — gate a feature on a moment. The decision helpers
|
|
86
|
+
// are exported because they are pure: a host building its own gating on top of
|
|
87
|
+
// `catalog` can reuse the SDK's exact rules rather than reimplement them slightly
|
|
88
|
+
// differently. `runRegister` and `RegisterDeps` stay internal — that seam exists
|
|
89
|
+
// for its own test.
|
|
90
|
+
export { resolveRegisterDecision, shouldRunFeature } from "./paywalls/register";
|
|
91
|
+
export type { RegisterDecision, RegisterFeature, RegisterResult } from "./paywalls/register";
|
|
92
|
+
export { DEFAULT_REGISTER_TIMEOUT_MS } from "./paywalls/PaywallProvider";
|
|
50
93
|
// A step that IS a paywall: `payload.moment` names a moment, and the audience
|
|
51
94
|
// waterfall behind it picks which paywall renders.
|
|
52
95
|
export type { PaywallStepType } from "./steps/Paywall/types";
|