@jolibox/implement 1.4.3 → 1.4.7

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.
@@ -1,9 +1,9 @@
1
1
  Invoking: npm run clean && npm run build:esm && tsc
2
2
 
3
- > @jolibox/implement@1.4.3 clean
3
+ > @jolibox/implement@1.4.7 clean
4
4
  > rimraf ./dist
5
5
 
6
6
 
7
- > @jolibox/implement@1.4.3 build:esm
7
+ > @jolibox/implement@1.4.7 build:esm
8
8
  > BUILD_VERSION=$(node -p "require('./package.json').version") node esbuild.config.js --format=esm
9
9
 
package/package.json CHANGED
@@ -1,17 +1,17 @@
1
1
  {
2
2
  "name": "@jolibox/implement",
3
3
  "description": "This project is Jolibox JS-SDk implement for Native && H5",
4
- "version": "1.4.3",
4
+ "version": "1.4.7",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
7
7
  "license": "MIT",
8
8
  "dependencies": {
9
- "@jolibox/common": "1.4.3",
10
- "@jolibox/types": "1.4.3",
11
- "@jolibox/native-bridge": "1.4.3",
12
- "@jolibox/ads": "1.4.3",
9
+ "@jolibox/common": "1.4.7",
10
+ "@jolibox/types": "1.4.7",
11
+ "@jolibox/native-bridge": "1.4.7",
12
+ "@jolibox/ads": "1.4.7",
13
13
  "localforage": "1.10.0",
14
- "@jolibox/ui": "1.4.3",
14
+ "@jolibox/ui": "1.4.7",
15
15
  "web-vitals": "4.2.4"
16
16
  },
17
17
  "devDependencies": {
@@ -0,0 +1,155 @@
1
+ import { applyNative } from '@jolibox/native-bridge';
2
+ import { ISubscriptionPlanData } from '@/native/subscription/type';
3
+
4
+ jest.mock('@jolibox/native-bridge', () => ({
5
+ applyNative: jest.fn(),
6
+ onNative: jest.fn(),
7
+ invokeNative: jest.fn()
8
+ }));
9
+
10
+ const mockApplyNative = applyNative as jest.Mock;
11
+
12
+ /**
13
+ * Replicate the core logic of getSpinSubscriptionPrice from payment.ts
14
+ * to test it in isolation without triggering the full module import chain.
15
+ */
16
+ async function getSpinSubscriptionPriceImpl(params: { appStoreProductId: string }) {
17
+ const { data } = await applyNative('requestSubscriptionPlansAsync', {
18
+ appStoreProductIds: [params.appStoreProductId],
19
+ type: 'SPIN'
20
+ });
21
+
22
+ if (!data) {
23
+ throw new Error('[JoliboxSDK]: get spin subscription price failed');
24
+ }
25
+
26
+ const plans = Object.values(data as Record<string, ISubscriptionPlanData>).map((plan) => ({
27
+ basePlanId: plan.basePlanId,
28
+ productId: plan.productId,
29
+ formattedPrice: plan.pricing.formattedPrice,
30
+ originalPrice: plan.pricing.originalPrice,
31
+ trialDay: plan.trial?.duration ?? 0
32
+ }));
33
+
34
+ return { plans };
35
+ }
36
+
37
+ describe('getSpinSubscriptionPrice', () => {
38
+ beforeEach(() => {
39
+ jest.clearAllMocks();
40
+ });
41
+
42
+ it('should call applyNative with type SPIN and return formatted plans', async () => {
43
+ mockApplyNative.mockResolvedValue({
44
+ data: {
45
+ 'spin-weekly': {
46
+ basePlanId: 'spin-weekly',
47
+ productId: 'jolibox_app_spin_sub',
48
+ pricing: {
49
+ formattedPrice: '$1.99',
50
+ originalPrice: '$1.99',
51
+ discountPercentage: null
52
+ },
53
+ trial: {
54
+ duration: 3,
55
+ description: ''
56
+ }
57
+ }
58
+ }
59
+ });
60
+
61
+ const result = await getSpinSubscriptionPriceImpl({
62
+ appStoreProductId: 'jolibox_app_spin_sub'
63
+ });
64
+
65
+ expect(mockApplyNative).toHaveBeenCalledWith('requestSubscriptionPlansAsync', {
66
+ appStoreProductIds: ['jolibox_app_spin_sub'],
67
+ type: 'SPIN'
68
+ });
69
+
70
+ expect(result).toEqual({
71
+ plans: [
72
+ {
73
+ basePlanId: 'spin-weekly',
74
+ productId: 'jolibox_app_spin_sub',
75
+ formattedPrice: '$1.99',
76
+ originalPrice: '$1.99',
77
+ trialDay: 3
78
+ }
79
+ ]
80
+ });
81
+ });
82
+
83
+ it('should handle multiple plans', async () => {
84
+ mockApplyNative.mockResolvedValue({
85
+ data: {
86
+ 'spin-weekly': {
87
+ basePlanId: 'spin-weekly',
88
+ productId: 'jolibox_app_spin_sub',
89
+ pricing: { formattedPrice: '$1.99', originalPrice: '$1.99' },
90
+ trial: { duration: 3 }
91
+ },
92
+ 'spin-monthly': {
93
+ basePlanId: 'spin-monthly',
94
+ productId: 'jolibox_app_spin_sub',
95
+ pricing: { formattedPrice: '$4.99', originalPrice: '$4.99' },
96
+ trial: { duration: 7 }
97
+ }
98
+ }
99
+ });
100
+
101
+ const result = await getSpinSubscriptionPriceImpl({
102
+ appStoreProductId: 'jolibox_app_spin_sub'
103
+ });
104
+
105
+ expect(result.plans).toHaveLength(2);
106
+ expect(result.plans).toEqual(
107
+ expect.arrayContaining([
108
+ expect.objectContaining({ basePlanId: 'spin-weekly', formattedPrice: '$1.99' }),
109
+ expect.objectContaining({ basePlanId: 'spin-monthly', formattedPrice: '$4.99' })
110
+ ])
111
+ );
112
+ });
113
+
114
+ it('should default trialDay to 0 when trial is undefined', async () => {
115
+ mockApplyNative.mockResolvedValue({
116
+ data: {
117
+ 'spin-weekly': {
118
+ basePlanId: 'spin-weekly',
119
+ productId: 'jolibox_app_spin_sub',
120
+ pricing: { formattedPrice: '$1.99', originalPrice: '$1.99' }
121
+ }
122
+ }
123
+ });
124
+
125
+ const result = await getSpinSubscriptionPriceImpl({
126
+ appStoreProductId: 'jolibox_app_spin_sub'
127
+ });
128
+
129
+ expect(result.plans[0].trialDay).toBe(0);
130
+ });
131
+
132
+ it('should throw error when native returns no data', async () => {
133
+ mockApplyNative.mockResolvedValue({ data: null });
134
+
135
+ await expect(getSpinSubscriptionPriceImpl({ appStoreProductId: 'jolibox_app_spin_sub' })).rejects.toThrow(
136
+ 'get spin subscription price failed'
137
+ );
138
+ });
139
+
140
+ it('should throw error when native returns undefined data', async () => {
141
+ mockApplyNative.mockResolvedValue({});
142
+
143
+ await expect(getSpinSubscriptionPriceImpl({ appStoreProductId: 'jolibox_app_spin_sub' })).rejects.toThrow(
144
+ 'get spin subscription price failed'
145
+ );
146
+ });
147
+
148
+ it('should throw error when native call rejects', async () => {
149
+ mockApplyNative.mockRejectedValue(new Error('Native bridge unavailable'));
150
+
151
+ await expect(getSpinSubscriptionPriceImpl({ appStoreProductId: 'jolibox_app_spin_sub' })).rejects.toThrow(
152
+ 'Native bridge unavailable'
153
+ );
154
+ });
155
+ });
@@ -3,8 +3,10 @@ import { createAPI, registerCanIUse, t } from './base';
3
3
  import { createJoliGemPaymentService } from '../payment/payment-service';
4
4
  import { createAPIError } from '@/common/report/errors';
5
5
  import { IPaymentChoice } from '@jolibox/ui/dist/bridge/coin';
6
- import { ISubscriptionTierData } from '@jolibox/types';
6
+ import { ISpinSubscriptionPriceData, ISubscriptionTierData } from '@jolibox/types';
7
7
  import { subscriptionService } from '@/native/subscription';
8
+ import { applyNative } from '@jolibox/native-bridge';
9
+ import { ISubscriptionPlanData } from '@/native/subscription/type';
8
10
 
9
11
  const commands = createCommands();
10
12
  const paymentService = createJoliGemPaymentService();
@@ -143,7 +145,43 @@ commands.registerCommand('PaymentSDK.getSubscriptionPlans', getSubscriptionPlans
143
145
  commands.registerCommand('PaymentSDK.subscribe', subscribe);
144
146
  commands.registerCommand('PaymentSDK.subscribeSpin', subscribeSpin);
145
147
 
148
+ const getSpinSubscriptionPrice = createAPI('getSpinSubscriptionPrice', {
149
+ paramsSchema: t.tuple(
150
+ t.object({
151
+ appStoreProductId: t.string()
152
+ })
153
+ ),
154
+ implement: async (params: {
155
+ appStoreProductId: string;
156
+ }): Promise<{ plans: ISpinSubscriptionPriceData[] }> => {
157
+ const { data } = await applyNative('requestSubscriptionPlansAsync', {
158
+ appStoreProductIds: [params.appStoreProductId],
159
+ type: 'SPIN'
160
+ });
161
+
162
+ if (!data) {
163
+ throw createAPIError({
164
+ code: -1,
165
+ msg: '[JoliboxSDK]: get spin subscription price failed'
166
+ });
167
+ }
168
+
169
+ const plans: ISpinSubscriptionPriceData[] = Object.values(
170
+ data as Record<string, ISubscriptionPlanData>
171
+ ).map((plan) => ({
172
+ basePlanId: plan.basePlanId,
173
+ productId: plan.productId,
174
+ formattedPrice: plan.pricing.formattedPrice,
175
+ originalPrice: plan.pricing.originalPrice,
176
+ trialDay: plan.trial?.duration ?? 0
177
+ }));
178
+
179
+ return { plans };
180
+ }
181
+ });
182
+
146
183
  commands.registerCommand('PaymentSDK.flushSubInfoCache', flushSubInfoCache);
184
+ commands.registerCommand('PaymentSDK.getSpinSubscriptionPrice', getSpinSubscriptionPrice);
147
185
 
148
186
  registerCanIUse('payment.purchaseGem', {
149
187
  version: '1.2.9'
@@ -168,3 +206,7 @@ registerCanIUse('payment.flushSubInfoCache', {
168
206
  registerCanIUse('payment.subscribeSpin', {
169
207
  version: '1.3.7'
170
208
  });
209
+
210
+ registerCanIUse('payment.getSpinSubscriptionPrice', {
211
+ version: '1.4.3'
212
+ });
@@ -175,12 +175,15 @@ const createRewardsAdsManager = createAPI('createRewardsAdsManager', {
175
175
  }
176
176
  });
177
177
  },
178
- showAd: async (callbacks: {
179
- onAdLoaded?: () => void;
180
- onAdError?: (error: Error) => void;
181
- onAdRewarded?: () => void;
182
- onAdDismissed?: () => void;
183
- }) => {
178
+ showAd: async (
179
+ callbacks: {
180
+ onAdLoaded?: () => void;
181
+ onAdError?: (error: Error) => void;
182
+ onAdRewarded?: () => void;
183
+ onAdDismissed?: () => void;
184
+ },
185
+ isSpinWheel = false
186
+ ) => {
184
187
  ads.adBreak({
185
188
  type: 'reward',
186
189
  beforeReward: (showAdFn) => {
@@ -204,9 +207,10 @@ const createRewardsAdsManager = createAPI('createRewardsAdsManager', {
204
207
  if (breakStatus === 'viewed') {
205
208
  // reward should be given to user
206
209
  console.log('-----> complete watch ads');
207
- taskTracker.reporter({
208
- event: 'COMPLETE_WATCH_ADS'
209
- });
210
+ !isSpinWheel &&
211
+ taskTracker.reporter({
212
+ event: 'COMPLETE_WATCH_ADS'
213
+ });
210
214
  callbacks.onAdRewarded?.();
211
215
  }
212
216