diff --git a/src/libs/SidebarUtils.ts b/src/libs/SidebarUtils.ts index f095ed8ffb01..59a1d4bfefd1 100644 --- a/src/libs/SidebarUtils.ts +++ b/src/libs/SidebarUtils.ts @@ -514,6 +514,9 @@ function getOptionData({ result.alternateText = getRenamedAction(lastAction); } else if (isTaskAction(lastAction)) { result.alternateText = formatReportLastMessageText(getTaskReportActionMessage(lastAction).text); + } else if (lastAction?.actionName === CONST.REPORT.ACTIONS.TYPE.POLICY_CHANGE_LOG.LEAVE_ROOM) { + const actionMessage = getReportActionMessageText(lastAction); + result.alternateText = actionMessage ? `${lastActorDisplayName}: ${actionMessage}` : ''; } else if (isInviteOrRemovedAction(lastAction)) { const lastActionOriginalMessage = lastAction?.actionName ? getOriginalMessage(lastAction) : null; const targetAccountIDs = lastActionOriginalMessage?.targetAccountIDs ?? []; diff --git a/tests/unit/SidebarUtilsTest.ts b/tests/unit/SidebarUtilsTest.ts index a666d7711f1f..3d216aac9566 100644 --- a/tests/unit/SidebarUtilsTest.ts +++ b/tests/unit/SidebarUtilsTest.ts @@ -1,12 +1,15 @@ /* eslint-disable @typescript-eslint/naming-convention */ import type {OnyxCollection, OnyxEntry} from 'react-native-onyx'; import Onyx from 'react-native-onyx'; +import {getReportActionMessageText} from '@libs/ReportActionsUtils'; import SidebarUtils from '@libs/SidebarUtils'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report, ReportAction, ReportActions, TransactionViolation, TransactionViolations} from '@src/types/onyx'; import type {ReportCollectionDataSet} from '@src/types/onyx/Report'; import type {TransactionViolationsCollectionDataSet} from '@src/types/onyx/TransactionViolation'; +import createRandomReportAction from '../utils/collections/reportActions'; +import createRandomReport from '../utils/collections/reports'; describe('SidebarUtils', () => { beforeAll(() => @@ -334,4 +337,61 @@ describe('SidebarUtils', () => { expect(result).toBe(false); }); }); + + describe('getOptionsData', () => { + it('returns the last action message as an alternate text if the action is POLICYCHANGELOG_LEAVEROOM type', async () => { + // When a report has last action of POLICYCHANGELOG_LEAVEROOM type + const report: Report = { + ...createRandomReport(1), + chatType: 'policyAdmins', + lastMessageHtml: 'removed 0 user', + lastMessageText: 'removed 0 user', + lastVisibleActionCreated: '2025-01-20 12:30:03.784', + participants: { + '18921695': { + notificationPreference: 'always', + }, + }, + }; + const lastAction: ReportAction = { + ...createRandomReportAction(2), + message: [ + { + type: 'COMMENT', + html: 'removed from #r1', + text: 'removed from #r1', + isDeletedParentAction: false, + deleted: '', + }, + ], + actionName: CONST.REPORT.ACTIONS.TYPE.POLICY_CHANGE_LOG.LEAVE_ROOM, + actorAccountID: 18921695, + person: [ + { + type: 'TEXT', + style: 'strong', + text: 'f50', + }, + ], + originalMessage: undefined, + }; + const reportActions: ReportActions = {[lastAction.reportActionID]: lastAction}; + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`, report); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${report.reportID}`, reportActions); + + const result = SidebarUtils.getOptionData({ + report, + reportActions, + reportNameValuePairs: {}, + hasViolations: false, + personalDetails: {}, + policy: undefined, + parentReportAction: undefined, + preferredLocale: CONST.LOCALES.EN, + }); + + // Then the alternate text should be equal to the message of the last action prepended with the last actor display name. + expect(result?.alternateText).toBe(`${lastAction.person?.[0].text}: ${getReportActionMessageText(lastAction)}`); + }); + }); });