From b03b116a31104a1f21dfe3997f140d1ece7ea641 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Fri, 20 Feb 2026 14:50:39 -0700 Subject: [PATCH 01/12] WIP on removing onyx refs --- .../MoneyRequestReceiptView.tsx | 2 +- ...tActions.ts => ClearReportActionErrors.ts} | 12 +- src/libs/actions/IOU/index.ts | 2 +- .../inbox/report/PureReportActionItem.tsx | 2 +- src/pages/inbox/report/ReportActionItem.tsx | 2 +- tests/unit/ClearReportActionErrorsTest.ts | 410 ++++++++++++++++++ 6 files changed, 417 insertions(+), 13 deletions(-) rename src/libs/actions/{ReportActions.ts => ClearReportActionErrors.ts} (94%) create mode 100644 tests/unit/ClearReportActionErrorsTest.ts diff --git a/src/components/ReportActionItem/MoneyRequestReceiptView.tsx b/src/components/ReportActionItem/MoneyRequestReceiptView.tsx index ef9edc6e7830..d0bbcb4133bd 100644 --- a/src/components/ReportActionItem/MoneyRequestReceiptView.tsx +++ b/src/components/ReportActionItem/MoneyRequestReceiptView.tsx @@ -45,7 +45,7 @@ import ViolationsUtils, {filterReceiptViolations} from '@libs/Violations/Violati import Navigation from '@navigation/Navigation'; import {cleanUpMoneyRequest} from '@userActions/IOU'; import {navigateToConciergeChatAndDeleteReport} from '@userActions/Report'; -import {clearAllRelatedReportActionErrors} from '@userActions/ReportActions'; +import {clearAllRelatedReportActionErrors} from '@userActions/ClearReportActionErrors'; import {clearError, getLastModifiedExpense, revert} from '@userActions/Transaction'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; diff --git a/src/libs/actions/ReportActions.ts b/src/libs/actions/ClearReportActionErrors.ts similarity index 94% rename from src/libs/actions/ReportActions.ts rename to src/libs/actions/ClearReportActionErrors.ts index 39529ea60884..2e83cf1ffb9b 100644 --- a/src/libs/actions/ReportActions.ts +++ b/src/libs/actions/ClearReportActionErrors.ts @@ -11,14 +11,14 @@ import {deleteReport} from './Report'; type IgnoreDirection = 'parent' | 'child'; let allReportActions: OnyxCollection; -Onyx.connect({ +Onyx.connectWithoutView({ key: ONYXKEYS.COLLECTION.REPORT_ACTIONS, waitForCollectionCallback: true, callback: (value) => (allReportActions = value), }); let allReports: OnyxCollection; -Onyx.connect({ +Onyx.connectWithoutView({ key: ONYXKEYS.COLLECTION.REPORT, waitForCollectionCallback: true, callback: (value) => { @@ -32,19 +32,16 @@ function clearReportActionErrors(reportID: string, reportAction: ReportAction, o } if (reportAction.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD || reportAction.isOptimisticAction) { - // If there's a linked transaction, delete that too const linkedTransactionID = getLinkedTransactionID(reportAction); if (linkedTransactionID) { Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${linkedTransactionID}`, null); Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`, null); } - // Delete the optimistic action Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`, { [reportAction.reportActionID]: null, }); - // Delete the failed task report too const taskReportID = getReportActionMessage(reportAction)?.taskReportID; if (taskReportID && isCreatedTaskReportAction(reportAction)) { deleteReport(taskReportID); @@ -113,7 +110,4 @@ function clearAllRelatedReportActionErrors( } export type {IgnoreDirection}; -export { - // eslint-disable-next-line import/prefer-default-export - clearAllRelatedReportActionErrors, -}; +export {clearAllRelatedReportActionErrors}; diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index d37630cc1b57..3b52e2a3ebe6 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -236,7 +236,7 @@ import type {BuildPolicyDataKeys} from '@userActions/Policy/Policy'; import {buildOptimisticPolicyRecentlyUsedTags} from '@userActions/Policy/Tag'; import type {GuidedSetupData} from '@userActions/Report'; import {buildInviteToRoomOnyxData, completeOnboarding, notifyNewAction, optimisticReportLastData} from '@userActions/Report'; -import {clearAllRelatedReportActionErrors} from '@userActions/ReportActions'; +import {clearAllRelatedReportActionErrors} from '@userActions/ClearReportActionErrors'; import {mergeTransactionIdsHighlightOnSearchRoute, sanitizeRecentWaypoints} from '@userActions/Transaction'; import {removeDraftTransaction, removeDraftTransactions} from '@userActions/TransactionEdit'; import {getOnboardingMessages} from '@userActions/Welcome/OnboardingFlow'; diff --git a/src/pages/inbox/report/PureReportActionItem.tsx b/src/pages/inbox/report/PureReportActionItem.tsx index 0d3d66e68a6f..9cf18edce4da 100644 --- a/src/pages/inbox/report/PureReportActionItem.tsx +++ b/src/pages/inbox/report/PureReportActionItem.tsx @@ -231,7 +231,7 @@ import { resolveConciergeCategoryOptions, resolveConciergeDescriptionOptions, } from '@userActions/Report'; -import type {IgnoreDirection} from '@userActions/ReportActions'; +import type {IgnoreDirection} from '@userActions/ClearReportActionErrors'; import {isAnonymousUser, signOutAndRedirectToSignIn} from '@userActions/Session'; import {isBlockedFromConcierge} from '@userActions/User'; import type {IOUAction} from '@src/CONST'; diff --git a/src/pages/inbox/report/ReportActionItem.tsx b/src/pages/inbox/report/ReportActionItem.tsx index baa357264828..2480bdb32f89 100644 --- a/src/pages/inbox/report/ReportActionItem.tsx +++ b/src/pages/inbox/report/ReportActionItem.tsx @@ -28,7 +28,7 @@ import { resolveActionableReportMentionWhisper, toggleEmojiReaction, } from '@userActions/Report'; -import {clearAllRelatedReportActionErrors} from '@userActions/ReportActions'; +import {clearAllRelatedReportActionErrors} from '@userActions/ClearReportActionErrors'; import {clearError} from '@userActions/Transaction'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; diff --git a/tests/unit/ClearReportActionErrorsTest.ts b/tests/unit/ClearReportActionErrorsTest.ts new file mode 100644 index 000000000000..ef5dcd93cd72 --- /dev/null +++ b/tests/unit/ClearReportActionErrorsTest.ts @@ -0,0 +1,410 @@ +import Onyx from 'react-native-onyx'; +import {clearAllRelatedReportActionErrors} from '@libs/actions/ClearReportActionErrors'; +import CONST from '@src/CONST'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Report, ReportAction, ReportActions} from '@src/types/onyx'; +import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; +import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; + +const REPORT_ID = '1'; +const PARENT_REPORT_ID = '2'; +const CHILD_REPORT_ID = '3'; +const REPORT_ACTION_ID = '100'; +const PARENT_REPORT_ACTION_ID = '200'; +const CHILD_REPORT_ACTION_ID = '300'; + +function createMockReportAction(overrides: Partial = {}): ReportAction { + return { + reportActionID: REPORT_ACTION_ID, + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + actorAccountID: 1, + created: '2024-01-01 00:00:00.000', + message: [{type: 'TEXT', text: 'Test message'}], + errors: {error1: 'Error message'}, + ...overrides, + } as ReportAction; +} + +function createMockReport(overrides: Partial = {}): Report { + return { + reportID: REPORT_ID, + reportName: 'Test Report', + type: CONST.REPORT.TYPE.CHAT, + ...overrides, + } as Report; +} + +function getReportActionsFromOnyx(reportID: string): Promise { + return new Promise((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + callback: (value) => { + Onyx.disconnect(connection); + resolve(value ?? undefined); + }, + }); + }); +} + +describe('ClearReportActionErrors', () => { + beforeAll(() => + Onyx.init({ + keys: ONYXKEYS, + evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS], + }), + ); + + beforeEach(() => { + wrapOnyxWithWaitForBatchedUpdates(Onyx); + return waitForBatchedUpdates(); + }); + + afterEach(() => { + Onyx.clear(); + }); + + describe('clearAllRelatedReportActionErrors', () => { + it('should return early when reportAction is null', async () => { + const report = createMockReport(); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, null, REPORT_ID); + await waitForBatchedUpdates(); + + const reportActions = await getReportActionsFromOnyx(REPORT_ID); + expect(reportActions).toBeUndefined(); + }); + + it('should return early when reportAction is undefined', async () => { + clearAllRelatedReportActionErrors(REPORT_ID, undefined, REPORT_ID); + await waitForBatchedUpdates(); + + const reportActions = await getReportActionsFromOnyx(REPORT_ID); + expect(reportActions).toBeUndefined(); + }); + + it('should return early when reportAction has no errors', async () => { + const reportAction = createMockReportAction({errors: undefined}); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); + await waitForBatchedUpdates(); + + const reportActions = await getReportActionsFromOnyx(REPORT_ID); + expect(reportActions).toBeUndefined(); + }); + + it('should return early when reportID is undefined', async () => { + const reportAction = createMockReportAction(); + + clearAllRelatedReportActionErrors(undefined, reportAction, REPORT_ID); + await waitForBatchedUpdates(); + + const reportActions = await getReportActionsFromOnyx(REPORT_ID); + expect(reportActions).toBeUndefined(); + }); + + it('should clear errors on the current report action', async () => { + const reportAction = createMockReportAction(); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); + await waitForBatchedUpdates(); + + const reportActions = await getReportActionsFromOnyx(REPORT_ID); + expect(reportActions?.[REPORT_ACTION_ID]?.errors).toBeUndefined(); + }); + + it('should clear only specified error keys when keys parameter is provided', async () => { + const reportAction = createMockReportAction({ + errors: {error1: 'Error 1', error2: 'Error 2', error3: 'Error 3'}, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID, undefined, ['error1', 'error2']); + await waitForBatchedUpdates(); + + const reportActions = await getReportActionsFromOnyx(REPORT_ID); + expect(reportActions?.[REPORT_ACTION_ID]?.errors).toEqual({ + error3: 'Error 3', + }); + }); + + it('should delete optimistic report action instead of clearing errors', async () => { + const reportAction = createMockReportAction({ + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); + await waitForBatchedUpdates(); + + const reportActions = await getReportActionsFromOnyx(REPORT_ID); + expect(reportActions?.[REPORT_ACTION_ID]).toBeUndefined(); + }); + + it('should delete isOptimisticAction report action instead of clearing errors', async () => { + const reportAction = createMockReportAction({ + isOptimisticAction: true, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); + await waitForBatchedUpdates(); + + const reportActions = await getReportActionsFromOnyx(REPORT_ID); + expect(reportActions?.[REPORT_ACTION_ID]).toBeUndefined(); + }); + + it('should clear errors on parent report action when matching error keys exist', async () => { + const report = createMockReport({ + parentReportID: PARENT_REPORT_ID, + parentReportActionID: PARENT_REPORT_ACTION_ID, + }); + const reportAction = createMockReportAction({ + errors: {sharedError: 'Error message'}, + }); + const parentReportAction = createMockReportAction({ + reportActionID: PARENT_REPORT_ACTION_ID, + errors: {sharedError: 'Parent error message'}, + }); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${PARENT_REPORT_ID}`, { + [PARENT_REPORT_ACTION_ID]: parentReportAction, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); + await waitForBatchedUpdates(); + + const parentReportActions = await getReportActionsFromOnyx(PARENT_REPORT_ID); + expect(parentReportActions?.[PARENT_REPORT_ACTION_ID]?.errors).toEqual({}); + }); + + it('should not clear parent errors when ignore is set to parent', async () => { + const report = createMockReport({ + parentReportID: PARENT_REPORT_ID, + parentReportActionID: PARENT_REPORT_ACTION_ID, + }); + const reportAction = createMockReportAction({ + errors: {sharedError: 'Error message'}, + }); + const parentReportAction = createMockReportAction({ + reportActionID: PARENT_REPORT_ACTION_ID, + errors: {sharedError: 'Parent error message'}, + }); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${PARENT_REPORT_ID}`, { + [PARENT_REPORT_ACTION_ID]: parentReportAction, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID, 'parent'); + await waitForBatchedUpdates(); + + const parentReportActions = await getReportActionsFromOnyx(PARENT_REPORT_ID); + expect(parentReportActions?.[PARENT_REPORT_ACTION_ID]?.errors).toEqual({sharedError: 'Parent error message'}); + }); + + it('should clear errors on child report actions when matching error keys exist', async () => { + const reportAction = createMockReportAction({ + childReportID: CHILD_REPORT_ID, + errors: {sharedError: 'Error message'}, + }); + const childReportAction = createMockReportAction({ + reportActionID: CHILD_REPORT_ACTION_ID, + errors: {sharedError: 'Child error message'}, + }); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, createMockReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${CHILD_REPORT_ID}`, { + [CHILD_REPORT_ACTION_ID]: childReportAction, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); + await waitForBatchedUpdates(); + + const childReportActions = await getReportActionsFromOnyx(CHILD_REPORT_ID); + expect(childReportActions?.[CHILD_REPORT_ACTION_ID]?.errors).toEqual({}); + }); + + it('should not clear child errors when ignore is set to child', async () => { + const reportAction = createMockReportAction({ + childReportID: CHILD_REPORT_ID, + errors: {sharedError: 'Error message'}, + }); + const childReportAction = createMockReportAction({ + reportActionID: CHILD_REPORT_ACTION_ID, + errors: {sharedError: 'Child error message'}, + }); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, createMockReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${CHILD_REPORT_ID}`, { + [CHILD_REPORT_ACTION_ID]: childReportAction, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID, 'child'); + await waitForBatchedUpdates(); + + const childReportActions = await getReportActionsFromOnyx(CHILD_REPORT_ID); + expect(childReportActions?.[CHILD_REPORT_ACTION_ID]?.errors).toEqual({sharedError: 'Child error message'}); + }); + + it('should not clear parent errors when error keys do not match', async () => { + const report = createMockReport({ + parentReportID: PARENT_REPORT_ID, + parentReportActionID: PARENT_REPORT_ACTION_ID, + }); + const reportAction = createMockReportAction({ + errors: {error1: 'Error message'}, + }); + const parentReportAction = createMockReportAction({ + reportActionID: PARENT_REPORT_ACTION_ID, + errors: {differentError: 'Parent error message'}, + }); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${PARENT_REPORT_ID}`, { + [PARENT_REPORT_ACTION_ID]: parentReportAction, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); + await waitForBatchedUpdates(); + + const parentReportActions = await getReportActionsFromOnyx(PARENT_REPORT_ID); + expect(parentReportActions?.[PARENT_REPORT_ACTION_ID]?.errors).toEqual({differentError: 'Parent error message'}); + }); + + it('should use originalReportID for clearing errors when it differs from reportID', async () => { + const originalReportID = '999'; + const reportAction = createMockReportAction(); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, originalReportID); + await waitForBatchedUpdates(); + + const reportActions = await getReportActionsFromOnyx(originalReportID); + expect(reportActions?.[REPORT_ACTION_ID]?.errors).toBeUndefined(); + }); + + it('should handle empty errors object', async () => { + const reportAction = createMockReportAction({errors: {}}); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); + await waitForBatchedUpdates(); + + const reportActions = await getReportActionsFromOnyx(REPORT_ID); + expect(reportActions).toBeUndefined(); + }); + + it('should handle report action without reportActionID', async () => { + const reportAction = createMockReportAction({reportActionID: ''}); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); + await waitForBatchedUpdates(); + + const reportActions = await getReportActionsFromOnyx(REPORT_ID); + expect(reportActions?.[REPORT_ACTION_ID]).toBeDefined(); + expect(reportActions?.[REPORT_ACTION_ID]?.errors).toEqual({error1: 'Error message'}); + }); + + it('should clear multiple child report action errors', async () => { + const reportAction = createMockReportAction({ + childReportID: CHILD_REPORT_ID, + errors: {sharedError: 'Error message'}, + }); + const childReportAction1 = createMockReportAction({ + reportActionID: CHILD_REPORT_ACTION_ID, + errors: {sharedError: 'Child error message 1'}, + }); + const childReportAction2 = createMockReportAction({ + reportActionID: '301', + errors: {sharedError: 'Child error message 2'}, + }); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, createMockReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${CHILD_REPORT_ID}`, { + [CHILD_REPORT_ACTION_ID]: childReportAction1, + '301': childReportAction2, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); + await waitForBatchedUpdates(); + + const childReportActions = await getReportActionsFromOnyx(CHILD_REPORT_ID); + expect(childReportActions?.[CHILD_REPORT_ACTION_ID]?.errors).toEqual({}); + expect(childReportActions?.['301']?.errors).toEqual({}); + }); + + it('should only clear child errors that match the error keys', async () => { + const reportAction = createMockReportAction({ + childReportID: CHILD_REPORT_ID, + errors: {error1: 'Error message'}, + }); + const childReportAction = createMockReportAction({ + reportActionID: CHILD_REPORT_ACTION_ID, + errors: {error1: 'Child error 1', error2: 'Child error 2'}, + }); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, createMockReport()); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${CHILD_REPORT_ID}`, { + [CHILD_REPORT_ACTION_ID]: childReportAction, + }); + await waitForBatchedUpdates(); + + clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); + await waitForBatchedUpdates(); + + const childReportActions = await getReportActionsFromOnyx(CHILD_REPORT_ID); + expect(childReportActions?.[CHILD_REPORT_ACTION_ID]?.errors).toEqual({error2: 'Child error 2'}); + }); + }); +}); From 6cb413220a61774d6de391d13b6405fa1c8bb5b2 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Tue, 24 Feb 2026 17:11:30 -0700 Subject: [PATCH 02/12] Add UI tests --- tests/ui/ClearReportActionErrorsUITest.tsx | 317 +++++++++++++++++++++ 1 file changed, 317 insertions(+) create mode 100644 tests/ui/ClearReportActionErrorsUITest.tsx diff --git a/tests/ui/ClearReportActionErrorsUITest.tsx b/tests/ui/ClearReportActionErrorsUITest.tsx new file mode 100644 index 000000000000..3f6e331fde63 --- /dev/null +++ b/tests/ui/ClearReportActionErrorsUITest.tsx @@ -0,0 +1,317 @@ +import {PortalProvider} from '@gorhom/portal'; +import * as NativeNavigation from '@react-navigation/native'; +import {act, fireEvent, render, screen} from '@testing-library/react-native'; +import React from 'react'; +import Onyx from 'react-native-onyx'; +import ComposeProviders from '@components/ComposeProviders'; +import HTMLEngineProvider from '@components/HTMLEngineProvider'; +import {LocaleContextProvider} from '@components/LocaleContextProvider'; +import OnyxListItemProvider from '@components/OnyxListItemProvider'; +import OptionsListContextProvider from '@components/OptionListContextProvider'; +import ScreenWrapper from '@components/ScreenWrapper'; +import {clearAllRelatedReportActionErrors} from '@libs/actions/ClearReportActionErrors'; +import {getIOUActionForReportID} from '@libs/ReportActionsUtils'; +import PureReportActionItem from '@pages/inbox/report/PureReportActionItem'; +import CONST from '@src/CONST'; +import * as ReportActionUtils from '@src/libs/ReportActionsUtils'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {Report, ReportAction, ReportActions} from '@src/types/onyx'; +import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; +import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; + +jest.mock('@react-navigation/native'); + +const ACTOR_ACCOUNT_ID = 123456789; +const ACTOR_EMAIL = 'test@test.com'; +const REPORT_ID = '12345'; +const REPORT_ACTION_ID = '99999'; +const CHILD_REPORT_ID = '67890'; +const CHILD_REPORT_ACTION_ID = '88888'; + +function createMockReportAction(overrides: Partial = {}): ReportAction { + const errorTimestamp = Date.now() * 1000; + return { + reportActionID: REPORT_ACTION_ID, + actorAccountID: ACTOR_ACCOUNT_ID, + created: '2025-01-01 12:00:00.000', + actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, + automatic: false, + shouldShow: true, + avatar: '', + person: [{type: 'TEXT', style: 'strong', text: ACTOR_EMAIL}], + message: [{type: 'COMMENT', html: 'Test message with error', text: 'Test message with error'}], + errors: { + [errorTimestamp]: 'Something went wrong. Please try again.', + }, + ...overrides, + } as ReportAction; +} + +function createMockReport(overrides: Partial = {}): Report { + return { + reportID: REPORT_ID, + reportName: 'Test Report', + type: CONST.REPORT.TYPE.CHAT, + ownerAccountID: ACTOR_ACCOUNT_ID, + ...overrides, + } as Report; +} + +function getReportActionsFromOnyx(reportID: string): Promise { + return new Promise((resolve) => { + const connection = Onyx.connect({ + key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`, + callback: (value) => { + Onyx.disconnect(connection); + resolve(value ?? undefined); + }, + }); + }); +} + +describe('ClearReportActionErrors UI', () => { + beforeAll(() => { + Onyx.init({ + keys: ONYXKEYS, + evictableKeys: [ONYXKEYS.COLLECTION.REPORT_ACTIONS], + }); + jest.spyOn(NativeNavigation, 'useRoute').mockReturnValue({key: '', name: ''}); + jest.spyOn(ReportActionUtils, 'getIOUActionForReportID').mockImplementation(getIOUActionForReportID); + }); + + beforeEach(async () => { + wrapOnyxWithWaitForBatchedUpdates(Onyx); + await act(async () => { + await Onyx.merge(ONYXKEYS.NETWORK, {isOffline: false}); + await Onyx.merge(`${ONYXKEYS.PERSONAL_DETAILS_LIST}`, { + [ACTOR_ACCOUNT_ID]: { + accountID: ACTOR_ACCOUNT_ID, + avatar: 'https://d2k5nsl2zxldvw.cloudfront.net/images/avatars/default-avatar_9.png', + displayName: ACTOR_EMAIL, + login: ACTOR_EMAIL, + }, + }); + }); + await waitForBatchedUpdatesWithAct(); + }); + + afterEach(async () => { + await act(async () => { + await Onyx.clear(); + }); + await waitForBatchedUpdatesWithAct(); + }); + + function renderReportActionItem( + action: ReportAction, + report: Report, + options?: { + clearErrorFn?: typeof clearAllRelatedReportActionErrors; + originalReportID?: string; + allReports?: Record; + }, + ) { + const {clearErrorFn, originalReportID = report.reportID, allReports} = options ?? {}; + return render( + + + + + + + + + , + ); + } + + describe('Error display and dismissal', () => { + it('should display error message when action has errors', async () => { + const action = createMockReportAction(); + const report = createMockReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: action, + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderReportActionItem(action, report); + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText('Something went wrong. Please try again.')).toBeOnTheScreen(); + }); + + it('should call clearAllRelatedReportActionErrors when error is dismissed', async () => { + const action = createMockReportAction(); + const report = createMockReport(); + const mockClearErrors = jest.fn(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: action, + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderReportActionItem(action, report, { + clearErrorFn: mockClearErrors, + originalReportID: REPORT_ID, + }); + await waitForBatchedUpdatesWithAct(); + + const dismissButton = screen.getByLabelText('Dismiss'); + fireEvent.press(dismissButton); + + expect(mockClearErrors).toHaveBeenCalledWith( + REPORT_ID, + expect.objectContaining({reportActionID: REPORT_ACTION_ID}), + REPORT_ID, + ); + }); + + it('should clear error from Onyx when dismissed', async () => { + const errorTimestamp = Date.now() * 1000; + const action = createMockReportAction({ + errors: {[errorTimestamp]: 'Test error message'}, + }); + const report = createMockReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: action, + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderReportActionItem(action, report, { + clearErrorFn: clearAllRelatedReportActionErrors, + originalReportID: REPORT_ID, + }); + await waitForBatchedUpdatesWithAct(); + + expect(screen.getByText('Test error message')).toBeOnTheScreen(); + + const dismissButton = screen.getByLabelText('Dismiss'); + fireEvent.press(dismissButton); + + await waitForBatchedUpdatesWithAct(); + + const reportActions = await getReportActionsFromOnyx(REPORT_ID); + expect(reportActions?.[REPORT_ACTION_ID]?.errors).toBeUndefined(); + }); + }); + + describe('Parent/child error propagation', () => { + it('should clear errors on child actions when parent error is dismissed', async () => { + const sharedErrorTimestamp = Date.now() * 1000; + const sharedError = {[sharedErrorTimestamp]: 'Shared error message'}; + + const parentAction = createMockReportAction({ + reportActionID: REPORT_ACTION_ID, + childReportID: CHILD_REPORT_ID, + errors: sharedError, + }); + + const childAction = createMockReportAction({ + reportActionID: CHILD_REPORT_ACTION_ID, + errors: sharedError, + }); + + const parentReport = createMockReport(); + const childReport = createMockReport({ + reportID: CHILD_REPORT_ID, + parentReportID: REPORT_ID, + parentReportActionID: REPORT_ACTION_ID, + }); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, parentReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${CHILD_REPORT_ID}`, childReport); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: parentAction, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${CHILD_REPORT_ID}`, { + [CHILD_REPORT_ACTION_ID]: childAction, + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderReportActionItem(parentAction, parentReport, { + clearErrorFn: clearAllRelatedReportActionErrors, + originalReportID: REPORT_ID, + allReports: { + [`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`]: parentReport, + [`${ONYXKEYS.COLLECTION.REPORT}${CHILD_REPORT_ID}`]: childReport, + }, + }); + await waitForBatchedUpdatesWithAct(); + + const dismissButton = screen.getByLabelText('Dismiss'); + fireEvent.press(dismissButton); + + await waitForBatchedUpdatesWithAct(); + + const parentReportActions = await getReportActionsFromOnyx(REPORT_ID); + const childReportActions = await getReportActionsFromOnyx(CHILD_REPORT_ID); + + expect(parentReportActions?.[REPORT_ACTION_ID]?.errors).toBeUndefined(); + expect(childReportActions?.[CHILD_REPORT_ACTION_ID]?.errors).toEqual({}); + }); + }); + + describe('Optimistic action deletion', () => { + it('should delete optimistic action instead of clearing errors', async () => { + const action = createMockReportAction({ + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + isOptimisticAction: true, + }); + const report = createMockReport(); + + await act(async () => { + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: action, + }); + }); + await waitForBatchedUpdatesWithAct(); + + renderReportActionItem(action, report, { + clearErrorFn: clearAllRelatedReportActionErrors, + originalReportID: REPORT_ID, + }); + await waitForBatchedUpdatesWithAct(); + + const dismissButton = screen.getByLabelText('Dismiss'); + fireEvent.press(dismissButton); + + await waitForBatchedUpdatesWithAct(); + + const reportActions = await getReportActionsFromOnyx(REPORT_ID); + expect(reportActions?.[REPORT_ACTION_ID]).toBeUndefined(); + }); + }); +}); From 4c16785d9a16c6d3deae8f7ee3c62e3d1b540fe9 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Tue, 24 Feb 2026 17:14:38 -0700 Subject: [PATCH 03/12] Remove redundant tests --- tests/ui/ClearReportActionErrorsUITest.tsx | 6 +-- tests/unit/ClearReportActionErrorsTest.ts | 56 ---------------------- 2 files changed, 1 insertion(+), 61 deletions(-) diff --git a/tests/ui/ClearReportActionErrorsUITest.tsx b/tests/ui/ClearReportActionErrorsUITest.tsx index 3f6e331fde63..f142b032864b 100644 --- a/tests/ui/ClearReportActionErrorsUITest.tsx +++ b/tests/ui/ClearReportActionErrorsUITest.tsx @@ -185,11 +185,7 @@ describe('ClearReportActionErrors UI', () => { const dismissButton = screen.getByLabelText('Dismiss'); fireEvent.press(dismissButton); - expect(mockClearErrors).toHaveBeenCalledWith( - REPORT_ID, - expect.objectContaining({reportActionID: REPORT_ACTION_ID}), - REPORT_ID, - ); + expect(mockClearErrors).toHaveBeenCalledWith(REPORT_ID, expect.objectContaining({reportActionID: REPORT_ACTION_ID}), REPORT_ID); }); it('should clear error from Onyx when dismissed', async () => { diff --git a/tests/unit/ClearReportActionErrorsTest.ts b/tests/unit/ClearReportActionErrorsTest.ts index ef5dcd93cd72..a03fa25f5bd2 100644 --- a/tests/unit/ClearReportActionErrorsTest.ts +++ b/tests/unit/ClearReportActionErrorsTest.ts @@ -104,20 +104,6 @@ describe('ClearReportActionErrors', () => { expect(reportActions).toBeUndefined(); }); - it('should clear errors on the current report action', async () => { - const reportAction = createMockReportAction(); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { - [REPORT_ACTION_ID]: reportAction, - }); - await waitForBatchedUpdates(); - - clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); - await waitForBatchedUpdates(); - - const reportActions = await getReportActionsFromOnyx(REPORT_ID); - expect(reportActions?.[REPORT_ACTION_ID]?.errors).toBeUndefined(); - }); - it('should clear only specified error keys when keys parameter is provided', async () => { const reportAction = createMockReportAction({ errors: {error1: 'Error 1', error2: 'Error 2', error3: 'Error 3'}, @@ -136,22 +122,6 @@ describe('ClearReportActionErrors', () => { }); }); - it('should delete optimistic report action instead of clearing errors', async () => { - const reportAction = createMockReportAction({ - pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, - }); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { - [REPORT_ACTION_ID]: reportAction, - }); - await waitForBatchedUpdates(); - - clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); - await waitForBatchedUpdates(); - - const reportActions = await getReportActionsFromOnyx(REPORT_ID); - expect(reportActions?.[REPORT_ACTION_ID]).toBeUndefined(); - }); - it('should delete isOptimisticAction report action instead of clearing errors', async () => { const reportAction = createMockReportAction({ isOptimisticAction: true, @@ -226,32 +196,6 @@ describe('ClearReportActionErrors', () => { expect(parentReportActions?.[PARENT_REPORT_ACTION_ID]?.errors).toEqual({sharedError: 'Parent error message'}); }); - it('should clear errors on child report actions when matching error keys exist', async () => { - const reportAction = createMockReportAction({ - childReportID: CHILD_REPORT_ID, - errors: {sharedError: 'Error message'}, - }); - const childReportAction = createMockReportAction({ - reportActionID: CHILD_REPORT_ACTION_ID, - errors: {sharedError: 'Child error message'}, - }); - - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, createMockReport()); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { - [REPORT_ACTION_ID]: reportAction, - }); - await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${CHILD_REPORT_ID}`, { - [CHILD_REPORT_ACTION_ID]: childReportAction, - }); - await waitForBatchedUpdates(); - - clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); - await waitForBatchedUpdates(); - - const childReportActions = await getReportActionsFromOnyx(CHILD_REPORT_ID); - expect(childReportActions?.[CHILD_REPORT_ACTION_ID]?.errors).toEqual({}); - }); - it('should not clear child errors when ignore is set to child', async () => { const reportAction = createMockReportAction({ childReportID: CHILD_REPORT_ID, From 7f0ba27ede9bcb35a80c04e0943b58f19b50adfc Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Tue, 24 Feb 2026 17:41:13 -0700 Subject: [PATCH 04/12] Add comments to tests --- tests/ui/ClearReportActionErrorsUITest.tsx | 15 +++++++ tests/unit/ClearReportActionErrorsTest.ts | 46 ++++++++++++++++++++++ 2 files changed, 61 insertions(+) diff --git a/tests/ui/ClearReportActionErrorsUITest.tsx b/tests/ui/ClearReportActionErrorsUITest.tsx index f142b032864b..4ff3e0ed14e7 100644 --- a/tests/ui/ClearReportActionErrorsUITest.tsx +++ b/tests/ui/ClearReportActionErrorsUITest.tsx @@ -146,6 +146,7 @@ describe('ClearReportActionErrors UI', () => { describe('Error display and dismissal', () => { it('should display error message when action has errors', async () => { + // Given a report action with errors stored in Onyx const action = createMockReportAction(); const report = createMockReport(); @@ -157,13 +158,16 @@ describe('ClearReportActionErrors UI', () => { }); await waitForBatchedUpdatesWithAct(); + // When the PureReportActionItem component is rendered renderReportActionItem(action, report); await waitForBatchedUpdatesWithAct(); + // Then the error message should be visible on screen expect(screen.getByText('Something went wrong. Please try again.')).toBeOnTheScreen(); }); it('should call clearAllRelatedReportActionErrors when error is dismissed', async () => { + // Given a rendered report action with errors and a mock clear function const action = createMockReportAction(); const report = createMockReport(); const mockClearErrors = jest.fn(); @@ -182,13 +186,16 @@ describe('ClearReportActionErrors UI', () => { }); await waitForBatchedUpdatesWithAct(); + // When the user presses the dismiss button const dismissButton = screen.getByLabelText('Dismiss'); fireEvent.press(dismissButton); + // Then clearAllRelatedReportActionErrors should be called with correct arguments expect(mockClearErrors).toHaveBeenCalledWith(REPORT_ID, expect.objectContaining({reportActionID: REPORT_ACTION_ID}), REPORT_ID); }); it('should clear error from Onyx when dismissed', async () => { + // Given a rendered report action with a visible error message const errorTimestamp = Date.now() * 1000; const action = createMockReportAction({ errors: {[errorTimestamp]: 'Test error message'}, @@ -211,11 +218,13 @@ describe('ClearReportActionErrors UI', () => { expect(screen.getByText('Test error message')).toBeOnTheScreen(); + // When the user presses the dismiss button const dismissButton = screen.getByLabelText('Dismiss'); fireEvent.press(dismissButton); await waitForBatchedUpdatesWithAct(); + // Then the error should be removed from Onyx const reportActions = await getReportActionsFromOnyx(REPORT_ID); expect(reportActions?.[REPORT_ACTION_ID]?.errors).toBeUndefined(); }); @@ -223,6 +232,7 @@ describe('ClearReportActionErrors UI', () => { describe('Parent/child error propagation', () => { it('should clear errors on child actions when parent error is dismissed', async () => { + // Given a parent action with a child report, both having errors with matching keys const sharedErrorTimestamp = Date.now() * 1000; const sharedError = {[sharedErrorTimestamp]: 'Shared error message'}; @@ -266,11 +276,13 @@ describe('ClearReportActionErrors UI', () => { }); await waitForBatchedUpdatesWithAct(); + // When the user dismisses the error on the parent action const dismissButton = screen.getByLabelText('Dismiss'); fireEvent.press(dismissButton); await waitForBatchedUpdatesWithAct(); + // Then both parent and child errors should be cleared const parentReportActions = await getReportActionsFromOnyx(REPORT_ID); const childReportActions = await getReportActionsFromOnyx(CHILD_REPORT_ID); @@ -281,6 +293,7 @@ describe('ClearReportActionErrors UI', () => { describe('Optimistic action deletion', () => { it('should delete optimistic action instead of clearing errors', async () => { + // Given an optimistic report action (pendingAction='add' and isOptimisticAction=true) with errors const action = createMockReportAction({ pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, isOptimisticAction: true, @@ -301,11 +314,13 @@ describe('ClearReportActionErrors UI', () => { }); await waitForBatchedUpdatesWithAct(); + // When the user dismisses the error const dismissButton = screen.getByLabelText('Dismiss'); fireEvent.press(dismissButton); await waitForBatchedUpdatesWithAct(); + // Then the entire report action should be deleted from Onyx (not just errors cleared) const reportActions = await getReportActionsFromOnyx(REPORT_ID); expect(reportActions?.[REPORT_ACTION_ID]).toBeUndefined(); }); diff --git a/tests/unit/ClearReportActionErrorsTest.ts b/tests/unit/ClearReportActionErrorsTest.ts index a03fa25f5bd2..3fad987abf0f 100644 --- a/tests/unit/ClearReportActionErrorsTest.ts +++ b/tests/unit/ClearReportActionErrorsTest.ts @@ -65,46 +65,60 @@ describe('ClearReportActionErrors', () => { describe('clearAllRelatedReportActionErrors', () => { it('should return early when reportAction is null', async () => { + // Given a report exists in Onyx const report = createMockReport(); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); await waitForBatchedUpdates(); + // When clearAllRelatedReportActionErrors is called with null reportAction clearAllRelatedReportActionErrors(REPORT_ID, null, REPORT_ID); await waitForBatchedUpdates(); + // Then no report actions should be created or modified const reportActions = await getReportActionsFromOnyx(REPORT_ID); expect(reportActions).toBeUndefined(); }); it('should return early when reportAction is undefined', async () => { + // Given no initial state + + // When clearAllRelatedReportActionErrors is called with undefined reportAction clearAllRelatedReportActionErrors(REPORT_ID, undefined, REPORT_ID); await waitForBatchedUpdates(); + // Then no report actions should be created or modified const reportActions = await getReportActionsFromOnyx(REPORT_ID); expect(reportActions).toBeUndefined(); }); it('should return early when reportAction has no errors', async () => { + // Given a report action with no errors const reportAction = createMockReportAction({errors: undefined}); + // When clearAllRelatedReportActionErrors is called clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); await waitForBatchedUpdates(); + // Then no report actions should be created or modified const reportActions = await getReportActionsFromOnyx(REPORT_ID); expect(reportActions).toBeUndefined(); }); it('should return early when reportID is undefined', async () => { + // Given a report action with errors const reportAction = createMockReportAction(); + // When clearAllRelatedReportActionErrors is called with undefined reportID clearAllRelatedReportActionErrors(undefined, reportAction, REPORT_ID); await waitForBatchedUpdates(); + // Then no report actions should be created or modified const reportActions = await getReportActionsFromOnyx(REPORT_ID); expect(reportActions).toBeUndefined(); }); it('should clear only specified error keys when keys parameter is provided', async () => { + // Given a report action with multiple errors in Onyx const reportAction = createMockReportAction({ errors: {error1: 'Error 1', error2: 'Error 2', error3: 'Error 3'}, }); @@ -113,9 +127,11 @@ describe('ClearReportActionErrors', () => { }); await waitForBatchedUpdates(); + // When clearAllRelatedReportActionErrors is called with specific keys to clear clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID, undefined, ['error1', 'error2']); await waitForBatchedUpdates(); + // Then only the specified errors should be cleared, leaving error3 const reportActions = await getReportActionsFromOnyx(REPORT_ID); expect(reportActions?.[REPORT_ACTION_ID]?.errors).toEqual({ error3: 'Error 3', @@ -123,6 +139,7 @@ describe('ClearReportActionErrors', () => { }); it('should delete isOptimisticAction report action instead of clearing errors', async () => { + // Given an optimistic report action with errors in Onyx const reportAction = createMockReportAction({ isOptimisticAction: true, }); @@ -131,14 +148,17 @@ describe('ClearReportActionErrors', () => { }); await waitForBatchedUpdates(); + // When clearAllRelatedReportActionErrors is called clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); await waitForBatchedUpdates(); + // Then the entire report action should be deleted (not just errors cleared) const reportActions = await getReportActionsFromOnyx(REPORT_ID); expect(reportActions?.[REPORT_ACTION_ID]).toBeUndefined(); }); it('should clear errors on parent report action when matching error keys exist', async () => { + // Given a child report with parent reference and both have errors with matching keys const report = createMockReport({ parentReportID: PARENT_REPORT_ID, parentReportActionID: PARENT_REPORT_ACTION_ID, @@ -160,14 +180,17 @@ describe('ClearReportActionErrors', () => { }); await waitForBatchedUpdates(); + // When clearAllRelatedReportActionErrors is called on the child action clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); await waitForBatchedUpdates(); + // Then the parent action's matching error should also be cleared const parentReportActions = await getReportActionsFromOnyx(PARENT_REPORT_ID); expect(parentReportActions?.[PARENT_REPORT_ACTION_ID]?.errors).toEqual({}); }); it('should not clear parent errors when ignore is set to parent', async () => { + // Given a child report with parent reference and both have errors with matching keys const report = createMockReport({ parentReportID: PARENT_REPORT_ID, parentReportActionID: PARENT_REPORT_ACTION_ID, @@ -189,14 +212,17 @@ describe('ClearReportActionErrors', () => { }); await waitForBatchedUpdates(); + // When clearAllRelatedReportActionErrors is called with ignore='parent' clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID, 'parent'); await waitForBatchedUpdates(); + // Then the parent action's error should remain unchanged const parentReportActions = await getReportActionsFromOnyx(PARENT_REPORT_ID); expect(parentReportActions?.[PARENT_REPORT_ACTION_ID]?.errors).toEqual({sharedError: 'Parent error message'}); }); it('should not clear child errors when ignore is set to child', async () => { + // Given a parent action with childReportID and both have errors with matching keys const reportAction = createMockReportAction({ childReportID: CHILD_REPORT_ID, errors: {sharedError: 'Error message'}, @@ -215,14 +241,17 @@ describe('ClearReportActionErrors', () => { }); await waitForBatchedUpdates(); + // When clearAllRelatedReportActionErrors is called with ignore='child' clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID, 'child'); await waitForBatchedUpdates(); + // Then the child action's error should remain unchanged const childReportActions = await getReportActionsFromOnyx(CHILD_REPORT_ID); expect(childReportActions?.[CHILD_REPORT_ACTION_ID]?.errors).toEqual({sharedError: 'Child error message'}); }); it('should not clear parent errors when error keys do not match', async () => { + // Given a child report with parent reference where error keys are different const report = createMockReport({ parentReportID: PARENT_REPORT_ID, parentReportActionID: PARENT_REPORT_ACTION_ID, @@ -244,14 +273,17 @@ describe('ClearReportActionErrors', () => { }); await waitForBatchedUpdates(); + // When clearAllRelatedReportActionErrors is called clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); await waitForBatchedUpdates(); + // Then the parent action's error should remain because the keys don't match const parentReportActions = await getReportActionsFromOnyx(PARENT_REPORT_ID); expect(parentReportActions?.[PARENT_REPORT_ACTION_ID]?.errors).toEqual({differentError: 'Parent error message'}); }); it('should use originalReportID for clearing errors when it differs from reportID', async () => { + // Given a report action stored under a different originalReportID const originalReportID = '999'; const reportAction = createMockReportAction(); @@ -260,24 +292,30 @@ describe('ClearReportActionErrors', () => { }); await waitForBatchedUpdates(); + // When clearAllRelatedReportActionErrors is called with different reportID and originalReportID clearAllRelatedReportActionErrors(REPORT_ID, reportAction, originalReportID); await waitForBatchedUpdates(); + // Then the errors should be cleared from the originalReportID location const reportActions = await getReportActionsFromOnyx(originalReportID); expect(reportActions?.[REPORT_ACTION_ID]?.errors).toBeUndefined(); }); it('should handle empty errors object', async () => { + // Given a report action with an empty errors object const reportAction = createMockReportAction({errors: {}}); + // When clearAllRelatedReportActionErrors is called clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); await waitForBatchedUpdates(); + // Then no report actions should be created or modified (early return) const reportActions = await getReportActionsFromOnyx(REPORT_ID); expect(reportActions).toBeUndefined(); }); it('should handle report action without reportActionID', async () => { + // Given a report action with empty reportActionID in Onyx const reportAction = createMockReportAction({reportActionID: ''}); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { @@ -285,15 +323,18 @@ describe('ClearReportActionErrors', () => { }); await waitForBatchedUpdates(); + // When clearAllRelatedReportActionErrors is called clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); await waitForBatchedUpdates(); + // Then the report action should remain unchanged (early return due to missing reportActionID) const reportActions = await getReportActionsFromOnyx(REPORT_ID); expect(reportActions?.[REPORT_ACTION_ID]).toBeDefined(); expect(reportActions?.[REPORT_ACTION_ID]?.errors).toEqual({error1: 'Error message'}); }); it('should clear multiple child report action errors', async () => { + // Given a parent action with childReportID pointing to a report with multiple actions const reportAction = createMockReportAction({ childReportID: CHILD_REPORT_ID, errors: {sharedError: 'Error message'}, @@ -317,15 +358,18 @@ describe('ClearReportActionErrors', () => { }); await waitForBatchedUpdates(); + // When clearAllRelatedReportActionErrors is called clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); await waitForBatchedUpdates(); + // Then all child actions with matching error keys should have their errors cleared const childReportActions = await getReportActionsFromOnyx(CHILD_REPORT_ID); expect(childReportActions?.[CHILD_REPORT_ACTION_ID]?.errors).toEqual({}); expect(childReportActions?.['301']?.errors).toEqual({}); }); it('should only clear child errors that match the error keys', async () => { + // Given a parent action and child action where child has additional non-matching errors const reportAction = createMockReportAction({ childReportID: CHILD_REPORT_ID, errors: {error1: 'Error message'}, @@ -344,9 +388,11 @@ describe('ClearReportActionErrors', () => { }); await waitForBatchedUpdates(); + // When clearAllRelatedReportActionErrors is called clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); await waitForBatchedUpdates(); + // Then only matching errors should be cleared, leaving non-matching errors intact const childReportActions = await getReportActionsFromOnyx(CHILD_REPORT_ID); expect(childReportActions?.[CHILD_REPORT_ACTION_ID]?.errors).toEqual({error2: 'Child error 2'}); }); From c495170ff69b657ae98397f71cd9b53142588733 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Wed, 25 Feb 2026 10:12:57 -0700 Subject: [PATCH 05/12] Fix TS --- tests/actions/IOUTest.ts | 2 +- tests/ui/ClearReportActionErrorsUITest.tsx | 9 ++------- 2 files changed, 3 insertions(+), 8 deletions(-) diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 92a3c04f51cc..95bff31684ef 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -9,6 +9,7 @@ import OnyxListItemProvider from '@components/OnyxListItemProvider'; import type {SearchQueryJSON, SearchStatus} from '@components/Search/types'; import useOnyx from '@hooks/useOnyx'; import useReportWithTransactionsAndViolations from '@hooks/useReportWithTransactionsAndViolations'; +import {clearAllRelatedReportActionErrors} from '@libs/actions/ClearReportActionErrors'; import type {PerDiemExpenseTransactionParams, RequestMoneyParticipantParams} from '@libs/actions/IOU'; import { approveMoneyRequest, @@ -51,7 +52,6 @@ import {completeSplitBill, splitBill, startSplitBill, updateSplitTransactionsFro import initOnyxDerivedValues from '@libs/actions/OnyxDerived'; import {createWorkspace, deleteWorkspace, generatePolicyID, setWorkspaceApprovalMode} from '@libs/actions/Policy/Policy'; import {addComment, createNewReport, deleteReport, notifyNewAction, openReport} from '@libs/actions/Report'; -import {clearAllRelatedReportActionErrors} from '@libs/actions/ReportActions'; import {subscribeToUserEvents} from '@libs/actions/User'; import type {ApiCommand} from '@libs/API/types'; import {WRITE_COMMANDS} from '@libs/API/types'; diff --git a/tests/ui/ClearReportActionErrorsUITest.tsx b/tests/ui/ClearReportActionErrorsUITest.tsx index 4ff3e0ed14e7..4b2da5dbad57 100644 --- a/tests/ui/ClearReportActionErrorsUITest.tsx +++ b/tests/ui/ClearReportActionErrorsUITest.tsx @@ -108,17 +108,15 @@ describe('ClearReportActionErrors UI', () => { options?: { clearErrorFn?: typeof clearAllRelatedReportActionErrors; originalReportID?: string; - allReports?: Record; }, ) { - const {clearErrorFn, originalReportID = report.reportID, allReports} = options ?? {}; + const {clearErrorFn, originalReportID = report.reportID} = options ?? {}; return render( { iouReportOfLinkedReport={undefined} currentUserAccountID={ACTOR_ACCOUNT_ID} allTransactionDrafts={undefined} + userBillingGraceEndPeriodCollection={undefined} clearAllRelatedReportActionErrors={clearErrorFn} originalReportID={originalReportID} /> @@ -269,10 +268,6 @@ describe('ClearReportActionErrors UI', () => { renderReportActionItem(parentAction, parentReport, { clearErrorFn: clearAllRelatedReportActionErrors, originalReportID: REPORT_ID, - allReports: { - [`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`]: parentReport, - [`${ONYXKEYS.COLLECTION.REPORT}${CHILD_REPORT_ID}`]: childReport, - }, }); await waitForBatchedUpdatesWithAct(); From fb7d1cf5b8ec0c196e0575f37fd7732ecd2e290b Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Wed, 25 Feb 2026 13:12:54 -0700 Subject: [PATCH 06/12] Fix lint and add comments --- src/libs/actions/ClearReportActionErrors.ts | 3 +++ tests/unit/ClearReportActionErrorsTest.ts | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/libs/actions/ClearReportActionErrors.ts b/src/libs/actions/ClearReportActionErrors.ts index 2e83cf1ffb9b..60dba0c7dfb8 100644 --- a/src/libs/actions/ClearReportActionErrors.ts +++ b/src/libs/actions/ClearReportActionErrors.ts @@ -32,16 +32,19 @@ function clearReportActionErrors(reportID: string, reportAction: ReportAction, o } if (reportAction.pendingAction === CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD || reportAction.isOptimisticAction) { + // If there's a linked transaction, delete that too const linkedTransactionID = getLinkedTransactionID(reportAction); if (linkedTransactionID) { Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${linkedTransactionID}`, null); Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${reportAction.childReportID}`, null); } + // Delete the optimistic action Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`, { [reportAction.reportActionID]: null, }); + // Delete the failed task report too const taskReportID = getReportActionMessage(reportAction)?.taskReportID; if (taskReportID && isCreatedTaskReportAction(reportAction)) { deleteReport(taskReportID); diff --git a/tests/unit/ClearReportActionErrorsTest.ts b/tests/unit/ClearReportActionErrorsTest.ts index 3fad987abf0f..1d58e05e749c 100644 --- a/tests/unit/ClearReportActionErrorsTest.ts +++ b/tests/unit/ClearReportActionErrorsTest.ts @@ -354,6 +354,8 @@ describe('ClearReportActionErrors', () => { }); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${CHILD_REPORT_ID}`, { [CHILD_REPORT_ACTION_ID]: childReportAction1, + + // eslint-disable-next-line @typescript-eslint/naming-convention '301': childReportAction2, }); await waitForBatchedUpdates(); From 15b39fc66a2c8328500ba4e4197e0ea84f83ceb6 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Wed, 25 Feb 2026 14:46:10 -0700 Subject: [PATCH 07/12] Add justification comment --- tests/unit/ClearReportActionErrorsTest.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit/ClearReportActionErrorsTest.ts b/tests/unit/ClearReportActionErrorsTest.ts index 1d58e05e749c..4f5eccade8ec 100644 --- a/tests/unit/ClearReportActionErrorsTest.ts +++ b/tests/unit/ClearReportActionErrorsTest.ts @@ -355,6 +355,7 @@ describe('ClearReportActionErrors', () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${CHILD_REPORT_ID}`, { [CHILD_REPORT_ACTION_ID]: childReportAction1, + // It's OK to disable the rule here because the key needs to be numeric for reportActionIDs // eslint-disable-next-line @typescript-eslint/naming-convention '301': childReportAction2, }); From 1d81969122bd2df35b43ff78fa186693132e72b8 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 26 Feb 2026 12:26:54 -0700 Subject: [PATCH 08/12] Fix imports --- src/components/ReportActionItem/MoneyRequestReceiptView.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/components/ReportActionItem/MoneyRequestReceiptView.tsx b/src/components/ReportActionItem/MoneyRequestReceiptView.tsx index b4dd90050ba0..4a1a201317ba 100644 --- a/src/components/ReportActionItem/MoneyRequestReceiptView.tsx +++ b/src/components/ReportActionItem/MoneyRequestReceiptView.tsx @@ -46,9 +46,9 @@ import { } from '@libs/TransactionUtils'; import ViolationsUtils, {filterReceiptViolations} from '@libs/Violations/ViolationsUtils'; import Navigation from '@navigation/Navigation'; +import {clearAllRelatedReportActionErrors} from '@userActions/ClearReportActionErrors'; import {cleanUpMoneyRequest} from '@userActions/IOU'; import {navigateToConciergeChatAndDeleteReport} from '@userActions/Report'; -import {clearAllRelatedReportActionErrors} from '@userActions/ClearReportActionErrors'; import {clearError, getLastModifiedExpense, revert} from '@userActions/Transaction'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; From 259b383f3cc00855eeedfc55491dc1f4fadc6147 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 26 Feb 2026 14:28:00 -0700 Subject: [PATCH 09/12] Remove unused parameter --- src/libs/actions/ClearReportActionErrors.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/ClearReportActionErrors.ts b/src/libs/actions/ClearReportActionErrors.ts index 60dba0c7dfb8..33f3fb8f9140 100644 --- a/src/libs/actions/ClearReportActionErrors.ts +++ b/src/libs/actions/ClearReportActionErrors.ts @@ -26,7 +26,7 @@ Onyx.connectWithoutView({ }, }); -function clearReportActionErrors(reportID: string, reportAction: ReportAction, originalReportID: string | undefined, keys?: string[]) { +function clearReportActionErrors(reportAction: ReportAction, originalReportID: string | undefined, keys?: string[]) { if (!reportAction?.reportActionID) { return; } @@ -90,7 +90,7 @@ function clearAllRelatedReportActionErrors( return; } - clearReportActionErrors(reportID, reportAction, originalReportID, keys); + clearReportActionErrors(reportAction, originalReportID, keys); const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; if (report?.parentReportID && report?.parentReportActionID && ignore !== 'parent') { From 30f055fdf2a3b07f144eea7abed35b1ba997e74b Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Thu, 26 Feb 2026 15:08:18 -0700 Subject: [PATCH 10/12] Fix imports --- src/libs/actions/IOU/index.ts | 2 +- src/pages/inbox/report/PureReportActionItem.tsx | 2 +- src/pages/inbox/report/ReportActionItem.tsx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index f12351dd3e10..af43fe1faf91 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -231,13 +231,13 @@ import { } from '@libs/TransactionUtils'; import ViolationsUtils from '@libs/Violations/ViolationsUtils'; import {clearByKey as clearPdfByOnyxKey} from '@userActions/CachedPDFPaths'; +import {clearAllRelatedReportActionErrors} from '@userActions/ClearReportActionErrors'; import {buildAddMembersToWorkspaceOnyxData, buildUpdateWorkspaceMembersRoleOnyxData} from '@userActions/Policy/Member'; import {buildPolicyData, generatePolicyID} from '@userActions/Policy/Policy'; import type {BuildPolicyDataKeys} from '@userActions/Policy/Policy'; import {buildOptimisticPolicyRecentlyUsedTags} from '@userActions/Policy/Tag'; import type {GuidedSetupData} from '@userActions/Report'; import {buildInviteToRoomOnyxData, completeOnboarding, notifyNewAction, optimisticReportLastData} from '@userActions/Report'; -import {clearAllRelatedReportActionErrors} from '@userActions/ClearReportActionErrors'; import {mergeTransactionIdsHighlightOnSearchRoute, sanitizeRecentWaypoints} from '@userActions/Transaction'; import {removeDraftTransaction, removeDraftTransactions, removeDraftTransactionsByIDs} from '@userActions/TransactionEdit'; import {getOnboardingMessages} from '@userActions/Welcome/OnboardingFlow'; diff --git a/src/pages/inbox/report/PureReportActionItem.tsx b/src/pages/inbox/report/PureReportActionItem.tsx index ff3557741aee..aeb5881670fe 100644 --- a/src/pages/inbox/report/PureReportActionItem.tsx +++ b/src/pages/inbox/report/PureReportActionItem.tsx @@ -226,6 +226,7 @@ import AttachmentModalContext from '@pages/media/AttachmentModalScreen/Attachmen import variables from '@styles/variables'; import {openPersonalBankAccountSetupView} from '@userActions/BankAccounts'; import {resolveFraudAlert} from '@userActions/Card'; +import type {IgnoreDirection} from '@userActions/ClearReportActionErrors'; import {hideEmojiPicker, isActive} from '@userActions/EmojiPickerAction'; import {acceptJoinRequest, declineJoinRequest} from '@userActions/Policy/Member'; import { @@ -235,7 +236,6 @@ import { resolveConciergeCategoryOptions, resolveConciergeDescriptionOptions, } from '@userActions/Report'; -import type {IgnoreDirection} from '@userActions/ClearReportActionErrors'; import {isAnonymousUser, signOutAndRedirectToSignIn} from '@userActions/Session'; import {isBlockedFromConcierge} from '@userActions/User'; import type {IOUAction} from '@src/CONST'; diff --git a/src/pages/inbox/report/ReportActionItem.tsx b/src/pages/inbox/report/ReportActionItem.tsx index 8a3f992e1863..3fafb482cdcd 100644 --- a/src/pages/inbox/report/ReportActionItem.tsx +++ b/src/pages/inbox/report/ReportActionItem.tsx @@ -22,6 +22,7 @@ import { isClosedExpenseReportWithNoExpenses, isCurrentUserTheOnlyParticipant, } from '@libs/ReportUtils'; +import {clearAllRelatedReportActionErrors} from '@userActions/ClearReportActionErrors'; import { deleteReportActionDraft, dismissTrackExpenseActionableWhisper, @@ -29,7 +30,6 @@ import { resolveActionableReportMentionWhisper, toggleEmojiReaction, } from '@userActions/Report'; -import {clearAllRelatedReportActionErrors} from '@userActions/ClearReportActionErrors'; import {clearError} from '@userActions/Transaction'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; From 38fc58ea1a83fb1cf6c847079d8fd05043c34de2 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Mon, 2 Mar 2026 15:14:48 -0700 Subject: [PATCH 11/12] Use helpers for fake report actions --- tests/ui/ClearReportActionErrorsUITest.tsx | 40 +++++--------- tests/unit/ClearReportActionErrorsTest.ts | 63 ++++++++-------------- 2 files changed, 37 insertions(+), 66 deletions(-) diff --git a/tests/ui/ClearReportActionErrorsUITest.tsx b/tests/ui/ClearReportActionErrorsUITest.tsx index 4b2da5dbad57..516501b9e157 100644 --- a/tests/ui/ClearReportActionErrorsUITest.tsx +++ b/tests/ui/ClearReportActionErrorsUITest.tsx @@ -16,6 +16,7 @@ import CONST from '@src/CONST'; import * as ReportActionUtils from '@src/libs/ReportActionsUtils'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report, ReportAction, ReportActions} from '@src/types/onyx'; +import {getFakeReportAction} from '../utils/ReportTestUtils'; import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; @@ -28,24 +29,8 @@ const REPORT_ACTION_ID = '99999'; const CHILD_REPORT_ID = '67890'; const CHILD_REPORT_ACTION_ID = '88888'; -function createMockReportAction(overrides: Partial = {}): ReportAction { - const errorTimestamp = Date.now() * 1000; - return { - reportActionID: REPORT_ACTION_ID, - actorAccountID: ACTOR_ACCOUNT_ID, - created: '2025-01-01 12:00:00.000', - actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, - automatic: false, - shouldShow: true, - avatar: '', - person: [{type: 'TEXT', style: 'strong', text: ACTOR_EMAIL}], - message: [{type: 'COMMENT', html: 'Test message with error', text: 'Test message with error'}], - errors: { - [errorTimestamp]: 'Something went wrong. Please try again.', - }, - ...overrides, - } as ReportAction; -} +const DEFAULT_ERROR_TIMESTAMP = Date.now() * 1000; +const DEFAULT_ERRORS = {[DEFAULT_ERROR_TIMESTAMP]: 'Something went wrong. Please try again.'}; function createMockReport(overrides: Partial = {}): Report { return { @@ -146,7 +131,7 @@ describe('ClearReportActionErrors UI', () => { describe('Error display and dismissal', () => { it('should display error message when action has errors', async () => { // Given a report action with errors stored in Onyx - const action = createMockReportAction(); + const action = getFakeReportAction(Number(REPORT_ACTION_ID), {actorAccountID: ACTOR_ACCOUNT_ID, errors: DEFAULT_ERRORS}); const report = createMockReport(); await act(async () => { @@ -167,7 +152,7 @@ describe('ClearReportActionErrors UI', () => { it('should call clearAllRelatedReportActionErrors when error is dismissed', async () => { // Given a rendered report action with errors and a mock clear function - const action = createMockReportAction(); + const action = getFakeReportAction(Number(REPORT_ACTION_ID), {actorAccountID: ACTOR_ACCOUNT_ID, errors: DEFAULT_ERRORS}); const report = createMockReport(); const mockClearErrors = jest.fn(); @@ -196,7 +181,8 @@ describe('ClearReportActionErrors UI', () => { it('should clear error from Onyx when dismissed', async () => { // Given a rendered report action with a visible error message const errorTimestamp = Date.now() * 1000; - const action = createMockReportAction({ + const action = getFakeReportAction(Number(REPORT_ACTION_ID), { + actorAccountID: ACTOR_ACCOUNT_ID, errors: {[errorTimestamp]: 'Test error message'}, }); const report = createMockReport(); @@ -235,14 +221,14 @@ describe('ClearReportActionErrors UI', () => { const sharedErrorTimestamp = Date.now() * 1000; const sharedError = {[sharedErrorTimestamp]: 'Shared error message'}; - const parentAction = createMockReportAction({ - reportActionID: REPORT_ACTION_ID, + const parentAction = getFakeReportAction(Number(REPORT_ACTION_ID), { + actorAccountID: ACTOR_ACCOUNT_ID, childReportID: CHILD_REPORT_ID, errors: sharedError, }); - const childAction = createMockReportAction({ - reportActionID: CHILD_REPORT_ACTION_ID, + const childAction = getFakeReportAction(Number(CHILD_REPORT_ACTION_ID), { + actorAccountID: ACTOR_ACCOUNT_ID, errors: sharedError, }); @@ -289,7 +275,9 @@ describe('ClearReportActionErrors UI', () => { describe('Optimistic action deletion', () => { it('should delete optimistic action instead of clearing errors', async () => { // Given an optimistic report action (pendingAction='add' and isOptimisticAction=true) with errors - const action = createMockReportAction({ + const action = getFakeReportAction(Number(REPORT_ACTION_ID), { + actorAccountID: ACTOR_ACCOUNT_ID, + errors: DEFAULT_ERRORS, pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, isOptimisticAction: true, }); diff --git a/tests/unit/ClearReportActionErrorsTest.ts b/tests/unit/ClearReportActionErrorsTest.ts index 4f5eccade8ec..7b553ab30515 100644 --- a/tests/unit/ClearReportActionErrorsTest.ts +++ b/tests/unit/ClearReportActionErrorsTest.ts @@ -2,7 +2,8 @@ import Onyx from 'react-native-onyx'; import {clearAllRelatedReportActionErrors} from '@libs/actions/ClearReportActionErrors'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Report, ReportAction, ReportActions} from '@src/types/onyx'; +import type {Report, ReportActions} from '@src/types/onyx'; +import {getFakeReportAction} from '../utils/ReportTestUtils'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; @@ -13,18 +14,6 @@ const REPORT_ACTION_ID = '100'; const PARENT_REPORT_ACTION_ID = '200'; const CHILD_REPORT_ACTION_ID = '300'; -function createMockReportAction(overrides: Partial = {}): ReportAction { - return { - reportActionID: REPORT_ACTION_ID, - actionName: CONST.REPORT.ACTIONS.TYPE.ADD_COMMENT, - actorAccountID: 1, - created: '2024-01-01 00:00:00.000', - message: [{type: 'TEXT', text: 'Test message'}], - errors: {error1: 'Error message'}, - ...overrides, - } as ReportAction; -} - function createMockReport(overrides: Partial = {}): Report { return { reportID: REPORT_ID, @@ -93,7 +82,7 @@ describe('ClearReportActionErrors', () => { it('should return early when reportAction has no errors', async () => { // Given a report action with no errors - const reportAction = createMockReportAction({errors: undefined}); + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), {errors: undefined}); // When clearAllRelatedReportActionErrors is called clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); @@ -106,7 +95,7 @@ describe('ClearReportActionErrors', () => { it('should return early when reportID is undefined', async () => { // Given a report action with errors - const reportAction = createMockReportAction(); + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), {errors: {error1: 'Error message'}}); // When clearAllRelatedReportActionErrors is called with undefined reportID clearAllRelatedReportActionErrors(undefined, reportAction, REPORT_ID); @@ -119,7 +108,7 @@ describe('ClearReportActionErrors', () => { it('should clear only specified error keys when keys parameter is provided', async () => { // Given a report action with multiple errors in Onyx - const reportAction = createMockReportAction({ + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), { errors: {error1: 'Error 1', error2: 'Error 2', error3: 'Error 3'}, }); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { @@ -140,7 +129,8 @@ describe('ClearReportActionErrors', () => { it('should delete isOptimisticAction report action instead of clearing errors', async () => { // Given an optimistic report action with errors in Onyx - const reportAction = createMockReportAction({ + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), { + errors: {error1: 'Error message'}, isOptimisticAction: true, }); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { @@ -163,11 +153,10 @@ describe('ClearReportActionErrors', () => { parentReportID: PARENT_REPORT_ID, parentReportActionID: PARENT_REPORT_ACTION_ID, }); - const reportAction = createMockReportAction({ + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), { errors: {sharedError: 'Error message'}, }); - const parentReportAction = createMockReportAction({ - reportActionID: PARENT_REPORT_ACTION_ID, + const parentReportAction = getFakeReportAction(Number(PARENT_REPORT_ACTION_ID), { errors: {sharedError: 'Parent error message'}, }); @@ -195,11 +184,10 @@ describe('ClearReportActionErrors', () => { parentReportID: PARENT_REPORT_ID, parentReportActionID: PARENT_REPORT_ACTION_ID, }); - const reportAction = createMockReportAction({ + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), { errors: {sharedError: 'Error message'}, }); - const parentReportAction = createMockReportAction({ - reportActionID: PARENT_REPORT_ACTION_ID, + const parentReportAction = getFakeReportAction(Number(PARENT_REPORT_ACTION_ID), { errors: {sharedError: 'Parent error message'}, }); @@ -223,12 +211,11 @@ describe('ClearReportActionErrors', () => { it('should not clear child errors when ignore is set to child', async () => { // Given a parent action with childReportID and both have errors with matching keys - const reportAction = createMockReportAction({ + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), { childReportID: CHILD_REPORT_ID, errors: {sharedError: 'Error message'}, }); - const childReportAction = createMockReportAction({ - reportActionID: CHILD_REPORT_ACTION_ID, + const childReportAction = getFakeReportAction(Number(CHILD_REPORT_ACTION_ID), { errors: {sharedError: 'Child error message'}, }); @@ -256,11 +243,10 @@ describe('ClearReportActionErrors', () => { parentReportID: PARENT_REPORT_ID, parentReportActionID: PARENT_REPORT_ACTION_ID, }); - const reportAction = createMockReportAction({ + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), { errors: {error1: 'Error message'}, }); - const parentReportAction = createMockReportAction({ - reportActionID: PARENT_REPORT_ACTION_ID, + const parentReportAction = getFakeReportAction(Number(PARENT_REPORT_ACTION_ID), { errors: {differentError: 'Parent error message'}, }); @@ -285,7 +271,7 @@ describe('ClearReportActionErrors', () => { it('should use originalReportID for clearing errors when it differs from reportID', async () => { // Given a report action stored under a different originalReportID const originalReportID = '999'; - const reportAction = createMockReportAction(); + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), {errors: {error1: 'Error message'}}); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`, { [REPORT_ACTION_ID]: reportAction, @@ -303,7 +289,7 @@ describe('ClearReportActionErrors', () => { it('should handle empty errors object', async () => { // Given a report action with an empty errors object - const reportAction = createMockReportAction({errors: {}}); + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), {errors: {}}); // When clearAllRelatedReportActionErrors is called clearAllRelatedReportActionErrors(REPORT_ID, reportAction, REPORT_ID); @@ -316,7 +302,7 @@ describe('ClearReportActionErrors', () => { it('should handle report action without reportActionID', async () => { // Given a report action with empty reportActionID in Onyx - const reportAction = createMockReportAction({reportActionID: ''}); + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), {reportActionID: '', errors: {error1: 'Error message'}}); await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { [REPORT_ACTION_ID]: reportAction, @@ -335,16 +321,14 @@ describe('ClearReportActionErrors', () => { it('should clear multiple child report action errors', async () => { // Given a parent action with childReportID pointing to a report with multiple actions - const reportAction = createMockReportAction({ + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), { childReportID: CHILD_REPORT_ID, errors: {sharedError: 'Error message'}, }); - const childReportAction1 = createMockReportAction({ - reportActionID: CHILD_REPORT_ACTION_ID, + const childReportAction1 = getFakeReportAction(Number(CHILD_REPORT_ACTION_ID), { errors: {sharedError: 'Child error message 1'}, }); - const childReportAction2 = createMockReportAction({ - reportActionID: '301', + const childReportAction2 = getFakeReportAction(301, { errors: {sharedError: 'Child error message 2'}, }); @@ -373,12 +357,11 @@ describe('ClearReportActionErrors', () => { it('should only clear child errors that match the error keys', async () => { // Given a parent action and child action where child has additional non-matching errors - const reportAction = createMockReportAction({ + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), { childReportID: CHILD_REPORT_ID, errors: {error1: 'Error message'}, }); - const childReportAction = createMockReportAction({ - reportActionID: CHILD_REPORT_ACTION_ID, + const childReportAction = getFakeReportAction(Number(CHILD_REPORT_ACTION_ID), { errors: {error1: 'Child error 1', error2: 'Child error 2'}, }); From 4da7616125a307a1f7de13be771fbcded0f16885 Mon Sep 17 00:00:00 2001 From: Tim Golen Date: Mon, 2 Mar 2026 15:25:19 -0700 Subject: [PATCH 12/12] Create a mock report utility --- tests/ui/ClearReportActionErrorsUITest.tsx | 23 +++++++--------------- tests/unit/ClearReportActionErrorsTest.ts | 14 ++----------- tests/utils/ReportTestUtils.ts | 12 +++++++++-- 3 files changed, 19 insertions(+), 30 deletions(-) diff --git a/tests/ui/ClearReportActionErrorsUITest.tsx b/tests/ui/ClearReportActionErrorsUITest.tsx index 516501b9e157..fc7a5f1de334 100644 --- a/tests/ui/ClearReportActionErrorsUITest.tsx +++ b/tests/ui/ClearReportActionErrorsUITest.tsx @@ -16,7 +16,7 @@ import CONST from '@src/CONST'; import * as ReportActionUtils from '@src/libs/ReportActionsUtils'; import ONYXKEYS from '@src/ONYXKEYS'; import type {Report, ReportAction, ReportActions} from '@src/types/onyx'; -import {getFakeReportAction} from '../utils/ReportTestUtils'; +import {createMockReport, getFakeReportAction} from '../utils/ReportTestUtils'; import waitForBatchedUpdatesWithAct from '../utils/waitForBatchedUpdatesWithAct'; import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; @@ -32,16 +32,6 @@ const CHILD_REPORT_ACTION_ID = '88888'; const DEFAULT_ERROR_TIMESTAMP = Date.now() * 1000; const DEFAULT_ERRORS = {[DEFAULT_ERROR_TIMESTAMP]: 'Something went wrong. Please try again.'}; -function createMockReport(overrides: Partial = {}): Report { - return { - reportID: REPORT_ID, - reportName: 'Test Report', - type: CONST.REPORT.TYPE.CHAT, - ownerAccountID: ACTOR_ACCOUNT_ID, - ...overrides, - } as Report; -} - function getReportActionsFromOnyx(reportID: string): Promise { return new Promise((resolve) => { const connection = Onyx.connect({ @@ -132,7 +122,7 @@ describe('ClearReportActionErrors UI', () => { it('should display error message when action has errors', async () => { // Given a report action with errors stored in Onyx const action = getFakeReportAction(Number(REPORT_ACTION_ID), {actorAccountID: ACTOR_ACCOUNT_ID, errors: DEFAULT_ERRORS}); - const report = createMockReport(); + const report = createMockReport({reportID: REPORT_ID, ownerAccountID: ACTOR_ACCOUNT_ID}); await act(async () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); @@ -153,7 +143,7 @@ describe('ClearReportActionErrors UI', () => { it('should call clearAllRelatedReportActionErrors when error is dismissed', async () => { // Given a rendered report action with errors and a mock clear function const action = getFakeReportAction(Number(REPORT_ACTION_ID), {actorAccountID: ACTOR_ACCOUNT_ID, errors: DEFAULT_ERRORS}); - const report = createMockReport(); + const report = createMockReport({reportID: REPORT_ID, ownerAccountID: ACTOR_ACCOUNT_ID}); const mockClearErrors = jest.fn(); await act(async () => { @@ -185,7 +175,7 @@ describe('ClearReportActionErrors UI', () => { actorAccountID: ACTOR_ACCOUNT_ID, errors: {[errorTimestamp]: 'Test error message'}, }); - const report = createMockReport(); + const report = createMockReport({reportID: REPORT_ID, ownerAccountID: ACTOR_ACCOUNT_ID}); await act(async () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); @@ -232,11 +222,12 @@ describe('ClearReportActionErrors UI', () => { errors: sharedError, }); - const parentReport = createMockReport(); + const parentReport = createMockReport({reportID: REPORT_ID, ownerAccountID: ACTOR_ACCOUNT_ID}); const childReport = createMockReport({ reportID: CHILD_REPORT_ID, parentReportID: REPORT_ID, parentReportActionID: REPORT_ACTION_ID, + ownerAccountID: ACTOR_ACCOUNT_ID, }); await act(async () => { @@ -281,7 +272,7 @@ describe('ClearReportActionErrors UI', () => { pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, isOptimisticAction: true, }); - const report = createMockReport(); + const report = createMockReport({reportID: REPORT_ID, ownerAccountID: ACTOR_ACCOUNT_ID}); await act(async () => { await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT}${REPORT_ID}`, report); diff --git a/tests/unit/ClearReportActionErrorsTest.ts b/tests/unit/ClearReportActionErrorsTest.ts index 7b553ab30515..30692730caf0 100644 --- a/tests/unit/ClearReportActionErrorsTest.ts +++ b/tests/unit/ClearReportActionErrorsTest.ts @@ -1,9 +1,8 @@ import Onyx from 'react-native-onyx'; import {clearAllRelatedReportActionErrors} from '@libs/actions/ClearReportActionErrors'; -import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; -import type {Report, ReportActions} from '@src/types/onyx'; -import {getFakeReportAction} from '../utils/ReportTestUtils'; +import type {ReportActions} from '@src/types/onyx'; +import {createMockReport, getFakeReportAction} from '../utils/ReportTestUtils'; import waitForBatchedUpdates from '../utils/waitForBatchedUpdates'; import wrapOnyxWithWaitForBatchedUpdates from '../utils/wrapOnyxWithWaitForBatchedUpdates'; @@ -14,15 +13,6 @@ const REPORT_ACTION_ID = '100'; const PARENT_REPORT_ACTION_ID = '200'; const CHILD_REPORT_ACTION_ID = '300'; -function createMockReport(overrides: Partial = {}): Report { - return { - reportID: REPORT_ID, - reportName: 'Test Report', - type: CONST.REPORT.TYPE.CHAT, - ...overrides, - } as Report; -} - function getReportActionsFromOnyx(reportID: string): Promise { return new Promise((resolve) => { const connection = Onyx.connect({ diff --git a/tests/utils/ReportTestUtils.ts b/tests/utils/ReportTestUtils.ts index dfdbfeb6f963..929e70dade9d 100644 --- a/tests/utils/ReportTestUtils.ts +++ b/tests/utils/ReportTestUtils.ts @@ -1,5 +1,5 @@ import CONST from '@src/CONST'; -import type {ReportAction, ReportActions} from '@src/types/onyx'; +import type {Report, ReportAction, ReportActions} from '@src/types/onyx'; import type ReportActionName from '@src/types/onyx/ReportActionName'; import createRandomReportAction from './collections/reportActions'; @@ -71,4 +71,12 @@ const getMockedReportActionsMap = (length = 100): ReportActions => { return Object.assign({}, ...mockReports) as ReportActions; }; -export {getFakeReportAction, getMockedSortedReportActions, getMockedReportActionsMap}; +const createMockReport = (overrides: Partial = {}): Report => + ({ + reportID: '1', + reportName: 'Test Report', + type: CONST.REPORT.TYPE.CHAT, + ...overrides, + }) as Report; + +export {getFakeReportAction, getMockedSortedReportActions, getMockedReportActionsMap, createMockReport};