@incodetech/web 2.0.0-alpha.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dev/README.md +163 -0
- package/dev/getToken.ts +36 -0
- package/dev/headless.html +875 -0
- package/dev/index.html +366 -0
- package/dev/main-headless.tsx +1332 -0
- package/dev/main-orchestrated-flow.tsx +1158 -0
- package/dev/main-preact.tsx +323 -0
- package/dev/main-simplified.tsx +123 -0
- package/dev/main-web-component.tsx +256 -0
- package/dev/main.tsx +332 -0
- package/dev/manual.html +27 -0
- package/dev/orchestrated-flow.html +64 -0
- package/dev/simplified.html +64 -0
- package/dev/tiktok-logo.svg +7 -0
- package/package.json +85 -0
- package/src/defineCustomElement.tsx +30 -0
- package/src/email/email.test.tsx +368 -0
- package/src/email/email.tsx +255 -0
- package/src/email/emailInput.test.tsx +264 -0
- package/src/email/emailInput.tsx +85 -0
- package/src/email/styles.css +59 -0
- package/src/flow/flow.test.tsx +796 -0
- package/src/flow/flow.tsx +292 -0
- package/src/flow/flowCompleted.css +30 -0
- package/src/flow/flowCompleted.test.tsx +331 -0
- package/src/flow/flowCompleted.tsx +121 -0
- package/src/flow/flowInit.test.ts +264 -0
- package/src/flow/flowInit.ts +94 -0
- package/src/flow/flowStart.css +58 -0
- package/src/flow/flowStart.test.tsx +49 -0
- package/src/flow/flowStart.tsx +41 -0
- package/src/flow/incode-logo.svg +8 -0
- package/src/flow/index.ts +7 -0
- package/src/flow/preloadFlow.test.ts +421 -0
- package/src/flow/preloadFlow.ts +171 -0
- package/src/flow/styles.css +9 -0
- package/src/flow/unsupportedModule.css +21 -0
- package/src/flow/unsupportedModule.tsx +39 -0
- package/src/flow/useFlowInitialization.test.tsx +292 -0
- package/src/flow/useFlowInitialization.ts +128 -0
- package/src/flow/useModuleLoader.test.tsx +212 -0
- package/src/flow/useModuleLoader.ts +92 -0
- package/src/hooks/index.ts +1 -0
- package/src/hooks/useManager.test.ts +91 -0
- package/src/hooks/useManager.ts +40 -0
- package/src/i18n/index.ts +3 -0
- package/src/i18n/instance.ts +16 -0
- package/src/i18n/setup.ts +184 -0
- package/src/i18n/useTranslation.ts +42 -0
- package/src/index.ts +27 -0
- package/src/permissions/assets/android-dots-icon.svg +7 -0
- package/src/permissions/assets/android-settings-icon.svg +16 -0
- package/src/permissions/assets/android-toggle-icon.svg +20 -0
- package/src/permissions/assets/bank-card-icon.svg +14 -0
- package/src/permissions/assets/camera-icon.svg +12 -0
- package/src/permissions/assets/camera-ios.svg +53 -0
- package/src/permissions/assets/check-icon.svg +8 -0
- package/src/permissions/assets/chrome-icon.svg +43 -0
- package/src/permissions/assets/password-icon.svg +11 -0
- package/src/permissions/assets/permissions-img.svg +51 -0
- package/src/permissions/assets/safari-icon.svg +37 -0
- package/src/permissions/assets/settings-icon.svg +33 -0
- package/src/permissions/assets/toggle-icon.svg +19 -0
- package/src/permissions/assets/warning-icon.svg +6 -0
- package/src/permissions/boldWithArrow.css +9 -0
- package/src/permissions/boldWithArrow.tsx +41 -0
- package/src/permissions/denied.css +37 -0
- package/src/permissions/denied.tsx +29 -0
- package/src/permissions/deniedAndroid.tsx +56 -0
- package/src/permissions/deniedDesktop.css +9 -0
- package/src/permissions/deniedDesktop.tsx +64 -0
- package/src/permissions/deniedIOS.tsx +73 -0
- package/src/permissions/deniedInstructions.tsx +19 -0
- package/src/permissions/iconWrapper.css +9 -0
- package/src/permissions/iconWrapper.tsx +15 -0
- package/src/permissions/learnMore.css +37 -0
- package/src/permissions/learnMore.tsx +85 -0
- package/src/permissions/numberedStep.css +13 -0
- package/src/permissions/numberedStep.tsx +14 -0
- package/src/permissions/permissions.css +13 -0
- package/src/permissions/permissions.tsx +68 -0
- package/src/phone/phone.tsx +246 -0
- package/src/phone/phoneInput.test.tsx +275 -0
- package/src/phone/phoneInput.tsx +249 -0
- package/src/phone/styles.css +158 -0
- package/src/selfie/cameraButton.css +13 -0
- package/src/selfie/cameraButton.tsx +35 -0
- package/src/selfie/capture.css +57 -0
- package/src/selfie/capture.tsx +232 -0
- package/src/selfie/errorModal.tsx +218 -0
- package/src/selfie/errorModalContent.css +33 -0
- package/src/selfie/errorModalContent.tsx +44 -0
- package/src/selfie/faceOutline.css +5 -0
- package/src/selfie/faceOutline.tsx +22 -0
- package/src/selfie/loadingBorder.css +12 -0
- package/src/selfie/loadingBorder.tsx +77 -0
- package/src/selfie/manualCaptureButton.css +13 -0
- package/src/selfie/manualCaptureButton.tsx +35 -0
- package/src/selfie/noMoreAttemptsModal.tsx +44 -0
- package/src/selfie/notification.css +9 -0
- package/src/selfie/notification.tsx +36 -0
- package/src/selfie/retryErrorModal.tsx +56 -0
- package/src/selfie/selfie.test.tsx +458 -0
- package/src/selfie/selfie.tsx +83 -0
- package/src/selfie/selfieTutorial.json +2626 -0
- package/src/selfie/styles.css +1 -0
- package/src/selfie/tutorial.test.tsx +200 -0
- package/src/selfie/tutorial.tsx +43 -0
- package/src/setup.ts +33 -0
- package/src/shared/baseTutorial/baseTutorial.css +21 -0
- package/src/shared/baseTutorial/baseTutorial.test.tsx +184 -0
- package/src/shared/baseTutorial/baseTutorial.tsx +55 -0
- package/src/shared/baseTutorial/replaceBaseTutorial.test.ts +267 -0
- package/src/shared/baseTutorial/replaceBaseTutorial.ts +68 -0
- package/src/shared/button/button.css +55 -0
- package/src/shared/button/button.test.tsx +101 -0
- package/src/shared/button/button.tsx +47 -0
- package/src/shared/componentRoot/incodeComponent.tsx +12 -0
- package/src/shared/countries/countries.test.ts +75 -0
- package/src/shared/countries/countries.ts +139 -0
- package/src/shared/countries/index.ts +6 -0
- package/src/shared/icons/chevronDown.tsx +22 -0
- package/src/shared/icons/index.ts +2 -0
- package/src/shared/icons/successIcon.css +5 -0
- package/src/shared/icons/successIcon.test.tsx +40 -0
- package/src/shared/icons/successIcon.tsx +26 -0
- package/src/shared/loader/loadingIcon.css +28 -0
- package/src/shared/loader/loadingIcon.tsx +67 -0
- package/src/shared/lottie/lottie.tsx +108 -0
- package/src/shared/otpInput/otpInput.css +85 -0
- package/src/shared/otpInput/otpInput.test.tsx +356 -0
- package/src/shared/otpInput/otpInput.tsx +241 -0
- package/src/shared/page/incode-logo.svg +3 -0
- package/src/shared/page/page.css +47 -0
- package/src/shared/page/page.test.tsx +97 -0
- package/src/shared/page/page.tsx +91 -0
- package/src/shared/page/pageUiConfig.test.ts +112 -0
- package/src/shared/page/pageUiConfig.ts +64 -0
- package/src/shared/page/verifiedByIncode.css +5 -0
- package/src/shared/page/verifiedByIncode.tsx +75 -0
- package/src/shared/spacer/spacer.css +149 -0
- package/src/shared/spacer/spacer.test.tsx +143 -0
- package/src/shared/spacer/spacer.tsx +88 -0
- package/src/shared/spinner/index.ts +2 -0
- package/src/shared/spinner/spinner.css +28 -0
- package/src/shared/spinner/spinner.test.tsx +82 -0
- package/src/shared/spinner/spinner.tsx +65 -0
- package/src/shared/title/title.css +7 -0
- package/src/shared/title/title.tsx +12 -0
- package/src/shared/uiConfig/uiConfig.ts +36 -0
- package/src/shared/webComponent/incodeModule.ts +29 -0
- package/src/shared/webComponent/registerIncodeElement.ts +15 -0
- package/src/styles/__mocks__/fetchTheme.ts +19 -0
- package/src/styles/applyTheme.ts +37 -0
- package/src/styles/cn.test.tsx +57 -0
- package/src/styles/cn.tsx +21 -0
- package/src/styles/core.css +12 -0
- package/src/styles/fetchTheme.test.ts +390 -0
- package/src/styles/fetchTheme.ts +88 -0
- package/src/styles/generatePalette.ts +111 -0
- package/src/styles/reset.css +65 -0
- package/src/styles/resolveCssVariableToHex.ts +28 -0
- package/src/styles/tailwind.css +291 -0
- package/src/styles/themeTypes.ts +18 -0
- package/src/styles/tokens/colors.css +190 -0
- package/src/styles/tokens/components.css +174 -0
- package/src/styles/tokens/index.css +4 -0
- package/src/styles/tokens/primitives.css +129 -0
- package/src/styles/tokens/semantic.css +51 -0
- package/src/svg.d.ts +4 -0
- package/src/types/assets.d.ts +1 -0
- package/src/types/custom-elements.d.ts +104 -0
- package/tsconfig.json +22 -0
- package/vite.config.ts +260 -0
- package/vitest.config.ts +40 -0
- package/vitest.setup.ts +16 -0
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { act, renderHook, waitFor } from '@testing-library/preact';
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
|
|
4
|
+
const mockFlowManager = {
|
|
5
|
+
load: vi.fn(),
|
|
6
|
+
getState: vi.fn(() => ({ status: 'idle' })),
|
|
7
|
+
subscribe: vi.fn(() => vi.fn()),
|
|
8
|
+
stop: vi.fn(),
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
const mockSetupSDK = vi.fn().mockResolvedValue(undefined);
|
|
12
|
+
const mockFetchTheme = vi.fn().mockResolvedValue({
|
|
13
|
+
theme: { logo: 'logo.png' },
|
|
14
|
+
uiConfig: { logoSrc: 'logo.png' },
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
vi.mock('./flowInit', () => ({
|
|
18
|
+
createFlowManager: vi.fn(() => mockFlowManager),
|
|
19
|
+
setupSDK: (...args: unknown[]) => mockSetupSDK(...args),
|
|
20
|
+
fetchTheme: () => mockFetchTheme(),
|
|
21
|
+
}));
|
|
22
|
+
|
|
23
|
+
const mockGetPreloadedData = vi.fn();
|
|
24
|
+
const mockConsumePreloadedData = vi.fn();
|
|
25
|
+
|
|
26
|
+
vi.mock('./preloadFlow', () => ({
|
|
27
|
+
getPreloadedData: (...args: unknown[]) => mockGetPreloadedData(...args),
|
|
28
|
+
consumePreloadedData: (...args: unknown[]) =>
|
|
29
|
+
mockConsumePreloadedData(...args),
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
const mockSetUiConfig = vi.fn();
|
|
33
|
+
vi.mock('../shared/uiConfig/uiConfig', () => ({
|
|
34
|
+
setUiConfig: (...args: unknown[]) => mockSetUiConfig(...args),
|
|
35
|
+
}));
|
|
36
|
+
|
|
37
|
+
import { useFlowInitialization } from './useFlowInitialization';
|
|
38
|
+
|
|
39
|
+
describe('useFlowInitialization', () => {
|
|
40
|
+
beforeEach(() => {
|
|
41
|
+
vi.clearAllMocks();
|
|
42
|
+
mockGetPreloadedData.mockReturnValue(null);
|
|
43
|
+
mockConsumePreloadedData.mockReturnValue(null);
|
|
44
|
+
mockSetupSDK.mockResolvedValue(undefined);
|
|
45
|
+
mockFetchTheme.mockResolvedValue({
|
|
46
|
+
theme: { logo: 'logo.png' },
|
|
47
|
+
uiConfig: { logoSrc: 'logo.png' },
|
|
48
|
+
});
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
afterEach(() => {
|
|
52
|
+
vi.resetAllMocks();
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
it('should start in initializing state when no preload', () => {
|
|
56
|
+
const { result } = renderHook(() =>
|
|
57
|
+
useFlowInitialization({
|
|
58
|
+
apiURL: 'https://api.example.com',
|
|
59
|
+
token: 'test-token',
|
|
60
|
+
}),
|
|
61
|
+
);
|
|
62
|
+
|
|
63
|
+
expect(result.current.status).toBe('initializing');
|
|
64
|
+
expect(result.current.flowManager).toBeDefined();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
it('should use preloaded data when available and ready', () => {
|
|
68
|
+
const preloadedFlowManager = {
|
|
69
|
+
...mockFlowManager,
|
|
70
|
+
preloaded: true,
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
mockGetPreloadedData.mockReturnValue({
|
|
74
|
+
status: 'ready',
|
|
75
|
+
flowManager: preloadedFlowManager,
|
|
76
|
+
uiConfig: { logoSrc: 'preloaded-logo.png' },
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
mockConsumePreloadedData.mockReturnValue({
|
|
80
|
+
status: 'ready',
|
|
81
|
+
flowManager: preloadedFlowManager,
|
|
82
|
+
uiConfig: { logoSrc: 'preloaded-logo.png' },
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
const { result } = renderHook(() =>
|
|
86
|
+
useFlowInitialization({
|
|
87
|
+
apiURL: 'https://api.example.com',
|
|
88
|
+
token: 'preloaded-token',
|
|
89
|
+
}),
|
|
90
|
+
);
|
|
91
|
+
|
|
92
|
+
expect(result.current.status).toBe('ready');
|
|
93
|
+
expect(result.current.flowManager).toBe(preloadedFlowManager);
|
|
94
|
+
expect(mockSetUiConfig).toHaveBeenCalledWith({
|
|
95
|
+
logoSrc: 'preloaded-logo.png',
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('should show error when preload failed', () => {
|
|
100
|
+
mockGetPreloadedData.mockReturnValue({
|
|
101
|
+
status: 'error',
|
|
102
|
+
error: 'Preload failed',
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
const { result } = renderHook(() =>
|
|
106
|
+
useFlowInitialization({
|
|
107
|
+
apiURL: 'https://api.example.com',
|
|
108
|
+
token: 'error-token',
|
|
109
|
+
}),
|
|
110
|
+
);
|
|
111
|
+
|
|
112
|
+
expect(result.current.status).toBe('error');
|
|
113
|
+
if (result.current.status === 'error') {
|
|
114
|
+
expect(result.current.error).toBe('Preload failed');
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
it('should wait for preload when loading', async () => {
|
|
119
|
+
let resolvePreload: (value: unknown) => void;
|
|
120
|
+
const readyPromise = new Promise((resolve) => {
|
|
121
|
+
resolvePreload = resolve;
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
const preloadedFlowManager = {
|
|
125
|
+
...mockFlowManager,
|
|
126
|
+
preloaded: true,
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
mockGetPreloadedData.mockReturnValue({
|
|
130
|
+
status: 'loading',
|
|
131
|
+
readyPromise,
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
const { result } = renderHook(() =>
|
|
135
|
+
useFlowInitialization({
|
|
136
|
+
apiURL: 'https://api.example.com',
|
|
137
|
+
token: 'loading-token',
|
|
138
|
+
}),
|
|
139
|
+
);
|
|
140
|
+
|
|
141
|
+
expect(result.current.status).toBe('initializing');
|
|
142
|
+
|
|
143
|
+
mockConsumePreloadedData.mockReturnValue({
|
|
144
|
+
status: 'ready',
|
|
145
|
+
flowManager: preloadedFlowManager,
|
|
146
|
+
uiConfig: { logoSrc: 'loaded-logo.png' },
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
await act(async () => {
|
|
150
|
+
resolvePreload?.(undefined);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
await waitFor(() => {
|
|
154
|
+
expect(result.current.status).toBe('ready');
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
expect(result.current.flowManager).toBe(preloadedFlowManager);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('should initialize SDK and become ready', async () => {
|
|
161
|
+
const { result } = renderHook(() =>
|
|
162
|
+
useFlowInitialization({
|
|
163
|
+
apiURL: 'https://api.example.com',
|
|
164
|
+
token: 'init-token',
|
|
165
|
+
lang: 'es',
|
|
166
|
+
}),
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
expect(result.current.status).toBe('initializing');
|
|
170
|
+
|
|
171
|
+
await waitFor(() => {
|
|
172
|
+
expect(result.current.status).toBe('ready');
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
expect(mockSetupSDK).toHaveBeenCalledWith({
|
|
176
|
+
apiURL: 'https://api.example.com',
|
|
177
|
+
token: 'init-token',
|
|
178
|
+
lang: 'es',
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
expect(mockFlowManager.load).toHaveBeenCalled();
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
it('should fetch theme during initialization', async () => {
|
|
185
|
+
const { result } = renderHook(() =>
|
|
186
|
+
useFlowInitialization({
|
|
187
|
+
apiURL: 'https://api.example.com',
|
|
188
|
+
token: 'theme-token',
|
|
189
|
+
disableDashboardTheme: false,
|
|
190
|
+
}),
|
|
191
|
+
);
|
|
192
|
+
|
|
193
|
+
await waitFor(() => {
|
|
194
|
+
expect(result.current.status).toBe('ready');
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
expect(mockFetchTheme).toHaveBeenCalled();
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('should skip theme fetch when disabled', async () => {
|
|
201
|
+
const { result } = renderHook(() =>
|
|
202
|
+
useFlowInitialization({
|
|
203
|
+
apiURL: 'https://api.example.com',
|
|
204
|
+
token: 'no-theme-token',
|
|
205
|
+
disableDashboardTheme: true,
|
|
206
|
+
}),
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
await waitFor(() => {
|
|
210
|
+
expect(result.current.status).toBe('ready');
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
expect(mockFetchTheme).not.toHaveBeenCalled();
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
it('should call onError when SDK setup fails', async () => {
|
|
217
|
+
mockSetupSDK.mockRejectedValueOnce(new Error('SDK error'));
|
|
218
|
+
|
|
219
|
+
const onError = vi.fn();
|
|
220
|
+
|
|
221
|
+
const { result } = renderHook(() =>
|
|
222
|
+
useFlowInitialization({
|
|
223
|
+
apiURL: 'https://api.example.com',
|
|
224
|
+
token: 'sdk-error-token',
|
|
225
|
+
onError,
|
|
226
|
+
}),
|
|
227
|
+
);
|
|
228
|
+
|
|
229
|
+
await waitFor(() => {
|
|
230
|
+
expect(result.current.status).toBe('error');
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
if (result.current.status === 'error') {
|
|
234
|
+
expect(result.current.error).toBe('SDK error');
|
|
235
|
+
}
|
|
236
|
+
expect(onError).toHaveBeenCalledWith('SDK error');
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
it('should handle preload promise rejection', async () => {
|
|
240
|
+
const readyPromise = Promise.reject(new Error('Preload rejected'));
|
|
241
|
+
|
|
242
|
+
mockGetPreloadedData.mockReturnValue({
|
|
243
|
+
status: 'loading',
|
|
244
|
+
readyPromise,
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const onError = vi.fn();
|
|
248
|
+
|
|
249
|
+
const { result } = renderHook(() =>
|
|
250
|
+
useFlowInitialization({
|
|
251
|
+
apiURL: 'https://api.example.com',
|
|
252
|
+
token: 'reject-token',
|
|
253
|
+
onError,
|
|
254
|
+
}),
|
|
255
|
+
);
|
|
256
|
+
|
|
257
|
+
await waitFor(() => {
|
|
258
|
+
expect(result.current.status).toBe('error');
|
|
259
|
+
});
|
|
260
|
+
|
|
261
|
+
if (result.current.status === 'error') {
|
|
262
|
+
expect(result.current.error).toBe('Preload rejected');
|
|
263
|
+
}
|
|
264
|
+
expect(onError).toHaveBeenCalledWith('Preload rejected');
|
|
265
|
+
});
|
|
266
|
+
|
|
267
|
+
it('should not re-initialize when options change after ready', async () => {
|
|
268
|
+
const { result, rerender } = renderHook<
|
|
269
|
+
ReturnType<typeof useFlowInitialization>,
|
|
270
|
+
{ apiURL: string; token: string; lang?: string }
|
|
271
|
+
>((props) => useFlowInitialization(props), {
|
|
272
|
+
initialProps: {
|
|
273
|
+
apiURL: 'https://api.example.com',
|
|
274
|
+
token: 'stable-token',
|
|
275
|
+
},
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
await waitFor(() => {
|
|
279
|
+
expect(result.current.status).toBe('ready');
|
|
280
|
+
});
|
|
281
|
+
|
|
282
|
+
expect(mockSetupSDK).toHaveBeenCalledTimes(1);
|
|
283
|
+
|
|
284
|
+
rerender({
|
|
285
|
+
apiURL: 'https://api.example.com',
|
|
286
|
+
token: 'stable-token',
|
|
287
|
+
lang: 'fr',
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
expect(mockSetupSDK).toHaveBeenCalledTimes(1);
|
|
291
|
+
});
|
|
292
|
+
});
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'preact/hooks';
|
|
2
|
+
import { setUiConfig } from '../shared/uiConfig/uiConfig';
|
|
3
|
+
import { createFlowManager, fetchTheme, setupSDK } from './flowInit';
|
|
4
|
+
import { consumePreloadedData, getPreloadedData } from './preloadFlow';
|
|
5
|
+
|
|
6
|
+
type FlowManager = ReturnType<typeof createFlowManager>;
|
|
7
|
+
|
|
8
|
+
type FlowInitializationOptions = {
|
|
9
|
+
apiURL: string;
|
|
10
|
+
token: string;
|
|
11
|
+
lang?: string;
|
|
12
|
+
disableDashboardTheme?: boolean;
|
|
13
|
+
onError?: (error: string) => void;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
type FlowInitializationState =
|
|
17
|
+
| { status: 'initializing'; flowManager: FlowManager }
|
|
18
|
+
| { status: 'ready'; flowManager: FlowManager }
|
|
19
|
+
| { status: 'error'; error: string; flowManager: FlowManager };
|
|
20
|
+
|
|
21
|
+
export function useFlowInitialization(
|
|
22
|
+
options: FlowInitializationOptions,
|
|
23
|
+
): FlowInitializationState {
|
|
24
|
+
const { apiURL, token, lang, disableDashboardTheme, onError } = options;
|
|
25
|
+
|
|
26
|
+
const flowManagerRef = useRef<FlowManager | null>(null);
|
|
27
|
+
|
|
28
|
+
const [state, setState] = useState<FlowInitializationState>(() => {
|
|
29
|
+
const preloaded = getPreloadedData(token);
|
|
30
|
+
|
|
31
|
+
if (preloaded?.status === 'ready') {
|
|
32
|
+
const consumed = consumePreloadedData(token);
|
|
33
|
+
if (consumed) {
|
|
34
|
+
flowManagerRef.current = consumed.flowManager;
|
|
35
|
+
if (consumed.uiConfig) {
|
|
36
|
+
setUiConfig(consumed.uiConfig);
|
|
37
|
+
}
|
|
38
|
+
return { status: 'ready', flowManager: consumed.flowManager };
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const flowManager = createFlowManager();
|
|
43
|
+
flowManagerRef.current = flowManager;
|
|
44
|
+
|
|
45
|
+
if (preloaded?.status === 'error') {
|
|
46
|
+
return {
|
|
47
|
+
status: 'error',
|
|
48
|
+
error: preloaded.error ?? 'Preload failed',
|
|
49
|
+
flowManager,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return { status: 'initializing', flowManager };
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const initStartedRef = useRef(false);
|
|
57
|
+
|
|
58
|
+
useEffect(() => {
|
|
59
|
+
if (state.status !== 'initializing') {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const preloaded = getPreloadedData(token);
|
|
64
|
+
|
|
65
|
+
if (preloaded?.status === 'loading') {
|
|
66
|
+
preloaded.readyPromise
|
|
67
|
+
.then(() => {
|
|
68
|
+
const consumed = consumePreloadedData(token);
|
|
69
|
+
if (consumed) {
|
|
70
|
+
flowManagerRef.current = consumed.flowManager;
|
|
71
|
+
if (consumed.uiConfig) {
|
|
72
|
+
setUiConfig(consumed.uiConfig);
|
|
73
|
+
}
|
|
74
|
+
setState({ status: 'ready', flowManager: consumed.flowManager });
|
|
75
|
+
}
|
|
76
|
+
})
|
|
77
|
+
.catch((error) => {
|
|
78
|
+
const errorMessage =
|
|
79
|
+
error instanceof Error ? error.message : 'Preload failed';
|
|
80
|
+
setState({
|
|
81
|
+
status: 'error',
|
|
82
|
+
error: errorMessage,
|
|
83
|
+
flowManager: state.flowManager,
|
|
84
|
+
});
|
|
85
|
+
onError?.(errorMessage);
|
|
86
|
+
});
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (initStartedRef.current) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
93
|
+
initStartedRef.current = true;
|
|
94
|
+
|
|
95
|
+
const initialize = async () => {
|
|
96
|
+
try {
|
|
97
|
+
await setupSDK({ apiURL, token, lang });
|
|
98
|
+
|
|
99
|
+
const themePromise = disableDashboardTheme
|
|
100
|
+
? Promise.resolve()
|
|
101
|
+
: fetchTheme().then((result) => {
|
|
102
|
+
if (result.uiConfig) {
|
|
103
|
+
setUiConfig(result.uiConfig);
|
|
104
|
+
}
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
state.flowManager.load();
|
|
108
|
+
|
|
109
|
+
await themePromise;
|
|
110
|
+
|
|
111
|
+
setState({ status: 'ready', flowManager: state.flowManager });
|
|
112
|
+
} catch (error) {
|
|
113
|
+
const errorMessage =
|
|
114
|
+
error instanceof Error ? error.message : 'Failed to initialize SDK';
|
|
115
|
+
setState({
|
|
116
|
+
status: 'error',
|
|
117
|
+
error: errorMessage,
|
|
118
|
+
flowManager: state.flowManager,
|
|
119
|
+
});
|
|
120
|
+
onError?.(errorMessage);
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
initialize();
|
|
125
|
+
}, [apiURL, token, lang, disableDashboardTheme, onError, state]);
|
|
126
|
+
|
|
127
|
+
return state;
|
|
128
|
+
}
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
import { renderHook, waitFor } from '@testing-library/preact';
|
|
2
|
+
import type { FC } from 'preact/compat';
|
|
3
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
4
|
+
|
|
5
|
+
const MockPhoneComponent: FC = () => <div>Phone</div>;
|
|
6
|
+
const MockEmailComponent: FC = () => <div>Email</div>;
|
|
7
|
+
const MockSelfieComponent: FC = () => <div>Selfie</div>;
|
|
8
|
+
|
|
9
|
+
vi.mock('./flowInit', () => ({
|
|
10
|
+
LAZY_UI_MODULES: {
|
|
11
|
+
PHONE: vi.fn(() => Promise.resolve(MockPhoneComponent)),
|
|
12
|
+
EMAIL: vi.fn(() => Promise.resolve(MockEmailComponent)),
|
|
13
|
+
SELFIE: vi.fn(() => Promise.resolve(MockSelfieComponent)),
|
|
14
|
+
},
|
|
15
|
+
}));
|
|
16
|
+
|
|
17
|
+
import { LAZY_UI_MODULES } from './flowInit';
|
|
18
|
+
import { useModuleLoader, usePrefetchNextModule } from './useModuleLoader';
|
|
19
|
+
|
|
20
|
+
describe('useModuleLoader', () => {
|
|
21
|
+
beforeEach(() => {
|
|
22
|
+
vi.clearAllMocks();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
vi.resetAllMocks();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it('should return idle state when moduleKey is undefined', () => {
|
|
30
|
+
const { result } = renderHook(() =>
|
|
31
|
+
useModuleLoader({ moduleKey: undefined }),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
expect(result.current.status).toBe('idle');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('should return unsupported state for unknown module', () => {
|
|
38
|
+
const { result } = renderHook(() =>
|
|
39
|
+
useModuleLoader({ moduleKey: 'UNKNOWN_MODULE' }),
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
expect(result.current.status).toBe('unsupported');
|
|
43
|
+
if (result.current.status === 'unsupported') {
|
|
44
|
+
expect(result.current.moduleKey).toBe('UNKNOWN_MODULE');
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('should load PHONE module successfully', async () => {
|
|
49
|
+
const onModuleLoading = vi.fn();
|
|
50
|
+
const onModuleLoaded = vi.fn();
|
|
51
|
+
|
|
52
|
+
const { result } = renderHook(() =>
|
|
53
|
+
useModuleLoader({
|
|
54
|
+
moduleKey: 'PHONE',
|
|
55
|
+
onModuleLoading,
|
|
56
|
+
onModuleLoaded,
|
|
57
|
+
}),
|
|
58
|
+
);
|
|
59
|
+
|
|
60
|
+
expect(onModuleLoading).toHaveBeenCalledWith('PHONE');
|
|
61
|
+
|
|
62
|
+
await waitFor(() => {
|
|
63
|
+
expect(result.current.status).toBe('loaded');
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
if (result.current.status === 'loaded') {
|
|
67
|
+
expect(result.current.moduleKey).toBe('PHONE');
|
|
68
|
+
expect(result.current.Component).toBe(MockPhoneComponent);
|
|
69
|
+
}
|
|
70
|
+
expect(onModuleLoaded).toHaveBeenCalledWith('PHONE');
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it('should handle module load error', async () => {
|
|
74
|
+
const loadError = new Error('Failed to load');
|
|
75
|
+
vi.mocked(LAZY_UI_MODULES.EMAIL).mockRejectedValueOnce(loadError);
|
|
76
|
+
|
|
77
|
+
const onError = vi.fn();
|
|
78
|
+
|
|
79
|
+
const { result } = renderHook(() =>
|
|
80
|
+
useModuleLoader({
|
|
81
|
+
moduleKey: 'EMAIL',
|
|
82
|
+
onError,
|
|
83
|
+
}),
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
await waitFor(() => {
|
|
87
|
+
expect(result.current.status).toBe('error');
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
if (result.current.status === 'error') {
|
|
91
|
+
expect(result.current.error).toBe('Failed to load');
|
|
92
|
+
}
|
|
93
|
+
expect(onError).toHaveBeenCalledWith(
|
|
94
|
+
'Failed to load EMAIL: Failed to load',
|
|
95
|
+
);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('should cancel loading when moduleKey changes', async () => {
|
|
99
|
+
const onModuleLoaded = vi.fn();
|
|
100
|
+
|
|
101
|
+
let resolvePhone: ((value: unknown) => void) | undefined;
|
|
102
|
+
const phonePromise = new Promise((resolve) => {
|
|
103
|
+
resolvePhone = resolve;
|
|
104
|
+
});
|
|
105
|
+
vi.mocked(LAZY_UI_MODULES.PHONE).mockReturnValueOnce(phonePromise as never);
|
|
106
|
+
|
|
107
|
+
const { rerender } = renderHook(
|
|
108
|
+
({ moduleKey }) =>
|
|
109
|
+
useModuleLoader({
|
|
110
|
+
moduleKey,
|
|
111
|
+
onModuleLoaded,
|
|
112
|
+
}),
|
|
113
|
+
{ initialProps: { moduleKey: 'PHONE' } },
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
rerender({ moduleKey: 'EMAIL' });
|
|
117
|
+
|
|
118
|
+
resolvePhone?.(MockPhoneComponent);
|
|
119
|
+
|
|
120
|
+
await waitFor(() => {
|
|
121
|
+
expect(onModuleLoaded).toHaveBeenCalledWith('EMAIL');
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
expect(onModuleLoaded).not.toHaveBeenCalledWith('PHONE');
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('should reset to idle when moduleKey becomes undefined', async () => {
|
|
128
|
+
const { result, rerender } = renderHook(
|
|
129
|
+
({ moduleKey }) => useModuleLoader({ moduleKey }),
|
|
130
|
+
{ initialProps: { moduleKey: 'PHONE' as string | undefined } },
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
await waitFor(() => {
|
|
134
|
+
expect(result.current.status).toBe('loaded');
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
rerender({ moduleKey: undefined });
|
|
138
|
+
|
|
139
|
+
expect(result.current.status).toBe('idle');
|
|
140
|
+
});
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
describe('usePrefetchNextModule', () => {
|
|
144
|
+
beforeEach(() => {
|
|
145
|
+
vi.clearAllMocks();
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
it('should prefetch next module when available', () => {
|
|
149
|
+
const steps = ['PHONE', 'EMAIL', 'SELFIE'];
|
|
150
|
+
|
|
151
|
+
renderHook(() => usePrefetchNextModule(steps, 0));
|
|
152
|
+
|
|
153
|
+
expect(LAZY_UI_MODULES.EMAIL).toHaveBeenCalled();
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
it('should not prefetch when at last step', () => {
|
|
157
|
+
const steps = ['PHONE', 'EMAIL', 'SELFIE'];
|
|
158
|
+
|
|
159
|
+
renderHook(() => usePrefetchNextModule(steps, 2));
|
|
160
|
+
|
|
161
|
+
expect(LAZY_UI_MODULES.PHONE).not.toHaveBeenCalled();
|
|
162
|
+
expect(LAZY_UI_MODULES.EMAIL).not.toHaveBeenCalled();
|
|
163
|
+
expect(LAZY_UI_MODULES.SELFIE).not.toHaveBeenCalled();
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
it('should not prefetch same module twice', () => {
|
|
167
|
+
const steps = ['PHONE', 'EMAIL', 'SELFIE'];
|
|
168
|
+
|
|
169
|
+
const { rerender } = renderHook(
|
|
170
|
+
({ index }) => usePrefetchNextModule(steps, index),
|
|
171
|
+
{ initialProps: { index: 0 } },
|
|
172
|
+
);
|
|
173
|
+
|
|
174
|
+
expect(LAZY_UI_MODULES.EMAIL).toHaveBeenCalledTimes(1);
|
|
175
|
+
|
|
176
|
+
rerender({ index: 0 });
|
|
177
|
+
|
|
178
|
+
expect(LAZY_UI_MODULES.EMAIL).toHaveBeenCalledTimes(1);
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
it('should handle empty steps array', () => {
|
|
182
|
+
expect(() => {
|
|
183
|
+
renderHook(() => usePrefetchNextModule([], 0));
|
|
184
|
+
}).not.toThrow();
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('should handle prefetch failure gracefully', async () => {
|
|
188
|
+
vi.mocked(LAZY_UI_MODULES.EMAIL).mockRejectedValueOnce(
|
|
189
|
+
new Error('Prefetch failed'),
|
|
190
|
+
);
|
|
191
|
+
|
|
192
|
+
const steps = ['PHONE', 'EMAIL', 'SELFIE'];
|
|
193
|
+
|
|
194
|
+
expect(() => {
|
|
195
|
+
renderHook(() => usePrefetchNextModule(steps, 0));
|
|
196
|
+
}).not.toThrow();
|
|
197
|
+
|
|
198
|
+
expect(LAZY_UI_MODULES.EMAIL).toHaveBeenCalledTimes(1);
|
|
199
|
+
|
|
200
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('should skip prefetch for unsupported modules', () => {
|
|
204
|
+
const steps = ['PHONE', 'UNKNOWN_MODULE', 'SELFIE'];
|
|
205
|
+
|
|
206
|
+
renderHook(() => usePrefetchNextModule(steps, 0));
|
|
207
|
+
|
|
208
|
+
expect(LAZY_UI_MODULES.PHONE).not.toHaveBeenCalled();
|
|
209
|
+
expect(LAZY_UI_MODULES.EMAIL).not.toHaveBeenCalled();
|
|
210
|
+
expect(LAZY_UI_MODULES.SELFIE).not.toHaveBeenCalled();
|
|
211
|
+
});
|
|
212
|
+
});
|