-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Expand file tree
/
Copy pathHeadlessHost.test.tsx
More file actions
437 lines (402 loc) · 15.6 KB
/
HeadlessHost.test.tsx
File metadata and controls
437 lines (402 loc) · 15.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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
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
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
209
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
288
289
290
291
292
293
294
295
296
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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import React from 'react';
import { act, fireEvent, screen, waitFor } from '@testing-library/react-native';
import HeadlessHost, {
HEADLESS_HOST_BACK_BUTTON_TEST_ID,
HEADLESS_HOST_CANCEL_BUTTON_TEST_ID,
HEADLESS_HOST_LOADER_TEST_ID,
HEADLESS_HOST_NO_SESSION_TEST_ID,
createHeadlessHostNavDetails,
type HeadlessHostParams,
} from './HeadlessHost';
import {
__resetSessionRegistryForTests,
closeSession,
createSession,
getSession,
} from '../../headless/sessionRegistry';
import type { Quote } from '../../types';
import { renderScreen } from '../../../../../util/test/renderWithProvider';
import Routes from '../../../../../constants/navigation/Routes';
// =============================================================================
// Mocks
// =============================================================================
// HeadlessHost is heavily orchestrated; mock every hook it consumes so the
// suite focuses on the orchestration logic itself (which paths fire, with
// what context, and how status / error guards interact). Integration with
// the real `useContinueWithQuote`, `useRampsPaymentMethods`, etc. is covered
// in their own test suites.
const mockGoBack = jest.fn();
const mockNavigate = jest.fn();
const mockContinueWithQuote = jest.fn();
const mockUseContinueWithQuoteOptions = jest.fn();
jest.mock('@react-navigation/native', () => {
const actual = jest.requireActual('@react-navigation/native');
return {
...actual,
useNavigation: () => ({
goBack: mockGoBack,
navigate: mockNavigate,
}),
};
});
jest.mock('../../hooks/useContinueWithQuote', () => ({
__esModule: true,
default: (options?: unknown) => {
mockUseContinueWithQuoteOptions(options);
return { continueWithQuote: mockContinueWithQuote };
},
}));
const mockUseRampAccountAddress = jest.fn();
jest.mock('../../hooks/useRampAccountAddress', () => ({
__esModule: true,
default: (chainId: unknown) => mockUseRampAccountAddress(chainId),
}));
const mockUseRampsUserRegion = jest.fn();
jest.mock('../../hooks/useRampsUserRegion', () => ({
useRampsUserRegion: () => mockUseRampsUserRegion(),
}));
const mockUseRampsPaymentMethods = jest.fn();
jest.mock('../../hooks/useRampsPaymentMethods', () => ({
useRampsPaymentMethods: () => mockUseRampsPaymentMethods(),
}));
// Logger is fire-and-forget; silence it here so failed-path assertions stay
// readable.
jest.mock('../../../../../util/Logger', () => ({
__esModule: true,
default: { error: jest.fn(), log: jest.fn() },
}));
// =============================================================================
// Fixtures
// =============================================================================
const SAMPLE_ASSET_ID = 'eip155:59144/erc20:0xabc';
const SAMPLE_AMOUNT = 25;
function buildAggregatorQuote(overrides: Partial<Quote> = {}): Quote {
return {
provider: '/providers/moonpay',
quote: {
amountIn: SAMPLE_AMOUNT,
amountOut: 0.01,
paymentMethod: '/payments/debit-credit-card',
cryptoTranslation: { symbol: 'mUSD' },
},
providerInfo: {
id: '/providers/moonpay',
name: 'MoonPay',
type: 'aggregator',
},
...overrides,
} as unknown as Quote;
}
function buildNativeQuote(overrides: Partial<Quote> = {}): Quote {
return {
provider: '/providers/transak-native',
quote: {
amountIn: SAMPLE_AMOUNT,
amountOut: 0.011,
paymentMethod: '/payments/debit-credit-card',
cryptoTranslation: { symbol: 'mUSD' },
},
providerInfo: {
id: '/providers/transak-native',
name: 'Transak',
type: 'native',
},
...overrides,
} as unknown as Quote;
}
function buildCallbacks() {
return {
onOrderCreated: jest.fn(),
onError: jest.fn(),
onClose: jest.fn(),
};
}
function seedSession(
quote: Quote,
paramsOverrides: Record<string, unknown> = {},
) {
return createSession(
{
quote,
assetId: SAMPLE_ASSET_ID,
amount: SAMPLE_AMOUNT,
...paramsOverrides,
} as never,
buildCallbacks(),
);
}
function renderHost(initialParams: Partial<HeadlessHostParams> = {}) {
return renderScreen(
HeadlessHost,
{ name: Routes.RAMP.HEADLESS_HOST },
{ state: {} },
initialParams as Record<string, unknown>,
);
}
// =============================================================================
// Suite
// =============================================================================
describe('HeadlessHost', () => {
beforeEach(() => {
jest.clearAllMocks();
__resetSessionRegistryForTests();
mockUseRampAccountAddress.mockReturnValue('0xWALLET');
mockUseRampsUserRegion.mockReturnValue({
userRegion: { country: { currency: 'EUR' } },
});
mockUseRampsPaymentMethods.mockReturnValue({
paymentMethods: [
{ id: '/payments/debit-credit-card', name: 'Debit / Credit Card' },
{ id: '/payments/bank-transfer', name: 'Bank transfer' },
],
});
mockContinueWithQuote.mockResolvedValue(undefined);
});
describe('Static surface', () => {
it('exposes a navigation helper that targets HEADLESS_HOST', () => {
const [name, params] = createHeadlessHostNavDetails({
headlessSessionId: 'headless-buy-abc',
});
expect(name).toBe(Routes.RAMP.HEADLESS_HOST);
expect(params).toEqual({ headlessSessionId: 'headless-buy-abc' });
});
it('renders the no-session message when the session id is unknown', () => {
renderHost({ headlessSessionId: 'headless-buy-not-real' });
expect(
screen.getByTestId(HEADLESS_HOST_NO_SESSION_TEST_ID),
).toBeOnTheScreen();
expect(
screen.queryByTestId(HEADLESS_HOST_LOADER_TEST_ID),
).not.toBeOnTheScreen();
expect(mockContinueWithQuote).not.toHaveBeenCalled();
});
it('renders the loader while a matching session is being orchestrated', () => {
// Make continueWithQuote hang so the loader stays on screen.
mockContinueWithQuote.mockImplementation(
() => new Promise(() => undefined),
);
const session = seedSession(buildAggregatorQuote());
renderHost({ headlessSessionId: session.id });
expect(
screen.getByTestId(HEADLESS_HOST_LOADER_TEST_ID),
).toBeOnTheScreen();
});
it('navigates back when the cancel button is pressed', () => {
renderHost();
fireEvent.press(screen.getByTestId(HEADLESS_HOST_CANCEL_BUTTON_TEST_ID));
expect(mockGoBack).toHaveBeenCalledTimes(1);
});
it('navigates back when the header back button is pressed', () => {
renderHost();
fireEvent.press(screen.getByTestId(HEADLESS_HOST_BACK_BUTTON_TEST_ID));
expect(mockGoBack).toHaveBeenCalledTimes(1);
});
});
describe('useTransakRouting wiring', () => {
it('pins the inner useContinueWithQuote to HEADLESS_HOST as the routing base', () => {
const session = seedSession(buildNativeQuote());
renderHost({ headlessSessionId: session.id });
expect(mockUseContinueWithQuoteOptions).toHaveBeenCalledWith({
transakRouting: {
baseRoute: Routes.RAMP.HEADLESS_HOST,
baseRouteParams: { headlessSessionId: session.id },
},
});
});
});
describe('Continue-on-focus orchestration', () => {
it('calls continueWithQuote once with a context derived from the session quote (aggregator)', async () => {
const quote = buildAggregatorQuote();
const session = seedSession(quote);
renderHost({ headlessSessionId: session.id });
await waitFor(() =>
expect(mockContinueWithQuote).toHaveBeenCalledTimes(1),
);
const [calledQuote, ctx] = mockContinueWithQuote.mock.calls[0];
expect(calledQuote).toBe(quote);
expect(ctx).toEqual({
amount: SAMPLE_AMOUNT,
assetId: SAMPLE_ASSET_ID,
chainId: 'eip155:59144',
walletAddress: '0xWALLET',
currency: 'EUR',
cryptoSymbol: 'mUSD',
paymentMethodId: '/payments/debit-credit-card',
providerName: 'MoonPay',
headlessSessionId: session.id,
});
});
it('calls continueWithQuote once for a native quote and flips the session status to `continued`', async () => {
const quote = buildNativeQuote();
const session = seedSession(quote);
renderHost({ headlessSessionId: session.id });
await waitFor(() =>
expect(mockContinueWithQuote).toHaveBeenCalledTimes(1),
);
// Status flip is what guards the auth-loop re-focus path: when
// OtpCode resets back to [HEADLESS_HOST, KycWebview], focus on the
// host briefly fires but session.status === 'continued' makes the
// effect a no-op. Asserting the flip lets us trust that contract
// without forcing a real re-focus inside this unit suite.
expect(getSession(session.id)?.status).toBe('continued');
});
it('honors an explicit paymentMethodId override from the session params instead of resolving from the quote', async () => {
const quote = buildNativeQuote();
const session = seedSession(quote, {
paymentMethodId: '/payments/bank-transfer',
currency: 'USD',
});
renderHost({ headlessSessionId: session.id });
await waitFor(() =>
expect(mockContinueWithQuote).toHaveBeenCalledTimes(1),
);
const [, ctx] = mockContinueWithQuote.mock.calls[0];
expect(ctx.paymentMethodId).toBe('/payments/bank-transfer');
expect(ctx.currency).toBe('USD');
});
it('falls back to userRegion currency when the session does not pin one', async () => {
const quote = buildAggregatorQuote();
const session = seedSession(quote);
renderHost({ headlessSessionId: session.id });
await waitFor(() =>
expect(mockContinueWithQuote).toHaveBeenCalledTimes(1),
);
const [, ctx] = mockContinueWithQuote.mock.calls[0];
expect(ctx.currency).toBe('EUR');
});
it('skips orchestration when the session has already been cancelled', async () => {
const quote = buildAggregatorQuote();
const session = seedSession(quote);
// Mark the session terminal *before* the screen mounts: the focus
// effect must respect that and avoid a stale re-trigger.
closeSession(session.id, { reason: 'consumer_cancelled' });
renderHost({ headlessSessionId: session.id });
// No session left → the no-session branch renders, continueWithQuote
// is never called.
expect(
screen.getByTestId(HEADLESS_HOST_NO_SESSION_TEST_ID),
).toBeOnTheScreen();
expect(mockContinueWithQuote).not.toHaveBeenCalled();
});
});
describe('Error handling', () => {
it('forwards a malformed assetId as onError(UNKNOWN, ...) and closes the session', async () => {
// Real hook: falsy chain id → null wallet. The invalid-assetId branch
// must run before the wallet deferral or the effect would return early
// forever (regression guard for guard ordering).
mockUseRampAccountAddress.mockReturnValue(null);
const quote = buildAggregatorQuote();
const session = seedSession(quote, { assetId: 'not-a-caip-19' });
const callbacks = session.callbacks;
renderHost({ headlessSessionId: session.id });
expect(callbacks.onError).toHaveBeenCalledWith({
code: 'UNKNOWN',
message: expect.stringContaining('not-a-caip-19'),
});
expect(callbacks.onClose).toHaveBeenCalledWith({ reason: 'unknown' });
expect(getSession(session.id)).toBeUndefined();
expect(mockContinueWithQuote).not.toHaveBeenCalled();
await waitFor(() => {
expect(screen.getByText(/not-a-caip-19/)).toBeOnTheScreen();
});
});
it('surfaces a continueWithQuote rejection as onError(UNKNOWN, ...) and renders the message', async () => {
mockContinueWithQuote.mockRejectedValueOnce(new Error('quote expired'));
const quote = buildAggregatorQuote();
const session = seedSession(quote);
const callbacks = session.callbacks;
renderHost({ headlessSessionId: session.id });
await waitFor(() => {
expect(callbacks.onError).toHaveBeenCalledWith({
code: 'UNKNOWN',
message: 'quote expired',
});
});
expect(callbacks.onClose).toHaveBeenCalledWith({ reason: 'unknown' });
expect(getSession(session.id)).toBeUndefined();
expect(screen.getByText('quote expired')).toBeOnTheScreen();
});
it('surfaces limit failures as onError(LIMIT_EXCEEDED, ...)', async () => {
const limitError = new Error('Daily limit exceeded');
limitError.name = 'LimitExceededError';
mockContinueWithQuote.mockRejectedValueOnce(limitError);
const quote = buildNativeQuote();
const session = seedSession(quote);
const callbacks = session.callbacks;
renderHost({ headlessSessionId: session.id });
await waitFor(() => {
expect(callbacks.onError).toHaveBeenCalledWith({
code: 'LIMIT_EXCEEDED',
message: 'Daily limit exceeded',
});
});
expect(callbacks.onClose).toHaveBeenCalledWith({ reason: 'unknown' });
expect(getSession(session.id)).toBeUndefined();
expect(screen.getByText('Daily limit exceeded')).toBeOnTheScreen();
});
it('does not run the continueWithQuote rejection path after unmount', async () => {
let rejectDeferred: ((error: Error) => void) | undefined;
mockContinueWithQuote.mockImplementation(
() =>
new Promise((_resolve, reject) => {
rejectDeferred = reject;
}),
);
const quote = buildAggregatorQuote();
const session = seedSession(quote);
const callbacks = session.callbacks;
const { unmount } = renderHost({ headlessSessionId: session.id });
await waitFor(() =>
expect(mockContinueWithQuote).toHaveBeenCalledTimes(1),
);
unmount();
await act(async () => {
rejectDeferred?.(new Error('late failure'));
});
expect(callbacks.onError).not.toHaveBeenCalled();
expect(callbacks.onClose).not.toHaveBeenCalled();
expect(getSession(session.id)?.status).toBe('continued');
});
it('forwards a nativeFlowError param as onError(AUTH_FAILED, ...), renders it, and closes the session', () => {
const quote = buildNativeQuote();
const session = seedSession(quote);
const callbacks = session.callbacks;
renderHost({
headlessSessionId: session.id,
nativeFlowError: 'OTP rejected',
});
expect(callbacks.onError).toHaveBeenCalledWith({
code: 'AUTH_FAILED',
message: 'OTP rejected',
});
expect(callbacks.onClose).toHaveBeenCalledWith({ reason: 'unknown' });
expect(getSession(session.id)).toBeUndefined();
// The auth-error path also short-circuits the continue-on-focus effect
// — we never want to push EnterEmail again on top of the error message.
expect(mockContinueWithQuote).not.toHaveBeenCalled();
expect(screen.getByText('OTP rejected')).toBeOnTheScreen();
});
it('does not crash when the consumer onError callback throws', async () => {
mockContinueWithQuote.mockRejectedValueOnce(new Error('boom'));
const quote = buildAggregatorQuote();
const session = createSession(
{
quote,
assetId: SAMPLE_ASSET_ID,
amount: SAMPLE_AMOUNT,
} as never,
{
onOrderCreated: jest.fn(),
onError: jest.fn(() => {
throw new Error('consumer is bad');
}),
onClose: jest.fn(),
},
);
renderHost({ headlessSessionId: session.id });
// Even though onError throws, the close path still runs and the
// session is gone from the registry.
await waitFor(() => expect(getSession(session.id)).toBeUndefined());
});
});
});