From 96ae45e6709dc1798014a673ba08cd3b759f2672 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Fri, 13 Dec 2024 21:08:11 +0800 Subject: [PATCH 1/2] set new member email and submitsTo optimistically --- src/libs/actions/Policy/Member.ts | 8 ++++++- tests/actions/PolicyMemberTest.ts | 36 +++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/libs/actions/Policy/Member.ts b/src/libs/actions/Policy/Member.ts index 8fb551cdec81..1bd55ea7a7df 100644 --- a/src/libs/actions/Policy/Member.ts +++ b/src/libs/actions/Policy/Member.ts @@ -19,6 +19,7 @@ import enhanceParameters from '@libs/Network/enhanceParameters'; import Parser from '@libs/Parser'; import * as PersonalDetailsUtils from '@libs/PersonalDetailsUtils'; import * as PhoneNumber from '@libs/PhoneNumber'; +import {getDefaultApprover} from '@libs/PolicyUtils'; import * as ReportActionsUtils from '@libs/ReportActionsUtils'; import * as ReportUtils from '@libs/ReportUtils'; import CONST from '@src/CONST'; @@ -627,7 +628,12 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs: InvitedEmailsToAccount const successMembersState: OnyxCollectionInputValue = {}; const failureMembersState: OnyxCollectionInputValue = {}; logins.forEach((email) => { - optimisticMembersState[email] = {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, role: CONST.POLICY.ROLE.USER}; + optimisticMembersState[email] = { + email, + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + role: CONST.POLICY.ROLE.USER, + submitsTo: getDefaultApprover(allPolicies?.[policyKey]), + }; successMembersState[email] = {pendingAction: null}; failureMembersState[email] = { errors: ErrorUtils.getMicroSecondOnyxErrorWithTranslationKey('workspace.people.error.genericAdd'), diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index f9ac32010a23..750787811137 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -228,4 +228,40 @@ describe('actions/PolicyMember', () => { }); }); }); + + describe('addMembersToWorkspace', () => { + it('Add new member to a workspace', async () => { + const policyID = '1'; + const defaultApprover = 'approver@gmail.com'; + const newUserEmail = 'user@gmail.com'; + + await Onyx.set(`${ONYXKEYS.COLLECTION.POLICY}${policyID}`, { + ...createRandomPolicy(Number(policyID)), + approver: defaultApprover, + }); + + mockFetch?.pause?.(); + Member.addMembersToWorkspace({[newUserEmail]: 1234}, 'Welcome', policyID, []); + + await waitForBatchedUpdates(); + + await new Promise((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.POLICY}${policyID}`, + waitForCollectionCallback: false, + callback: (policy) => { + Onyx.disconnect(connection); + const newEmployee = policy?.employeeList?.[newUserEmail]; + expect(newEmployee).not.toBeUndefined(); + expect(newEmployee?.email).toBe(newUserEmail); + expect(newEmployee?.pendingAction).toBe(CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD); + expect(newEmployee?.role).toBe(CONST.POLICY.ROLE.USER); + expect(newEmployee?.submitsTo).toBe(defaultApprover); + resolve(); + }, + }); + }); + await mockFetch?.resume?.(); + }); + }); }); From d3e2dfcbdb90fa22eef93b658c79282b3d5c04c6 Mon Sep 17 00:00:00 2001 From: Bernhard Owen Josephus Date: Fri, 13 Dec 2024 22:57:28 +0800 Subject: [PATCH 2/2] update test wording --- tests/actions/PolicyMemberTest.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/actions/PolicyMemberTest.ts b/tests/actions/PolicyMemberTest.ts index 750787811137..6efbeece1cdb 100644 --- a/tests/actions/PolicyMemberTest.ts +++ b/tests/actions/PolicyMemberTest.ts @@ -230,7 +230,7 @@ describe('actions/PolicyMember', () => { }); describe('addMembersToWorkspace', () => { - it('Add new member to a workspace', async () => { + it('Add a new member to a workspace', async () => { const policyID = '1'; const defaultApprover = 'approver@gmail.com'; const newUserEmail = 'user@gmail.com';