-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
ReportUtilsTest.ts
1002 lines (891 loc) · 45.4 KB
/
ReportUtilsTest.ts
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable @typescript-eslint/naming-convention */
import {addDays, format as formatDate} from 'date-fns';
import type {OnyxEntry} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import DateUtils from '@libs/DateUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {PersonalDetailsList, Policy, Report, ReportAction} from '@src/types/onyx';
import {toCollectionDataSet} from '@src/types/utils/CollectionDataSet';
import * as NumberUtils from '../../src/libs/NumberUtils';
import * as LHNTestUtils from '../utils/LHNTestUtils';
import waitForBatchedUpdates from '../utils/waitForBatchedUpdates';
// Be sure to include the mocked permissions library or else the beta tests won't work
jest.mock('@libs/Permissions');
const currentUserEmail = 'bjorn@vikings.net';
const currentUserAccountID = 5;
const participantsPersonalDetails: PersonalDetailsList = {
'1': {
accountID: 1,
displayName: 'Ragnar Lothbrok',
firstName: 'Ragnar',
login: 'ragnar@vikings.net',
},
'2': {
accountID: 2,
login: 'floki@vikings.net',
displayName: 'floki@vikings.net',
},
'3': {
accountID: 3,
displayName: 'Lagertha Lothbrok',
firstName: 'Lagertha',
login: 'lagertha@vikings.net',
pronouns: 'She/her',
},
'4': {
accountID: 4,
login: '+18332403627@expensify.sms',
displayName: '(833) 240-3627',
},
'5': {
accountID: 5,
displayName: 'Lagertha Lothbrok',
firstName: 'Lagertha',
login: 'lagertha2@vikings.net',
pronouns: 'She/her',
},
};
const policy: Policy = {
id: '1',
name: 'Vikings Policy',
role: 'user',
type: 'free',
owner: '',
outputCurrency: '',
isPolicyExpenseChatEnabled: false,
};
Onyx.init({keys: ONYXKEYS});
describe('ReportUtils', () => {
beforeAll(() => {
const policyCollectionDataSet = toCollectionDataSet(ONYXKEYS.COLLECTION.POLICY, [policy], (current) => current.id);
Onyx.multiSet({
[ONYXKEYS.PERSONAL_DETAILS_LIST]: participantsPersonalDetails,
[ONYXKEYS.SESSION]: {email: currentUserEmail, accountID: currentUserAccountID},
[ONYXKEYS.COUNTRY_CODE]: 1,
...policyCollectionDataSet,
});
return waitForBatchedUpdates();
});
beforeEach(() => Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.DEFAULT).then(waitForBatchedUpdates));
describe('getIconsForParticipants', () => {
it('returns sorted avatar source by name, then accountID', () => {
const participants = ReportUtils.getIconsForParticipants([1, 2, 3, 4, 5], participantsPersonalDetails);
expect(participants).toHaveLength(5);
expect(participants[0].source).toBeInstanceOf(Function);
expect(participants[0].name).toBe('(833) 240-3627');
expect(participants[0].id).toBe(4);
expect(participants[0].type).toBe('avatar');
expect(participants[1].source).toBeInstanceOf(Function);
expect(participants[1].name).toBe('floki@vikings.net');
expect(participants[1].id).toBe(2);
expect(participants[1].type).toBe('avatar');
});
});
describe('getDisplayNamesWithTooltips', () => {
test('withSingleParticipantReport', () => {
const participants = ReportUtils.getDisplayNamesWithTooltips(participantsPersonalDetails, false);
expect(participants).toHaveLength(5);
expect(participants[0].displayName).toBe('(833) 240-3627');
expect(participants[0].login).toBe('+18332403627@expensify.sms');
expect(participants[2].displayName).toBe('Lagertha Lothbrok');
expect(participants[2].login).toBe('lagertha@vikings.net');
expect(participants[2].accountID).toBe(3);
expect(participants[2].pronouns).toBe('She/her');
expect(participants[4].displayName).toBe('Ragnar Lothbrok');
expect(participants[4].login).toBe('ragnar@vikings.net');
expect(participants[4].accountID).toBe(1);
expect(participants[4].pronouns).toBeUndefined();
});
});
describe('getReportName', () => {
describe('1:1 DM', () => {
test('with displayName', () => {
expect(
ReportUtils.getReportName({
reportID: '',
participants: ReportUtils.buildParticipantsFromAccountIDs([currentUserAccountID, 1]),
}),
).toBe('Ragnar Lothbrok');
});
test('no displayName', () => {
expect(
ReportUtils.getReportName({
reportID: '',
participants: ReportUtils.buildParticipantsFromAccountIDs([currentUserAccountID, 2]),
}),
).toBe('floki@vikings.net');
});
test('SMS', () => {
expect(
ReportUtils.getReportName({
reportID: '',
participants: ReportUtils.buildParticipantsFromAccountIDs([currentUserAccountID, 4]),
}),
).toBe('(833) 240-3627');
});
});
test('Group DM', () => {
expect(
ReportUtils.getReportName({
reportID: '',
participants: ReportUtils.buildParticipantsFromAccountIDs([currentUserAccountID, 1, 2, 3, 4]),
}),
).toBe('Ragnar, floki@vikings.net, Lagertha, (833) 240-3627');
});
describe('Default Policy Room', () => {
const baseAdminsRoom = {
reportID: '',
chatType: CONST.REPORT.CHAT_TYPE.POLICY_ADMINS,
reportName: '#admins',
};
test('Active', () => {
expect(ReportUtils.getReportName(baseAdminsRoom)).toBe('#admins');
});
test('Archived', () => {
const archivedAdminsRoom = {
...baseAdminsRoom,
statusNum: CONST.REPORT.STATUS_NUM.CLOSED,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
// eslint-disable-next-line @typescript-eslint/naming-convention
private_isArchived: DateUtils.getDBTime(),
};
expect(ReportUtils.getReportName(archivedAdminsRoom)).toBe('#admins (archived)');
return Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.ES).then(() => expect(ReportUtils.getReportName(archivedAdminsRoom)).toBe('#admins (archivado)'));
});
});
describe('User-Created Policy Room', () => {
const baseUserCreatedRoom = {
reportID: '',
chatType: CONST.REPORT.CHAT_TYPE.POLICY_ROOM,
reportName: '#VikingsChat',
};
test('Active', () => {
expect(ReportUtils.getReportName(baseUserCreatedRoom)).toBe('#VikingsChat');
});
test('Archived', () => {
const archivedPolicyRoom = {
...baseUserCreatedRoom,
statusNum: CONST.REPORT.STATUS_NUM.CLOSED,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
// eslint-disable-next-line @typescript-eslint/naming-convention
private_isArchived: DateUtils.getDBTime(),
};
expect(ReportUtils.getReportName(archivedPolicyRoom)).toBe('#VikingsChat (archived)');
return Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.ES).then(() => expect(ReportUtils.getReportName(archivedPolicyRoom)).toBe('#VikingsChat (archivado)'));
});
});
describe('PolicyExpenseChat', () => {
describe('Active', () => {
test('as member', () => {
expect(
ReportUtils.getReportName({
reportID: '',
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
policyID: policy.id,
isOwnPolicyExpenseChat: true,
ownerAccountID: 1,
}),
).toBe('Vikings Policy');
});
test('as admin', () => {
expect(
ReportUtils.getReportName({
reportID: '',
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
policyID: policy.id,
isOwnPolicyExpenseChat: false,
ownerAccountID: 1,
}),
).toBe('Ragnar Lothbrok');
});
});
describe('Archived', () => {
const baseArchivedPolicyExpenseChat = {
reportID: '',
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
ownerAccountID: 1,
policyID: policy.id,
oldPolicyName: policy.name,
statusNum: CONST.REPORT.STATUS_NUM.CLOSED,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
// eslint-disable-next-line @typescript-eslint/naming-convention
private_isArchived: DateUtils.getDBTime(),
};
test('as member', () => {
const memberArchivedPolicyExpenseChat = {
...baseArchivedPolicyExpenseChat,
isOwnPolicyExpenseChat: true,
};
expect(ReportUtils.getReportName(memberArchivedPolicyExpenseChat)).toBe('Vikings Policy (archived)');
return Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.ES).then(() =>
expect(ReportUtils.getReportName(memberArchivedPolicyExpenseChat)).toBe('Vikings Policy (archivado)'),
);
});
test('as admin', () => {
const adminArchivedPolicyExpenseChat = {
...baseArchivedPolicyExpenseChat,
isOwnPolicyExpenseChat: false,
};
expect(ReportUtils.getReportName(adminArchivedPolicyExpenseChat)).toBe('Ragnar Lothbrok (archived)');
return Onyx.set(ONYXKEYS.NVP_PREFERRED_LOCALE, CONST.LOCALES.ES).then(() =>
expect(ReportUtils.getReportName(adminArchivedPolicyExpenseChat)).toBe('Ragnar Lothbrok (archivado)'),
);
});
});
});
});
describe('requiresAttentionFromCurrentUser', () => {
afterEach(async () => {
await Onyx.clear();
await Onyx.set(ONYXKEYS.SESSION, {email: currentUserEmail, accountID: currentUserAccountID});
});
it('returns false when there is no report', () => {
expect(ReportUtils.requiresAttentionFromCurrentUser(undefined)).toBe(false);
});
it('returns false when the matched IOU report does not have an owner accountID', () => {
const report = {
...LHNTestUtils.getFakeReport(),
ownerAccountID: undefined,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(false);
});
it('returns false when the linked iou report has an oustanding IOU', () => {
const report = {
...LHNTestUtils.getFakeReport(),
iouReportID: '1',
};
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}1`, {
reportID: '1',
ownerAccountID: 99,
}).then(() => {
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(false);
});
});
it('returns false when the report has no outstanding IOU but is waiting for a bank account and the logged user is the report owner', () => {
const report = {
...LHNTestUtils.getFakeReport(),
ownerAccountID: currentUserAccountID,
isWaitingOnBankAccount: true,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(false);
});
it('returns false when the report has outstanding IOU and is not waiting for a bank account and the logged user is the report owner', () => {
const report = {
...LHNTestUtils.getFakeReport(),
ownerAccountID: currentUserAccountID,
isWaitingOnBankAccount: false,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(false);
});
it('returns false when the report has no outstanding IOU but is waiting for a bank account and the logged user is not the report owner', () => {
const report = {
...LHNTestUtils.getFakeReport(),
ownerAccountID: 97,
isWaitingOnBankAccount: true,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(false);
});
it('returns true when the report has an unread mention', () => {
const report = {
...LHNTestUtils.getFakeReport(),
isUnreadWithMention: true,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(true);
});
it('returns true when the report is an outstanding task', () => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.TASK,
managerID: currentUserAccountID,
isUnreadWithMention: false,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(true);
});
it('returns true when the report has outstanding child expense', () => {
const report = {
...LHNTestUtils.getFakeReport(),
ownerAccountID: 99,
hasOutstandingChildRequest: true,
isWaitingOnBankAccount: false,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(true);
});
it('returns false if the user is not on free trial', async () => {
await Onyx.multiSet({
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: null, // not on free trial
[ONYXKEYS.NVP_BILLING_FUND_ID]: null, // no payment card added
});
const report: Report = {
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.SYSTEM,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(false);
});
it("returns false if the user free trial hasn't ended yet", async () => {
await Onyx.multiSet({
[ONYXKEYS.NVP_LAST_DAY_FREE_TRIAL]: formatDate(addDays(new Date(), 1), CONST.DATE.FNS_DATE_TIME_FORMAT_STRING), // trial not ended
[ONYXKEYS.NVP_BILLING_FUND_ID]: null, // no payment card added
});
const report: Report = {
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.SYSTEM,
};
expect(ReportUtils.requiresAttentionFromCurrentUser(report)).toBe(false);
});
});
describe('getMoneyRequestOptions', () => {
const participantsAccountIDs = Object.keys(participantsPersonalDetails).map(Number);
beforeAll(() => {
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS_LIST, {
[currentUserAccountID]: {
accountID: currentUserAccountID,
login: currentUserEmail,
},
});
});
afterAll(() => Onyx.clear());
describe('return empty iou options if', () => {
it('participants array contains excluded expensify iou emails', () => {
const allEmpty = CONST.EXPENSIFY_ACCOUNT_IDS.every((accountID) => {
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(undefined, undefined, [currentUserAccountID, accountID]);
return moneyRequestOptions.length === 0;
});
expect(allEmpty).toBe(true);
});
it('it is a room with no participants except self', () => {
const report = {
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_ROOM,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID]);
expect(moneyRequestOptions.length).toBe(0);
});
it('its not your policy expense chat', () => {
const report = {
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
isOwnPolicyExpenseChat: false,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID]);
expect(moneyRequestOptions.length).toBe(0);
});
it('its paid IOU report', () => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.IOU,
statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID]);
expect(moneyRequestOptions.length).toBe(0);
});
it('its approved Expense report', () => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.EXPENSE,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.APPROVED,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID]);
expect(moneyRequestOptions.length).toBe(0);
});
it('its paid Expense report', () => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.EXPENSE,
statusNum: CONST.REPORT.STATUS_NUM.REIMBURSED,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID]);
expect(moneyRequestOptions.length).toBe(0);
});
it('it is an expense report tied to a policy expense chat user does not own', () => {
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}100`, {
reportID: '100',
isOwnPolicyExpenseChat: false,
}).then(() => {
const report = {
...LHNTestUtils.getFakeReport(),
parentReportID: '100',
type: CONST.REPORT.TYPE.EXPENSE,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID]);
expect(moneyRequestOptions.length).toBe(0);
});
});
it("it is a submitted report tied to user's own policy expense chat and the policy does not have Instant Submit frequency", () => {
const paidPolicy: Policy = {
id: '3f54cca8',
type: CONST.POLICY.TYPE.TEAM,
name: '',
role: 'user',
owner: '',
outputCurrency: '',
isPolicyExpenseChatEnabled: false,
};
Promise.all([
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${paidPolicy.id}`, paidPolicy),
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}101`, {
reportID: '101',
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
isOwnPolicyExpenseChat: true,
}),
]).then(() => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.EXPENSE,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
parentReportID: '101',
policyID: paidPolicy.id,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, paidPolicy, [currentUserAccountID, participantsAccountIDs[0]]);
expect(moneyRequestOptions.length).toBe(0);
});
});
});
describe('return only iou split option if', () => {
it('it is a chat room with more than one participant that is not an announce room', () => {
const onlyHaveSplitOption = [CONST.REPORT.CHAT_TYPE.POLICY_ADMINS, CONST.REPORT.CHAT_TYPE.DOMAIN_ALL, CONST.REPORT.CHAT_TYPE.POLICY_ROOM].every((chatType) => {
const report = {
...LHNTestUtils.getFakeReport(),
chatType,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID, participantsAccountIDs[0]]);
return moneyRequestOptions.length === 1 && moneyRequestOptions.includes(CONST.IOU.TYPE.SPLIT);
});
expect(onlyHaveSplitOption).toBe(true);
});
it('has multiple participants excluding self', () => {
const report = {
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_ROOM,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID, ...participantsAccountIDs]);
expect(moneyRequestOptions.length).toBe(1);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SPLIT)).toBe(true);
});
it('user has pay expense permission', () => {
const report = {
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_ROOM,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID, ...participantsAccountIDs]);
expect(moneyRequestOptions.length).toBe(1);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SPLIT)).toBe(true);
});
it("it's a group DM report", () => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.CHAT,
participantsAccountIDs: [currentUserAccountID, ...participantsAccountIDs],
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID, ...participantsAccountIDs.map(Number)]);
expect(moneyRequestOptions.length).toBe(1);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SPLIT)).toBe(true);
});
});
describe('return only submit expense option if', () => {
it('it is an IOU report in submitted state', () => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.IOU,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
managerID: currentUserAccountID,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID, participantsAccountIDs[0]]);
expect(moneyRequestOptions.length).toBe(1);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SUBMIT)).toBe(true);
});
it('it is an IOU report in submitted state even with pay expense permissions', () => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.IOU,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
managerID: currentUserAccountID,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID, participantsAccountIDs[0]]);
expect(moneyRequestOptions.length).toBe(1);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SUBMIT)).toBe(true);
});
});
describe('return only submit expense and track expense options if', () => {
it("it is an expense report tied to user's own policy expense chat", () => {
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}102`, {
reportID: '102',
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
isOwnPolicyExpenseChat: true,
}).then(() => {
const report = {
...LHNTestUtils.getFakeReport(),
parentReportID: '102',
type: CONST.REPORT.TYPE.EXPENSE,
managerID: currentUserAccountID,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID]);
expect(moneyRequestOptions.length).toBe(2);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SUBMIT)).toBe(true);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.TRACK)).toBe(true);
});
});
it("it is an open expense report tied to user's own policy expense chat", () => {
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}103`, {
reportID: '103',
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
isOwnPolicyExpenseChat: true,
}).then(() => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.EXPENSE,
stateNum: CONST.REPORT.STATE_NUM.OPEN,
statusNum: CONST.REPORT.STATUS_NUM.OPEN,
parentReportID: '103',
managerID: currentUserAccountID,
};
const paidPolicy = {
type: CONST.POLICY.TYPE.TEAM,
id: '',
name: '',
role: 'user',
owner: '',
outputCurrency: '',
isPolicyExpenseChatEnabled: false,
} as const;
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, paidPolicy, [currentUserAccountID, participantsAccountIDs[0]]);
expect(moneyRequestOptions.length).toBe(2);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SUBMIT)).toBe(true);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.TRACK)).toBe(true);
});
});
it('it is an IOU report in submitted state', () => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.IOU,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
managerID: currentUserAccountID,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID, participantsAccountIDs[0]]);
expect(moneyRequestOptions.length).toBe(1);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SUBMIT)).toBe(true);
});
it('it is an IOU report in submitted state even with pay expense permissions', () => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.IOU,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
managerID: currentUserAccountID,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID, participantsAccountIDs[0]]);
expect(moneyRequestOptions.length).toBe(1);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SUBMIT)).toBe(true);
});
it("it is a submitted expense report in user's own policyExpenseChat and the policy has Instant Submit frequency", () => {
const paidPolicy: Policy = {
id: 'ef72dfeb',
type: CONST.POLICY.TYPE.TEAM,
autoReporting: true,
autoReportingFrequency: CONST.POLICY.AUTO_REPORTING_FREQUENCIES.INSTANT,
name: '',
role: 'user',
owner: '',
outputCurrency: '',
isPolicyExpenseChatEnabled: false,
};
Promise.all([
Onyx.merge(`${ONYXKEYS.COLLECTION.POLICY}${paidPolicy.id}`, paidPolicy),
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}101`, {
reportID: '101',
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
isOwnPolicyExpenseChat: true,
}),
]).then(() => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.EXPENSE,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
statusNum: CONST.REPORT.STATUS_NUM.SUBMITTED,
parentReportID: '101',
policyID: paidPolicy.id,
managerID: currentUserAccountID,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, paidPolicy, [currentUserAccountID, participantsAccountIDs[0]]);
expect(moneyRequestOptions.length).toBe(2);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SUBMIT)).toBe(true);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.TRACK)).toBe(true);
});
});
});
describe('return multiple expense options if', () => {
it('it is a 1:1 DM', () => {
const report = {
...LHNTestUtils.getFakeReport(),
type: CONST.REPORT.TYPE.CHAT,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID, participantsAccountIDs[0]]);
expect(moneyRequestOptions.length).toBe(3);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SPLIT)).toBe(true);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SUBMIT)).toBe(true);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.PAY)).toBe(true);
});
it("it is user's own policy expense chat", () => {
const report = {
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.POLICY_EXPENSE_CHAT,
isOwnPolicyExpenseChat: true,
managerID: currentUserAccountID,
};
const moneyRequestOptions = ReportUtils.temporary_getMoneyRequestOptions(report, undefined, [currentUserAccountID, ...participantsAccountIDs]);
expect(moneyRequestOptions.length).toBe(3);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SUBMIT)).toBe(true);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.SPLIT)).toBe(true);
expect(moneyRequestOptions.includes(CONST.IOU.TYPE.TRACK)).toBe(true);
});
});
});
describe('getReportIDFromLink', () => {
it('should get the correct reportID from a deep link', () => {
expect(ReportUtils.getReportIDFromLink('new-expensify://r/75431276')).toBe('75431276');
expect(ReportUtils.getReportIDFromLink('https://www.expensify.cash/r/75431276')).toBe('75431276');
expect(ReportUtils.getReportIDFromLink('https://staging.new.expensify.com/r/75431276')).toBe('75431276');
expect(ReportUtils.getReportIDFromLink('https://dev.new.expensify.com/r/75431276')).toBe('75431276');
expect(ReportUtils.getReportIDFromLink('https://staging.expensify.cash/r/75431276')).toBe('75431276');
expect(ReportUtils.getReportIDFromLink('https://new.expensify.com/r/75431276')).toBe('75431276');
});
it("shouldn't get the correct reportID from a deep link", () => {
expect(ReportUtils.getReportIDFromLink('new-expensify-not-valid://r/75431276')).toBe('');
expect(ReportUtils.getReportIDFromLink('new-expensify://settings')).toBe('');
});
});
describe('getMostRecentlyVisitedReport', () => {
it('should filter out report without reportID & lastReadTime and return the most recently visited report', () => {
const reports: Array<OnyxEntry<Report>> = [
{reportID: '1', lastReadTime: '2023-07-08 07:15:44.030'},
{reportID: '2', lastReadTime: undefined},
{reportID: '3', lastReadTime: '2023-07-06 07:15:44.030'},
{reportID: '4', lastReadTime: '2023-07-07 07:15:44.030', type: CONST.REPORT.TYPE.IOU},
{lastReadTime: '2023-07-09 07:15:44.030'} as Report,
{reportID: '6'},
undefined,
];
const latestReport: OnyxEntry<Report> = {reportID: '1', lastReadTime: '2023-07-08 07:15:44.030'};
expect(ReportUtils.getMostRecentlyVisitedReport(reports, undefined)).toEqual(latestReport);
});
});
describe('shouldDisableThread', () => {
const reportID = '1';
it('should disable on thread-disabled actions', () => {
const reportAction = ReportUtils.buildOptimisticCreatedReportAction('email1@test.com');
expect(ReportUtils.shouldDisableThread(reportAction, reportID)).toBeTruthy();
});
it('should disable thread on split expense actions', () => {
const reportAction = ReportUtils.buildOptimisticIOUReportAction(
CONST.IOU.REPORT_ACTION_TYPE.SPLIT,
50000,
CONST.CURRENCY.USD,
'',
[{login: 'email1@test.com'}, {login: 'email2@test.com'}],
NumberUtils.rand64(),
) as ReportAction;
expect(ReportUtils.shouldDisableThread(reportAction, reportID)).toBeTruthy();
});
it('should disable on deleted and not-thread actions', () => {
const reportAction = {
message: [
{
translationKey: '',
type: 'COMMENT',
html: '',
text: '',
isEdited: true,
},
],
childVisibleActionCount: 1,
} as ReportAction;
expect(ReportUtils.shouldDisableThread(reportAction, reportID)).toBeFalsy();
reportAction.childVisibleActionCount = 0;
expect(ReportUtils.shouldDisableThread(reportAction, reportID)).toBeTruthy();
});
it('should disable on archived reports and not-thread actions', () => {
Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`, {
statusNum: CONST.REPORT.STATUS_NUM.CLOSED,
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
})
.then(() => waitForBatchedUpdates())
.then(() => {
const reportAction = {
childVisibleActionCount: 1,
} as ReportAction;
expect(ReportUtils.shouldDisableThread(reportAction, reportID)).toBeFalsy();
reportAction.childVisibleActionCount = 0;
expect(ReportUtils.shouldDisableThread(reportAction, reportID)).toBeTruthy();
});
});
it("should disable on a whisper action and it's neither a report preview nor IOU action", () => {
const reportAction = {
actionName: CONST.REPORT.ACTIONS.TYPE.MODIFIED_EXPENSE,
originalMessage: {
whisperedTo: [123456],
},
} as ReportAction;
expect(ReportUtils.shouldDisableThread(reportAction, reportID)).toBeTruthy();
});
it('should disable on thread first chat', () => {
const reportAction = {
childReportID: reportID,
} as ReportAction;
expect(ReportUtils.shouldDisableThread(reportAction, reportID)).toBeTruthy();
});
});
describe('getAllAncestorReportActions', () => {
const reports: Report[] = [
{reportID: '1', lastReadTime: '2024-02-01 04:56:47.233', reportName: 'Report'},
{reportID: '2', lastReadTime: '2024-02-01 04:56:47.233', parentReportActionID: '1', parentReportID: '1', reportName: 'Report'},
{reportID: '3', lastReadTime: '2024-02-01 04:56:47.233', parentReportActionID: '2', parentReportID: '2', reportName: 'Report'},
{reportID: '4', lastReadTime: '2024-02-01 04:56:47.233', parentReportActionID: '3', parentReportID: '3', reportName: 'Report'},
{reportID: '5', lastReadTime: '2024-02-01 04:56:47.233', parentReportActionID: '4', parentReportID: '4', reportName: 'Report'},
];
const reportActions: ReportAction[] = [
{reportActionID: '1', created: '2024-02-01 04:42:22.965', actionName: 'MARKEDREIMBURSED'},
{reportActionID: '2', created: '2024-02-01 04:42:28.003', actionName: 'MARKEDREIMBURSED'},
{reportActionID: '3', created: '2024-02-01 04:42:31.742', actionName: 'MARKEDREIMBURSED'},
{reportActionID: '4', created: '2024-02-01 04:42:35.619', actionName: 'MARKEDREIMBURSED'},
];
beforeAll(() => {
const reportCollectionDataSet = toCollectionDataSet(ONYXKEYS.COLLECTION.REPORT, reports, (report) => report.reportID);
const reportActionCollectionDataSet = toCollectionDataSet(
ONYXKEYS.COLLECTION.REPORT_ACTIONS,
reportActions.map((reportAction) => ({[reportAction.reportActionID]: reportAction})),
(actions) => Object.values(actions)[0].reportActionID,
);
Onyx.multiSet({
...reportCollectionDataSet,
...reportActionCollectionDataSet,
});
return waitForBatchedUpdates();
});
afterAll(() => Onyx.clear());
it('should return correctly all ancestors of a thread report', () => {
const resultAncestors = [
{report: reports[0], reportAction: reportActions[0], shouldDisplayNewMarker: false},
{report: reports[1], reportAction: reportActions[1], shouldDisplayNewMarker: false},
{report: reports[2], reportAction: reportActions[2], shouldDisplayNewMarker: false},
{report: reports[3], reportAction: reportActions[3], shouldDisplayNewMarker: false},
];
expect(ReportUtils.getAllAncestorReportActions(reports[4])).toEqual(resultAncestors);
});
});
describe('isChatUsedForOnboarding', () => {
afterEach(async () => {
await Onyx.clear();
await Onyx.set(ONYXKEYS.SESSION, {email: currentUserEmail, accountID: currentUserAccountID});
});
it('should return false if the report is neither the system or concierge chat', () => {
expect(ReportUtils.isChatUsedForOnboarding(LHNTestUtils.getFakeReport())).toBeFalsy();
});
it('should return true if the user account ID is odd and report is the system chat', async () => {
const accountID = 1;
await Onyx.multiSet({
[ONYXKEYS.PERSONAL_DETAILS_LIST]: {
[accountID]: {
accountID,
},
},
[ONYXKEYS.SESSION]: {email: currentUserEmail, accountID},
});
const report: Report = {
...LHNTestUtils.getFakeReport(),
chatType: CONST.REPORT.CHAT_TYPE.SYSTEM,
};
expect(ReportUtils.isChatUsedForOnboarding(report)).toBeTruthy();
});
it('should return true if the user account ID is even and report is the concierge chat', async () => {
const accountID = 2;
await Onyx.multiSet({
[ONYXKEYS.PERSONAL_DETAILS_LIST]: {
[accountID]: {
accountID,
},
},
[ONYXKEYS.SESSION]: {email: currentUserEmail, accountID},
});
const report: Report = {
...LHNTestUtils.getFakeReport([accountID, CONST.ACCOUNT_ID.CONCIERGE]),
};
expect(ReportUtils.isChatUsedForOnboarding(report)).toBeTruthy();
});
it("should use the report id from the onboarding NVP if it's set", async () => {
const reportID = '8010';
await Onyx.multiSet({
[ONYXKEYS.NVP_ONBOARDING]: {chatReportID: reportID, hasCompletedGuidedSetupFlow: true},
});
const report1: Report = {
...LHNTestUtils.getFakeReport(),
reportID,
};
expect(ReportUtils.isChatUsedForOnboarding(report1)).toBeTruthy();
const report2: Report = {
...LHNTestUtils.getFakeReport(),
reportID: '8011',
};
expect(ReportUtils.isChatUsedForOnboarding(report2)).toBeFalsy();
});
});
describe('getChatByParticipants', () => {
const userAccountID = 1;
const userAccountID2 = 2;
let oneOnOneChatReport: Report;
let groupChatReport: Report;
beforeAll(() => {
const invoiceReport: Report = {
reportID: '1',
type: CONST.REPORT.TYPE.INVOICE,
participants: {[userAccountID]: {hidden: false}, [currentUserAccountID]: {hidden: false}},
};
const taskReport: Report = {
reportID: '2',
type: CONST.REPORT.TYPE.TASK,
participants: {[userAccountID]: {hidden: false}, [currentUserAccountID]: {hidden: false}},
};
const iouReport: Report = {
reportID: '3',
type: CONST.REPORT.TYPE.IOU,
participants: {[userAccountID]: {hidden: false}, [currentUserAccountID]: {hidden: false}},
};
groupChatReport = {
reportID: '4',
type: CONST.REPORT.TYPE.CHAT,
chatType: CONST.REPORT.CHAT_TYPE.GROUP,
participants: {[userAccountID]: {hidden: false}, [userAccountID2]: {hidden: false}, [currentUserAccountID]: {hidden: false}},
};
oneOnOneChatReport = {
reportID: '5',
type: CONST.REPORT.TYPE.CHAT,
participants: {[userAccountID]: {hidden: false}, [currentUserAccountID]: {hidden: false}},
};
const reportCollectionDataSet = toCollectionDataSet(
ONYXKEYS.COLLECTION.REPORT,
[invoiceReport, taskReport, iouReport, groupChatReport, oneOnOneChatReport],
(item) => item.reportID,
);
return Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, reportCollectionDataSet);
});
it('should return the 1:1 chat', () => {
const report = ReportUtils.getChatByParticipants([currentUserAccountID, userAccountID]);
expect(report?.reportID).toEqual(oneOnOneChatReport.reportID);
});
it('should return the group chat', () => {
const report = ReportUtils.getChatByParticipants([currentUserAccountID, userAccountID, userAccountID2], undefined, true);
expect(report?.reportID).toEqual(groupChatReport.reportID);
});
it('should return undefined when no report is found', () => {
const report = ReportUtils.getChatByParticipants([currentUserAccountID, userAccountID2], undefined);
expect(report).toEqual(undefined);
});