2
2
import Onyx from 'react-native-onyx' ;
3
3
import type { OnyxEntry } from 'react-native-onyx' ;
4
4
import DateUtils from '@libs/DateUtils' ;
5
- import { getActivePolicies , getPolicyNameByID , getRateDisplayValue , getSubmitToAccountID , getUnitRateValue , shouldShowPolicy } from '@libs/PolicyUtils' ;
5
+ import { getActivePolicies , getManagerAccountID , getPolicyNameByID , getRateDisplayValue , getSubmitToAccountID , getUnitRateValue , shouldShowPolicy } from '@libs/PolicyUtils' ;
6
6
import CONST from '@src/CONST' ;
7
7
import ONYXKEYS from '@src/ONYXKEYS' ;
8
8
import type { PersonalDetailsList , Policy , PolicyEmployeeList , Report , Transaction } from '@src/types/onyx' ;
@@ -20,6 +20,12 @@ const CARLOS_ACCOUNT_ID = 1;
20
20
function toLocaleDigitMock ( dot : string ) : string {
21
21
return dot ;
22
22
}
23
+ const GENERATED_ACCOUNT_ID = '555555' ;
24
+
25
+ jest . mock ( '@libs/UserUtils' , ( ) => ( {
26
+ // generateAccountID: () => GENERATED_ACCOUNT_ID,
27
+ generateAccountID : jest . fn ( ) . mockReturnValue ( GENERATED_ACCOUNT_ID ) ,
28
+ } ) ) ;
23
29
24
30
const testDate = DateUtils . getDBTime ( ) ;
25
31
const employeeList : PolicyEmployeeList = {
@@ -67,19 +73,22 @@ const categoryapprover2AccountID = 4;
67
73
const tagapprover1AccountID = 5 ;
68
74
const tagapprover2AccountID = 6 ;
69
75
const ownerAccountID = 7 ;
76
+ const employeeEmail = '[email protected] ' ;
77
+ const adminEmail = '[email protected] ' ;
78
+ const categoryApprover1Email = '[email protected] ' ;
70
79
71
80
const personalDetails : PersonalDetailsList = {
72
81
'1' : {
73
82
accountID : adminAccountID ,
74
-
83
+ login : adminEmail ,
75
84
} ,
76
85
'2' : {
77
86
accountID : employeeAccountID ,
78
-
87
+ login : employeeEmail ,
79
88
} ,
80
89
'3' : {
81
90
accountID : categoryapprover1AccountID ,
82
-
91
+ login : categoryApprover1Email ,
83
92
} ,
84
93
'4' : {
85
94
accountID : categoryapprover2AccountID ,
@@ -499,4 +508,85 @@ describe('PolicyUtils', () => {
499
508
expect ( getPolicyNameByID ( '1' ) ) . toBe ( '1' ) ;
500
509
} ) ;
501
510
} ) ;
511
+
512
+ describe ( 'getManagerAccountID' , ( ) => {
513
+ beforeEach ( ( ) => {
514
+ wrapOnyxWithWaitForBatchedUpdates ( Onyx ) ;
515
+ Onyx . set ( ONYXKEYS . PERSONAL_DETAILS_LIST , personalDetails ) ;
516
+ } ) ;
517
+ afterEach ( async ( ) => {
518
+ await Onyx . clear ( ) ;
519
+ await waitForBatchedUpdatesWithAct ( ) ;
520
+ } ) ;
521
+
522
+ it ( 'should return default approver for personal workspaces' , ( ) => {
523
+ const policy : Policy = {
524
+ ...createRandomPolicy ( 0 ) ,
525
+ type : CONST . POLICY . TYPE . PERSONAL ,
526
+ approver : categoryApprover1Email ,
527
+ } ;
528
+ const report : Report = {
529
+ ...createRandomReport ( 0 ) ,
530
+ } ;
531
+ const result = getManagerAccountID ( policy , report ) ;
532
+
533
+ expect ( result ) . toBe ( categoryapprover1AccountID ) ;
534
+ } ) ;
535
+
536
+ it ( 'should return -1 if there is no employee or default approver' , ( ) => {
537
+ const policy : Policy = {
538
+ ...createRandomPolicy ( 0 ) ,
539
+ type : CONST . POLICY . TYPE . TEAM ,
540
+ approvalMode : undefined ,
541
+ approver : undefined ,
542
+ owner : '' ,
543
+ } ;
544
+ const report : Report = {
545
+ ...createRandomReport ( 0 ) ,
546
+ } ;
547
+
548
+ const result = getManagerAccountID ( policy , report ) ;
549
+
550
+ expect ( result ) . toBe ( - 1 ) ;
551
+ } ) ;
552
+
553
+ it ( 'should return submitsTo account ID' , ( ) => {
554
+ const policy : Policy = {
555
+ ...createRandomPolicy ( 0 ) ,
556
+ type : CONST . POLICY . TYPE . TEAM ,
557
+ approvalMode : undefined ,
558
+ employeeList : {
559
+ [ employeeEmail ] : {
560
+ email : employeeEmail ,
561
+ submitsTo : adminEmail ,
562
+ } ,
563
+ } ,
564
+ } ;
565
+ const report : Report = {
566
+ ...createRandomReport ( 0 ) ,
567
+ ownerAccountID : employeeAccountID ,
568
+ } ;
569
+
570
+ const result = getManagerAccountID ( policy , report ) ;
571
+
572
+ expect ( result ) . toBe ( adminAccountID ) ;
573
+ } ) ;
574
+
575
+ it ( 'should return the default approver' , ( ) => {
576
+ const policy : Policy = {
577
+ ...createRandomPolicy ( 0 ) ,
578
+ type : CONST . POLICY . TYPE . TEAM ,
579
+ approvalMode : undefined ,
580
+ approver : categoryApprover1Email ,
581
+ } ;
582
+ const report : Report = {
583
+ ...createRandomReport ( 0 ) ,
584
+ ownerAccountID : employeeAccountID ,
585
+ } ;
586
+
587
+ const result = getManagerAccountID ( policy , report ) ;
588
+
589
+ expect ( result ) . toBe ( categoryapprover1AccountID ) ;
590
+ } ) ;
591
+ } ) ;
502
592
} ) ;
0 commit comments