diff --git a/src/components/ReportActionItem/MoneyRequestReceiptView.tsx b/src/components/ReportActionItem/MoneyRequestReceiptView.tsx index d4336c2987b9..79b541baf632 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, replaceReceipt} from '@userActions/IOU'; import {navigateToConciergeChatAndDeleteReport} from '@userActions/Report'; -import {clearAllRelatedReportActionErrors} from '@userActions/ReportActions'; 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 acc723e47e79..0d7107881ce8 100644 --- a/src/libs/actions/ReportActions.ts +++ b/src/libs/actions/ClearReportActionErrors.ts @@ -12,14 +12,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) => { @@ -27,7 +27,7 @@ Onyx.connect({ }, }); -function clearReportActionErrors(reportID: string, reportAction: ReportAction, originalReportID: string | undefined, keys?: string[]) { +function clearReportActionErrors(reportAction: ReportAction, originalReportID: string | undefined, keys?: string[]) { if (!reportAction?.reportActionID) { return; } @@ -112,7 +112,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') { @@ -135,7 +135,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 2d6b7272ce79..16fd3e79f9a7 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/ReportActions'; 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 574108e5c04f..6f9e1ff87185 100644 --- a/src/pages/inbox/report/PureReportActionItem.tsx +++ b/src/pages/inbox/report/PureReportActionItem.tsx @@ -225,6 +225,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 { @@ -234,7 +235,6 @@ import { resolveConciergeCategoryOptions, resolveConciergeDescriptionOptions, } from '@userActions/Report'; -import type {IgnoreDirection} from '@userActions/ReportActions'; 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 19833f34a465..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/ReportActions'; import {clearError} from '@userActions/Transaction'; import CONST from '@src/CONST'; import ONYXKEYS from '@src/ONYXKEYS'; diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index e022574ef85f..a382aa01ef37 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 new file mode 100644 index 000000000000..fc7a5f1de334 --- /dev/null +++ b/tests/ui/ClearReportActionErrorsUITest.tsx @@ -0,0 +1,302 @@ +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 {createMockReport, getFakeReportAction} from '../utils/ReportTestUtils'; +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'; + +const DEFAULT_ERROR_TIMESTAMP = Date.now() * 1000; +const DEFAULT_ERRORS = {[DEFAULT_ERROR_TIMESTAMP]: 'Something went wrong. Please try again.'}; + +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; + }, + ) { + const {clearErrorFn, originalReportID = report.reportID} = options ?? {}; + return render( + + + + + + + + + , + ); + } + + 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 = getFakeReportAction(Number(REPORT_ACTION_ID), {actorAccountID: ACTOR_ACCOUNT_ID, errors: DEFAULT_ERRORS}); + const report = createMockReport({reportID: REPORT_ID, ownerAccountID: ACTOR_ACCOUNT_ID}); + + 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(); + + // 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 = getFakeReportAction(Number(REPORT_ACTION_ID), {actorAccountID: ACTOR_ACCOUNT_ID, errors: DEFAULT_ERRORS}); + const report = createMockReport({reportID: REPORT_ID, ownerAccountID: ACTOR_ACCOUNT_ID}); + 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(); + + // 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 = getFakeReportAction(Number(REPORT_ACTION_ID), { + actorAccountID: ACTOR_ACCOUNT_ID, + errors: {[errorTimestamp]: 'Test error message'}, + }); + const report = createMockReport({reportID: REPORT_ID, ownerAccountID: ACTOR_ACCOUNT_ID}); + + 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(); + + // 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(); + }); + }); + + 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'}; + + const parentAction = getFakeReportAction(Number(REPORT_ACTION_ID), { + actorAccountID: ACTOR_ACCOUNT_ID, + childReportID: CHILD_REPORT_ID, + errors: sharedError, + }); + + const childAction = getFakeReportAction(Number(CHILD_REPORT_ACTION_ID), { + actorAccountID: ACTOR_ACCOUNT_ID, + errors: sharedError, + }); + + 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 () => { + 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, + }); + 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); + + 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 () => { + // Given an optimistic report action (pendingAction='add' and isOptimisticAction=true) with errors + const action = getFakeReportAction(Number(REPORT_ACTION_ID), { + actorAccountID: ACTOR_ACCOUNT_ID, + errors: DEFAULT_ERRORS, + pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD, + isOptimisticAction: true, + }); + const report = createMockReport({reportID: REPORT_ID, ownerAccountID: ACTOR_ACCOUNT_ID}); + + 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(); + + // 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 new file mode 100644 index 000000000000..30692730caf0 --- /dev/null +++ b/tests/unit/ClearReportActionErrorsTest.ts @@ -0,0 +1,376 @@ +import Onyx from 'react-native-onyx'; +import {clearAllRelatedReportActionErrors} from '@libs/actions/ClearReportActionErrors'; +import ONYXKEYS from '@src/ONYXKEYS'; +import type {ReportActions} from '@src/types/onyx'; +import {createMockReport, getFakeReportAction} from '../utils/ReportTestUtils'; +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 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 () => { + // 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 = getFakeReportAction(Number(REPORT_ACTION_ID), {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 = getFakeReportAction(Number(REPORT_ACTION_ID), {errors: {error1: 'Error message'}}); + + // 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 = getFakeReportAction(Number(REPORT_ACTION_ID), { + 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(); + + // 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', + }); + }); + + it('should delete isOptimisticAction report action instead of clearing errors', async () => { + // Given an optimistic report action with errors in Onyx + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), { + errors: {error1: 'Error message'}, + isOptimisticAction: true, + }); + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + 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, + }); + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), { + errors: {sharedError: 'Error message'}, + }); + const parentReportAction = getFakeReportAction(Number(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(); + + // 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, + }); + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), { + errors: {sharedError: 'Error message'}, + }); + const parentReportAction = getFakeReportAction(Number(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(); + + // 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 = getFakeReportAction(Number(REPORT_ACTION_ID), { + childReportID: CHILD_REPORT_ID, + errors: {sharedError: 'Error message'}, + }); + const childReportAction = getFakeReportAction(Number(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(); + + // 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, + }); + const reportAction = getFakeReportAction(Number(REPORT_ACTION_ID), { + errors: {error1: 'Error message'}, + }); + const parentReportAction = getFakeReportAction(Number(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(); + + // 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 = getFakeReportAction(Number(REPORT_ACTION_ID), {errors: {error1: 'Error message'}}); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + 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 = getFakeReportAction(Number(REPORT_ACTION_ID), {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 = getFakeReportAction(Number(REPORT_ACTION_ID), {reportActionID: '', errors: {error1: 'Error message'}}); + + await Onyx.merge(`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${REPORT_ID}`, { + [REPORT_ACTION_ID]: reportAction, + }); + 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 = getFakeReportAction(Number(REPORT_ACTION_ID), { + childReportID: CHILD_REPORT_ID, + errors: {sharedError: 'Error message'}, + }); + const childReportAction1 = getFakeReportAction(Number(CHILD_REPORT_ACTION_ID), { + errors: {sharedError: 'Child error message 1'}, + }); + const childReportAction2 = getFakeReportAction(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, + + // 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, + }); + 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 = getFakeReportAction(Number(REPORT_ACTION_ID), { + childReportID: CHILD_REPORT_ID, + errors: {error1: 'Error message'}, + }); + const childReportAction = getFakeReportAction(Number(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(); + + // 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'}); + }); + }); +}); 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};