@explorins/pers-sdk-react-native 2.3.7 → 2.3.8
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/LICENSE +21 -0
- package/README.md +1266 -1244
- package/dist/__tests__/PersSDKProvider.test.js +33 -0
- package/dist/hooks/useBusiness.d.ts +2 -2
- package/dist/hooks/useBusiness.d.ts.map +1 -1
- package/dist/hooks/useBusiness.js +2 -2
- package/dist/hooks/useIPFSUrl.d.ts +3 -3
- package/dist/hooks/useIPFSUrl.d.ts.map +1 -1
- package/dist/hooks/useIPFSUrl.js +7 -7
- package/dist/hooks/useWeb3.js +4 -4
- package/dist/index.js +2400 -487
- package/dist/index.js.map +1 -1
- package/dist/providers/PersSDKProvider.d.ts +4 -0
- package/dist/providers/PersSDKProvider.d.ts.map +1 -1
- package/dist/providers/PersSDKProvider.js +14 -0
- package/dist/providers/react-native-http-client.d.ts +2 -0
- package/dist/providers/react-native-http-client.d.ts.map +1 -1
- package/dist/providers/react-native-http-client.js +5 -2
- package/dist/storage/rn-secure-storage.d.ts.map +1 -1
- package/dist/storage/rn-secure-storage.js +21 -8
- package/package.json +3 -3
- package/src/__tests__/PersSDKProvider.test.tsx +484 -420
- package/src/__tests__/setup.ts +40 -40
- package/src/hooks/index.ts +60 -60
- package/src/hooks/useAnalytics.ts +327 -327
- package/src/hooks/useAuth.ts +265 -265
- package/src/hooks/useBookings.ts +191 -191
- package/src/hooks/useBusiness.ts +251 -251
- package/src/hooks/useCampaigns.ts +500 -500
- package/src/hooks/useDonations.ts +66 -66
- package/src/hooks/useEvents.ts +372 -372
- package/src/hooks/useFiles.ts +155 -155
- package/src/hooks/useIPFSUrl.ts +192 -192
- package/src/hooks/usePurchases.ts +65 -65
- package/src/hooks/useRedemptions.ts +467 -467
- package/src/hooks/useTenants.ts +72 -72
- package/src/hooks/useTokenBalances.ts +385 -385
- package/src/hooks/useTokens.ts +313 -313
- package/src/hooks/useTransactionSigner.ts +611 -611
- package/src/hooks/useTransactions.ts +299 -299
- package/src/hooks/useTriggerSources.ts +301 -301
- package/src/hooks/useUserStatus.ts +61 -61
- package/src/hooks/useUsers.ts +167 -167
- package/src/hooks/useWeb3.ts +355 -355
- package/src/index.ts +777 -777
- package/src/polyfills/index.ts +160 -160
- package/src/polyfills/web3-polyfills.ts +97 -97
- package/src/providers/PersSDKProvider.tsx +393 -374
- package/src/providers/react-native-auth-provider.ts +110 -110
- package/src/providers/react-native-http-client.ts +150 -145
- package/src/providers/rn-dpop-provider.ts +190 -190
- package/src/storage/async-storage-token-storage.ts +185 -185
- package/src/storage/rn-secure-storage.ts +182 -170
- package/src/types/react-native-globals.d.ts +45 -45
- package/src/utils/index.ts +8 -8
- package/src/utils/platform-detection.ts +86 -86
- package/dist/index-CzLxqePZ.js +0 -7717
- package/dist/index-CzLxqePZ.js.map +0 -1
|
@@ -1,420 +1,484 @@
|
|
|
1
|
-
import React from 'react';
|
|
2
|
-
import { render, act, waitFor } from '@testing-library/react';
|
|
3
|
-
import '@testing-library/jest-dom';
|
|
4
|
-
import { PersSDKProvider, usePersSDK, PersSDKContext } from '../providers/PersSDKProvider';
|
|
5
|
-
|
|
6
|
-
// Store SDK instances for event emission
|
|
7
|
-
let mockSdkInstance: any = null;
|
|
8
|
-
|
|
9
|
-
// Mock the SDK core module BEFORE any imports use it
|
|
10
|
-
jest.mock('@explorins/pers-sdk/core', () => {
|
|
11
|
-
const createMockEventEmitter = () => {
|
|
12
|
-
const subscribers: Array<{ callback: (event: any) => void; options?: any }> = [];
|
|
13
|
-
|
|
14
|
-
return {
|
|
15
|
-
subscribe: jest.fn((callback: (event: any) => void, options?: any) => {
|
|
16
|
-
subscribers.push({ callback, options });
|
|
17
|
-
return () => {
|
|
18
|
-
const index = subscribers.findIndex(s => s.callback === callback);
|
|
19
|
-
if (index > -1) subscribers.splice(index, 1);
|
|
20
|
-
};
|
|
21
|
-
}),
|
|
22
|
-
emit: (event: any) => {
|
|
23
|
-
subscribers.forEach(({ callback, options }) => {
|
|
24
|
-
if (!options?.domains || options.domains.includes(event.domain)) {
|
|
25
|
-
callback(event);
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
},
|
|
29
|
-
_subscribers: subscribers
|
|
30
|
-
};
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
const MockPersSDK = jest.fn().mockImplementation(() => {
|
|
34
|
-
const events = createMockEventEmitter();
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
},
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
},
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
jest.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
expect(capturedContext!.
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
});
|
|
207
|
-
|
|
208
|
-
it('
|
|
209
|
-
let capturedContext: PersSDKContext | null = null;
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
}
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
await
|
|
288
|
-
|
|
289
|
-
});
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
await
|
|
297
|
-
|
|
298
|
-
});
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
'
|
|
331
|
-
);
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
await
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
it('should throw error when
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
it('should
|
|
403
|
-
let capturedContext: PersSDKContext | null = null;
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { render, act, waitFor } from '@testing-library/react';
|
|
3
|
+
import '@testing-library/jest-dom';
|
|
4
|
+
import { PersSDKProvider, usePersSDK, PersSDKContext } from '../providers/PersSDKProvider';
|
|
5
|
+
|
|
6
|
+
// Store SDK instances for event emission
|
|
7
|
+
let mockSdkInstance: any = null;
|
|
8
|
+
|
|
9
|
+
// Mock the SDK core module BEFORE any imports use it
|
|
10
|
+
jest.mock('@explorins/pers-sdk/core', () => {
|
|
11
|
+
const createMockEventEmitter = () => {
|
|
12
|
+
const subscribers: Array<{ callback: (event: any) => void; options?: any }> = [];
|
|
13
|
+
|
|
14
|
+
return {
|
|
15
|
+
subscribe: jest.fn((callback: (event: any) => void, options?: any) => {
|
|
16
|
+
subscribers.push({ callback, options });
|
|
17
|
+
return () => {
|
|
18
|
+
const index = subscribers.findIndex(s => s.callback === callback);
|
|
19
|
+
if (index > -1) subscribers.splice(index, 1);
|
|
20
|
+
};
|
|
21
|
+
}),
|
|
22
|
+
emit: (event: any) => {
|
|
23
|
+
subscribers.forEach(({ callback, options }) => {
|
|
24
|
+
if (!options?.domains || options.domains.includes(event.domain)) {
|
|
25
|
+
callback(event);
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
},
|
|
29
|
+
_subscribers: subscribers
|
|
30
|
+
};
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const MockPersSDK = jest.fn().mockImplementation(() => {
|
|
34
|
+
const events = createMockEventEmitter();
|
|
35
|
+
let currentLocale: string | null = null;
|
|
36
|
+
mockSdkInstance = {
|
|
37
|
+
events,
|
|
38
|
+
getLocale: jest.fn(() => currentLocale),
|
|
39
|
+
setLocale: jest.fn((next: string | null) => { currentLocale = next ? next.toLowerCase() : null; }),
|
|
40
|
+
auth: {
|
|
41
|
+
ensureValidToken: jest.fn().mockResolvedValue(true)
|
|
42
|
+
},
|
|
43
|
+
users: {
|
|
44
|
+
getCurrentUser: jest.fn().mockResolvedValue({ id: 'refreshed-user', email: 'refreshed@test.com' })
|
|
45
|
+
},
|
|
46
|
+
restoreSession: jest.fn().mockResolvedValue(null)
|
|
47
|
+
};
|
|
48
|
+
return mockSdkInstance;
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
return {
|
|
52
|
+
PersSDK: MockPersSDK,
|
|
53
|
+
DefaultAuthProvider: jest.fn().mockImplementation(() => ({
|
|
54
|
+
getToken: jest.fn().mockResolvedValue('mock-token')
|
|
55
|
+
}))
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
// Mock platform-specific providers
|
|
60
|
+
jest.mock('../providers/react-native-http-client', () => ({
|
|
61
|
+
ReactNativeHttpClient: jest.fn().mockImplementation(() => ({}))
|
|
62
|
+
}));
|
|
63
|
+
|
|
64
|
+
jest.mock('../providers/react-native-auth-provider', () => ({
|
|
65
|
+
createReactNativeAuthProvider: jest.fn().mockReturnValue({
|
|
66
|
+
getToken: jest.fn().mockResolvedValue('mock-token')
|
|
67
|
+
})
|
|
68
|
+
}));
|
|
69
|
+
|
|
70
|
+
jest.mock('../providers/rn-dpop-provider', () => ({
|
|
71
|
+
ReactNativeDPoPProvider: jest.fn().mockImplementation(() => ({}))
|
|
72
|
+
}));
|
|
73
|
+
|
|
74
|
+
// Helper component to access context
|
|
75
|
+
const TestConsumer: React.FC<{ onContext: (ctx: PersSDKContext) => void }> = ({ onContext }) => {
|
|
76
|
+
const context = usePersSDK();
|
|
77
|
+
React.useEffect(() => {
|
|
78
|
+
onContext(context);
|
|
79
|
+
}, [context, onContext]);
|
|
80
|
+
return <div data-testid="consumer">Consumer</div>;
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// Helper to emit events on the mock SDK
|
|
84
|
+
const emitAuthEvent = (type: string, details: any = {}) => {
|
|
85
|
+
if (mockSdkInstance?.events) {
|
|
86
|
+
mockSdkInstance.events.emit({
|
|
87
|
+
type,
|
|
88
|
+
domain: 'authentication',
|
|
89
|
+
details
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
describe('PersSDKProvider', () => {
|
|
95
|
+
beforeEach(() => {
|
|
96
|
+
jest.clearAllMocks();
|
|
97
|
+
mockSdkInstance = null;
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
describe('initialization', () => {
|
|
101
|
+
it('should initialize with null SDK when no config provided', async () => {
|
|
102
|
+
let capturedContext: PersSDKContext | null = null;
|
|
103
|
+
|
|
104
|
+
render(
|
|
105
|
+
<PersSDKProvider>
|
|
106
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
107
|
+
</PersSDKProvider>
|
|
108
|
+
);
|
|
109
|
+
|
|
110
|
+
await waitFor(() => {
|
|
111
|
+
expect(capturedContext).not.toBeNull();
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
expect(capturedContext!.sdk).toBeNull();
|
|
115
|
+
expect(capturedContext!.isInitialized).toBe(false);
|
|
116
|
+
expect(capturedContext!.isAuthenticated).toBe(false);
|
|
117
|
+
expect(capturedContext!.user).toBeNull();
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
it('should auto-initialize when config is provided', async () => {
|
|
121
|
+
let capturedContext: PersSDKContext | null = null;
|
|
122
|
+
|
|
123
|
+
render(
|
|
124
|
+
<PersSDKProvider config={{ apiProjectKey: 'test-project' }}>
|
|
125
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
126
|
+
</PersSDKProvider>
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
await waitFor(() => {
|
|
130
|
+
expect(capturedContext?.isInitialized).toBe(true);
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
expect(capturedContext!.sdk).not.toBeNull();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('should prevent multiple initializations', async () => {
|
|
137
|
+
const { PersSDK } = require('@explorins/pers-sdk/core');
|
|
138
|
+
|
|
139
|
+
let capturedContext: PersSDKContext | null = null;
|
|
140
|
+
|
|
141
|
+
render(
|
|
142
|
+
<PersSDKProvider config={{ apiProjectKey: 'test-project' }}>
|
|
143
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
144
|
+
</PersSDKProvider>
|
|
145
|
+
);
|
|
146
|
+
|
|
147
|
+
await waitFor(() => {
|
|
148
|
+
expect(capturedContext?.isInitialized).toBe(true);
|
|
149
|
+
});
|
|
150
|
+
|
|
151
|
+
// Try to initialize again - should be ignored
|
|
152
|
+
await act(async () => {
|
|
153
|
+
await capturedContext!.initialize({ apiProjectKey: 'different-project' });
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
// Should only have been called once
|
|
157
|
+
expect(PersSDK).toHaveBeenCalledTimes(1);
|
|
158
|
+
});
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
describe('locale', () => {
|
|
162
|
+
it('exposes a null locale until one is set', async () => {
|
|
163
|
+
let capturedContext: PersSDKContext | null = null;
|
|
164
|
+
|
|
165
|
+
render(
|
|
166
|
+
<PersSDKProvider config={{ apiProjectKey: 'test-project' }}>
|
|
167
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
168
|
+
</PersSDKProvider>
|
|
169
|
+
);
|
|
170
|
+
|
|
171
|
+
await waitFor(() => expect(capturedContext?.isInitialized).toBe(true));
|
|
172
|
+
expect(capturedContext!.locale).toBeNull();
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('passes the configured locale through to the SDK config', async () => {
|
|
176
|
+
const { PersSDK } = require('@explorins/pers-sdk/core');
|
|
177
|
+
|
|
178
|
+
render(
|
|
179
|
+
<PersSDKProvider config={{ apiProjectKey: 'test-project', locale: 'auto' }}>
|
|
180
|
+
<div />
|
|
181
|
+
</PersSDKProvider>
|
|
182
|
+
);
|
|
183
|
+
|
|
184
|
+
await waitFor(() => expect(PersSDK).toHaveBeenCalledTimes(1));
|
|
185
|
+
expect(PersSDK.mock.calls[0][1]).toMatchObject({ locale: 'auto' });
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it('setLocale updates the SDK and the context so consumers re-render', async () => {
|
|
189
|
+
let capturedContext: PersSDKContext | null = null;
|
|
190
|
+
|
|
191
|
+
render(
|
|
192
|
+
<PersSDKProvider config={{ apiProjectKey: 'test-project' }}>
|
|
193
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
194
|
+
</PersSDKProvider>
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
await waitFor(() => expect(capturedContext?.isInitialized).toBe(true));
|
|
198
|
+
|
|
199
|
+
act(() => { capturedContext!.setLocale('ar-SA'); });
|
|
200
|
+
|
|
201
|
+
await waitFor(() => expect(capturedContext?.locale).toBe('ar-sa'));
|
|
202
|
+
expect(mockSdkInstance.setLocale).toHaveBeenCalledWith('ar-SA');
|
|
203
|
+
|
|
204
|
+
act(() => { capturedContext!.setLocale(null); });
|
|
205
|
+
await waitFor(() => expect(capturedContext?.locale).toBeNull());
|
|
206
|
+
});
|
|
207
|
+
|
|
208
|
+
it('setLocale throws before the SDK is initialized', async () => {
|
|
209
|
+
let capturedContext: PersSDKContext | null = null;
|
|
210
|
+
|
|
211
|
+
render(
|
|
212
|
+
<PersSDKProvider>
|
|
213
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
214
|
+
</PersSDKProvider>
|
|
215
|
+
);
|
|
216
|
+
|
|
217
|
+
await waitFor(() => expect(capturedContext).not.toBeNull());
|
|
218
|
+
expect(() => capturedContext!.setLocale('de')).toThrow('SDK not initialized');
|
|
219
|
+
});
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
describe('event handling', () => {
|
|
223
|
+
it('should set user on session_restored event', async () => {
|
|
224
|
+
let capturedContext: PersSDKContext | null = null;
|
|
225
|
+
const mockUser = { id: 'user-123', email: 'test@example.com' };
|
|
226
|
+
|
|
227
|
+
render(
|
|
228
|
+
<PersSDKProvider config={{ apiProjectKey: 'test-project' }}>
|
|
229
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
230
|
+
</PersSDKProvider>
|
|
231
|
+
);
|
|
232
|
+
|
|
233
|
+
await waitFor(() => {
|
|
234
|
+
expect(capturedContext?.sdk).not.toBeNull();
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
// Emit session_restored event
|
|
238
|
+
await act(async () => {
|
|
239
|
+
emitAuthEvent('session_restored', { user: mockUser });
|
|
240
|
+
});
|
|
241
|
+
|
|
242
|
+
await waitFor(() => {
|
|
243
|
+
expect(capturedContext?.isAuthenticated).toBe(true);
|
|
244
|
+
expect(capturedContext?.user).toEqual(mockUser);
|
|
245
|
+
});
|
|
246
|
+
});
|
|
247
|
+
|
|
248
|
+
it('should set user on login_success event', async () => {
|
|
249
|
+
let capturedContext: PersSDKContext | null = null;
|
|
250
|
+
const mockUser = { id: 'user-456', email: 'login@example.com' };
|
|
251
|
+
|
|
252
|
+
render(
|
|
253
|
+
<PersSDKProvider config={{ apiProjectKey: 'test-project' }}>
|
|
254
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
255
|
+
</PersSDKProvider>
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
await waitFor(() => {
|
|
259
|
+
expect(capturedContext?.sdk).not.toBeNull();
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
await act(async () => {
|
|
263
|
+
emitAuthEvent('login_success', { user: mockUser });
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
await waitFor(() => {
|
|
267
|
+
expect(capturedContext?.isAuthenticated).toBe(true);
|
|
268
|
+
expect(capturedContext?.user).toEqual(mockUser);
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
|
|
272
|
+
it('should clear user on logout_success event', async () => {
|
|
273
|
+
let capturedContext: PersSDKContext | null = null;
|
|
274
|
+
const mockUser = { id: 'user-789', email: 'logout@example.com' };
|
|
275
|
+
|
|
276
|
+
render(
|
|
277
|
+
<PersSDKProvider config={{ apiProjectKey: 'test-project' }}>
|
|
278
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
279
|
+
</PersSDKProvider>
|
|
280
|
+
);
|
|
281
|
+
|
|
282
|
+
await waitFor(() => {
|
|
283
|
+
expect(capturedContext?.sdk).not.toBeNull();
|
|
284
|
+
});
|
|
285
|
+
|
|
286
|
+
// First, log in
|
|
287
|
+
await act(async () => {
|
|
288
|
+
emitAuthEvent('login_success', { user: mockUser });
|
|
289
|
+
});
|
|
290
|
+
|
|
291
|
+
await waitFor(() => {
|
|
292
|
+
expect(capturedContext?.isAuthenticated).toBe(true);
|
|
293
|
+
});
|
|
294
|
+
|
|
295
|
+
// Then logout
|
|
296
|
+
await act(async () => {
|
|
297
|
+
emitAuthEvent('logout_success', {});
|
|
298
|
+
});
|
|
299
|
+
|
|
300
|
+
await waitFor(() => {
|
|
301
|
+
expect(capturedContext?.isAuthenticated).toBe(false);
|
|
302
|
+
expect(capturedContext?.user).toBeNull();
|
|
303
|
+
});
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
it('should clear user on session_restoration_failed event', async () => {
|
|
307
|
+
let capturedContext: PersSDKContext | null = null;
|
|
308
|
+
|
|
309
|
+
render(
|
|
310
|
+
<PersSDKProvider config={{ apiProjectKey: 'test-project' }}>
|
|
311
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
312
|
+
</PersSDKProvider>
|
|
313
|
+
);
|
|
314
|
+
|
|
315
|
+
await waitFor(() => {
|
|
316
|
+
expect(capturedContext?.sdk).not.toBeNull();
|
|
317
|
+
});
|
|
318
|
+
|
|
319
|
+
// Set initial authenticated state
|
|
320
|
+
await act(async () => {
|
|
321
|
+
capturedContext!.setAuthenticationState({ id: 'temp-user' } as any, true);
|
|
322
|
+
});
|
|
323
|
+
|
|
324
|
+
await waitFor(() => {
|
|
325
|
+
expect(capturedContext?.isAuthenticated).toBe(true);
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
// Emit session_restoration_failed
|
|
329
|
+
await act(async () => {
|
|
330
|
+
emitAuthEvent('session_restoration_failed', { error: 'Token expired' });
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
await waitFor(() => {
|
|
334
|
+
expect(capturedContext?.isAuthenticated).toBe(false);
|
|
335
|
+
expect(capturedContext?.user).toBeNull();
|
|
336
|
+
});
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
it('should NOT respond to AUTH_FAILED code (dead code was removed)', async () => {
|
|
340
|
+
// This test verifies that we no longer check for event.code === 'AUTH_FAILED'
|
|
341
|
+
// which was dead code that never triggered
|
|
342
|
+
let capturedContext: PersSDKContext | null = null;
|
|
343
|
+
const mockUser = { id: 'user-stay', email: 'stay@example.com' };
|
|
344
|
+
|
|
345
|
+
render(
|
|
346
|
+
<PersSDKProvider config={{ apiProjectKey: 'test-project' }}>
|
|
347
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
348
|
+
</PersSDKProvider>
|
|
349
|
+
);
|
|
350
|
+
|
|
351
|
+
await waitFor(() => {
|
|
352
|
+
expect(capturedContext?.sdk).not.toBeNull();
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
// Log in first
|
|
356
|
+
await act(async () => {
|
|
357
|
+
emitAuthEvent('login_success', { user: mockUser });
|
|
358
|
+
});
|
|
359
|
+
|
|
360
|
+
await waitFor(() => {
|
|
361
|
+
expect(capturedContext?.isAuthenticated).toBe(true);
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
// Emit an event with code: 'AUTH_FAILED' - this should NOT clear user
|
|
365
|
+
// because we removed the dead code that checked for this
|
|
366
|
+
await act(async () => {
|
|
367
|
+
if (mockSdkInstance?.events) {
|
|
368
|
+
mockSdkInstance.events.emit({
|
|
369
|
+
type: 'some_other_event',
|
|
370
|
+
code: 'AUTH_FAILED', // This was the dead code path we removed
|
|
371
|
+
domain: 'authentication',
|
|
372
|
+
details: {}
|
|
373
|
+
});
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
|
|
377
|
+
// User should STILL be authenticated - the code: 'AUTH_FAILED' check is gone
|
|
378
|
+
expect(capturedContext!.isAuthenticated).toBe(true);
|
|
379
|
+
expect(capturedContext!.user).toEqual(mockUser);
|
|
380
|
+
});
|
|
381
|
+
});
|
|
382
|
+
|
|
383
|
+
describe('usePersSDK hook', () => {
|
|
384
|
+
it('should throw error when used outside provider', () => {
|
|
385
|
+
// Suppress the expected error
|
|
386
|
+
const spy = jest.spyOn(console, 'error').mockImplementation(() => {});
|
|
387
|
+
|
|
388
|
+
const TestComponent = () => {
|
|
389
|
+
usePersSDK();
|
|
390
|
+
return null;
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
expect(() => render(<TestComponent />)).toThrow(
|
|
394
|
+
'usePersSDK must be used within a PersSDKProvider'
|
|
395
|
+
);
|
|
396
|
+
|
|
397
|
+
spy.mockRestore();
|
|
398
|
+
});
|
|
399
|
+
});
|
|
400
|
+
|
|
401
|
+
describe('context methods', () => {
|
|
402
|
+
it('should allow manual authentication state updates', async () => {
|
|
403
|
+
let capturedContext: PersSDKContext | null = null;
|
|
404
|
+
const mockUser = { id: 'manual-user', email: 'manual@example.com' };
|
|
405
|
+
|
|
406
|
+
render(
|
|
407
|
+
<PersSDKProvider config={{ apiProjectKey: 'test-project' }}>
|
|
408
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
409
|
+
</PersSDKProvider>
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
await waitFor(() => {
|
|
413
|
+
expect(capturedContext?.isInitialized).toBe(true);
|
|
414
|
+
});
|
|
415
|
+
|
|
416
|
+
await act(async () => {
|
|
417
|
+
capturedContext!.setAuthenticationState(mockUser as any, true);
|
|
418
|
+
});
|
|
419
|
+
|
|
420
|
+
await waitFor(() => {
|
|
421
|
+
expect(capturedContext?.isAuthenticated).toBe(true);
|
|
422
|
+
expect(capturedContext?.user).toEqual(mockUser);
|
|
423
|
+
});
|
|
424
|
+
});
|
|
425
|
+
|
|
426
|
+
it('should refresh user data from SDK', async () => {
|
|
427
|
+
let capturedContext: PersSDKContext | null = null;
|
|
428
|
+
|
|
429
|
+
render(
|
|
430
|
+
<PersSDKProvider config={{ apiProjectKey: 'test-project' }}>
|
|
431
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
432
|
+
</PersSDKProvider>
|
|
433
|
+
);
|
|
434
|
+
|
|
435
|
+
await waitFor(() => {
|
|
436
|
+
expect(capturedContext?.isInitialized).toBe(true);
|
|
437
|
+
});
|
|
438
|
+
|
|
439
|
+
await act(async () => {
|
|
440
|
+
await capturedContext!.refreshUserData();
|
|
441
|
+
});
|
|
442
|
+
|
|
443
|
+
await waitFor(() => {
|
|
444
|
+
expect(capturedContext?.user).toEqual({ id: 'refreshed-user', email: 'refreshed@test.com' });
|
|
445
|
+
});
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
it('should throw error when refreshUserData called before init', async () => {
|
|
449
|
+
let capturedContext: PersSDKContext | null = null;
|
|
450
|
+
|
|
451
|
+
render(
|
|
452
|
+
<PersSDKProvider>
|
|
453
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
454
|
+
</PersSDKProvider>
|
|
455
|
+
);
|
|
456
|
+
|
|
457
|
+
await waitFor(() => {
|
|
458
|
+
expect(capturedContext).not.toBeNull();
|
|
459
|
+
});
|
|
460
|
+
|
|
461
|
+
await expect(capturedContext!.refreshUserData()).rejects.toThrow(
|
|
462
|
+
'SDK not initialized'
|
|
463
|
+
);
|
|
464
|
+
});
|
|
465
|
+
|
|
466
|
+
it('should throw error when restoreSession called before init', async () => {
|
|
467
|
+
let capturedContext: PersSDKContext | null = null;
|
|
468
|
+
|
|
469
|
+
render(
|
|
470
|
+
<PersSDKProvider>
|
|
471
|
+
<TestConsumer onContext={(ctx) => { capturedContext = ctx; }} />
|
|
472
|
+
</PersSDKProvider>
|
|
473
|
+
);
|
|
474
|
+
|
|
475
|
+
await waitFor(() => {
|
|
476
|
+
expect(capturedContext).not.toBeNull();
|
|
477
|
+
});
|
|
478
|
+
|
|
479
|
+
await expect(capturedContext!.restoreSession()).rejects.toThrow(
|
|
480
|
+
'SDK not initialized'
|
|
481
|
+
);
|
|
482
|
+
});
|
|
483
|
+
});
|
|
484
|
+
});
|