diff --git a/package.json b/package.json index 3dd61e7d92bf..737e176d5be5 100644 --- a/package.json +++ b/package.json @@ -45,7 +45,7 @@ "perf-test": "NODE_OPTIONS=--experimental-vm-modules npx reassure", "typecheck": "NODE_OPTIONS=--max_old_space_size=8192 tsc", "typecheck-tsgo": "tsgo --project tsconfig.tsgo.json", - "lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=383 --cache --cache-location=node_modules/.cache/eslint --cache-strategy content --concurrency=auto", + "lint": "NODE_OPTIONS=--max_old_space_size=8192 eslint . --max-warnings=382 --cache --cache-location=node_modules/.cache/eslint --cache-strategy content --concurrency=auto", "lint-changed": "NODE_OPTIONS=--max_old_space_size=8192 ./scripts/lintChanged.sh", "check-lazy-loading": "ts-node scripts/checkLazyLoading.ts", "lint-watch": "npx eslint-watch --watch --changed", diff --git a/src/libs/actions/Report/index.ts b/src/libs/actions/Report/index.ts index ca315fcabdf4..72170401acd1 100644 --- a/src/libs/actions/Report/index.ts +++ b/src/libs/actions/Report/index.ts @@ -311,7 +311,6 @@ type AddAttachmentWithCommentParams = { }; const addNewMessageWithText = new Set([WRITE_COMMANDS.ADD_COMMENT, WRITE_COMMANDS.ADD_TEXT_AND_ATTACHMENT]); -let conciergeReportIDOnyxConnect: string | undefined; let deprecatedCurrentUserAccountID = -1; /** @deprecated This value is deprecated and will be removed soon after migration. Use the email from useCurrentUserPersonalDetails hook instead. */ let deprecatedCurrentUserLogin: string | undefined; @@ -321,7 +320,6 @@ Onyx.connect({ callback: (value) => { // When signed out, val is undefined if (!value?.accountID) { - conciergeReportIDOnyxConnect = undefined; return; } // eslint-disable-next-line @typescript-eslint/no-deprecated @@ -330,11 +328,6 @@ Onyx.connect({ }, }); -Onyx.connect({ - key: ONYXKEYS.CONCIERGE_REPORT_ID, - callback: (value) => (conciergeReportIDOnyxConnect = value), -}); - // map of reportID to all reportActions for that report const allReportActions: OnyxCollection = {}; @@ -4056,9 +4049,9 @@ function navigateToMostRecentReport(currentReport: OnyxEntry, conciergeR } } -function getMostRecentReportID(currentReport: OnyxEntry) { +function getMostRecentReportID(currentReport: OnyxEntry, conciergeReportID: string | undefined) { const lastAccessedReportID = findLastAccessedReport(false, false, currentReport?.reportID)?.reportID; - return lastAccessedReportID ?? conciergeReportIDOnyxConnect; + return lastAccessedReportID ?? conciergeReportID; } function joinRoom(report: OnyxEntry, currentUserAccountID: number) { diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index e00ba5ea1f24..d72ad78549b5 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -1025,7 +1025,7 @@ function getShareDestination( * @param report - The task report being deleted * @returns The URL to navigate to */ -function getNavigationUrlOnTaskDelete(report: OnyxEntry): string | undefined { +function getNavigationUrlOnTaskDelete(report: OnyxEntry, conciergeReportID: string | undefined): string | undefined { if (!report) { return undefined; } @@ -1040,7 +1040,7 @@ function getNavigationUrlOnTaskDelete(report: OnyxEntry): stri } // If no parent report, try to navigate to most recent report - const mostRecentReportID = getMostRecentReportID(report); + const mostRecentReportID = getMostRecentReportID(report, conciergeReportID); if (mostRecentReportID) { return ROUTES.REPORT_WITH_ID.getRoute(mostRecentReportID); } @@ -1058,6 +1058,7 @@ function deleteTask( currentUserAccountID: number, hasOutstandingChildTask: boolean, parentReportAction: OnyxEntry, + conciergeReportID: string | undefined, ancestors: ReportUtils.Ancestor[] = [], ) { if (!report) { @@ -1183,7 +1184,7 @@ function deleteTask( API.write(WRITE_COMMANDS.CANCEL_TASK, parameters, {optimisticData, successData, failureData}); notifyNewAction(report.reportID, undefined, true); - const urlToNavigateBack = getNavigationUrlOnTaskDelete(report); + const urlToNavigateBack = getNavigationUrlOnTaskDelete(report, conciergeReportID); if (urlToNavigateBack) { Navigation.goBack(); return urlToNavigateBack; diff --git a/src/pages/ReportDetailsPage.tsx b/src/pages/ReportDetailsPage.tsx index 9cbda7d61d0b..4249f0ecc19e 100644 --- a/src/pages/ReportDetailsPage.tsx +++ b/src/pages/ReportDetailsPage.tsx @@ -875,7 +875,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail const deleteTransaction = useCallback(() => { if (caseID === CASES.DEFAULT) { - deleteTask(report, parentReport, isReportArchived, currentUserPersonalDetails.accountID, hasOutstandingChildTask, parentReportAction, ancestors); + deleteTask(report, parentReport, isReportArchived, currentUserPersonalDetails.accountID, hasOutstandingChildTask, parentReportAction, conciergeReportID, ancestors); return; } @@ -928,6 +928,7 @@ function ReportDetailsPage({policy, report, route, reportMetadata}: ReportDetail deleteTransactions, currentSearchHash, removeTransaction, + conciergeReportID, ]); // Where to navigate back to after deleting the transaction and its report. diff --git a/tests/actions/TaskTest.ts b/tests/actions/TaskTest.ts index 333a34327b88..97e69a53ccce 100644 --- a/tests/actions/TaskTest.ts +++ b/tests/actions/TaskTest.ts @@ -5,13 +5,26 @@ import Onyx from 'react-native-onyx'; import type {OnyxEntry} from 'react-native-onyx'; import useParentReport from '@hooks/useParentReport'; import useReportIsArchived from '@hooks/useReportIsArchived'; -import {canActionTask, canModifyTask, completeTask, completeTestDriveTask, createTaskAndNavigate, getFinishOnboardingTaskOnyxData} from '@libs/actions/Task'; +// eslint-disable-next-line no-restricted-syntax -- this is required to allow mocking +import * as ReportModule from '@libs/actions/Report'; +import { + canActionTask, + canModifyTask, + completeTask, + completeTestDriveTask, + createTaskAndNavigate, + deleteTask, + getFinishOnboardingTaskOnyxData, + getNavigationUrlOnTaskDelete, +} from '@libs/actions/Task'; // eslint-disable-next-line no-restricted-syntax -- this is required to allow mocking import * as API from '@libs/API'; // eslint-disable-next-line no-restricted-syntax -- this is required to allow mocking import DateUtils from '@libs/DateUtils'; import Navigation from '@libs/Navigation/Navigation'; // eslint-disable-next-line no-restricted-syntax -- this is required to allow mocking +import * as ReportActionsUtils from '@libs/ReportActionsUtils'; +// eslint-disable-next-line no-restricted-syntax -- this is required to allow mocking import * as ReportUtils from '@libs/ReportUtils'; import initOnyxDerivedValues from '@userActions/OnyxDerived'; import CONST from '@src/CONST'; @@ -837,4 +850,286 @@ describe('actions/Task', () => { expect(parentReportActionUpdate).toBeUndefined(); }); }); + + describe('getNavigationUrlOnTaskDelete', () => { + let doesReportHaveVisibleActionsSpy: jest.SpyInstance; + let getMostRecentReportIDSpy: jest.SpyInstance; + + beforeEach(() => { + doesReportHaveVisibleActionsSpy = jest.spyOn(ReportActionsUtils, 'doesReportHaveVisibleActions'); + getMostRecentReportIDSpy = jest.spyOn(ReportModule, 'getMostRecentReportID'); + }); + + afterEach(() => { + doesReportHaveVisibleActionsSpy.mockRestore(); + getMostRecentReportIDSpy.mockRestore(); + }); + + it('should return undefined when report is undefined', () => { + expect(getNavigationUrlOnTaskDelete(undefined, 'concierge_123')).toBeUndefined(); + }); + + it('should return undefined when report has visible actions (should not delete)', () => { + const taskReport = getFakeReport(); + doesReportHaveVisibleActionsSpy.mockReturnValue(true); + + expect(getNavigationUrlOnTaskDelete(taskReport, 'concierge_123')).toBeUndefined(); + }); + + it('should return parent report route when report has parentReportID and no visible actions', () => { + const parentReportID = 'parent_123'; + const taskReport = {...getFakeReport(), parentReportID}; + doesReportHaveVisibleActionsSpy.mockReturnValue(false); + + const result = getNavigationUrlOnTaskDelete(taskReport, 'concierge_123'); + expect(result).toBe(`r/${parentReportID}`); + }); + + it('should return most recent report route when no parentReportID and getMostRecentReportID returns a value', () => { + const taskReport = {...getFakeReport(), parentReportID: undefined}; + const mostRecentReportID = 'recent_456'; + doesReportHaveVisibleActionsSpy.mockReturnValue(false); + getMostRecentReportIDSpy.mockReturnValue(mostRecentReportID); + + const result = getNavigationUrlOnTaskDelete(taskReport, 'concierge_123'); + expect(result).toBe(`r/${mostRecentReportID}`); + expect(getMostRecentReportIDSpy).toHaveBeenCalledWith(taskReport, 'concierge_123'); + }); + + it('should pass conciergeReportID to getMostRecentReportID as fallback', () => { + const taskReport = {...getFakeReport(), parentReportID: undefined}; + const conciergeReportID = 'concierge_789'; + doesReportHaveVisibleActionsSpy.mockReturnValue(false); + getMostRecentReportIDSpy.mockReturnValue(conciergeReportID); + + const result = getNavigationUrlOnTaskDelete(taskReport, conciergeReportID); + expect(result).toBe(`r/${conciergeReportID}`); + expect(getMostRecentReportIDSpy).toHaveBeenCalledWith(taskReport, conciergeReportID); + }); + + it('should return undefined when no parentReportID, no most recent report, and conciergeReportID is undefined', () => { + const taskReport = {...getFakeReport(), parentReportID: undefined}; + doesReportHaveVisibleActionsSpy.mockReturnValue(false); + getMostRecentReportIDSpy.mockReturnValue(undefined); + + expect(getNavigationUrlOnTaskDelete(taskReport, undefined)).toBeUndefined(); + }); + }); + + describe('deleteTask', () => { + let doesReportHaveVisibleActionsSpy: jest.SpyInstance; + let getMostRecentReportIDSpy: jest.SpyInstance; + const mockCurrentUserAccountID = 123; + + beforeEach(async () => { + jest.clearAllMocks(); + writeSpy.mockClear(); + + global.fetch = getGlobalFetchMock(); + + doesReportHaveVisibleActionsSpy = jest.spyOn(ReportActionsUtils, 'doesReportHaveVisibleActions'); + getMostRecentReportIDSpy = jest.spyOn(ReportModule, 'getMostRecentReportID'); + + await act(async () => { + await Onyx.clear(); + await Onyx.set(ONYXKEYS.SESSION, { + email: 'user@example.com', + accountID: mockCurrentUserAccountID, + }); + }); + await waitForBatchedUpdatesWithAct(); + }); + + afterEach(() => { + doesReportHaveVisibleActionsSpy.mockRestore(); + getMostRecentReportIDSpy.mockRestore(); + }); + + it('should return early when report is undefined', () => { + deleteTask(undefined, undefined, false, mockCurrentUserAccountID, false, undefined, 'concierge_123'); + + // eslint-disable-next-line rulesdir/no-multiple-api-calls + expect(API.write).not.toHaveBeenCalled(); + }); + + it('should call API.write with CancelTask and pass conciergeReportID through to navigation', async () => { + const taskReportID = 'task_report_delete_1'; + const parentReportID = 'parent_report_delete_1'; + const parentReportActionID = 'parent_action_delete_1'; + + const taskReport = { + reportID: taskReportID, + type: CONST.REPORT.TYPE.TASK, + reportName: 'Test Task', + parentReportID, + parentReportActionID, + stateNum: CONST.REPORT.STATE_NUM.OPEN, + statusNum: CONST.REPORT.STATUS_NUM.OPEN, + ownerAccountID: mockCurrentUserAccountID, + }; + + const parentReport = { + reportID: parentReportID, + type: CONST.REPORT.TYPE.CHAT, + }; + + const parentReportAction = { + reportActionID: parentReportActionID, + reportID: parentReportID, + childReportID: taskReportID, + } as OnyxEntry; + + await act(async () => { + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`, taskReport); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, parentReport); + }); + await waitForBatchedUpdatesWithAct(); + + doesReportHaveVisibleActionsSpy.mockReturnValue(true); + + deleteTask(taskReport, parentReport, false, mockCurrentUserAccountID, false, parentReportAction, 'concierge_123'); + + // eslint-disable-next-line rulesdir/no-multiple-api-calls + expect(API.write).toHaveBeenCalledWith( + 'CancelTask', + expect.objectContaining({ + taskReportID, + }), + expect.objectContaining({ + optimisticData: expect.any(Array), + successData: expect.any(Array), + failureData: expect.any(Array), + }), + ); + }); + + it('should return navigation URL when task report has no visible actions and has parentReportID', async () => { + const taskReportID = 'task_report_delete_2'; + const parentReportID = 'parent_report_delete_2'; + + const taskReport = { + reportID: taskReportID, + type: CONST.REPORT.TYPE.TASK, + reportName: 'Test Task To Delete', + parentReportID, + stateNum: CONST.REPORT.STATE_NUM.OPEN, + statusNum: CONST.REPORT.STATUS_NUM.OPEN, + ownerAccountID: mockCurrentUserAccountID, + }; + + const parentReport = { + reportID: parentReportID, + type: CONST.REPORT.TYPE.CHAT, + }; + + await act(async () => { + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`, taskReport); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, parentReport); + }); + await waitForBatchedUpdatesWithAct(); + + // No visible actions means the task report should be deleted + doesReportHaveVisibleActionsSpy.mockReturnValue(false); + + const result = deleteTask(taskReport, parentReport, false, mockCurrentUserAccountID, false, undefined, 'concierge_123'); + + expect(result).toBe(`r/${parentReportID}`); + expect(Navigation.goBack).toHaveBeenCalled(); + }); + + it('should return conciergeReportID-based URL when no parentReportID and no recent report', async () => { + const taskReportID = 'task_report_delete_3'; + const conciergeReportID = 'concierge_456'; + + const taskReport = { + reportID: taskReportID, + type: CONST.REPORT.TYPE.TASK, + reportName: 'Orphan Task', + parentReportID: undefined, + stateNum: CONST.REPORT.STATE_NUM.OPEN, + statusNum: CONST.REPORT.STATUS_NUM.OPEN, + ownerAccountID: mockCurrentUserAccountID, + }; + + await act(async () => { + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`, taskReport); + }); + await waitForBatchedUpdatesWithAct(); + + doesReportHaveVisibleActionsSpy.mockReturnValue(false); + getMostRecentReportIDSpy.mockReturnValue(conciergeReportID); + + const result = deleteTask(taskReport, undefined, false, mockCurrentUserAccountID, false, undefined, conciergeReportID); + + expect(result).toBe(`r/${conciergeReportID}`); + expect(getMostRecentReportIDSpy).toHaveBeenCalledWith(taskReport, conciergeReportID); + expect(Navigation.goBack).toHaveBeenCalled(); + }); + + it('should not return navigation URL when task report has visible actions', async () => { + const taskReportID = 'task_report_delete_4'; + const parentReportID = 'parent_report_delete_4'; + + const taskReport = { + reportID: taskReportID, + type: CONST.REPORT.TYPE.TASK, + reportName: 'Task With Visible Actions', + parentReportID, + stateNum: CONST.REPORT.STATE_NUM.OPEN, + statusNum: CONST.REPORT.STATUS_NUM.OPEN, + ownerAccountID: mockCurrentUserAccountID, + }; + + const parentReport = { + reportID: parentReportID, + type: CONST.REPORT.TYPE.CHAT, + }; + + await act(async () => { + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`, taskReport); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${parentReportID}`, parentReport); + }); + await waitForBatchedUpdatesWithAct(); + + // Has visible actions, so should not navigate away + doesReportHaveVisibleActionsSpy.mockReturnValue(true); + + const result = deleteTask(taskReport, parentReport, false, mockCurrentUserAccountID, false, undefined, 'concierge_123'); + + expect(result).toBeUndefined(); + expect(Navigation.goBack).not.toHaveBeenCalled(); + }); + + it('should return undefined when no parentReportID, no recent report, and conciergeReportID is undefined', async () => { + const taskReportID = 'task_report_delete_5'; + + const taskReport = { + reportID: taskReportID, + type: CONST.REPORT.TYPE.TASK, + reportName: 'Orphan Task No Fallback', + parentReportID: undefined, + stateNum: CONST.REPORT.STATE_NUM.OPEN, + statusNum: CONST.REPORT.STATUS_NUM.OPEN, + ownerAccountID: mockCurrentUserAccountID, + }; + + await act(async () => { + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`, taskReport); + }); + await waitForBatchedUpdatesWithAct(); + + doesReportHaveVisibleActionsSpy.mockReturnValue(false); + getMostRecentReportIDSpy.mockReturnValue(undefined); + + const result = deleteTask(taskReport, undefined, false, mockCurrentUserAccountID, false, undefined, undefined); + + // API.write should still be called + // eslint-disable-next-line rulesdir/no-multiple-api-calls + expect(API.write).toHaveBeenCalledWith('CancelTask', expect.any(Object), expect.any(Object)); + + // But no navigation should happen + expect(result).toBeUndefined(); + expect(Navigation.goBack).not.toHaveBeenCalled(); + }); + }); });