@infosoftas/s4-orders-js-sdk 0.1.21 → 0.2.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/.editorconfig +0 -0
- package/.release-it.json +0 -0
- package/README.md +572 -471
- package/build/postcss-scope-tailwind.cjs +0 -0
- package/components.json +0 -0
- package/package.json +62 -58
- package/src/App.completion.test.tsx +677 -0
- package/src/App.css +0 -14
- package/src/App.paymentReturn.test.tsx +341 -0
- package/src/App.tsx +722 -639
- package/src/ErrorBoundary.tsx +0 -0
- package/src/api/InvoiceApi.ts +0 -0
- package/src/api/OrdersApi.ts +74 -59
- package/src/api/SubscribeApi.ts +0 -0
- package/src/bundle.ts +0 -0
- package/src/components/Alert/Alert.tsx +0 -0
- package/src/components/Button/Button.tsx +0 -0
- package/src/components/EHFForm/EHFForm.tsx +250 -253
- package/src/components/FormFields/AddressField.tsx +0 -0
- package/src/components/FormFields/EmailField.tsx +0 -0
- package/src/components/FormFields/FormFields.helper.ts +0 -0
- package/src/components/FormFields/FormFieldsMapper.ts +0 -0
- package/src/components/FormFields/InputField.tsx +0 -0
- package/src/components/FormFields/NameField.tsx +0 -0
- package/src/components/FormFields/PhoneField.tsx +0 -0
- package/src/components/FormFields/TermsCheckbox.tsx +0 -0
- package/src/components/FormFields/ToggleFiled.tsx +0 -0
- package/src/components/FormFields/fieldValidators.ts +0 -0
- package/src/components/OIOForm/OIOForm.tsx +304 -307
- package/src/components/OrderDenialModal/OrderDenialModal.tsx +0 -0
- package/src/components/OrderForm/ContactDetailsSection.tsx +0 -0
- package/src/components/OrderForm/OrderForm.tsx +417 -420
- package/src/components/OrderForm/PaymentMethodContent.tsx +0 -0
- package/src/components/OrderForm/PaymentMethodSection.tsx +0 -0
- package/src/components/OrderForm/SubmitConfirmationDialog.tsx +0 -0
- package/src/components/OrderForm/orderFormState.ts +0 -0
- package/src/components/OrderForm/useSlotElement.tsx +0 -0
- package/src/components/ui/alert-dialog.tsx +0 -0
- package/src/components/ui/alert.tsx +0 -0
- package/src/components/ui/button.tsx +0 -0
- package/src/components/ui/card.tsx +0 -0
- package/src/components/ui/checkbox.tsx +0 -0
- package/src/components/ui/field.tsx +0 -0
- package/src/components/ui/input.tsx +0 -0
- package/src/components/ui/label.tsx +0 -0
- package/src/components/ui/radio-group.tsx +0 -0
- package/src/components/ui/separator.tsx +0 -0
- package/src/components/ui/skeleton.tsx +0 -0
- package/src/components/ui/spinner.tsx +0 -0
- package/src/components/ui/switch.tsx +0 -0
- package/src/components/ui/textarea.tsx +0 -0
- package/src/constants/index.ts +0 -0
- package/src/custom.d.ts +0 -0
- package/src/enums/api.ts +0 -0
- package/src/enums/general.ts +42 -40
- package/src/hooks/useOrderForm.tsx +400 -403
- package/src/hooks/usePaymentReturn.tsx +139 -0
- package/src/hooks/useQueryParams.ts +0 -0
- package/src/index.tsx +0 -0
- package/src/lib/utils.ts +0 -0
- package/src/types/api.ts +0 -0
- package/src/types/general.ts +153 -136
- package/src/types/order.ts +91 -70
- package/src/utils/fetcher.ts +131 -113
- package/src/utils/helper.ts +0 -0
- package/src/utils/order.helper.test.ts +81 -0
- package/src/utils/order.helper.ts +285 -272
- package/src/utils/waitForOrderCompletion.ts +125 -0
- package/tsconfig.json +39 -38
- package/src/components/MainIframe/MainIframe.tsx +0 -42
- package/src/hooks/useIframe.tsx +0 -59
- package/src/hooks/useMessageEvent.tsx +0 -194
|
@@ -0,0 +1,677 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
act,
|
|
5
|
+
cleanup,
|
|
6
|
+
fireEvent,
|
|
7
|
+
render,
|
|
8
|
+
screen,
|
|
9
|
+
} from '@testing-library/react';
|
|
10
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
11
|
+
|
|
12
|
+
import App from './App';
|
|
13
|
+
import { MessageEventTypeEnum } from './enums/general';
|
|
14
|
+
import type { ConfigType } from './types/general';
|
|
15
|
+
import type { OrderInfoType } from './types/order';
|
|
16
|
+
|
|
17
|
+
const submission = vi.hoisted(() => ({
|
|
18
|
+
url: null as string | null,
|
|
19
|
+
data: {
|
|
20
|
+
orderId: 'invoice-order',
|
|
21
|
+
paymentMethod: 'Invoice',
|
|
22
|
+
subscriberId: 'subscriber-7',
|
|
23
|
+
} as OrderInfoType,
|
|
24
|
+
}));
|
|
25
|
+
|
|
26
|
+
const invoiceSubmission = submission.data;
|
|
27
|
+
|
|
28
|
+
vi.mock('./components/OrderForm/OrderForm', () => ({
|
|
29
|
+
default: ({
|
|
30
|
+
callback,
|
|
31
|
+
}: {
|
|
32
|
+
callback: (url: string | null, data: OrderInfoType) => void;
|
|
33
|
+
}) => (
|
|
34
|
+
<button onClick={() => callback(submission.url, submission.data)}>
|
|
35
|
+
Submit invoice
|
|
36
|
+
</button>
|
|
37
|
+
),
|
|
38
|
+
}));
|
|
39
|
+
|
|
40
|
+
const response = (body: unknown, status = 200, headers?: HeadersInit) =>
|
|
41
|
+
new Response(JSON.stringify(body), { status, headers });
|
|
42
|
+
|
|
43
|
+
const fetchMock = vi.fn<typeof fetch>();
|
|
44
|
+
const settings = {
|
|
45
|
+
successText: 'Order completed',
|
|
46
|
+
failureText: 'Order failed',
|
|
47
|
+
orderProcessingText: 'Processing order',
|
|
48
|
+
orderReceivedText: 'Order received',
|
|
49
|
+
orderDelayedText: 'Order received, taking longer than expected',
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const renderApp = (props: Partial<ConfigType> = {}) =>
|
|
53
|
+
render(
|
|
54
|
+
<App
|
|
55
|
+
apiKey="tenant-key"
|
|
56
|
+
apiUrl="https://orders.example.test"
|
|
57
|
+
domElementId="sdk-order"
|
|
58
|
+
templatePackageId="template-1"
|
|
59
|
+
subscriberId=""
|
|
60
|
+
userId=""
|
|
61
|
+
identityProviderId=""
|
|
62
|
+
organizationId="organization-1"
|
|
63
|
+
settings={settings}
|
|
64
|
+
{...props}
|
|
65
|
+
/>
|
|
66
|
+
);
|
|
67
|
+
|
|
68
|
+
// A SwedbankPay full-page return. Must be set before the SDK mounts, because
|
|
69
|
+
// returns are read from the URL on load.
|
|
70
|
+
const paymentReturn = () =>
|
|
71
|
+
window.history.replaceState(
|
|
72
|
+
{},
|
|
73
|
+
'',
|
|
74
|
+
'/?action=complete&S4OrderId=returned-order&TransactionId=agreement-3'
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const methods = () => fetchMock.mock.calls.map(([, init]) => init?.method);
|
|
78
|
+
|
|
79
|
+
const advance = async (ms = 0) => {
|
|
80
|
+
await act(async () => {
|
|
81
|
+
await vi.advanceTimersByTimeAsync(ms);
|
|
82
|
+
});
|
|
83
|
+
};
|
|
84
|
+
|
|
85
|
+
const completionMessages = () =>
|
|
86
|
+
vi
|
|
87
|
+
.mocked(window.postMessage)
|
|
88
|
+
.mock.calls.map(([message]) => message as { type?: string })
|
|
89
|
+
.filter(
|
|
90
|
+
(message) =>
|
|
91
|
+
message.type === MessageEventTypeEnum.ORDER_FLOW_COMPLETE
|
|
92
|
+
);
|
|
93
|
+
|
|
94
|
+
describe('SDK-managed order completion', () => {
|
|
95
|
+
beforeEach(() => {
|
|
96
|
+
vi.useFakeTimers();
|
|
97
|
+
fetchMock.mockReset();
|
|
98
|
+
vi.stubGlobal('fetch', fetchMock);
|
|
99
|
+
window.history.replaceState({}, '', '/');
|
|
100
|
+
sessionStorage.clear();
|
|
101
|
+
localStorage.clear();
|
|
102
|
+
submission.url = null;
|
|
103
|
+
submission.data = invoiceSubmission;
|
|
104
|
+
vi.spyOn(window, 'postMessage').mockImplementation(() => undefined);
|
|
105
|
+
vi.spyOn(console, 'log').mockImplementation(() => undefined);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
afterEach(() => {
|
|
109
|
+
cleanup();
|
|
110
|
+
vi.useRealTimers();
|
|
111
|
+
vi.restoreAllMocks();
|
|
112
|
+
vi.unstubAllGlobals();
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('waits for Completed and emits the exact submitted ID after a full redirect', async () => {
|
|
116
|
+
fetchMock
|
|
117
|
+
.mockResolvedValueOnce(
|
|
118
|
+
response({ id: 'returned-order', status: 'InProgress' })
|
|
119
|
+
)
|
|
120
|
+
.mockResolvedValueOnce(
|
|
121
|
+
response({ id: 'returned-order', status: 'InProgress' })
|
|
122
|
+
)
|
|
123
|
+
.mockResolvedValueOnce(
|
|
124
|
+
response({ id: 'returned-order', status: 'Completed' })
|
|
125
|
+
);
|
|
126
|
+
const orderAcceptedCallback = vi.fn();
|
|
127
|
+
paymentReturn();
|
|
128
|
+
renderApp({
|
|
129
|
+
orderAcceptedCallback,
|
|
130
|
+
userId: 'user-9',
|
|
131
|
+
identityProviderId: 'identity-2',
|
|
132
|
+
});
|
|
133
|
+
await advance();
|
|
134
|
+
|
|
135
|
+
expect(screen.queryByText('Order completed')).toBeNull();
|
|
136
|
+
expect(screen.getByText('Processing order')).toBeTruthy();
|
|
137
|
+
expect(completionMessages()).toHaveLength(0);
|
|
138
|
+
expect(orderAcceptedCallback).toHaveBeenCalledWith({
|
|
139
|
+
orderId: 'returned-order',
|
|
140
|
+
orderInfo: null,
|
|
141
|
+
});
|
|
142
|
+
expect(fetchMock.mock.calls[0][0]).toBe(
|
|
143
|
+
'https://orders.example.test/order/returned-order/complete'
|
|
144
|
+
);
|
|
145
|
+
expect(JSON.parse(fetchMock.mock.calls[0][1]?.body as string)).toEqual({
|
|
146
|
+
userId: 'user-9',
|
|
147
|
+
identityProviderId: 'identity-2',
|
|
148
|
+
});
|
|
149
|
+
expect(fetchMock.mock.calls[1][0]).toBe(
|
|
150
|
+
'https://orders.example.test/order/returned-order'
|
|
151
|
+
);
|
|
152
|
+
expect(
|
|
153
|
+
new Headers(fetchMock.mock.calls[1][1]?.headers).get(
|
|
154
|
+
'S4-ORDERS-API-KEY'
|
|
155
|
+
)
|
|
156
|
+
).toBe('tenant-key');
|
|
157
|
+
await advance(499);
|
|
158
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
159
|
+
await advance(1);
|
|
160
|
+
expect(screen.getByText('Order completed')).toBeTruthy();
|
|
161
|
+
expect(completionMessages()).toEqual([
|
|
162
|
+
expect.objectContaining({
|
|
163
|
+
orderId: 'returned-order',
|
|
164
|
+
isCompleted: true,
|
|
165
|
+
orderInfo: null,
|
|
166
|
+
}),
|
|
167
|
+
]);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it('shows received-but-delayed at ten seconds without success, resubmission or further reads', async () => {
|
|
171
|
+
fetchMock.mockImplementation(async (_url, init) =>
|
|
172
|
+
response({
|
|
173
|
+
id: 'returned-order',
|
|
174
|
+
status: init?.method === 'POST' ? 'Completed' : 'InProgress',
|
|
175
|
+
})
|
|
176
|
+
);
|
|
177
|
+
const orderCompletionDelayedCallback = vi.fn();
|
|
178
|
+
paymentReturn();
|
|
179
|
+
renderApp({ orderCompletionDelayedCallback });
|
|
180
|
+
await advance(9999);
|
|
181
|
+
expect(
|
|
182
|
+
screen.queryByText('Order received, taking longer than expected')
|
|
183
|
+
).toBeNull();
|
|
184
|
+
await advance(1);
|
|
185
|
+
expect(
|
|
186
|
+
screen.getByText('Order received, taking longer than expected')
|
|
187
|
+
).toBeTruthy();
|
|
188
|
+
expect(
|
|
189
|
+
screen.queryByRole('button', { name: 'Submit invoice' })
|
|
190
|
+
).toBeNull();
|
|
191
|
+
expect(orderCompletionDelayedCallback).toHaveBeenCalledExactlyOnceWith({
|
|
192
|
+
orderId: 'returned-order',
|
|
193
|
+
orderInfo: null,
|
|
194
|
+
reason: 'timeout',
|
|
195
|
+
});
|
|
196
|
+
expect(completionMessages()).toHaveLength(0);
|
|
197
|
+
const calls = fetchMock.mock.calls.length;
|
|
198
|
+
await advance(20000);
|
|
199
|
+
expect(fetchMock).toHaveBeenCalledTimes(calls);
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
it('bounds a hung status read and ignores its eventual Completed response', async () => {
|
|
203
|
+
let finish!: (value: Response) => void;
|
|
204
|
+
fetchMock.mockResolvedValueOnce(response(null)).mockImplementationOnce(
|
|
205
|
+
() =>
|
|
206
|
+
new Promise((resolve) => {
|
|
207
|
+
finish = resolve;
|
|
208
|
+
})
|
|
209
|
+
);
|
|
210
|
+
const orderCompletionDelayedCallback = vi.fn();
|
|
211
|
+
paymentReturn();
|
|
212
|
+
renderApp({ orderCompletionDelayedCallback });
|
|
213
|
+
await advance();
|
|
214
|
+
await advance(10000);
|
|
215
|
+
expect(
|
|
216
|
+
screen.getByText('Order received, taking longer than expected')
|
|
217
|
+
).toBeTruthy();
|
|
218
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
219
|
+
expect(fetchMock.mock.calls[1][1]?.signal?.aborted).toBe(true);
|
|
220
|
+
await act(async () => {
|
|
221
|
+
finish(response({ id: 'returned-order', status: 'Completed' }));
|
|
222
|
+
});
|
|
223
|
+
expect(screen.queryByText('Order completed')).toBeNull();
|
|
224
|
+
expect(orderCompletionDelayedCallback).toHaveBeenCalledOnce();
|
|
225
|
+
expect(completionMessages()).toHaveLength(0);
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('lets external mode receive acceptance without polling or announcing success', async () => {
|
|
229
|
+
fetchMock.mockResolvedValueOnce(response(null));
|
|
230
|
+
const orderAcceptedCallback = vi.fn();
|
|
231
|
+
paymentReturn();
|
|
232
|
+
renderApp({ completionMode: 'external', orderAcceptedCallback });
|
|
233
|
+
await advance(20000);
|
|
234
|
+
expect(orderAcceptedCallback).toHaveBeenCalledExactlyOnceWith({
|
|
235
|
+
orderId: 'returned-order',
|
|
236
|
+
orderInfo: null,
|
|
237
|
+
});
|
|
238
|
+
expect(screen.getByText('Order received')).toBeTruthy();
|
|
239
|
+
expect(screen.queryByText('Order completed')).toBeNull();
|
|
240
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
241
|
+
expect(completionMessages()).toHaveLength(0);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it.each(['Cancelled', 'Rejected'])(
|
|
245
|
+
'reports %s without success or reopening submission',
|
|
246
|
+
async (status) => {
|
|
247
|
+
fetchMock
|
|
248
|
+
.mockResolvedValueOnce(response(null))
|
|
249
|
+
.mockResolvedValueOnce(
|
|
250
|
+
response({ id: 'returned-order', status })
|
|
251
|
+
);
|
|
252
|
+
const orderCompletionFailedCallback = vi.fn();
|
|
253
|
+
paymentReturn();
|
|
254
|
+
renderApp({ orderCompletionFailedCallback });
|
|
255
|
+
await advance(10000);
|
|
256
|
+
expect(screen.getByText('Order failed')).toBeTruthy();
|
|
257
|
+
expect(
|
|
258
|
+
screen.queryByRole('button', { name: 'Submit invoice' })
|
|
259
|
+
).toBeNull();
|
|
260
|
+
expect(
|
|
261
|
+
orderCompletionFailedCallback
|
|
262
|
+
).toHaveBeenCalledExactlyOnceWith({
|
|
263
|
+
orderId: 'returned-order',
|
|
264
|
+
orderInfo: null,
|
|
265
|
+
status,
|
|
266
|
+
});
|
|
267
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
268
|
+
expect(completionMessages()).toHaveLength(0);
|
|
269
|
+
}
|
|
270
|
+
);
|
|
271
|
+
|
|
272
|
+
it('does not overlap status reads while one is pending', async () => {
|
|
273
|
+
let finish!: (value: Response) => void;
|
|
274
|
+
fetchMock
|
|
275
|
+
.mockResolvedValueOnce(response(null))
|
|
276
|
+
.mockImplementationOnce(
|
|
277
|
+
() =>
|
|
278
|
+
new Promise((resolve) => {
|
|
279
|
+
finish = resolve;
|
|
280
|
+
})
|
|
281
|
+
)
|
|
282
|
+
.mockResolvedValueOnce(
|
|
283
|
+
response({ id: 'returned-order', status: 'Completed' })
|
|
284
|
+
);
|
|
285
|
+
paymentReturn();
|
|
286
|
+
renderApp();
|
|
287
|
+
await advance(1500);
|
|
288
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
289
|
+
await act(async () => {
|
|
290
|
+
finish(response({ id: 'returned-order', status: 'InProgress' }));
|
|
291
|
+
});
|
|
292
|
+
await advance(499);
|
|
293
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
294
|
+
await advance(1);
|
|
295
|
+
expect(screen.getByText('Order completed')).toBeTruthy();
|
|
296
|
+
expect(fetchMock).toHaveBeenCalledTimes(3);
|
|
297
|
+
expect(methods()).toEqual(['POST', 'GET', 'GET']);
|
|
298
|
+
expect(completionMessages()).toHaveLength(1);
|
|
299
|
+
});
|
|
300
|
+
|
|
301
|
+
it('aborts on unmount without callbacks or a stale success event', async () => {
|
|
302
|
+
let finish!: (value: Response) => void;
|
|
303
|
+
fetchMock.mockResolvedValueOnce(response(null)).mockImplementationOnce(
|
|
304
|
+
() =>
|
|
305
|
+
new Promise((resolve) => {
|
|
306
|
+
finish = resolve;
|
|
307
|
+
})
|
|
308
|
+
);
|
|
309
|
+
const orderCompletionDelayedCallback = vi.fn();
|
|
310
|
+
paymentReturn();
|
|
311
|
+
const view = renderApp({ orderCompletionDelayedCallback });
|
|
312
|
+
await advance();
|
|
313
|
+
view.unmount();
|
|
314
|
+
expect(fetchMock.mock.calls[1][1]?.signal?.aborted).toBe(true);
|
|
315
|
+
await act(async () => {
|
|
316
|
+
finish(response({ id: 'returned-order', status: 'Completed' }));
|
|
317
|
+
});
|
|
318
|
+
await advance(10000);
|
|
319
|
+
expect(orderCompletionDelayedCallback).not.toHaveBeenCalled();
|
|
320
|
+
expect(completionMessages()).toHaveLength(0);
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('uses invoice submission ID and order information without a payment return', async () => {
|
|
324
|
+
fetchMock
|
|
325
|
+
.mockResolvedValueOnce(response(null))
|
|
326
|
+
.mockResolvedValueOnce(
|
|
327
|
+
response({ id: 'invoice-order', status: 'Completed' })
|
|
328
|
+
);
|
|
329
|
+
renderApp();
|
|
330
|
+
fireEvent.click(screen.getByRole('button', { name: 'Submit invoice' }));
|
|
331
|
+
await advance();
|
|
332
|
+
expect(screen.getByText('Order completed')).toBeTruthy();
|
|
333
|
+
expect(completionMessages()).toEqual([
|
|
334
|
+
expect.objectContaining({
|
|
335
|
+
orderId: 'invoice-order',
|
|
336
|
+
orderInfo: {
|
|
337
|
+
orderId: 'invoice-order',
|
|
338
|
+
subscriberId: 'subscriber-7',
|
|
339
|
+
paymentMethod: 'Invoice',
|
|
340
|
+
},
|
|
341
|
+
}),
|
|
342
|
+
]);
|
|
343
|
+
});
|
|
344
|
+
|
|
345
|
+
it('preserves POST validation failures without starting polling or acceptance callbacks', async () => {
|
|
346
|
+
fetchMock.mockResolvedValueOnce(
|
|
347
|
+
response(
|
|
348
|
+
{
|
|
349
|
+
status: 400,
|
|
350
|
+
title: 'Invalid order',
|
|
351
|
+
errors: { email: ['Email is invalid'] },
|
|
352
|
+
},
|
|
353
|
+
400
|
|
354
|
+
)
|
|
355
|
+
);
|
|
356
|
+
const orderAcceptedCallback = vi.fn();
|
|
357
|
+
paymentReturn();
|
|
358
|
+
renderApp({ orderAcceptedCallback });
|
|
359
|
+
await advance();
|
|
360
|
+
expect(screen.getByText('Invalid order')).toBeTruthy();
|
|
361
|
+
expect(screen.getByText('Email is invalid')).toBeTruthy();
|
|
362
|
+
expect(
|
|
363
|
+
screen.getByRole('button', { name: 'Submit invoice' })
|
|
364
|
+
).toBeTruthy();
|
|
365
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
366
|
+
expect(orderAcceptedCallback).not.toHaveBeenCalled();
|
|
367
|
+
expect(completionMessages()).toHaveLength(0);
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
it('preserves Vipps conflict cancellation', async () => {
|
|
371
|
+
fetchMock
|
|
372
|
+
.mockResolvedValueOnce(
|
|
373
|
+
response({ status: 409, title: 'Payment conflict' }, 409)
|
|
374
|
+
)
|
|
375
|
+
.mockResolvedValueOnce(response(null));
|
|
376
|
+
window.history.replaceState(
|
|
377
|
+
{},
|
|
378
|
+
'',
|
|
379
|
+
'/?orderId=returned-order&agreementId=agreement-3'
|
|
380
|
+
);
|
|
381
|
+
const cancelVippsCallback = vi.fn();
|
|
382
|
+
renderApp({ cancelVippsCallback });
|
|
383
|
+
await advance();
|
|
384
|
+
expect(cancelVippsCallback).toHaveBeenCalledOnce();
|
|
385
|
+
expect(
|
|
386
|
+
fetchMock.mock.calls.map(([, options]) => options?.method)
|
|
387
|
+
).toEqual(['POST', 'DELETE']);
|
|
388
|
+
expect(completionMessages()).toHaveLength(0);
|
|
389
|
+
});
|
|
390
|
+
|
|
391
|
+
it('retries network, temporary 404 and 503 errors before confirming completion', async () => {
|
|
392
|
+
fetchMock
|
|
393
|
+
.mockResolvedValueOnce(response(null))
|
|
394
|
+
.mockRejectedValueOnce(new TypeError('Network unavailable'))
|
|
395
|
+
.mockResolvedValueOnce(response({ title: 'Not visible yet' }, 404))
|
|
396
|
+
.mockResolvedValueOnce(response({ title: 'Try later' }, 503))
|
|
397
|
+
.mockResolvedValueOnce(
|
|
398
|
+
response({ id: 'returned-order', status: 'Completed' })
|
|
399
|
+
);
|
|
400
|
+
paymentReturn();
|
|
401
|
+
renderApp();
|
|
402
|
+
await advance(1499);
|
|
403
|
+
expect(completionMessages()).toHaveLength(0);
|
|
404
|
+
await advance(1);
|
|
405
|
+
expect(screen.getByText('Order completed')).toBeTruthy();
|
|
406
|
+
expect(fetchMock).toHaveBeenCalledTimes(5);
|
|
407
|
+
});
|
|
408
|
+
|
|
409
|
+
it.each([
|
|
410
|
+
['seconds', '2'],
|
|
411
|
+
['HTTP date', new Date(Date.UTC(2026, 10, 16, 12, 0, 2)).toUTCString()],
|
|
412
|
+
])('honors Retry-After expressed as %s', async (_label, retryAfter) => {
|
|
413
|
+
vi.setSystemTime(new Date(Date.UTC(2026, 10, 16, 12, 0, 0)));
|
|
414
|
+
fetchMock
|
|
415
|
+
.mockResolvedValueOnce(response(null))
|
|
416
|
+
.mockResolvedValueOnce(
|
|
417
|
+
response(null, 429, { 'Retry-After': retryAfter })
|
|
418
|
+
)
|
|
419
|
+
.mockResolvedValueOnce(
|
|
420
|
+
response({ id: 'returned-order', status: 'Completed' })
|
|
421
|
+
);
|
|
422
|
+
paymentReturn();
|
|
423
|
+
renderApp();
|
|
424
|
+
await advance(1999);
|
|
425
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
426
|
+
expect(completionMessages()).toHaveLength(0);
|
|
427
|
+
await advance(1);
|
|
428
|
+
expect(screen.getByText('Order completed')).toBeTruthy();
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
it('does not extend the deadline for a long Retry-After', async () => {
|
|
432
|
+
fetchMock
|
|
433
|
+
.mockResolvedValueOnce(response(null))
|
|
434
|
+
.mockResolvedValueOnce(
|
|
435
|
+
response(null, 429, { 'Retry-After': '60' })
|
|
436
|
+
);
|
|
437
|
+
const orderCompletionDelayedCallback = vi.fn();
|
|
438
|
+
paymentReturn();
|
|
439
|
+
renderApp({ orderCompletionDelayedCallback });
|
|
440
|
+
await advance(10000);
|
|
441
|
+
expect(
|
|
442
|
+
screen.getByText('Order received, taking longer than expected')
|
|
443
|
+
).toBeTruthy();
|
|
444
|
+
expect(orderCompletionDelayedCallback).toHaveBeenCalledOnce();
|
|
445
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
446
|
+
expect(completionMessages()).toHaveLength(0);
|
|
447
|
+
});
|
|
448
|
+
|
|
449
|
+
it('does not retry a rate-limited read when Retry-After is unavailable to the browser', async () => {
|
|
450
|
+
fetchMock
|
|
451
|
+
.mockResolvedValueOnce(response(null))
|
|
452
|
+
.mockResolvedValueOnce(response(null, 429));
|
|
453
|
+
paymentReturn();
|
|
454
|
+
renderApp();
|
|
455
|
+
await advance(9999);
|
|
456
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
457
|
+
await advance(1);
|
|
458
|
+
expect(
|
|
459
|
+
screen.getByText('Order received, taking longer than expected')
|
|
460
|
+
).toBeTruthy();
|
|
461
|
+
expect(completionMessages()).toHaveLength(0);
|
|
462
|
+
});
|
|
463
|
+
|
|
464
|
+
it.each(['-1', '1.5', '0x1'])(
|
|
465
|
+
'does not retry a rate-limited read for invalid Retry-After %s',
|
|
466
|
+
async (retryAfter) => {
|
|
467
|
+
fetchMock
|
|
468
|
+
.mockResolvedValueOnce(response(null))
|
|
469
|
+
.mockResolvedValueOnce(
|
|
470
|
+
response(null, 429, { 'Retry-After': retryAfter })
|
|
471
|
+
)
|
|
472
|
+
.mockResolvedValueOnce(
|
|
473
|
+
response({ id: 'returned-order', status: 'Completed' })
|
|
474
|
+
);
|
|
475
|
+
const orderCompletionDelayedCallback = vi.fn();
|
|
476
|
+
paymentReturn();
|
|
477
|
+
renderApp({ orderCompletionDelayedCallback });
|
|
478
|
+
await advance(9999);
|
|
479
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
480
|
+
expect(orderCompletionDelayedCallback).not.toHaveBeenCalled();
|
|
481
|
+
await advance(1);
|
|
482
|
+
expect(
|
|
483
|
+
screen.getByText('Order received, taking longer than expected')
|
|
484
|
+
).toBeTruthy();
|
|
485
|
+
expect(
|
|
486
|
+
orderCompletionDelayedCallback
|
|
487
|
+
).toHaveBeenCalledExactlyOnceWith({
|
|
488
|
+
orderId: 'returned-order',
|
|
489
|
+
orderInfo: null,
|
|
490
|
+
reason: 'timeout',
|
|
491
|
+
});
|
|
492
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
493
|
+
expect(completionMessages()).toHaveLength(0);
|
|
494
|
+
}
|
|
495
|
+
);
|
|
496
|
+
|
|
497
|
+
it.each([
|
|
498
|
+
[401, 'Not JSON'],
|
|
499
|
+
[403, 'Not JSON'],
|
|
500
|
+
[400, 'Not JSON'],
|
|
501
|
+
[400, 'null'],
|
|
502
|
+
] as const)(
|
|
503
|
+
'stops status reads on HTTP %s (%s) without claiming order rejection',
|
|
504
|
+
async (status, body) => {
|
|
505
|
+
fetchMock
|
|
506
|
+
.mockResolvedValueOnce(response(null))
|
|
507
|
+
.mockResolvedValueOnce(new Response(body, { status }));
|
|
508
|
+
const orderCompletionDelayedCallback = vi.fn();
|
|
509
|
+
const orderCompletionFailedCallback = vi.fn();
|
|
510
|
+
paymentReturn();
|
|
511
|
+
renderApp({
|
|
512
|
+
orderCompletionDelayedCallback,
|
|
513
|
+
orderCompletionFailedCallback,
|
|
514
|
+
});
|
|
515
|
+
await advance(10000);
|
|
516
|
+
expect(
|
|
517
|
+
orderCompletionDelayedCallback
|
|
518
|
+
).toHaveBeenCalledExactlyOnceWith({
|
|
519
|
+
orderId: 'returned-order',
|
|
520
|
+
orderInfo: null,
|
|
521
|
+
reason: 'statusUnavailable',
|
|
522
|
+
});
|
|
523
|
+
expect(
|
|
524
|
+
screen.getByText('Order received, taking longer than expected')
|
|
525
|
+
).toBeTruthy();
|
|
526
|
+
expect(orderCompletionFailedCallback).not.toHaveBeenCalled();
|
|
527
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
528
|
+
expect(completionMessages()).toHaveLength(0);
|
|
529
|
+
}
|
|
530
|
+
);
|
|
531
|
+
|
|
532
|
+
it.each([
|
|
533
|
+
null,
|
|
534
|
+
{ id: 'returned-order' },
|
|
535
|
+
{ id: 'returned-order', status: 'FutureStatus' },
|
|
536
|
+
{ id: 'different-order', status: 'Completed' },
|
|
537
|
+
])(
|
|
538
|
+
'never treats an unknown or malformed status response as success: %j',
|
|
539
|
+
async (body) => {
|
|
540
|
+
fetchMock
|
|
541
|
+
.mockResolvedValueOnce(response(null))
|
|
542
|
+
.mockResolvedValueOnce(response(body));
|
|
543
|
+
const orderCompletionDelayedCallback = vi.fn();
|
|
544
|
+
paymentReturn();
|
|
545
|
+
renderApp({ orderCompletionDelayedCallback });
|
|
546
|
+
await advance(10000);
|
|
547
|
+
expect(
|
|
548
|
+
screen.getByText('Order received, taking longer than expected')
|
|
549
|
+
).toBeTruthy();
|
|
550
|
+
expect(orderCompletionDelayedCallback).toHaveBeenCalledWith({
|
|
551
|
+
orderId: 'returned-order',
|
|
552
|
+
orderInfo: null,
|
|
553
|
+
reason: 'statusUnavailable',
|
|
554
|
+
});
|
|
555
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
556
|
+
expect(completionMessages()).toHaveLength(0);
|
|
557
|
+
}
|
|
558
|
+
);
|
|
559
|
+
|
|
560
|
+
it.each(['Created', 'InProgress'])(
|
|
561
|
+
'keeps polling while the order status is %s',
|
|
562
|
+
async (status) => {
|
|
563
|
+
fetchMock
|
|
564
|
+
.mockResolvedValueOnce(response(null))
|
|
565
|
+
.mockResolvedValueOnce(
|
|
566
|
+
response({ id: 'returned-order', status })
|
|
567
|
+
)
|
|
568
|
+
.mockResolvedValueOnce(
|
|
569
|
+
response({ id: 'returned-order', status: 'Completed' })
|
|
570
|
+
);
|
|
571
|
+
paymentReturn();
|
|
572
|
+
renderApp();
|
|
573
|
+
await advance();
|
|
574
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
575
|
+
await advance(500);
|
|
576
|
+
expect(fetchMock).toHaveBeenCalledTimes(3);
|
|
577
|
+
expect(screen.getByText('Order completed')).toBeTruthy();
|
|
578
|
+
expect(completionMessages()).toHaveLength(1);
|
|
579
|
+
}
|
|
580
|
+
);
|
|
581
|
+
|
|
582
|
+
it.each(['Pending', 'Processing', 'Draft', 'Failed', 'created', ''])(
|
|
583
|
+
'stops polling for non-pollable status "%s"',
|
|
584
|
+
async (status) => {
|
|
585
|
+
fetchMock
|
|
586
|
+
.mockResolvedValueOnce(response(null))
|
|
587
|
+
.mockResolvedValueOnce(
|
|
588
|
+
response({ id: 'returned-order', status })
|
|
589
|
+
)
|
|
590
|
+
.mockResolvedValueOnce(
|
|
591
|
+
response({ id: 'returned-order', status: 'Completed' })
|
|
592
|
+
);
|
|
593
|
+
const orderCompletionDelayedCallback = vi.fn();
|
|
594
|
+
paymentReturn();
|
|
595
|
+
renderApp({ orderCompletionDelayedCallback });
|
|
596
|
+
await advance(10000);
|
|
597
|
+
expect(
|
|
598
|
+
orderCompletionDelayedCallback
|
|
599
|
+
).toHaveBeenCalledExactlyOnceWith({
|
|
600
|
+
orderId: 'returned-order',
|
|
601
|
+
orderInfo: null,
|
|
602
|
+
reason: 'statusUnavailable',
|
|
603
|
+
});
|
|
604
|
+
expect(fetchMock).toHaveBeenCalledTimes(2);
|
|
605
|
+
expect(completionMessages()).toHaveLength(0);
|
|
606
|
+
}
|
|
607
|
+
);
|
|
608
|
+
|
|
609
|
+
it('isolates host callback failures from accepted order state and polling', async () => {
|
|
610
|
+
vi.spyOn(console, 'error').mockImplementation(() => undefined);
|
|
611
|
+
fetchMock
|
|
612
|
+
.mockResolvedValueOnce(response(null))
|
|
613
|
+
.mockResolvedValueOnce(
|
|
614
|
+
response({ id: 'returned-order', status: 'Completed' })
|
|
615
|
+
);
|
|
616
|
+
paymentReturn();
|
|
617
|
+
renderApp({
|
|
618
|
+
orderAcceptedCallback: () => {
|
|
619
|
+
throw new Error('Host callback error');
|
|
620
|
+
},
|
|
621
|
+
orderCompletedCallback: async () => {
|
|
622
|
+
throw new Error('Async callback error');
|
|
623
|
+
},
|
|
624
|
+
});
|
|
625
|
+
await advance();
|
|
626
|
+
expect(screen.getByText('Order completed')).toBeTruthy();
|
|
627
|
+
expect(
|
|
628
|
+
screen.queryByRole('button', { name: 'Submit invoice' })
|
|
629
|
+
).toBeNull();
|
|
630
|
+
expect(completionMessages()).toHaveLength(1);
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
it('does not start status reads after acceptance callback unmounts SDK', async () => {
|
|
634
|
+
fetchMock.mockResolvedValueOnce(response(null));
|
|
635
|
+
let unmount!: () => void;
|
|
636
|
+
paymentReturn();
|
|
637
|
+
const view = renderApp({ orderAcceptedCallback: () => unmount() });
|
|
638
|
+
unmount = view.unmount;
|
|
639
|
+
await advance(10000);
|
|
640
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
641
|
+
expect(completionMessages()).toHaveLength(0);
|
|
642
|
+
});
|
|
643
|
+
|
|
644
|
+
it('ignores a pending POST response after unmount', async () => {
|
|
645
|
+
let finish!: (value: Response) => void;
|
|
646
|
+
fetchMock.mockImplementationOnce(
|
|
647
|
+
() =>
|
|
648
|
+
new Promise((resolve) => {
|
|
649
|
+
finish = resolve;
|
|
650
|
+
})
|
|
651
|
+
);
|
|
652
|
+
const orderAcceptedCallback = vi.fn();
|
|
653
|
+
paymentReturn();
|
|
654
|
+
const view = renderApp({ orderAcceptedCallback });
|
|
655
|
+
view.unmount();
|
|
656
|
+
await act(async () => {
|
|
657
|
+
finish(response(null));
|
|
658
|
+
});
|
|
659
|
+
await advance(10000);
|
|
660
|
+
expect(orderAcceptedCallback).not.toHaveBeenCalled();
|
|
661
|
+
expect(fetchMock).toHaveBeenCalledTimes(1);
|
|
662
|
+
expect(completionMessages()).toHaveLength(0);
|
|
663
|
+
});
|
|
664
|
+
|
|
665
|
+
it('shows a useful delayed message even when settings contain no custom polling text', async () => {
|
|
666
|
+
fetchMock.mockImplementation(async () =>
|
|
667
|
+
response({ id: 'returned-order', status: 'InProgress' })
|
|
668
|
+
);
|
|
669
|
+
paymentReturn();
|
|
670
|
+
renderApp({ settings: {} });
|
|
671
|
+
await advance(10000);
|
|
672
|
+
expect(screen.getByRole('status').textContent).toContain(
|
|
673
|
+
'Your order was received, but things are taking longer than expected.'
|
|
674
|
+
);
|
|
675
|
+
expect(completionMessages()).toHaveLength(0);
|
|
676
|
+
});
|
|
677
|
+
});
|
package/src/App.css
CHANGED
|
@@ -263,7 +263,6 @@
|
|
|
263
263
|
.sdk-scope video,
|
|
264
264
|
.sdk-scope canvas,
|
|
265
265
|
.sdk-scope audio,
|
|
266
|
-
.sdk-scope iframe,
|
|
267
266
|
.sdk-scope embed,
|
|
268
267
|
.sdk-scope object {
|
|
269
268
|
display: block;
|
|
@@ -393,17 +392,4 @@
|
|
|
393
392
|
*::after {
|
|
394
393
|
box-sizing: border-box;
|
|
395
394
|
}
|
|
396
|
-
|
|
397
|
-
.sdk-iframe-wrapper {
|
|
398
|
-
display: flex;
|
|
399
|
-
flex-direction: column;
|
|
400
|
-
align-items: center;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
iframe {
|
|
404
|
-
max-width: 640px;
|
|
405
|
-
border: none;
|
|
406
|
-
background: transparent;
|
|
407
|
-
border-radius: calc(var(--radius) * 2);
|
|
408
|
-
}
|
|
409
395
|
}
|