diff --git a/src/libs/actions/Task.ts b/src/libs/actions/Task.ts index 8fd0693b709f0..c30d2106db859 100644 --- a/src/libs/actions/Task.ts +++ b/src/libs/actions/Task.ts @@ -1276,8 +1276,8 @@ function clearTaskErrors(reportID: string | undefined) { function getFinishOnboardingTaskOnyxData(taskName: keyof OnyxTypes.IntroSelected): OnyxData { const taskReportID = introSelected?.[taskName]; - if (taskReportID) { - const taskReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`]; + const taskReport = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${taskReportID}`]; + if (taskReportID && canActionTask(taskReport, currentUserAccountID)) { if (taskReport) { if (taskReport.stateNum !== CONST.REPORT.STATE_NUM.APPROVED || taskReport.statusNum !== CONST.REPORT.STATUS_NUM.APPROVED) { return completeTask(taskReport); diff --git a/tests/actions/TaskTest.ts b/tests/actions/TaskTest.ts index b02896ebd849f..6938358d43314 100644 --- a/tests/actions/TaskTest.ts +++ b/tests/actions/TaskTest.ts @@ -1,10 +1,11 @@ import {renderHook} from '@testing-library/react-native'; import Onyx from 'react-native-onyx'; import useReportIsArchived from '@hooks/useReportIsArchived'; -import {canModifyTask, completeTestDriveTask} from '@libs/actions/Task'; +import {canModifyTask, completeTestDriveTask, getFinishOnboardingTaskOnyxData} from '@libs/actions/Task'; // eslint-disable-next-line no-restricted-syntax -- this is required to allow mocking import * as API from '@libs/API'; import {WRITE_COMMANDS} from '@libs/API/types'; +import DateUtils from '@libs/DateUtils'; import Parser from '@libs/Parser'; // eslint-disable-next-line no-restricted-syntax -- this is required to allow mocking import * as ReportUtils from '@libs/ReportUtils'; @@ -141,4 +142,33 @@ describe('actions/Task', () => { expect(writeSpy).toHaveBeenCalledWith(WRITE_COMMANDS.COMPLETE_TASK, expect.anything(), expect.anything()); }); }); + + describe('getFinishOnboardingTaskOnyxData', () => { + const parentReport: Report = LHNTestUtils.getFakeReport(); + const taskReport: Report = {...LHNTestUtils.getFakeReport(), type: CONST.REPORT.TYPE.TASK, ownerAccountID: 1, managerID: 2, parentReportID: parentReport.reportID}; + const reportCollectionDataSet: ReportCollectionDataSet = { + [`${ONYXKEYS.COLLECTION.REPORT}${taskReport.reportID}`]: taskReport, + [`${ONYXKEYS.COLLECTION.REPORT}${parentReport.reportID}`]: parentReport, + }; + beforeEach(async () => { + await Onyx.clear(); + await Onyx.multiSet({ + ...reportCollectionDataSet, + }); + await Onyx.set(ONYXKEYS.SESSION, {email: 'user1@gmail.com', accountID: 2}); + await Onyx.set(`${ONYXKEYS.NVP_INTRO_SELECTED}`, {choice: CONST.ONBOARDING_CHOICES.MANAGE_TEAM, setupCategories: taskReport.reportID}); + await waitForBatchedUpdates(); + }); + it('Return not empty object', () => { + expect(Object.values(getFinishOnboardingTaskOnyxData('setupCategories')).length).toBeGreaterThan(0); + }); + it('Return empty object', async () => { + const reportNameValuePairs = { + private_isArchived: DateUtils.getDBTime(), + }; + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT_NAME_VALUE_PAIRS}${parentReport.reportID}`, reportNameValuePairs); + await waitForBatchedUpdates(); + expect(Object.values(getFinishOnboardingTaskOnyxData('setupCategories')).length).toBe(0); + }); + }); });