diff --git a/src/libs/SearchUIUtils.ts b/src/libs/SearchUIUtils.ts index e1e6c25d7569..81ca21cad7a2 100644 --- a/src/libs/SearchUIUtils.ts +++ b/src/libs/SearchUIUtils.ts @@ -2590,8 +2590,8 @@ function getTaskSections( const report = getReportOrDraftReport(taskItem.reportID) ?? taskItem; const parentReport = getReportOrDraftReport(taskItem.parentReportID) ?? data[`${ONYXKEYS.COLLECTION.REPORT}${taskItem.parentReportID}`]; - const reportName = StringUtils.lineBreaksToSpaces(Parser.htmlToText(taskItem.reportName)); - const description = StringUtils.lineBreaksToSpaces(Parser.htmlToText(taskItem.description)); + const reportName = StringUtils.lineBreaksToSpaces(Parser.htmlToText(taskItem.reportName ?? '')); + const description = StringUtils.lineBreaksToSpaces(Parser.htmlToText(taskItem.description ?? '')); const result: TaskListItemType = { ...taskItem, diff --git a/tests/unit/Search/SearchUIUtilsTest.ts b/tests/unit/Search/SearchUIUtilsTest.ts index fa39006076e2..882cd0cd9c5d 100644 --- a/tests/unit/Search/SearchUIUtilsTest.ts +++ b/tests/unit/Search/SearchUIUtilsTest.ts @@ -2674,6 +2674,57 @@ describe('SearchUIUtils', () => { expect(allReportActionsLength).toBe(6); }); + it('should not crash when a task has no description or reportName (type TASK)', () => { + const taskReportID = '987654321'; + const taskReport = createMock({ + type: CONST.REPORT.TYPE.TASK, + accountID: adminAccountID, + managerID: adminAccountID, + created: '2024-01-01 00:00:00', + parentReportID: '123456789', + reportID: taskReportID, + stateNum: CONST.REPORT.STATE_NUM.OPEN, + statusNum: CONST.REPORT.STATUS_NUM.OPEN, + // description and reportName intentionally omitted: a task created without a description + // has `description === undefined`, which used to crash htmlToText. + }); + const taskData = createMock({ + personalDetailsList: { + [adminAccountID]: { + accountID: adminAccountID, + avatar: '', + displayName: 'Task Admin', + login: adminEmail, + }, + }, + [`report_${taskReportID}`]: taskReport, + }); + + const getTaskSectionsResult = () => + getSectionsByType( + SearchUIUtils.getSections({ + type: CONST.SEARCH.DATA_TYPES.TASK, + data: taskData, + currentAccountID: adminAccountID, + currentUserEmail: adminEmail, + translate: translateLocal, + formatPhoneNumber, + bankAccountList: {}, + conciergeReportID: undefined, + convertToDisplayString, + reportAttributesDerivedValue: {}, + }), + SearchUIUtils.isTaskListItemType, + )[0]; + + expect(getTaskSectionsResult).not.toThrow(); + + const [task] = getTaskSectionsResult(); + expect(task.reportID).toBe(taskReportID); + expect(task.description).toBe(''); + expect(task.reportName).toBe(''); + }); + it('should return getTransactionsSections result when groupBy is undefined', () => { expect( SearchUIUtils.getSections({