@swan-io/shared-business 18.3.0 → 18.5.0

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.
Files changed (68) hide show
  1. package/package.json +3 -3
  2. package/src/components/FraudReport/FraudReportLayout.d.ts +23 -0
  3. package/src/components/FraudReport/FraudReportLayout.js +35 -0
  4. package/src/components/FraudReport/IncidentStep.d.ts +7 -0
  5. package/src/components/FraudReport/IncidentStep.js +55 -0
  6. package/src/components/FraudReport/IntroStep.d.ts +5 -0
  7. package/src/components/FraudReport/IntroStep.js +28 -0
  8. package/src/components/FraudReport/TransactionsStep.d.ts +16 -0
  9. package/src/components/FraudReport/TransactionsStep.js +138 -0
  10. package/src/components/FraudReport/VictimStep.d.ts +9 -0
  11. package/src/components/FraudReport/VictimStep.js +55 -0
  12. package/src/components/InlineDatePicker.js +7 -6
  13. package/src/constants/__tests__/countries.test.js +32 -1
  14. package/src/constants/countries.d.ts +2 -1
  15. package/src/constants/countries.js +36 -2
  16. package/src/hooks/__tests__/useFraudReportFlow.test.d.ts +1 -0
  17. package/src/hooks/__tests__/useFraudReportFlow.test.js +979 -0
  18. package/src/hooks/useFraudReportFlow.d.ts +32 -0
  19. package/src/hooks/useFraudReportFlow.js +260 -0
  20. package/src/locales/de.json +76 -0
  21. package/src/locales/en.json +76 -0
  22. package/src/locales/es.json +76 -0
  23. package/src/locales/fi.json +76 -0
  24. package/src/locales/fr.json +76 -0
  25. package/src/locales/it.json +76 -0
  26. package/src/locales/nl.json +76 -0
  27. package/src/locales/pt.json +76 -0
  28. package/src/utils/__tests__/fraudReport.fakeApi.test.js +42 -13
  29. package/src/utils/__tests__/fraudReport.prefill.test.d.ts +1 -0
  30. package/src/utils/__tests__/fraudReport.prefill.test.js +149 -0
  31. package/src/utils/__tests__/fraudReport.rejections.test.d.ts +1 -0
  32. package/src/utils/__tests__/fraudReport.rejections.test.js +163 -0
  33. package/src/utils/__tests__/fraudReport.resume.test.d.ts +1 -0
  34. package/src/utils/__tests__/fraudReport.resume.test.js +67 -0
  35. package/src/utils/__tests__/fraudReport.stepper.test.d.ts +1 -0
  36. package/src/utils/__tests__/fraudReport.stepper.test.js +102 -0
  37. package/src/utils/__tests__/fraudReport.toInput.test.d.ts +1 -0
  38. package/src/utils/__tests__/fraudReport.toInput.test.js +549 -0
  39. package/src/utils/__tests__/fraudReport.transactions.test.d.ts +1 -0
  40. package/src/utils/__tests__/fraudReport.transactions.test.js +28 -0
  41. package/src/utils/__tests__/fraudReport.types.test.d.ts +1 -0
  42. package/src/utils/__tests__/fraudReport.types.test.js +22 -0
  43. package/src/utils/__tests__/fraudReport.victim.test.d.ts +1 -0
  44. package/src/utils/__tests__/fraudReport.victim.test.js +142 -0
  45. package/src/utils/__tests__/phone.test.js +60 -5
  46. package/src/utils/fraudReport/fakeApi.d.ts +7 -4
  47. package/src/utils/fraudReport/fakeApi.js +18 -5
  48. package/src/utils/fraudReport/prefill.d.ts +3 -0
  49. package/src/utils/fraudReport/prefill.js +14 -0
  50. package/src/utils/fraudReport/rail.d.ts +5 -0
  51. package/src/utils/fraudReport/rail.js +14 -0
  52. package/src/utils/fraudReport/rejections.d.ts +10 -0
  53. package/src/utils/fraudReport/rejections.js +53 -0
  54. package/src/utils/fraudReport/resume.d.ts +4 -0
  55. package/src/utils/fraudReport/resume.js +26 -0
  56. package/src/utils/fraudReport/stepper.d.ts +16 -0
  57. package/src/utils/fraudReport/stepper.js +21 -0
  58. package/src/utils/fraudReport/toInput.d.ts +30 -0
  59. package/src/utils/fraudReport/toInput.js +82 -0
  60. package/src/utils/fraudReport/transactions.d.ts +6 -0
  61. package/src/utils/fraudReport/transactions.js +7 -0
  62. package/src/utils/fraudReport/types.d.ts +8 -0
  63. package/src/utils/fraudReport/validation.d.ts +15 -0
  64. package/src/utils/fraudReport/validation.js +55 -0
  65. package/src/utils/fraudReport/victim.d.ts +30 -0
  66. package/src/utils/fraudReport/victim.js +28 -0
  67. package/src/utils/phone.d.ts +1 -0
  68. package/src/utils/phone.js +31 -4
