@@ -362,7 +362,7 @@ describe('actions/PolicyMember', () => {
362362 } ) ;
363363
364364 describe ( 'addMembersToWorkspace' , ( ) => {
365- const currentUserAccountID = 1 ;
365+ const currentUser = { accountID : 1 , displayName : 'Current User' , email : 'current@example.com' } ;
366366
367367 it ( 'Add a new member to a workspace' , async ( ) => {
368368 const policyID = '1' ;
@@ -376,7 +376,7 @@ describe('actions/PolicyMember', () => {
376376 await Onyx . set ( `${ ONYXKEYS . COLLECTION . POLICY } ${ policyID } ` , policy ) ;
377377
378378 mockFetch ?. pause ?.( ) ;
379- Member . addMembersToWorkspace ( { [ newUserEmail ] : 1234 } , 'Welcome' , policy , [ ] , CONST . POLICY . ROLE . USER , TestHelper . formatPhoneNumber , currentUserAccountID ) ;
379+ Member . addMembersToWorkspace ( { [ newUserEmail ] : 1234 } , 'Welcome' , policy , [ ] , CONST . POLICY . ROLE . USER , TestHelper . formatPhoneNumber , currentUser ) ;
380380
381381 await waitForBatchedUpdates ( ) ;
382382
@@ -430,9 +430,9 @@ describe('actions/PolicyMember', () => {
430430
431431 // When adding a new admin, auditor, and user members
432432 mockFetch ?. pause ?.( ) ;
433- Member . addMembersToWorkspace ( { [ adminEmail ] : adminAccountID } , 'Welcome' , policy , [ ] , CONST . POLICY . ROLE . ADMIN , TestHelper . formatPhoneNumber , currentUserAccountID ) ;
434- Member . addMembersToWorkspace ( { [ auditorEmail ] : auditorAccountID } , 'Welcome' , policy , [ ] , CONST . POLICY . ROLE . AUDITOR , TestHelper . formatPhoneNumber , currentUserAccountID ) ;
435- Member . addMembersToWorkspace ( { [ userEmail ] : userAccountID } , 'Welcome' , policy , [ ] , CONST . POLICY . ROLE . USER , TestHelper . formatPhoneNumber , currentUserAccountID ) ;
433+ Member . addMembersToWorkspace ( { [ adminEmail ] : adminAccountID } , 'Welcome' , policy , [ ] , CONST . POLICY . ROLE . ADMIN , TestHelper . formatPhoneNumber , currentUser ) ;
434+ Member . addMembersToWorkspace ( { [ auditorEmail ] : auditorAccountID } , 'Welcome' , policy , [ ] , CONST . POLICY . ROLE . AUDITOR , TestHelper . formatPhoneNumber , currentUser ) ;
435+ Member . addMembersToWorkspace ( { [ userEmail ] : userAccountID } , 'Welcome' , policy , [ ] , CONST . POLICY . ROLE . USER , TestHelper . formatPhoneNumber , currentUser ) ;
436436
437437 await waitForBatchedUpdates ( ) ;
438438
@@ -495,7 +495,7 @@ describe('actions/PolicyMember', () => {
495495 } ) ;
496496
497497 // When adding the user to the workspace
498- Member . addMembersToWorkspace ( { [ userEmail ] : userAccountID } , 'Welcome' , policy , [ ] , CONST . POLICY . ROLE . USER , TestHelper . formatPhoneNumber , currentUserAccountID ) ;
498+ Member . addMembersToWorkspace ( { [ userEmail ] : userAccountID } , 'Welcome' , policy , [ ] , CONST . POLICY . ROLE . USER , TestHelper . formatPhoneNumber , currentUser ) ;
499499
500500 await waitForBatchedUpdates ( ) ;
501501
@@ -562,7 +562,7 @@ describe('actions/PolicyMember', () => {
562562 [ ] ,
563563 CONST . POLICY . ROLE . USER ,
564564 TestHelper . formatPhoneNumber ,
565- currentUserAccountID ,
565+ currentUser ,
566566 undefined ,
567567 reportActionsList ,
568568 ) ;
@@ -580,6 +580,62 @@ describe('actions/PolicyMember', () => {
580580 } ) ;
581581 expect ( isExpenseReportArchived ) . toBe ( false ) ;
582582 } ) ;
583+
584+ describe ( 'buildAddMembersToWorkspaceOnyxData currentUser dependency' , ( ) => {
585+ const buildForCurrentUser = ( currentUserInput : { accountID : number ; displayName ?: string ; email ?: string ; avatar ?: string } ) =>
586+ Member . buildAddMembersToWorkspaceOnyxData (
587+ // eslint-disable-next-line @typescript-eslint/naming-convention
588+ { 'new-member@example.com' : 9001 } ,
589+ createRandomPolicy ( 101 ) ,
590+ [ ] ,
591+ CONST . POLICY . ROLE . USER ,
592+ TestHelper . formatPhoneNumber ,
593+ currentUserInput ,
594+ ) ;
595+
596+ type BuildResult = ReturnType < typeof buildForCurrentUser > ;
597+
598+ const findOptimisticCreatedAction = ( optimisticData : BuildResult [ 'optimisticData' ] ) => {
599+ for ( const update of optimisticData ) {
600+ if ( ! update . key . startsWith ( ONYXKEYS . COLLECTION . REPORT_ACTIONS ) ) {
601+ continue ;
602+ }
603+ const value = update . value as Record < string , ReportAction > | null | undefined ;
604+ if ( ! value ) {
605+ continue ;
606+ }
607+ const createdAction = Object . values ( value ) . find ( ( action ) => action ?. actionName === CONST . REPORT . ACTIONS . TYPE . CREATED ) ;
608+ if ( createdAction ) {
609+ return createdAction ;
610+ }
611+ }
612+ return undefined ;
613+ } ;
614+
615+ it ( 'actorAccountID on the optimistic CREATED action equals currentUser.accountID' , ( ) => {
616+ const { optimisticData : dataForA } = buildForCurrentUser ( { accountID : 42 , displayName : 'A' , email : 'a@example.com' } ) ;
617+ const { optimisticData : dataForB } = buildForCurrentUser ( { accountID : 99 , displayName : 'B' , email : 'b@example.com' } ) ;
618+
619+ expect ( findOptimisticCreatedAction ( dataForA ) ?. actorAccountID ) . toBe ( 42 ) ;
620+ expect ( findOptimisticCreatedAction ( dataForB ) ?. actorAccountID ) . toBe ( 99 ) ;
621+ } ) ;
622+
623+ it ( 'person text on the optimistic CREATED action equals currentUser.displayName when defined' , ( ) => {
624+ const { optimisticData} = buildForCurrentUser ( { accountID : 1 , displayName : 'Alice Smith' , email : 'alice@example.com' } ) ;
625+ expect ( findOptimisticCreatedAction ( optimisticData ) ?. person ?. at ( 0 ) ?. text ) . toBe ( 'Alice Smith' ) ;
626+ } ) ;
627+
628+ it ( 'person text on the optimistic CREATED action falls back to currentUser.email when displayName is undefined' , ( ) => {
629+ const { optimisticData} = buildForCurrentUser ( { accountID : 1 , email : 'fallback@example.com' } ) ;
630+ expect ( findOptimisticCreatedAction ( optimisticData ) ?. person ?. at ( 0 ) ?. text ) . toBe ( 'fallback@example.com' ) ;
631+ } ) ;
632+
633+ it ( 'avatar on the optimistic CREATED action equals currentUser.avatar when defined' , ( ) => {
634+ const avatar = 'https://avatar.example/me.png' ;
635+ const { optimisticData} = buildForCurrentUser ( { accountID : 1 , displayName : 'Alice' , email : 'alice@example.com' , avatar} ) ;
636+ expect ( findOptimisticCreatedAction ( optimisticData ) ?. avatar ) . toBe ( avatar ) ;
637+ } ) ;
638+ } ) ;
583639 } ) ;
584640
585641 describe ( 'removeMembers' , ( ) => {
0 commit comments