@rocapine/react-native-onboarding 1.60.0 → 1.62.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/dist/OnboardingStudioClient.d.ts +23 -5
- package/dist/OnboardingStudioClient.d.ts.map +1 -1
- package/dist/OnboardingStudioClient.js +82 -6
- package/dist/OnboardingStudioClient.js.map +1 -1
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +8 -1
- package/dist/index.js.map +1 -1
- package/dist/infra/provider/OnboardingProvider.d.ts +37 -1
- package/dist/infra/provider/OnboardingProvider.d.ts.map +1 -1
- package/dist/infra/provider/OnboardingProvider.js +76 -2
- package/dist/infra/provider/OnboardingProvider.js.map +1 -1
- package/dist/infra/queries/cacheKey.d.ts +22 -4
- package/dist/infra/queries/cacheKey.d.ts.map +1 -1
- package/dist/infra/queries/cacheKey.js +26 -5
- package/dist/infra/queries/cacheKey.js.map +1 -1
- package/dist/paywalls/PaywallProvider.d.ts +66 -0
- package/dist/paywalls/PaywallProvider.d.ts.map +1 -0
- package/dist/paywalls/PaywallProvider.js +227 -0
- package/dist/paywalls/PaywallProvider.js.map +1 -0
- package/dist/paywalls/getPaywalls.query.d.ts +37 -0
- package/dist/paywalls/getPaywalls.query.d.ts.map +1 -0
- package/dist/paywalls/getPaywalls.query.js +114 -0
- package/dist/paywalls/getPaywalls.query.js.map +1 -0
- package/dist/paywalls/present.d.ts +100 -0
- package/dist/paywalls/present.d.ts.map +1 -0
- package/dist/paywalls/present.js +113 -0
- package/dist/paywalls/present.js.map +1 -0
- package/dist/paywalls/types.d.ts +95 -0
- package/dist/paywalls/types.d.ts.map +1 -0
- package/dist/paywalls/types.js +10 -0
- package/dist/paywalls/types.js.map +1 -0
- package/dist/paywalls/usePaywall.d.ts +29 -0
- package/dist/paywalls/usePaywall.d.ts.map +1 -0
- package/dist/paywalls/usePaywall.js +17 -0
- package/dist/paywalls/usePaywall.js.map +1 -0
- package/dist/products/ProductRuntimeContext.d.ts +15 -0
- package/dist/products/ProductRuntimeContext.d.ts.map +1 -0
- package/dist/products/ProductRuntimeContext.js +19 -0
- package/dist/products/ProductRuntimeContext.js.map +1 -0
- package/dist/products/adapters/expoIap.d.ts +4 -0
- package/dist/products/adapters/expoIap.d.ts.map +1 -0
- package/dist/products/adapters/expoIap.js +95 -0
- package/dist/products/adapters/expoIap.js.map +1 -0
- package/dist/products/adapters/revenueCat.d.ts +7 -0
- package/dist/products/adapters/revenueCat.d.ts.map +1 -0
- package/dist/products/adapters/revenueCat.js +116 -0
- package/dist/products/adapters/revenueCat.js.map +1 -0
- package/dist/products/adapters/stub.d.ts +8 -0
- package/dist/products/adapters/stub.d.ts.map +1 -0
- package/dist/products/adapters/stub.js +21 -0
- package/dist/products/adapters/stub.js.map +1 -0
- package/dist/products/derive.d.ts +19 -0
- package/dist/products/derive.d.ts.map +1 -0
- package/dist/products/derive.js +79 -0
- package/dist/products/derive.js.map +1 -0
- package/dist/products/duration.d.ts +3 -0
- package/dist/products/duration.d.ts.map +1 -0
- package/dist/products/duration.js +25 -0
- package/dist/products/duration.js.map +1 -0
- package/dist/products/index.d.ts +10 -0
- package/dist/products/index.d.ts.map +1 -0
- package/dist/products/index.js +23 -0
- package/dist/products/index.js.map +1 -0
- package/dist/products/mergeProductRuntimes.d.ts +55 -0
- package/dist/products/mergeProductRuntimes.d.ts.map +1 -0
- package/dist/products/mergeProductRuntimes.js +79 -0
- package/dist/products/mergeProductRuntimes.js.map +1 -0
- package/dist/products/toVariables.d.ts +18 -0
- package/dist/products/toVariables.d.ts.map +1 -0
- package/dist/products/toVariables.js +52 -0
- package/dist/products/toVariables.js.map +1 -0
- package/dist/products/types.d.ts +96 -0
- package/dist/products/types.d.ts.map +1 -0
- package/dist/products/types.js +3 -0
- package/dist/products/types.js.map +1 -0
- package/dist/products/useProducts.d.ts +12 -0
- package/dist/products/useProducts.d.ts.map +1 -0
- package/dist/products/useProducts.js +90 -0
- package/dist/products/useProducts.js.map +1 -0
- package/dist/screens/elements/AnimatedTextElement.d.ts +1 -24
- package/dist/screens/elements/AnimatedTextElement.d.ts.map +1 -1
- package/dist/screens/elements/BaseBoxProps.d.ts +1 -24
- package/dist/screens/elements/BaseBoxProps.d.ts.map +1 -1
- package/dist/screens/elements/ButtonElement.d.ts +6 -121
- package/dist/screens/elements/ButtonElement.d.ts.map +1 -1
- package/dist/screens/elements/CarouselElement.d.ts +1 -24
- package/dist/screens/elements/CarouselElement.d.ts.map +1 -1
- package/dist/screens/elements/CheckboxGroupElement.d.ts +1 -24
- package/dist/screens/elements/CheckboxGroupElement.d.ts.map +1 -1
- package/dist/screens/elements/DatePickerElement.d.ts +1 -24
- package/dist/screens/elements/DatePickerElement.d.ts.map +1 -1
- package/dist/screens/elements/DrawingPadElement.d.ts +1 -24
- package/dist/screens/elements/DrawingPadElement.d.ts.map +1 -1
- package/dist/screens/elements/IconElement.d.ts +1 -24
- package/dist/screens/elements/IconElement.d.ts.map +1 -1
- package/dist/screens/elements/ImageElement.d.ts +1 -24
- package/dist/screens/elements/ImageElement.d.ts.map +1 -1
- package/dist/screens/elements/InputElement.d.ts +1 -24
- package/dist/screens/elements/InputElement.d.ts.map +1 -1
- package/dist/screens/elements/KeyboardAvoidingViewElement.d.ts +1 -24
- package/dist/screens/elements/KeyboardAvoidingViewElement.d.ts.map +1 -1
- package/dist/screens/elements/LottieElement.d.ts +1 -24
- package/dist/screens/elements/LottieElement.d.ts.map +1 -1
- package/dist/screens/elements/ProgressIndicatorElement.d.ts +1 -24
- package/dist/screens/elements/ProgressIndicatorElement.d.ts.map +1 -1
- package/dist/screens/elements/ProgressiveBlurImageElement.d.ts +1 -24
- package/dist/screens/elements/ProgressiveBlurImageElement.d.ts.map +1 -1
- package/dist/screens/elements/RadioGroupElement.d.ts +1 -24
- package/dist/screens/elements/RadioGroupElement.d.ts.map +1 -1
- package/dist/screens/elements/RichTextElement.d.ts +1 -24
- package/dist/screens/elements/RichTextElement.d.ts.map +1 -1
- package/dist/screens/elements/RiveElement.d.ts +1 -24
- package/dist/screens/elements/RiveElement.d.ts.map +1 -1
- package/dist/screens/elements/SafeAreaViewElement.d.ts +1 -24
- package/dist/screens/elements/SafeAreaViewElement.d.ts.map +1 -1
- package/dist/screens/elements/ScrollViewElement.d.ts +1 -24
- package/dist/screens/elements/ScrollViewElement.d.ts.map +1 -1
- package/dist/screens/elements/SliderElement.d.ts +1 -24
- package/dist/screens/elements/SliderElement.d.ts.map +1 -1
- package/dist/screens/elements/StackElement.d.ts +1 -24
- package/dist/screens/elements/StackElement.d.ts.map +1 -1
- package/dist/screens/elements/TextElement.d.ts +1 -24
- package/dist/screens/elements/TextElement.d.ts.map +1 -1
- package/dist/screens/elements/TypewriterTextElement.d.ts +1 -24
- package/dist/screens/elements/TypewriterTextElement.d.ts.map +1 -1
- package/dist/screens/elements/VideoElement.d.ts +1 -24
- package/dist/screens/elements/VideoElement.d.ts.map +1 -1
- package/dist/screens/elements/WheelPickerElement.d.ts +1 -24
- package/dist/screens/elements/WheelPickerElement.d.ts.map +1 -1
- package/dist/screens/elements/ZStackElement.d.ts +1 -24
- package/dist/screens/elements/ZStackElement.d.ts.map +1 -1
- package/dist/steps/Carousel/types.d.ts +1 -1
- package/dist/steps/Commitment/types.d.ts +1 -1
- package/dist/steps/ComposableScreen/types.d.ts +1 -1
- package/dist/steps/Loader/types.d.ts +1 -1
- package/dist/steps/MediaContent/types.d.ts +1 -1
- package/dist/steps/Picker/types.d.ts +1 -1
- package/dist/steps/Question/types.d.ts +1 -1
- package/dist/steps/Ratings/types.d.ts +1 -1
- package/dist/steps/common.types.d.ts +44 -30
- package/dist/steps/common.types.d.ts.map +1 -1
- package/dist/steps/common.types.js +27 -3
- package/dist/steps/common.types.js.map +1 -1
- package/package.json +1 -1
- package/src/OnboardingStudioClient.ts +110 -7
- package/src/__tests__/mergeProductRuntimes.test.ts +172 -0
- package/src/__tests__/productAdapters.test.ts +58 -0
- package/src/__tests__/productDerive.test.ts +111 -0
- package/src/__tests__/productVariables.test.ts +109 -0
- package/src/__tests__/resolveLocalProductArgs.test.ts +41 -0
- package/src/index.ts +14 -0
- package/src/infra/provider/OnboardingProvider.tsx +101 -2
- package/src/infra/queries/cacheKey.ts +27 -4
- package/src/paywalls/PaywallProvider.tsx +342 -0
- package/src/paywalls/__tests__/getPaywalls.test.ts +334 -0
- package/src/paywalls/__tests__/present.test.ts +190 -0
- package/src/paywalls/getPaywalls.query.ts +123 -0
- package/src/paywalls/present.ts +141 -0
- package/src/paywalls/types.ts +99 -0
- package/src/paywalls/usePaywall.ts +35 -0
- package/src/products/ProductRuntimeContext.tsx +17 -0
- package/src/products/adapters/expoIap.ts +93 -0
- package/src/products/adapters/revenueCat.ts +108 -0
- package/src/products/adapters/stub.ts +20 -0
- package/src/products/derive.ts +93 -0
- package/src/products/duration.ts +21 -0
- package/src/products/index.ts +20 -0
- package/src/products/mergeProductRuntimes.ts +78 -0
- package/src/products/toVariables.ts +54 -0
- package/src/products/types.ts +88 -0
- package/src/products/useProducts.ts +106 -0
- package/src/steps/common.types.ts +83 -6
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
import type { ProductRef, ResolvedProduct } from "../products/types";
|
|
3
|
+
|
|
4
|
+
// Neither react-native-purchases nor expo-iap is a dependency of this repo —
|
|
5
|
+
// their absence IS the condition under test (not simulated). Mirrors the
|
|
6
|
+
// `react-native` mock getOnboarding.query.test.ts already uses for the same
|
|
7
|
+
// import, so `Platform` resolves without pulling in real react-native.
|
|
8
|
+
vi.mock("react-native", () => ({ Platform: { OS: "ios" } }));
|
|
9
|
+
|
|
10
|
+
const REF: ProductRef = { key: "yearly", ios: "com.app.yearly" };
|
|
11
|
+
|
|
12
|
+
const FAKE_PRODUCT: ResolvedProduct = {
|
|
13
|
+
key: "yearly",
|
|
14
|
+
productId: "com.app.yearly",
|
|
15
|
+
store: "app_store",
|
|
16
|
+
title: "Yearly",
|
|
17
|
+
description: "",
|
|
18
|
+
price: "$59.99",
|
|
19
|
+
priceAmount: 59.99,
|
|
20
|
+
currencyCode: "USD",
|
|
21
|
+
period: "year",
|
|
22
|
+
periodCount: 1,
|
|
23
|
+
periodIso: "P1Y",
|
|
24
|
+
};
|
|
25
|
+
|
|
26
|
+
describe("revenueCatProductProvider — degrades cleanly when react-native-purchases is absent", () => {
|
|
27
|
+
it("importing the module and calling the factory do not throw", async () => {
|
|
28
|
+
const { revenueCatProductProvider } = await import("../products/adapters/revenueCat");
|
|
29
|
+
expect(() => revenueCatProductProvider()).not.toThrow();
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it("defers the missing-dependency error to call time, and names the missing package", async () => {
|
|
33
|
+
const { revenueCatProductProvider } = await import("../products/adapters/revenueCat");
|
|
34
|
+
const provider = revenueCatProductProvider();
|
|
35
|
+
await expect(provider.getProducts([REF])).rejects.toThrow(
|
|
36
|
+
/react-native-purchases is not installed/
|
|
37
|
+
);
|
|
38
|
+
await expect(provider.purchase(FAKE_PRODUCT)).rejects.toThrow(
|
|
39
|
+
/react-native-purchases is not installed/
|
|
40
|
+
);
|
|
41
|
+
await expect(provider.restore()).rejects.toThrow(/react-native-purchases is not installed/);
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("expoIapProductProvider — degrades cleanly when expo-iap is absent", () => {
|
|
46
|
+
it("importing the module and calling the factory do not throw", async () => {
|
|
47
|
+
const { expoIapProductProvider } = await import("../products/adapters/expoIap");
|
|
48
|
+
expect(() => expoIapProductProvider()).not.toThrow();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
it("defers the missing-dependency error to call time, and names the missing package", async () => {
|
|
52
|
+
const { expoIapProductProvider } = await import("../products/adapters/expoIap");
|
|
53
|
+
const provider = expoIapProductProvider();
|
|
54
|
+
await expect(provider.getProducts([REF])).rejects.toThrow(/expo-iap is not installed/);
|
|
55
|
+
await expect(provider.purchase(FAKE_PRODUCT)).rejects.toThrow(/expo-iap is not installed/);
|
|
56
|
+
await expect(provider.restore()).rejects.toThrow(/expo-iap is not installed/);
|
|
57
|
+
});
|
|
58
|
+
});
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { parseIsoDuration } from "../products/duration";
|
|
3
|
+
import { formatCurrency, deriveProductFields, deriveAll } from "../products/derive";
|
|
4
|
+
import type { ResolvedProduct, ProductRef } from "../products/types";
|
|
5
|
+
|
|
6
|
+
const make = (over: Partial<ResolvedProduct> = {}): ResolvedProduct => ({
|
|
7
|
+
key: "yearly",
|
|
8
|
+
productId: "com.app.yearly",
|
|
9
|
+
store: "app_store",
|
|
10
|
+
title: "Yearly",
|
|
11
|
+
description: "",
|
|
12
|
+
price: "$59.99",
|
|
13
|
+
priceAmount: 59.99,
|
|
14
|
+
currencyCode: "USD",
|
|
15
|
+
period: "year",
|
|
16
|
+
periodCount: 1,
|
|
17
|
+
periodIso: "P1Y",
|
|
18
|
+
...over,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
describe("parseIsoDuration", () => {
|
|
22
|
+
it("parses the store-standard periods to days", () => {
|
|
23
|
+
expect(parseIsoDuration("P1W")).toBe(7);
|
|
24
|
+
expect(parseIsoDuration("P1M")).toBe(30);
|
|
25
|
+
expect(parseIsoDuration("P3M")).toBe(90);
|
|
26
|
+
expect(parseIsoDuration("P6M")).toBe(180);
|
|
27
|
+
expect(parseIsoDuration("P1Y")).toBe(365);
|
|
28
|
+
expect(parseIsoDuration("P7D")).toBe(7);
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
it("returns null for junk rather than guessing", () => {
|
|
32
|
+
expect(parseIsoDuration("")).toBeNull();
|
|
33
|
+
expect(parseIsoDuration("1Y")).toBeNull();
|
|
34
|
+
expect(parseIsoDuration("PT1H")).toBeNull();
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
describe("formatCurrency", () => {
|
|
39
|
+
it("formats in the product's currency, not the device's", () => {
|
|
40
|
+
expect(formatCurrency(1.15, "USD", "en-US")).toBe("$1.15");
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it("falls back to a plain amount+code string when Intl rejects the currency code", () => {
|
|
44
|
+
// "XYZ" is syntactically valid (3 letters) and Intl accepts it even though it's
|
|
45
|
+
// not a real currency, so it never exercises the catch branch. "ZZZZZZ" is the
|
|
46
|
+
// wrong shape and Intl genuinely throws on it.
|
|
47
|
+
expect(formatCurrency(5, "ZZZZZZ", "en-US")).toBe("5.00 ZZZZZZ");
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
describe("deriveProductFields", () => {
|
|
52
|
+
it("computes per-period prices from the ISO period", () => {
|
|
53
|
+
const d = deriveProductFields(make(), { locale: "en-US" });
|
|
54
|
+
// 59.99 / (365/7) weeks
|
|
55
|
+
expect(d.pricePerWeekAmount).toBeCloseTo(1.1504, 3);
|
|
56
|
+
expect(d.pricePerWeek).toBe("$1.15");
|
|
57
|
+
expect(d.pricePerMonthAmount).toBeCloseTo(4.9307, 3);
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("leaves savingsPct undefined without a compareTo", () => {
|
|
61
|
+
expect(deriveProductFields(make(), { locale: "en-US" }).savingsPct).toBeUndefined();
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("computes savingsPct against a normalized per-day comparison", () => {
|
|
65
|
+
const yearly = make(); // 59.99 / 365d
|
|
66
|
+
const monthly = make({ key: "monthly", priceAmount: 9.99, periodIso: "P1M" }); // 9.99 / 30d
|
|
67
|
+
const d = deriveProductFields(yearly, { compareTo: monthly, locale: "en-US" });
|
|
68
|
+
// yearly/day = 0.16436, monthly/day = 0.333 → ~51% cheaper
|
|
69
|
+
expect(d.savingsPct).toBe(51);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it("omits savingsPct when the comparison is not cheaper", () => {
|
|
73
|
+
const a = make({ priceAmount: 400 });
|
|
74
|
+
const b = make({ key: "monthly", priceAmount: 9.99, periodIso: "P1M" });
|
|
75
|
+
expect(deriveProductFields(a, { compareTo: b }).savingsPct).toBeUndefined();
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("derives trialDays from the trial period", () => {
|
|
79
|
+
const p = make({ trial: { period: "week", periodCount: 1, days: 0 } });
|
|
80
|
+
expect(deriveProductFields(p).trialDays).toBe(7);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it("returns no per-period prices when the period is unparseable", () => {
|
|
84
|
+
const d = deriveProductFields(make({ periodIso: null, period: null }));
|
|
85
|
+
expect(d.pricePerWeek).toBeUndefined();
|
|
86
|
+
expect(d.pricePerWeekAmount).toBeUndefined();
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe("deriveAll", () => {
|
|
91
|
+
const refs: ProductRef[] = [
|
|
92
|
+
{ key: "yearly", ios: "com.app.yearly", compareTo: "monthly" },
|
|
93
|
+
{ key: "monthly", ios: "com.app.monthly" },
|
|
94
|
+
];
|
|
95
|
+
|
|
96
|
+
it("keys results by ref key and resolves compareTo between them", () => {
|
|
97
|
+
const products = [
|
|
98
|
+
make(),
|
|
99
|
+
make({ key: "monthly", priceAmount: 9.99, periodIso: "P1M", period: "month" }),
|
|
100
|
+
];
|
|
101
|
+
const out = deriveAll(products, refs, "en-US");
|
|
102
|
+
expect(Object.keys(out).sort()).toEqual(["monthly", "yearly"]);
|
|
103
|
+
expect(out.yearly.savingsPct).toBe(51);
|
|
104
|
+
expect(out.monthly.savingsPct).toBeUndefined();
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("ignores a compareTo pointing at a missing key", () => {
|
|
108
|
+
const out = deriveAll([make()], [{ key: "yearly", compareTo: "nope" }], "en-US");
|
|
109
|
+
expect(out.yearly.savingsPct).toBeUndefined();
|
|
110
|
+
});
|
|
111
|
+
});
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import { describe, it, expect } from "vitest";
|
|
2
|
+
import { productVariables } from "../products/toVariables";
|
|
3
|
+
import type { ProductWithDerived } from "../products/types";
|
|
4
|
+
|
|
5
|
+
const yearly: ProductWithDerived = {
|
|
6
|
+
key: "yearly",
|
|
7
|
+
productId: "com.app.yearly",
|
|
8
|
+
store: "app_store",
|
|
9
|
+
title: "Yearly Plan",
|
|
10
|
+
description: "A year",
|
|
11
|
+
price: "$59.99",
|
|
12
|
+
priceAmount: 59.99,
|
|
13
|
+
currencyCode: "USD",
|
|
14
|
+
period: "year",
|
|
15
|
+
periodCount: 1,
|
|
16
|
+
periodIso: "P1Y",
|
|
17
|
+
pricePerWeek: "$1.15",
|
|
18
|
+
pricePerWeekAmount: 1.1504,
|
|
19
|
+
savingsPct: 51,
|
|
20
|
+
trialDays: 7,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
// Every optional field populated, so the dotted-key test below catches both a
|
|
24
|
+
// typo in an emitted key and an accidentally-dropped field.
|
|
25
|
+
const complete: ProductWithDerived = {
|
|
26
|
+
...yearly,
|
|
27
|
+
key: "complete",
|
|
28
|
+
productId: "com.app.complete",
|
|
29
|
+
pricePerMonth: "$4.93",
|
|
30
|
+
pricePerMonthAmount: 4.9307,
|
|
31
|
+
pricePerYear: "$59.99",
|
|
32
|
+
pricePerYearAmount: 59.99,
|
|
33
|
+
introOffer: { price: "$0.99", priceAmount: 0.99, period: "month", periodCount: 1, cycles: 1 },
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
describe("productVariables", () => {
|
|
37
|
+
it("emits flat dotted keys the interpolator can resolve", () => {
|
|
38
|
+
const v = productVariables({ products: { yearly }, status: "ready", purchasing: false });
|
|
39
|
+
expect(v["product.yearly.price"].value).toBe("$59.99");
|
|
40
|
+
expect(v["product.yearly.pricePerWeek"].value).toBe("$1.15");
|
|
41
|
+
expect(v["product.yearly.savingsPct"].value).toBe("51");
|
|
42
|
+
expect(v["product.yearly.trialDays"].value).toBe("7");
|
|
43
|
+
expect(v["product.yearly.title"].value).toBe("Yearly Plan");
|
|
44
|
+
expect(v["product.yearly.period"].value).toBe("year");
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
it("stringifies every value — the variable bag is string-based", () => {
|
|
48
|
+
const v = productVariables({ products: { yearly }, status: "ready", purchasing: false });
|
|
49
|
+
for (const entry of Object.values(v)) {
|
|
50
|
+
expect(typeof entry.value).toBe("string");
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("emits the complete set of dotted keys for a fully-populated product", () => {
|
|
55
|
+
const v = productVariables({ products: { complete }, status: "ready", purchasing: false });
|
|
56
|
+
expect(Object.keys(v).sort()).toEqual(
|
|
57
|
+
[
|
|
58
|
+
"product.complete.productId",
|
|
59
|
+
"product.complete.title",
|
|
60
|
+
"product.complete.description",
|
|
61
|
+
"product.complete.price",
|
|
62
|
+
"product.complete.priceAmount",
|
|
63
|
+
"product.complete.currencyCode",
|
|
64
|
+
"product.complete.period",
|
|
65
|
+
"product.complete.periodCount",
|
|
66
|
+
"product.complete.pricePerWeek",
|
|
67
|
+
"product.complete.pricePerWeekAmount",
|
|
68
|
+
"product.complete.pricePerMonth",
|
|
69
|
+
"product.complete.pricePerMonthAmount",
|
|
70
|
+
"product.complete.pricePerYear",
|
|
71
|
+
"product.complete.pricePerYearAmount",
|
|
72
|
+
"product.complete.savingsPct",
|
|
73
|
+
"product.complete.trialDays",
|
|
74
|
+
"product.complete.introPrice",
|
|
75
|
+
"products.loaded",
|
|
76
|
+
"products.purchasing",
|
|
77
|
+
"products.error",
|
|
78
|
+
].sort()
|
|
79
|
+
);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it("omits absent optional fields rather than emitting empty strings", () => {
|
|
83
|
+
const bare = { ...yearly, savingsPct: undefined, trialDays: undefined };
|
|
84
|
+
const v = productVariables({ products: { yearly: bare }, status: "ready", purchasing: false });
|
|
85
|
+
expect(v["product.yearly.savingsPct"]).toBeUndefined();
|
|
86
|
+
expect(v["product.yearly.trialDays"]).toBeUndefined();
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("publishes status flags authors gate the CTA on", () => {
|
|
90
|
+
const ready = productVariables({ products: {}, status: "ready", purchasing: false });
|
|
91
|
+
expect(ready["products.loaded"].value).toBe("true");
|
|
92
|
+
expect(ready["products.purchasing"].value).toBe("false");
|
|
93
|
+
expect(ready["products.error"].value).toBe("");
|
|
94
|
+
|
|
95
|
+
const failed = productVariables({
|
|
96
|
+
products: {},
|
|
97
|
+
status: "error",
|
|
98
|
+
error: "network down",
|
|
99
|
+
purchasing: false,
|
|
100
|
+
});
|
|
101
|
+
expect(failed["products.loaded"].value).toBe("false");
|
|
102
|
+
expect(failed["products.error"].value).toBe("network down");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("reports loaded=false while still loading", () => {
|
|
106
|
+
const v = productVariables({ products: {}, status: "loading", purchasing: false });
|
|
107
|
+
expect(v["products.loaded"].value).toBe("false");
|
|
108
|
+
});
|
|
109
|
+
});
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { describe, it, expect, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
// OnboardingProvider.tsx transitively imports `react-native` (via
|
|
4
|
+
// OnboardingStudioClient/registry.ts) and AsyncStorage — mocked the same way
|
|
5
|
+
// getOnboarding.query.test.ts does, so the module loads under Node.
|
|
6
|
+
vi.mock("@react-native-async-storage/async-storage", () => ({
|
|
7
|
+
default: { getItem: vi.fn(), setItem: vi.fn(), removeItem: vi.fn() },
|
|
8
|
+
}));
|
|
9
|
+
vi.mock("react-native", () => ({ Platform: { OS: "ios" } }));
|
|
10
|
+
|
|
11
|
+
import { resolveLocalProductArgs } from "../infra/provider/OnboardingProvider";
|
|
12
|
+
import type { ProductProvider, ProductRef } from "../products/types";
|
|
13
|
+
|
|
14
|
+
const REFS: ProductRef[] = [{ key: "yearly", ios: "com.app.yearly" }];
|
|
15
|
+
const PROVIDER: ProductProvider = {
|
|
16
|
+
getProducts: async () => [],
|
|
17
|
+
purchase: async () => ({ status: "cancelled" }),
|
|
18
|
+
restore: async () => ({ status: "nothing_to_restore" }),
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
// Finding 5, 2026-08-17 final review: this function used to zero refs/provider
|
|
22
|
+
// out whenever a context runtime existed, on the assumption that the paywall
|
|
23
|
+
// catalog's product union is always a superset of the host's own `productRefs`
|
|
24
|
+
// — that silently broke a documented Phase 3 prop the moment a `PaywallProvider`
|
|
25
|
+
// was mounted above. It now passes the host's own args through UNCONDITIONALLY;
|
|
26
|
+
// `mergeProductRuntimes` (see its own test) does the unioning at the runtime
|
|
27
|
+
// level. This test guards the pass-through itself against a regression back to
|
|
28
|
+
// the old zeroing behavior.
|
|
29
|
+
describe("resolveLocalProductArgs", () => {
|
|
30
|
+
it("passes the host's own refs/provider through unchanged", () => {
|
|
31
|
+
const result = resolveLocalProductArgs(REFS, PROVIDER);
|
|
32
|
+
expect(result.refs).toBe(REFS);
|
|
33
|
+
expect(result.provider).toBe(PROVIDER);
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
it("stays undefined when the host declared no productRefs/productProvider props", () => {
|
|
37
|
+
const result = resolveLocalProductArgs(undefined, undefined);
|
|
38
|
+
expect(result.refs).toBeUndefined();
|
|
39
|
+
expect(result.provider).toBeUndefined();
|
|
40
|
+
});
|
|
41
|
+
});
|
package/src/index.ts
CHANGED
|
@@ -77,3 +77,17 @@ export {
|
|
|
77
77
|
isUnaryConditionOperator,
|
|
78
78
|
ONBOARDING_END_STEP_ID,
|
|
79
79
|
} from "./steps/common.types";
|
|
80
|
+
// Product runtime (paywall phase 3) — vendor-neutral store products
|
|
81
|
+
export * from "./products";
|
|
82
|
+
// Paywalls (paywall phase 5) — catalog types, provider, and the `usePaywall` hook
|
|
83
|
+
export type {
|
|
84
|
+
Paywall,
|
|
85
|
+
PaywallCatalog,
|
|
86
|
+
PaywallOptions,
|
|
87
|
+
GetPaywallsResponseHeaders,
|
|
88
|
+
PresentResult,
|
|
89
|
+
} from "./paywalls/types";
|
|
90
|
+
export { PaywallProvider, usePaywallHost } from "./paywalls/PaywallProvider";
|
|
91
|
+
export type { PaywallContextValue } from "./paywalls/PaywallProvider";
|
|
92
|
+
export { usePaywall } from "./paywalls/usePaywall";
|
|
93
|
+
export type { UsePaywallResult } from "./paywalls/usePaywall";
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createContext, useCallback, useEffect, useRef, useState } from "react";
|
|
1
|
+
import { createContext, useCallback, useEffect, useMemo, useRef, useState } from "react";
|
|
2
2
|
import { QueryClient, QueryClientProvider, useQuery } from "@tanstack/react-query";
|
|
3
3
|
import { OnboardingStudioClient } from "../../OnboardingStudioClient";
|
|
4
4
|
import { getOnboardingQuery } from "../queries/getOnboarding.query";
|
|
@@ -10,6 +10,10 @@ import { extractAssetUrls } from "../preload/extractAssetUrls";
|
|
|
10
10
|
import { preloadAssets } from "../preload/preloadAssets";
|
|
11
11
|
import { OnboardingNavigationAdapter } from "../navigation/types";
|
|
12
12
|
import { expoRouterAdapter } from "../navigation/expoRouterAdapter";
|
|
13
|
+
import { useProducts } from "../../products/useProducts";
|
|
14
|
+
import { ProductProvider, ProductRef, ProductRuntime } from "../../products/types";
|
|
15
|
+
import { useProductRuntime } from "../../products/ProductRuntimeContext";
|
|
16
|
+
import { mergeProductRuntimes } from "../../products/mergeProductRuntimes";
|
|
13
17
|
|
|
14
18
|
const queryClient = new QueryClient({
|
|
15
19
|
defaultOptions: {
|
|
@@ -19,6 +23,52 @@ const queryClient = new QueryClient({
|
|
|
19
23
|
},
|
|
20
24
|
})
|
|
21
25
|
|
|
26
|
+
// Module-scope so the context default is referentially stable — an inline
|
|
27
|
+
// object literal here would allocate a fresh value on every read of the
|
|
28
|
+
// default, defeating the same stability contract useProducts upholds.
|
|
29
|
+
const EMPTY_PRODUCT_RUNTIME: ProductRuntime = {
|
|
30
|
+
products: {},
|
|
31
|
+
status: "idle",
|
|
32
|
+
purchasing: false,
|
|
33
|
+
purchase: async () => ({ status: "error", error: new Error("No ProductProvider") }),
|
|
34
|
+
restore: async () => ({ status: "error", error: new Error("No ProductProvider") }),
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Args for the local `useProducts` call.
|
|
39
|
+
*
|
|
40
|
+
* This function used to zero refs/provider out entirely whenever a
|
|
41
|
+
* `PaywallProvider` (or other ancestor) already published a `ProductRuntime`
|
|
42
|
+
* via `ProductRuntimeContext` — on the assumption that the paywall catalog's
|
|
43
|
+
* product union is always a superset of whatever `productRefs` this
|
|
44
|
+
* `OnboardingProvider` declares. Finding 5 (2026-08-17 final review) found
|
|
45
|
+
* that assumption wrong: it is a claim about a host-supplied prop, not a
|
|
46
|
+
* property of the code, and it silently stopped resolving any `productRefs`
|
|
47
|
+
* key absent from the catalog union the moment a host added a
|
|
48
|
+
* `PaywallProvider` above an existing `OnboardingProvider` — no error,
|
|
49
|
+
* nothing that type-checks, just a variable that renders empty.
|
|
50
|
+
*
|
|
51
|
+
* `productRefs`/`productProvider` are therefore passed through UNCHANGED
|
|
52
|
+
* regardless of whether a context runtime exists — `mergeProductRuntimes`
|
|
53
|
+
* (in `OnboardingProvider` below) unions the resulting local runtime back
|
|
54
|
+
* into the context one. Exported so the pass-through itself is covered by an
|
|
55
|
+
* importable, real test, not only inspection: this is the exact seam Finding
|
|
56
|
+
* 5's silent regression went through.
|
|
57
|
+
*/
|
|
58
|
+
export const resolveLocalProductArgs = (
|
|
59
|
+
productRefs: ProductRef[] | undefined,
|
|
60
|
+
productProvider: ProductProvider | undefined
|
|
61
|
+
): { refs: ProductRef[] | undefined; provider: ProductProvider | undefined } => ({
|
|
62
|
+
refs: productRefs,
|
|
63
|
+
provider: productProvider,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
// Frozen at module scope, not `= {}` inline: a default PARAMETER re-allocates on
|
|
67
|
+
// every render, and `customActions` is a RenderContext dependency, so a host that
|
|
68
|
+
// omits the prop would get a fresh `ctx` on every variable write and re-render the
|
|
69
|
+
// whole tree. Same reason EMPTY_PRODUCT_RUNTIME above is hoisted.
|
|
70
|
+
const EMPTY_CUSTOM_ACTIONS: CustomActions = Object.freeze({});
|
|
71
|
+
|
|
22
72
|
export type CustomActionHandler = (args: {
|
|
23
73
|
variables: Record<string, ComposableVariableEntry | undefined>;
|
|
24
74
|
/**
|
|
@@ -81,6 +131,15 @@ interface OnboardingProviderProps {
|
|
|
81
131
|
* logic (mark onboarding done, navigate to paywall/home, etc.). Optional.
|
|
82
132
|
*/
|
|
83
133
|
onComplete?: OnboardingCompleteHandler;
|
|
134
|
+
/**
|
|
135
|
+
* Billing adapter. Omit for an app with no paywall. Must be a stable reference.
|
|
136
|
+
*/
|
|
137
|
+
productProvider?: ProductProvider;
|
|
138
|
+
/**
|
|
139
|
+
* Product slots to resolve at mount, e.g.
|
|
140
|
+
* `[{ key: "yearly", ios: "com.app.yr", android: "com.app.yr:p1y", compareTo: "monthly" }]`.
|
|
141
|
+
*/
|
|
142
|
+
productRefs?: ProductRef[];
|
|
84
143
|
}
|
|
85
144
|
|
|
86
145
|
interface OnboardingDataGateProps {
|
|
@@ -131,10 +190,12 @@ export const OnboardingProvider = ({
|
|
|
131
190
|
client,
|
|
132
191
|
locale = "en",
|
|
133
192
|
customAudienceParams = {},
|
|
134
|
-
customActions =
|
|
193
|
+
customActions = EMPTY_CUSTOM_ACTIONS,
|
|
135
194
|
fontsFallback,
|
|
136
195
|
navigation = expoRouterAdapter,
|
|
137
196
|
onComplete,
|
|
197
|
+
productProvider,
|
|
198
|
+
productRefs,
|
|
138
199
|
}: OnboardingProviderProps) => {
|
|
139
200
|
const [activeStep, setActiveStep] = useState({
|
|
140
201
|
number: 0,
|
|
@@ -177,6 +238,41 @@ export const OnboardingProvider = ({
|
|
|
177
238
|
});
|
|
178
239
|
}, [onComplete, onboarding]);
|
|
179
240
|
|
|
241
|
+
// A ProductRuntime may already be published above by a `PaywallProvider`
|
|
242
|
+
// (Phase 5 Task 6) sharing one product catalog with this provider. Hooks
|
|
243
|
+
// can't be called conditionally, so `useProducts` is always called, with
|
|
244
|
+
// this provider's OWN `productRefs`/`productProvider` regardless of whether
|
|
245
|
+
// a context runtime exists (`resolveLocalProductArgs`) — when a context
|
|
246
|
+
// runtime is present, `mergeProductRuntimes` unions the two into the one
|
|
247
|
+
// `ProductRuntime` published below (Finding 5, 2026-08-17 final review: the
|
|
248
|
+
// two runtimes used to never merge, so a context runtime silently made
|
|
249
|
+
// `productRefs` unresolvable). Do not "simplify" this into a conditional
|
|
250
|
+
// hook call.
|
|
251
|
+
const contextRuntime = useProductRuntime();
|
|
252
|
+
const { refs: localProductRefs, provider: localProductProvider } = resolveLocalProductArgs(
|
|
253
|
+
productRefs,
|
|
254
|
+
productProvider
|
|
255
|
+
);
|
|
256
|
+
const localProductRuntime = useProducts(localProductRefs, localProductProvider, locale);
|
|
257
|
+
const hasLocalRefs = (productRefs?.length ?? 0) > 0;
|
|
258
|
+
const hasLocalProvider = !!localProductProvider;
|
|
259
|
+
// Memoized — round 2 of the final review (N1) caught that calling
|
|
260
|
+
// `mergeProductRuntimes` inline in the render body allocates a fresh object
|
|
261
|
+
// (and a fresh `purchase` closure) on EVERY render whenever `hasLocalRefs`,
|
|
262
|
+
// breaking `ScreenHost.products`'s "referentially stable across variable
|
|
263
|
+
// writes" contract (`ScreenHost.ts`) the moment a host uses `productRefs`
|
|
264
|
+
// alongside a `PaywallProvider` ancestor — every memoized element would
|
|
265
|
+
// re-render on every keystroke, since `setVariables` always allocates a new
|
|
266
|
+
// object. Keyed on the two runtimes plus the two booleans that change the
|
|
267
|
+
// merge's shape, exactly the inputs the function reads.
|
|
268
|
+
const productRuntime = useMemo(
|
|
269
|
+
() =>
|
|
270
|
+
contextRuntime
|
|
271
|
+
? mergeProductRuntimes(contextRuntime, localProductRuntime, hasLocalRefs, hasLocalProvider)
|
|
272
|
+
: localProductRuntime,
|
|
273
|
+
[contextRuntime, localProductRuntime, hasLocalRefs, hasLocalProvider]
|
|
274
|
+
);
|
|
275
|
+
|
|
180
276
|
return (
|
|
181
277
|
<QueryClientProvider client={queryClient}>
|
|
182
278
|
<OnboardingProgressContext.Provider
|
|
@@ -198,6 +294,7 @@ export const OnboardingProvider = ({
|
|
|
198
294
|
completeOnboarding,
|
|
199
295
|
customActions,
|
|
200
296
|
navigation,
|
|
297
|
+
products: productRuntime,
|
|
201
298
|
}}
|
|
202
299
|
>
|
|
203
300
|
<OnboardingDataGate
|
|
@@ -232,6 +329,7 @@ export const OnboardingProgressContext = createContext<{
|
|
|
232
329
|
completeOnboarding: () => void;
|
|
233
330
|
customActions: CustomActions;
|
|
234
331
|
navigation: OnboardingNavigationAdapter;
|
|
332
|
+
products: ProductRuntime;
|
|
235
333
|
}>({
|
|
236
334
|
activeStep: { number: 0, displayProgressHeader: false },
|
|
237
335
|
setActiveStep: () => { },
|
|
@@ -250,4 +348,5 @@ export const OnboardingProgressContext = createContext<{
|
|
|
250
348
|
completeOnboarding: () => { },
|
|
251
349
|
customActions: {},
|
|
252
350
|
navigation: expoRouterAdapter,
|
|
351
|
+
products: EMPTY_PRODUCT_RUNTIME,
|
|
253
352
|
});
|
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* AsyncStorage key derivation for the cached onboarding
|
|
2
|
+
* AsyncStorage key derivation for the SDK's cached payloads (onboarding
|
|
3
|
+
* steps, paywall catalog).
|
|
3
4
|
*
|
|
4
|
-
* Lives in its own module so
|
|
5
|
-
* `OnboardingStudioClient` can use
|
|
6
|
-
* (the query
|
|
5
|
+
* Lives in its own module so `getOnboarding.query.ts`, `getPaywalls.query.ts`
|
|
6
|
+
* and `OnboardingStudioClient` can all use these as values without a circular
|
|
7
|
+
* import (the query modules import `OnboardingStudioClient` for typing) —
|
|
8
|
+
* this module itself has no imports.
|
|
7
9
|
*/
|
|
8
10
|
|
|
9
11
|
/** Default cache key used when the host does not provide a custom one. */
|
|
@@ -22,3 +24,24 @@ export const getOnboardingCacheKey = (customKey?: string): string =>
|
|
|
22
24
|
customKey
|
|
23
25
|
? `rocapine-onboarding-sdk-${customKey}`
|
|
24
26
|
: DEFAULT_ONBOARDING_CACHE_KEY;
|
|
27
|
+
|
|
28
|
+
/** Default cache key used when the host does not provide a custom one. */
|
|
29
|
+
export const DEFAULT_PAYWALLS_CACHE_KEY = "rocapine-paywalls-studio";
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Derives the AsyncStorage key for the cached paywall catalog.
|
|
33
|
+
*
|
|
34
|
+
* Namespaced separately from `getOnboardingCacheKey`
|
|
35
|
+
* (`rocapine-paywalls-*` vs `rocapine-onboarding-*`) so the two caches never
|
|
36
|
+
* collide, even though both derive from the same `client.options.cacheKey`.
|
|
37
|
+
*
|
|
38
|
+
* - No custom key → the shared default `"rocapine-paywalls-studio"`
|
|
39
|
+
* (stale-while-revalidate: served cache-first and healed in the background).
|
|
40
|
+
* - Custom key → `"rocapine-paywalls-sdk-{customKey}"`, which the SDK persists
|
|
41
|
+
* cache-first with **no** background revalidation, so the host owns when to
|
|
42
|
+
* refetch (see `OnboardingStudioClient.clearCache`).
|
|
43
|
+
*/
|
|
44
|
+
export const getPaywallsCacheKey = (customKey?: string): string =>
|
|
45
|
+
customKey
|
|
46
|
+
? `rocapine-paywalls-sdk-${customKey}`
|
|
47
|
+
: DEFAULT_PAYWALLS_CACHE_KEY;
|