@@ -0,0 +1,979 @@
1
+ import { Future, Result } from "@bloodyowl/boxed";
2
+ import { act, renderHook } from "@testing-library/react";
3
+ import { afterEach, describe, expect, test, vi } from "vitest";
4
+ import { createFakeFraudReportApi, createFakeTransaction, } from "../../utils/fraudReport/fakeApi";
5
+ import { DRAFT_LOOKUP_TIMEOUT_MS, useFraudReportFlow, } from "../useFraudReportFlow";
6
+ const card = createFakeTransaction("card-1", "CardOutDebit");
7
+ const transfer = createFakeTransaction("transfer-1", "SepaCreditTransferOut");
8
+ const incident = {
9
+ reportedReason: "Unauthorized",
10
+ reportedTypology: "Other",
11
+ description: "Payments I didn't make",
12
+ };
13
+ const seededTransactions = () => [
14
+ {
15
+ id: "card-1",
16
+ accountId: "account-1",
17
+ type: "CardOutDebit",
18
+ cardholderAccountMembershipId: "member-1",
19
+ },
20
+ { id: "transfer-1", accountId: "account-1", type: "SepaCreditTransferOut" },
21
+ ];
22
+ const setup = (config = {}, { fake = createFakeFraudReportApi({
23
+ transactions: seededTransactions(),
24
+ accountMemberships: [
25
+ { id: "member-1", accountId: "account-1" },
26
+ { id: "member-2", accountId: "account-1" },
27
+ ],
28
+ ...config,
29
+ }), resumeFrom, } = {}) => {
30
+ const calls = [];
31
+ const api = {
32
+ draft: input => {
33
+ calls.push({ name: "draft", input });
34
+ return fake.draft(input);
35
+ },
36
+ update: input => {
37
+ calls.push({ name: "update", input });
38
+ return fake.update(input);
39
+ },
40
+ submit: input => {
41
+ calls.push({ name: "submit", input });
42
+ return fake.submit(input);
43
+ },
44
+ cancel: input => {
45
+ calls.push({ name: "cancel", input });
46
+ return fake.cancel(input);
47
+ },
48
+ findDrafts: input => {
49
+ calls.push({ name: "findDrafts", input });
50
+ return fake.findDrafts(input);
51
+ },
52
+ };
53
+ const onExit = vi.fn();
54
+ const { result, unmount } = renderHook(() => useFraudReportFlow({ api, onExit, resumeFrom }));
55
+ return {
56
+ result,
57
+ fake,
58
+ calls,
59
+ onExit,
60
+ unmount,
61
+ callNames: () => calls.map(({ name }) => name),
62
+ next: (values) => act(() => result.current.next(values)),
63
+ previous: () => act(() => result.current.previous()),
64
+ cancel: () => act(() => result.current.cancel()),
65
+ resume: () => act(() => result.current.resume()),
66
+ startOver: () => act(() => result.current.startOver()),
67
+ };
68
+ };
69
+ const startReport = (flow, { victim, transactions }) => {
70
+ flow.next();
71
+ flow.next({ victimAccountMembershipId: victim });
72
+ flow.next({ transactions });
73
+ };
74
+ afterEach(() => {
75
+ vi.useRealTimers();
76
+ });
77
+ describe("step moves", () => {
78
+ test("walks a card-only report from the intro to the submitted step", () => {
79
+ const flow = setup();
80
+ const steps = [flow.result.current.step];
81
+ for (const values of [
82
+ undefined,
83
+ { victimAccountMembershipId: "member-1" },
84
+ { transactions: [card] },
85
+ incident,
86
+ { isLostOrStolen: true },
87
+ undefined,
88
+ ]) {
89
+ flow.next(values);
90
+ steps.push(flow.result.current.step);
91
+ }
92
+ expect(steps).toEqual([
93
+ "Intro",
94
+ "Victim",
95
+ "Transactions",
96
+ "Incident",
97
+ "Card",
98
+ "Documents",
99
+ "Submitted",
100
+ ]);
101
+ });
102
+ test("goes through every Details sub-step of the report, both ways", () => {
103
+ const flow = setup();
104
+ startReport(flow, { victim: "member-2", transactions: [transfer, card] });
105
+ flow.next({ ...incident, reportedReason: "Manipulated" });
106
+ const forward = [];
107
+ for (let index = 0; index < 4 && flow.result.current.step !== "Documents"; index++) {
108
+ forward.push(flow.result.current.step);
109
+ flow.next();
110
+ }
111
+ const backward = [];
112
+ for (let index = 0; index < 4 && flow.result.current.step !== "Incident"; index++) {
113
+ flow.previous();
114
+ backward.push(flow.result.current.step);
115
+ }
116
+ expect(forward).toEqual(["Card", "Transfer", "Fraudster"]);
117
+ expect(backward).toEqual(["Fraudster", "Transfer", "Card", "Incident"]);
118
+ expect(flow.result.current.detailsSteps).toEqual(["Card", "Transfer", "Fraudster"]);
119
+ });
120
+ test("goes back one step at a time without calling the API", () => {
121
+ const flow = setup();
122
+ startReport(flow, { victim: "member-1", transactions: [card] });
123
+ flow.next(incident);
124
+ const callsBefore = flow.callNames();
125
+ const steps = [flow.result.current.step];
126
+ for (let index = 0; index < 5; index++) {
127
+ flow.previous();
128
+ steps.push(flow.result.current.step);
129
+ }
130
+ expect(steps).toEqual(["Card", "Incident", "Transactions", "Victim", "Intro", "Intro"]);
131
+ expect(flow.callNames()).toEqual(callsBefore);
132
+ });
133
+ test("keeps the values entered on a step when the user goes back to it", () => {
134
+ const flow = setup();
135
+ startReport(flow, { victim: "member-1", transactions: [card] });
136
+ flow.next(incident);
137
+ flow.previous();
138
+ expect(flow.result.current.values).toEqual({
139
+ transactions: [card],
140
+ victimAccountMembershipId: "member-1",
141
+ ...incident,
142
+ });
143
+ });
144
+ });
145
+ describe("API calls", () => {
146
+ test("calls nothing before the transactions are confirmed, then drafts once and updates once per step", () => {
147
+ const flow = setup();
148
+ flow.next();
149
+ flow.next({ victimAccountMembershipId: "member-2" });
150
+ expect(flow.callNames()).toEqual([]);
151
+ flow.next({ transactions: [transfer] });
152
+ expect(flow.callNames()).toEqual(["draft"]);
153
+ flow.next({ ...incident, reportedReason: "Manipulated" });
154
+ flow.next();
155
+ flow.next({ fraudsterChannels: ["Phone"] });
156
+ expect(flow.callNames()).toEqual(["draft", "update", "update", "update"]);
157
+ flow.next();
158
+ expect(flow.callNames()).toEqual(["draft", "update", "update", "update", "submit"]);
159
+ });
160
+ test("updates the draft when the selection changes after it exists", () => {
161
+ var _a;
162
+ const flow = setup();
163
+ startReport(flow, { victim: "member-1", transactions: [card] });
164
+ flow.previous();
165
+ flow.next({ transactions: [card, transfer] });
166
+ expect(flow.calls).toEqual([
167
+ { name: "draft", input: { transactionIds: ["card-1"] } },
168
+ {
169
+ name: "update",
170
+ input: expect.objectContaining({ transactionIds: ["card-1", "transfer-1"] }),
171
+ },
172
+ ]);
173
+ expect(flow.result.current.step).toBe("Incident");
174
+ expect(flow.result.current.detailsSteps).toEqual(["Card", "Transfer"]);
175
+ flow.next(incident);
176
+ expect((_a = flow.calls.at(-1)) === null || _a === void 0 ? void 0 : _a.input).not.toHaveProperty("transactionIds");
177
+ });
178
+ test("leaves the transaction IDs out of an update when the selection didn't change", () => {
179
+ var _a, _b;
180
+ const flow = setup();
181
+ startReport(flow, { victim: "member-1", transactions: [card] });
182
+ flow.next(incident);
183
+ expect((_a = flow.calls.at(-1)) === null || _a === void 0 ? void 0 : _a.name).toBe("update");
184
+ expect((_b = flow.calls.at(-1)) === null || _b === void 0 ? void 0 : _b.input).not.toHaveProperty("transactionIds");
185
+ });
186
+ test("sends the new selection again when its update failed", () => {
187
+ var _a;
188
+ const flow = setup();
189
+ startReport(flow, { victim: "member-1", transactions: [card] });
190
+ flow.previous();
191
+ vi.spyOn(flow.fake, "update").mockReturnValueOnce(Future.value(Result.Error({ kind: "error", error: new Error("Network error") })));
192
+ flow.next({ transactions: [card, transfer] });
193
+ flow.next({ transactions: [card, transfer] });
194
+ expect((_a = flow.calls.at(-1)) === null || _a === void 0 ? void 0 : _a.input).toHaveProperty("transactionIds", ["card-1", "transfer-1"]);
195
+ });
196
+ test("shows the call as pending and ignores Next and Previous until it ends", () => {
197
+ vi.useFakeTimers();
198
+ const flow = setup({ delay: 100 });
199
+ startReport(flow, { victim: "member-1", transactions: [card] });
200
+ expect(flow.result.current.saving).toBe(true);
201
+ expect(flow.result.current.step).toBe("Transactions");
202
+ flow.next({ transactions: [card] });
203
+ flow.previous();
204
+ expect(flow.callNames()).toEqual(["draft"]);
205
+ expect(flow.result.current.step).toBe("Transactions");
206
+ const nextWhileSaving = flow.result.current.next;
207
+ const previousWhileSaving = flow.result.current.previous;
208
+ act(() => vi.advanceTimersByTime(100));
209
+ expect(flow.result.current.saving).toBe(false);
210
+ expect(flow.result.current.step).toBe("Incident");
211
+ act(() => nextWhileSaving({ transactions: [card] }));
212
+ expect(flow.callNames()).toEqual(["draft"]);
213
+ act(() => previousWhileSaving());
214
+ expect(flow.result.current.step).toBe("Incident");
215
+ });
216
+ test("ignores a Next called after a call ends but before the hook re-renders", () => {
217
+ vi.useFakeTimers();
218
+ const flow = setup({ delay: 100 });
219
+ startReport(flow, { victim: "member-1", transactions: [card] });
220
+ act(() => {
221
+ vi.advanceTimersByTime(100);
222
+ flow.result.current.next({ transactions: [card] });
223
+ });
224
+ expect(flow.callNames()).toEqual(["draft"]);
225
+ });
226
+ test("ignores a Previous called right after Next, before the hook re-renders", () => {
227
+ vi.useFakeTimers();
228
+ const flow = setup({ delay: 100 });
229
+ flow.next();
230
+ flow.next({ victimAccountMembershipId: "member-1" });
231
+ act(() => {
232
+ flow.result.current.next({ transactions: [card] });
233
+ flow.result.current.previous();
234
+ });
235
+ expect(flow.result.current.step).toBe("Transactions");
236
+ });
237
+ test("ignores a second Next called before the first one re-renders", () => {
238
+ vi.useFakeTimers();
239
+ const flow = setup({ delay: 100 });
240
+ flow.next();
241
+ flow.next({ victimAccountMembershipId: "member-1" });
242
+ act(() => {
243
+ flow.result.current.next({ transactions: [card] });
244
+ flow.result.current.next({ transactions: [card] });
245
+ });
246
+ expect(flow.callNames()).toEqual(["draft"]);
247
+ });
248
+ });
249
+ describe("victim", () => {
250
+ test("exposes the victim only in the values, and the case ID once the draft exists", () => {
251
+ const flow = setup();
252
+ flow.next();
253
+ flow.next({ victimAccountMembershipId: "member-2" });
254
+ expect(flow.result.current.disputeCaseId.isNone()).toBe(true);
255
+ flow.next({ transactions: [transfer] });
256
+ expect(flow.result.current.disputeCaseId.isSome()).toBe(true);
257
+ expect(flow.result.current).not.toHaveProperty("victimAccountMembershipId");
258
+ });
259
+ test("keeps the victim once the draft exists", () => {
260
+ const flow = setup();
261
+ startReport(flow, { victim: "member-2", transactions: [transfer] });
262
+ flow.previous();
263
+ flow.previous();
264
+ expect(flow.result.current.step).toBe("Victim");
265
+ expect(flow.result.current.values.victimAccountMembershipId).toBe("member-2");
266
+ flow.next({ victimAccountMembershipId: "member-1" });
267
+ flow.next({ transactions: [transfer] });
268
+ expect(flow.result.current.values.victimAccountMembershipId).toBe("member-2");
269
+ expect(flow.calls.at(-1)).toEqual({
270
+ name: "update",
271
+ input: expect.objectContaining({ victimAccountMembershipId: "member-2" }),
272
+ });
273
+ });
274
+ test("sends the victim on a transfer-only case", () => {
275
+ const flow = setup();
276
+ startReport(flow, { victim: "member-2", transactions: [transfer] });
277
+ expect(flow.calls).toEqual([
278
+ {
279
+ name: "draft",
280
+ input: { transactionIds: ["transfer-1"], victimAccountMembershipId: "member-2" },
281
+ },
282
+ ]);
283
+ });
284
+ });
285
+ describe("submit", () => {
286
+ test("submits on the documents step and keeps the case ID for the submitted step", () => {
287
+ const flow = setup();
288
+ startReport(flow, { victim: "member-1", transactions: [card] });
289
+ const disputeCaseId = flow.result.current.disputeCaseId.toUndefined();
290
+ flow.next(incident);
291
+ flow.next();
292
+ flow.next();
293
+ expect(flow.result.current.step).toBe("Submitted");
294
+ expect(flow.calls.at(-1)).toEqual({ name: "submit", input: { disputeCaseId } });
295
+ expect(flow.result.current.disputeCaseId.toUndefined()).toBe(disputeCaseId);
296
+ flow.previous();
297
+ flow.next();
298
+ expect(flow.result.current.step).toBe("Submitted");
299
+ expect(flow.callNames()).toEqual(["draft", "update", "update", "submit"]);
300
+ });
301
+ });
302
+ const rejected = (rejection) => Future.value(Result.Error({ kind: "rejection", rejection }));
303
+ const expectRejection = (flow, rejection) => {
304
+ expect(flow.result.current.error.toUndefined()).toEqual({
305
+ kind: "rejection",
306
+ rejection: expect.objectContaining(rejection),
307
+ });
308
+ };
309
+ const DAY_MS = 24 * 60 * 60 * 1000;
310
+ describe("rejections", () => {
311
+ test.each([
312
+ {
313
+ name: "a transaction isn't found",
314
+ transactions: seededTransactions(),
315
+ selection: [createFakeTransaction("unknown", "CardOutDebit")],
316
+ fixedSelection: [card],
317
+ rejection: { __typename: "DisputeCaseTransactionsNotFoundRejection" },
318
+ },
319
+ {
320
+ name: "the selection has too many transactions",
321
+ transactions: seededTransactions(),
322
+ selection: Array.from({ length: 101 }, (_, index) => createFakeTransaction(`card-${index}`, "CardOutDebit")),
323
+ fixedSelection: [card],
324
+ rejection: {
325
+ __typename: "DisputeCaseTransactionsLimitExceededRejection",
326
+ maxTransactionCount: 100,
327
+ },
328
+ },
329
+ {
330
+ name: "a transaction can't be reported",
331
+ transactions: [
332
+ {
333
+ id: "card-1",
334
+ accountId: "account-1",
335
+ type: "CardOutDebit",
336
+ cardholderAccountMembershipId: "member-1",
337
+ notReportableReason: "TransactionTypeNotReportable",
338
+ },
339
+ { id: "transfer-1", accountId: "account-1", type: "SepaCreditTransferOut" },
340
+ ],
341
+ selection: [card],
342
+ fixedSelection: [transfer],
343
+ rejection: { __typename: "DisputeCaseTransactionsNotReportableRejection" },
344
+ },
345
+ {
346
+ name: "the transactions belong to several accounts",
347
+ transactions: [
348
+ ...seededTransactions(),
349
+ { id: "transfer-2", accountId: "account-2", type: "SepaCreditTransferOut" },
350
+ ],
351
+ selection: [transfer, createFakeTransaction("transfer-2", "SepaCreditTransferOut")],
352
+ fixedSelection: [transfer],
353
+ rejection: { __typename: "DisputeCaseMultipleAccountsRejection" },
354
+ },
355
+ {
356
+ name: "the cards belong to several cardholders",
357
+ transactions: [
358
+ ...seededTransactions(),
359
+ {
360
+ id: "card-2",
361
+ accountId: "account-1",
362
+ type: "CardOutDebit",
363
+ cardholderAccountMembershipId: "member-2",
364
+ },
365
+ ],
366
+ selection: [card, createFakeTransaction("card-2", "CardOutDebit")],
367
+ fixedSelection: [card],
368
+ rejection: { __typename: "DisputeCaseMultipleCardHoldersRejection" },
369
+ },
370
+ ])("keeps the user on the transactions step when $name, until a fixed selection is sent", ({ transactions, selection, fixedSelection, rejection }) => {
371
+ const flow = setup({ transactions });
372
+ startReport(flow, { victim: "member-2", transactions: selection });
373
+ expect(flow.result.current.step).toBe("Transactions");
374
+ expectRejection(flow, rejection);
375
+ expect(flow.result.current.values.transactions).toEqual(selection);
376
+ flow.next({ transactions: fixedSelection });
377
+ expect(flow.result.current.step).toBe("Incident");
378
+ expect(flow.result.current.error.isNone()).toBe(true);
379
+ });
380
+ test("goes back to the victim step when the victim isn't found before the draft", () => {
381
+ const flow = setup();
382
+ startReport(flow, { victim: "member-unknown", transactions: [transfer] });
383
+ expect(flow.result.current.step).toBe("Victim");
384
+ expectRejection(flow, { __typename: "AccountMembershipNotFoundRejection" });
385
+ expect(flow.result.current.values).toEqual({
386
+ victimAccountMembershipId: "member-unknown",
387
+ transactions: [transfer],
388
+ });
389
+ flow.next({ victimAccountMembershipId: "member-2" });
390
+ flow.next({ transactions: [transfer] });
391
+ expect(flow.result.current.step).toBe("Incident");
392
+ expect(flow.result.current.error.isNone()).toBe(true);
393
+ });
394
+ test("goes back to the transactions step when a transaction can no longer be reported", () => {
395
+ const flow = setup();
396
+ startReport(flow, { victim: "member-1", transactions: [card] });
397
+ vi.spyOn(flow.fake, "update").mockReturnValueOnce(rejected({
398
+ __typename: "DisputeCaseTransactionsNotReportableRejection",
399
+ message: "Some transactions can't be reported",
400
+ transactions: [{ transactionId: "card-1", reason: "TransactionStatusNotReportable" }],
401
+ }));
402
+ flow.next(incident);
403
+ expect(flow.result.current.step).toBe("Transactions");
404
+ expectRejection(flow, { __typename: "DisputeCaseTransactionsNotReportableRejection" });
405
+ expect(flow.result.current.values).toMatchObject(incident);
406
+ });
407
+ test("goes back to the transactions step when details don't apply to the selection", () => {
408
+ const flow = setup();
409
+ startReport(flow, { victim: "member-1", transactions: [card] });
410
+ vi.spyOn(flow.fake, "update").mockReturnValueOnce(rejected({
411
+ __typename: "DisputeCaseDetailsNotApplicableRejection",
412
+ message: "Some details don't apply to this case",
413
+ fields: [{ path: ["fraudCardDetails"] }],
414
+ }));
415
+ flow.next(incident);
416
+ expect(flow.result.current.step).toBe("Transactions");
417
+ expectRejection(flow, { __typename: "DisputeCaseDetailsNotApplicableRejection" });
418
+ });
419
+ test("goes back to the incident step when the submitted case is incomplete", () => {
420
+ const flow = setup();
421
+ startReport(flow, { victim: "member-1", transactions: [card] });
422
+ flow.next();
423
+ flow.next();
424
+ flow.next();
425
+ expect(flow.callNames().at(-1)).toBe("submit");
426
+ expect(flow.result.current.step).toBe("Incident");
427
+ expectRejection(flow, {
428
+ __typename: "DisputeCaseIncompleteRejection",
429
+ missingFields: [{ path: ["fraudDetails"] }],
430
+ });
431
+ flow.next(incident);
432
+ expect(flow.result.current.step).toBe("Card");
433
+ expect(flow.result.current.error.isNone()).toBe(true);
434
+ });
435
+ test.each([
436
+ { reportedReason: "Manipulated", detailsSteps: ["Card", "Fraudster"], step: "Fraudster" },
437
+ { reportedReason: "Unauthorized", detailsSteps: ["Card"], step: "Incident" },
438
+ ])("sends missing fraudster answers to $step when the report goes through $detailsSteps", ({ reportedReason, detailsSteps, step }) => {
439
+ const flow = setup();
440
+ startReport(flow, { victim: "member-1", transactions: [card] });
441
+ flow.next({ ...incident, reportedReason });
442
+ for (const detailsStep of detailsSteps) {
443
+ expect(flow.result.current.step).toBe(detailsStep);
444
+ flow.next();
445
+ }
446
+ expect(flow.result.current.step).toBe("Documents");
447
+ vi.spyOn(flow.fake, "submit").mockReturnValueOnce(rejected({
448
+ __typename: "DisputeCaseIncompleteRejection",
449
+ message: "The case is incomplete",
450
+ missingFields: [{ path: ["fraudDetails", "fraudsterChannels"] }],
451
+ }));
452
+ flow.next();
453
+ expect(flow.result.current.step).toBe(step);
454
+ expectRejection(flow, { __typename: "DisputeCaseIncompleteRejection" });
455
+ });
456
+ test("goes to the submitted step, with no error, when the case was submitted elsewhere", () => {
457
+ const flow = setup();
458
+ startReport(flow, { victim: "member-1", transactions: [card] });
459
+ flow.next(incident);
460
+ const disputeCaseId = flow.result.current.disputeCaseId.getOr("");
461
+ act(() => {
462
+ flow.fake.submit({ disputeCaseId });
463
+ });
464
+ flow.next({ isLostOrStolen: true });
465
+ expect(flow.callNames().at(-1)).toBe("update");
466
+ expect(flow.result.current.step).toBe("Submitted");
467
+ expect(flow.result.current.error.isNone()).toBe(true);
468
+ expect(flow.result.current.disputeCaseId.toUndefined()).toBe(disputeCaseId);
469
+ });
470
+ test.each([
471
+ {
472
+ name: "a transaction is outside its reporting window",
473
+ config: {
474
+ transactions: [
475
+ {
476
+ id: "transfer-1",
477
+ accountId: "account-1",
478
+ type: "SepaCreditTransferOut",
479
+ reportingWindowEndsAt: "2020-01-01T00:00:00.000Z",
480
+ },
481
+ ],
482
+ },
483
+ run: flow => startReport(flow, { victim: "member-2", transactions: [transfer] }),
484
+ rejection: { __typename: "DisputeCaseTransactionsOutsideReportingWindowRejection" },
485
+ },
486
+ {
487
+ name: "a transaction is already reported",
488
+ config: { alreadyReported: [{ transactionId: "card-1", disputeCaseId: null }] },
489
+ run: flow => {
490
+ startReport(flow, { victim: "member-1", transactions: [card] });
491
+ flow.next(incident);
492
+ flow.next();
493
+ flow.next();
494
+ },
495
+ rejection: { __typename: "DisputeCaseTransactionsAlreadyReportedRejection" },
496
+ },
497
+ {
498
+ name: "the victim isn't found once the draft exists",
499
+ config: {
500
+ transactions: [
501
+ ...seededTransactions(),
502
+ { id: "transfer-2", accountId: "account-2", type: "SepaCreditTransferOut" },
503
+ ],
504
+ },
505
+ run: flow => {
506
+ startReport(flow, { victim: "member-2", transactions: [transfer] });
507
+ flow.previous();
508
+ flow.next({ transactions: [createFakeTransaction("transfer-2", "SepaCreditTransferOut")] });
509
+ },
510
+ rejection: { __typename: "AccountMembershipNotFoundRejection" },
511
+ },
512
+ {
513
+ name: "the case isn't found",
514
+ run: flow => {
515
+ startReport(flow, { victim: "member-1", transactions: [card] });
516
+ vi.spyOn(flow.fake, "update").mockReturnValueOnce(rejected({
517
+ __typename: "DisputeCaseNotFoundRejection",
518
+ message: "Dispute case not found",
519
+ disputeCaseId: flow.result.current.disputeCaseId.getOr(""),
520
+ }));
521
+ flow.next(incident);
522
+ },
523
+ rejection: { __typename: "DisputeCaseNotFoundRejection" },
524
+ },
525
+ ])("ends the report on the rejected step when $name", ({ config, run, rejection }) => {
526
+ const flow = setup(config);
527
+ run(flow);
528
+ expect(flow.result.current.step).toBe("Rejected");
529
+ expectRejection(flow, rejection);
530
+ });
531
+ test("ends the report on the rejected step when the draft expired", () => {
532
+ let date = new Date("2026-11-26T10:00:00.000Z");
533
+ const flow = setup({ now: () => date });
534
+ startReport(flow, { victim: "member-1", transactions: [card] });
535
+ date = new Date(date.getTime() + 31 * DAY_MS);
536
+ flow.next(incident);
537
+ expect(flow.result.current.step).toBe("Rejected");
538
+ expectRejection(flow, {
539
+ __typename: "DisputeCaseWrongStatusRejection",
540
+ currentStatus: "Expired",
541
+ });
542
+ });
543
+ test("stays on the rejected step with the values and the rejection, calling nothing", () => {
544
+ const flow = setup({ alreadyReported: [{ transactionId: "card-1", disputeCaseId: null }] });
545
+ startReport(flow, { victim: "member-1", transactions: [card] });
546
+ flow.next(incident);
547
+ flow.next();
548
+ flow.next();
549
+ const { values, error } = flow.result.current;
550
+ const callsBefore = flow.callNames();
551
+ flow.previous();
552
+ flow.next({ transactions: [transfer] });
553
+ expect(flow.result.current.step).toBe("Rejected");
554
+ expect(flow.result.current.values).toEqual(values);
555
+ expect(flow.result.current.error).toEqual(error);
556
+ expect(flow.callNames()).toEqual(callsBefore);
557
+ });
558
+ });
559
+ describe("errors", () => {
560
+ test("stays on the step and exposes an error, cleared when going back", () => {
561
+ const error = new Error("Network error");
562
+ const flow = setup();
563
+ vi.spyOn(flow.fake, "draft").mockReturnValue(Future.value(Result.Error({ kind: "error", error })));
564
+ startReport(flow, { victim: "member-1", transactions: [card] });
565
+ expect(flow.result.current.step).toBe("Transactions");
566
+ expect(flow.result.current.error.toUndefined()).toEqual({ kind: "error", error });
567
+ expect(flow.result.current.saving).toBe(false);
568
+ flow.previous();
569
+ expect(flow.result.current.error.isNone()).toBe(true);
570
+ });
571
+ test.each([
572
+ { name: "a network error", error: new Error("Network error") },
573
+ { name: "a FORBIDDEN error", error: new Error("FORBIDDEN") },
574
+ ])("stays on the step and keeps the form on $name, then retries on Next", ({ error }) => {
575
+ const flow = setup();
576
+ startReport(flow, { victim: "member-1", transactions: [card] });
577
+ flow.next(incident);
578
+ vi.spyOn(flow.fake, "update").mockReturnValueOnce(Future.value(Result.Error({ kind: "error", error })));
579
+ flow.next({ isLostOrStolen: true });
580
+ expect(flow.result.current.step).toBe("Card");
581
+ expect(flow.result.current.error.toUndefined()).toEqual({ kind: "error", error });
582
+ expect(flow.result.current.values).toMatchObject({ ...incident, isLostOrStolen: true });
583
+ flow.next({ isLostOrStolen: true });
584
+ expect(flow.result.current.step).toBe("Documents");
585
+ expect(flow.result.current.error.isNone()).toBe(true);
586
+ });
587
+ test("stays on the step and keeps the form on a BAD_USER_INPUT error, then retries on Next", () => {
588
+ const flow = setup();
589
+ startReport(flow, { victim: "member-1", transactions: [card, card] });
590
+ expect(flow.result.current.step).toBe("Transactions");
591
+ expect(flow.result.current.error.toUndefined()).toEqual({
592
+ kind: "error",
593
+ error: expect.objectContaining({ message: expect.stringContaining("BAD_USER_INPUT") }),
594
+ });
595
+ expect(flow.result.current.values.transactions).toEqual([card, card]);
596
+ flow.next({ transactions: [card] });
597
+ expect(flow.result.current.step).toBe("Incident");
598
+ });
599
+ });
600
+ const expectReset = (flow) => {
601
+ expect(flow.result.current).toMatchObject({ step: "Intro", values: { transactions: [] } });
602
+ expect(flow.result.current.disputeCaseId.isNone()).toBe(true);
603
+ expect(flow.result.current.error.isNone()).toBe(true);
604
+ };
605
+ const getStatus = (flow, disputeCaseId = "") => {
606
+ let status;
607
+ flow.fake.getCase({ disputeCaseId }).onResolve(result => {
608
+ var _a;
609
+ status = (_a = result
610
+ .toOption()
611
+ .flatMap(item => item)
612
+ .toUndefined()) === null || _a === void 0 ? void 0 : _a.statusInfo.status;
613
+ });
614
+ return status;
615
+ };
616
+ describe("cancel", () => {
617
+ test("leaves without calling the API before the draft exists", () => {
618
+ const flow = setup();
619
+ flow.next();
620
+ flow.next({ victimAccountMembershipId: "member-1" });
621
+ flow.cancel();
622
+ expect(flow.callNames()).toEqual([]);
623
+ expect(flow.onExit).toHaveBeenCalledTimes(1);
624
+ expectReset(flow);
625
+ });
626
+ test("cancels the draft through the adapter, then leaves", () => {
627
+ const flow = setup();
628
+ startReport(flow, { victim: "member-1", transactions: [card] });
629
+ flow.next(incident);
630
+ const disputeCaseId = flow.result.current.disputeCaseId.toUndefined();
631
+ flow.cancel();
632
+ expect(getStatus(flow, disputeCaseId)).toBe("Canceled");
633
+ expect(flow.calls.at(-1)).toEqual({ name: "cancel", input: { disputeCaseId } });
634
+ expect(flow.onExit).toHaveBeenCalledTimes(1);
635
+ expectReset(flow);
636
+ expect(flow.result.current.saving).toBe(false);
637
+ });
638
+ test("leaves when the draft was already canceled", () => {
639
+ const flow = setup();
640
+ startReport(flow, { victim: "member-1", transactions: [card] });
641
+ flow.result.current.disputeCaseId.tapSome(disputeCaseId => {
642
+ flow.fake.cancel({ disputeCaseId });
643
+ });
644
+ flow.cancel();
645
+ expect(flow.callNames()).toEqual(["draft", "cancel"]);
646
+ expect(flow.onExit).toHaveBeenCalledTimes(1);
647
+ expectReset(flow);
648
+ });
649
+ test("moves to the submitted step without an error when the case was submitted elsewhere", () => {
650
+ const flow = setup();
651
+ startReport(flow, { victim: "member-1", transactions: [card] });
652
+ flow.next(incident);
653
+ flow.result.current.disputeCaseId.tapSome(disputeCaseId => {
654
+ flow.fake.submit({ disputeCaseId });
655
+ });
656
+ flow.cancel();
657
+ expect(flow.result.current.step).toBe("Submitted");
658
+ expect(flow.result.current.error.isNone()).toBe(true);
659
+ expect(flow.onExit).not.toHaveBeenCalled();
660
+ });
661
+ test("ends the report on the rejected step when the draft expired", () => {
662
+ let date = new Date("2026-11-26T10:00:00.000Z");
663
+ const flow = setup({ now: () => date });
664
+ startReport(flow, { victim: "member-1", transactions: [card] });
665
+ date = new Date(date.getTime() + 31 * DAY_MS);
666
+ flow.cancel();
667
+ expect(flow.result.current.step).toBe("Rejected");
668
+ expectRejection(flow, {
669
+ __typename: "DisputeCaseWrongStatusRejection",
670
+ currentStatus: "Expired",
671
+ });
672
+ expect(flow.onExit).not.toHaveBeenCalled();
673
+ });
674
+ test("ends the report on the rejected step when the case isn't found", () => {
675
+ const flow = setup();
676
+ startReport(flow, { victim: "member-1", transactions: [card] });
677
+ vi.spyOn(flow.fake, "cancel").mockReturnValueOnce(rejected({
678
+ __typename: "DisputeCaseNotFoundRejection",
679
+ disputeCaseId: "case-1",
680
+ message: "",
681
+ }));
682
+ flow.cancel();
683
+ expect(flow.result.current.step).toBe("Rejected");
684
+ expectRejection(flow, { __typename: "DisputeCaseNotFoundRejection" });
685
+ expect(flow.onExit).not.toHaveBeenCalled();
686
+ });
687
+ test.each([
688
+ { name: "a network error", error: new Error("Network error") },
689
+ { name: "a FORBIDDEN error", error: new Error("FORBIDDEN") },
690
+ ])("stays on the step and keeps the form on $name, then retries", ({ error }) => {
691
+ const flow = setup();
692
+ startReport(flow, { victim: "member-1", transactions: [card] });
693
+ flow.next(incident);
694
+ const disputeCaseId = flow.result.current.disputeCaseId.toUndefined();
695
+ vi.spyOn(flow.fake, "cancel").mockReturnValueOnce(Future.value(Result.Error({ kind: "error", error })));
696
+ flow.cancel();
697
+ expect(flow.result.current.step).toBe("Card");
698
+ expect(flow.result.current.error.toUndefined()).toEqual({ kind: "error", error });
699
+ expect(flow.result.current.values).toMatchObject(incident);
700
+ expect(flow.onExit).not.toHaveBeenCalled();
701
+ flow.cancel();
702
+ expect(getStatus(flow, disputeCaseId)).toBe("Canceled");
703
+ expect(flow.result.current.error.isNone()).toBe(true);
704
+ expect(flow.onExit).toHaveBeenCalledTimes(1);
705
+ });
706
+ test("does nothing while a call is pending", () => {
707
+ const flow = setup();
708
+ startReport(flow, { victim: "member-1", transactions: [card] });
709
+ vi.spyOn(flow.fake, "update").mockReturnValueOnce(Future.make(() => { }));
710
+ flow.next(incident);
711
+ flow.cancel();
712
+ expect(flow.callNames()).toEqual(["draft", "update"]);
713
+ expect(flow.onExit).not.toHaveBeenCalled();
714
+ });
715
+ test("doesn't leave when the cancel ends after the flow is gone", () => {
716
+ const flow = setup();
717
+ startReport(flow, { victim: "member-1", transactions: [card] });
718
+ let resolve = () => { };
719
+ vi.spyOn(flow.fake, "cancel").mockReturnValueOnce(Future.make(resolver => {
720
+ resolve = resolver;
721
+ }));
722
+ flow.cancel();
723
+ flow.unmount();
724
+ resolve(Result.Ok({}));
725
+ expect(flow.callNames()).toEqual(["draft", "cancel"]);
726
+ expect(flow.onExit).not.toHaveBeenCalled();
727
+ });
728
+ test("clears the last error and does nothing else while a cancel is pending", () => {
729
+ const flow = setup();
730
+ startReport(flow, { victim: "member-1", transactions: [card] });
731
+ vi.spyOn(flow.fake, "cancel")
732
+ .mockReturnValueOnce(Future.value(Result.Error({ kind: "error", error: new Error("Network error") })))
733
+ .mockReturnValueOnce(Future.make(() => { }));
734
+ flow.cancel();
735
+ flow.cancel();
736
+ flow.cancel();
737
+ flow.next(incident);
738
+ expect(flow.callNames()).toEqual(["draft", "cancel", "cancel"]);
739
+ expect(flow.result.current.saving).toBe(true);
740
+ expect(flow.result.current.error.isNone()).toBe(true);
741
+ expect(flow.onExit).not.toHaveBeenCalled();
742
+ });
743
+ test.each([
744
+ {
745
+ name: "submitted",
746
+ config: {},
747
+ step: "Submitted",
748
+ },
749
+ {
750
+ name: "rejected",
751
+ config: { alreadyReported: [{ transactionId: "card-1", disputeCaseId: null }] },
752
+ step: "Rejected",
753
+ },
754
+ ])("leaves the $name step without calling the API", ({ config, step }) => {
755
+ const flow = setup(config);
756
+ startReport(flow, { victim: "member-1", transactions: [card] });
757
+ flow.next(incident);
758
+ flow.next({ isLostOrStolen: true });
759
+ flow.next();
760
+ expect(flow.result.current.step).toBe(step);
761
+ const callsBefore = flow.callNames();
762
+ flow.cancel();
763
+ expect(flow.callNames()).toEqual(callsBefore);
764
+ expect(flow.onExit).toHaveBeenCalledTimes(1);
765
+ expectReset(flow);
766
+ });
767
+ });
768
+ const getOfferedId = (flow) => flow.result.current.resumable
769
+ .toOption()
770
+ .flatMap(draft => draft)
771
+ .map(({ id }) => id)
772
+ .toUndefined();
773
+ const fromCard = { accountId: "account-1", transactionIds: ["card-1"] };
774
+ // A draft left by an earlier visit, with the incident answered
775
+ const leaveDraft = () => {
776
+ const earlier = setup();
777
+ startReport(earlier, { victim: "member-1", transactions: [card] });
778
+ earlier.next(incident);
779
+ return earlier;
780
+ };
781
+ describe("resume", () => {
782
+ test("starts a new report without looking for drafts when it isn't opened from a transaction", () => {
783
+ const flow = setup();
784
+ expect(flow.result.current.resumable.isNotAsked()).toBe(true);
785
+ expect(flow.callNames()).toEqual([]);
786
+ flow.next();
787
+ expect(flow.result.current.resumable.isNotAsked()).toBe(true);
788
+ });
789
+ test("starts a new report when the transactions have no draft", () => {
790
+ const flow = setup({}, { resumeFrom: fromCard });
791
+ expect(flow.calls).toEqual([{ name: "findDrafts", input: fromCard }]);
792
+ expect(getOfferedId(flow)).toBeUndefined();
793
+ expect(flow.result.current.step).toBe("Intro");
794
+ });
795
+ test("offers the draft and resumes it, prefilled, on its first unanswered step", () => {
796
+ var _a;
797
+ const earlier = leaveDraft();
798
+ const disputeCaseId = earlier.result.current.disputeCaseId.toUndefined();
799
+ const flow = setup({}, { fake: earlier.fake, resumeFrom: fromCard });
800
+ expect(getOfferedId(flow)).toBe(disputeCaseId);
801
+ flow.resume();
802
+ expect(flow.result.current.step).toBe("Card");
803
+ expect(flow.result.current.values).toMatchObject({ transactions: [card], ...incident });
804
+ expect(flow.result.current.disputeCaseId.toUndefined()).toBe(disputeCaseId);
805
+ expect(getOfferedId(flow)).toBeUndefined();
806
+ flow.next({ isLostOrStolen: true });
807
+ expect(flow.callNames()).toEqual(["findDrafts", "update"]);
808
+ expect((_a = flow.calls.at(-1)) === null || _a === void 0 ? void 0 : _a.input).not.toHaveProperty("transactionIds");
809
+ expect(flow.result.current.step).toBe("Documents");
810
+ });
811
+ test("resumes on the transactions step when the draft doesn't hold every start transaction", () => {
812
+ const earlier = leaveDraft();
813
+ const disputeCaseId = earlier.result.current.disputeCaseId.toUndefined();
814
+ const flow = setup({}, {
815
+ fake: earlier.fake,
816
+ resumeFrom: { accountId: "account-1", transactionIds: ["card-1", "transfer-1"] },
817
+ });
818
+ flow.resume();
819
+ expect(flow.result.current.step).toBe("Transactions");
820
+ expect(flow.result.current.values).toMatchObject({ transactions: [card], ...incident });
821
+ flow.next({ transactions: [card, transfer] });
822
+ expect(flow.calls.at(-1)).toMatchObject({
823
+ name: "update",
824
+ input: { disputeCaseId, transactionIds: ["card-1", "transfer-1"] },
825
+ });
826
+ });
827
+ test("offers the latest draft when there are several", () => {
828
+ const fake = leaveDraft().fake;
829
+ const later = setup({}, { fake });
830
+ startReport(later, { victim: "member-1", transactions: [card] });
831
+ const latestId = later.result.current.disputeCaseId.toUndefined();
832
+ const flow = setup({}, { fake, resumeFrom: fromCard });
833
+ expect(getOfferedId(flow)).toBe(latestId);
834
+ });
835
+ test("starts over: cancels the old draft and starts a new report", () => {
836
+ const earlier = leaveDraft();
837
+ const disputeCaseId = earlier.result.current.disputeCaseId.toUndefined();
838
+ const flow = setup({}, { fake: earlier.fake, resumeFrom: fromCard });
839
+ flow.startOver();
840
+ expect(flow.calls.at(-1)).toEqual({ name: "cancel", input: { disputeCaseId } });
841
+ expect(getStatus(flow, disputeCaseId)).toBe("Canceled");
842
+ expect(getOfferedId(flow)).toBeUndefined();
843
+ expect(flow.result.current.step).toBe("Intro");
844
+ startReport(flow, { victim: "member-1", transactions: [card] });
845
+ expect(flow.callNames()).toEqual(["findDrafts", "cancel", "draft"]);
846
+ expect(flow.result.current.disputeCaseId.toUndefined()).not.toBe(disputeCaseId);
847
+ });
848
+ test("waits for the old draft's cancel before starting over", () => {
849
+ const earlier = leaveDraft();
850
+ const disputeCaseId = earlier.result.current.disputeCaseId.toUndefined();
851
+ const flow = setup({}, { fake: earlier.fake, resumeFrom: fromCard });
852
+ vi.spyOn(earlier.fake, "cancel").mockReturnValueOnce(Future.make(() => { }));
853
+ flow.startOver();
854
+ flow.next();
855
+ flow.resume();
856
+ expect(flow.result.current.saving).toBe(true);
857
+ expect(getOfferedId(flow)).toBe(disputeCaseId);
858
+ expect(flow.result.current.step).toBe("Intro");
859
+ });
860
+ test("keeps the offer and exposes the error when the old draft can't be canceled, then retries", () => {
861
+ const error = new Error("Network error");
862
+ const earlier = leaveDraft();
863
+ const disputeCaseId = earlier.result.current.disputeCaseId.toUndefined();
864
+ const flow = setup({}, { fake: earlier.fake, resumeFrom: fromCard });
865
+ vi.spyOn(earlier.fake, "cancel").mockReturnValueOnce(Future.value(Result.Error({ kind: "error", error })));
866
+ flow.startOver();
867
+ expect(getOfferedId(flow)).toBe(disputeCaseId);
868
+ expect(flow.result.current.error.toUndefined()).toEqual({ kind: "error", error });
869
+ expect(flow.result.current.saving).toBe(false);
870
+ expect(flow.result.current.step).toBe("Intro");
871
+ flow.startOver();
872
+ expect(getStatus(flow, disputeCaseId)).toBe("Canceled");
873
+ expect(getOfferedId(flow)).toBeUndefined();
874
+ expect(flow.result.current.error.isNone()).toBe(true);
875
+ });
876
+ test("starts over when the old draft is no longer a draft", () => {
877
+ const earlier = leaveDraft();
878
+ const flow = setup({}, { fake: earlier.fake, resumeFrom: fromCard });
879
+ vi.spyOn(earlier.fake, "cancel").mockReturnValueOnce(rejected({
880
+ __typename: "DisputeCaseNotFoundRejection",
881
+ disputeCaseId: "case-1",
882
+ message: "",
883
+ }));
884
+ flow.startOver();
885
+ expect(getOfferedId(flow)).toBeUndefined();
886
+ expect(flow.result.current.error.isNone()).toBe(true);
887
+ expect(flow.result.current.step).toBe("Intro");
888
+ });
889
+ test("goes to the submitted step, with no error, when the old draft was submitted elsewhere", () => {
890
+ const earlier = leaveDraft();
891
+ earlier.next({ isLostOrStolen: true });
892
+ const disputeCaseId = earlier.result.current.disputeCaseId.toUndefined();
893
+ const flow = setup({}, { fake: earlier.fake, resumeFrom: fromCard });
894
+ earlier.next();
895
+ expect(getStatus(flow, disputeCaseId)).toBe("Submitted");
896
+ flow.startOver();
897
+ expect(flow.result.current.step).toBe("Submitted");
898
+ expect(flow.result.current.error.isNone()).toBe(true);
899
+ expect(flow.result.current.disputeCaseId.toUndefined()).toBe(disputeCaseId);
900
+ expect(getOfferedId(flow)).toBeUndefined();
901
+ expect(flow.result.current.saving).toBe(false);
902
+ });
903
+ test("starts a new report when looking for drafts takes too long", () => {
904
+ vi.useFakeTimers();
905
+ const { fake } = setup();
906
+ const stop = vi.fn();
907
+ vi.spyOn(fake, "findDrafts").mockReturnValueOnce(Future.make(() => stop));
908
+ const flow = setup({}, { fake, resumeFrom: fromCard });
909
+ act(() => {
910
+ vi.advanceTimersByTime(DRAFT_LOOKUP_TIMEOUT_MS - 1);
911
+ });
912
+ expect(flow.result.current.resumable.isLoading()).toBe(true);
913
+ act(() => {
914
+ vi.advanceTimersByTime(1);
915
+ });
916
+ flow.next();
917
+ expect(stop).toHaveBeenCalledTimes(1);
918
+ expect(getOfferedId(flow)).toBeUndefined();
919
+ expect(flow.result.current.step).toBe("Victim");
920
+ });
921
+ test("starts a new report when the drafts can't be read", () => {
922
+ const { fake } = setup();
923
+ vi.spyOn(fake, "findDrafts").mockReturnValueOnce(Future.value(Result.Error({ kind: "error", error: new Error("Network error") })));
924
+ const flow = setup({}, { fake, resumeFrom: fromCard });
925
+ expect(flow.result.current.resumable.isDone()).toBe(true);
926
+ expect(getOfferedId(flow)).toBeUndefined();
927
+ expect(flow.result.current.error.isNone()).toBe(true);
928
+ });
929
+ test("waits on the intro while it looks for drafts", () => {
930
+ const earlier = leaveDraft();
931
+ let resolve = () => { };
932
+ vi.spyOn(earlier.fake, "findDrafts").mockReturnValueOnce(Future.make(resolver => {
933
+ resolve = resolver;
934
+ }));
935
+ const flow = setup({}, { fake: earlier.fake, resumeFrom: fromCard });
936
+ flow.next();
937
+ expect(flow.result.current.resumable.isLoading()).toBe(true);
938
+ expect(flow.result.current.step).toBe("Intro");
939
+ act(() => {
940
+ resolve(Result.Ok([{ id: "found" }]));
941
+ });
942
+ expect(getOfferedId(flow)).toBe("found");
943
+ });
944
+ test("closes the offer when the report starts, keeping the old draft", () => {
945
+ const earlier = leaveDraft();
946
+ const disputeCaseId = earlier.result.current.disputeCaseId.toUndefined();
947
+ const flow = setup({}, { fake: earlier.fake, resumeFrom: fromCard });
948
+ startReport(flow, { victim: "member-1", transactions: [card] });
949
+ const newId = flow.result.current.disputeCaseId.toUndefined();
950
+ flow.resume();
951
+ flow.startOver();
952
+ expect(getOfferedId(flow)).toBeUndefined();
953
+ expect(flow.result.current.disputeCaseId.toUndefined()).toBe(newId);
954
+ expect(flow.callNames()).toEqual(["findDrafts", "draft"]);
955
+ expect(getStatus(flow, disputeCaseId)).toBe("Draft");
956
+ });
957
+ test("doesn't offer a draft found after the user left", () => {
958
+ const earlier = leaveDraft();
959
+ let resolve = () => { };
960
+ vi.spyOn(earlier.fake, "findDrafts").mockReturnValueOnce(Future.make(resolver => {
961
+ resolve = resolver;
962
+ }));
963
+ const flow = setup({}, { fake: earlier.fake, resumeFrom: fromCard });
964
+ flow.cancel();
965
+ act(() => {
966
+ resolve(Result.Ok([{ id: "late" }]));
967
+ });
968
+ expect(flow.onExit).toHaveBeenCalledTimes(1);
969
+ expect(getOfferedId(flow)).toBeUndefined();
970
+ });
971
+ test("stops looking for drafts when the flow is gone", () => {
972
+ const { fake } = setup();
973
+ const stop = vi.fn();
974
+ vi.spyOn(fake, "findDrafts").mockReturnValueOnce(Future.make(() => stop));
975
+ const flow = setup({}, { fake, resumeFrom: fromCard });
976
+ flow.unmount();
977
+ expect(stop).toHaveBeenCalledTimes(1);
978
+ });
979
+ });