diff --git a/src/libs/actions/MergeTransaction.ts b/src/libs/actions/MergeTransaction.ts index 4e511060558c..009321b90137 100644 --- a/src/libs/actions/MergeTransaction.ts +++ b/src/libs/actions/MergeTransaction.ts @@ -180,6 +180,9 @@ function getTransactionsForMerging({ cardList?: CardList; }) { const transactionID = targetTransaction.transactionID; + if (!transactionID) { + return; + } // Collect/Control workspaces: // - Admins and approvers: The list of eligible expenses will only contain the expenses from the report that the admin/approver triggered the merge from. This is intentionally limited since they’ll only be reviewing one report at a time. diff --git a/tests/actions/MergeTransactionTest.ts b/tests/actions/MergeTransactionTest.ts index 9c9ac45b50b2..259b7e37de5a 100644 --- a/tests/actions/MergeTransactionTest.ts +++ b/tests/actions/MergeTransactionTest.ts @@ -1,5 +1,5 @@ import {getReportPreviewAction} from '@libs/actions/IOU/MoneyRequestBuilder'; -import {areTransactionsEligibleForMerge, mergeTransactionRequest, setMergeTransactionKey, setupMergeTransactionData} from '@libs/actions/MergeTransaction'; +import {areTransactionsEligibleForMerge, getTransactionsForMerging, mergeTransactionRequest, setMergeTransactionKey, setupMergeTransactionData} from '@libs/actions/MergeTransaction'; import {addComment, openReport} from '@libs/actions/Report'; import {WRITE_COMMANDS} from '@libs/API/types'; import {getLoginsByAccountIDs} from '@libs/PersonalDetailsUtils'; @@ -1428,6 +1428,32 @@ describe('mergeTransactionRequest', () => { }); }); +describe('getTransactionsForMerging', () => { + beforeEach(() => { + return Onyx.clear().then(waitForBatchedUpdates); + }); + + it('should do nothing when the target transaction has no transactionID', async () => { + // Given a target transaction with an empty transactionID + const targetTransaction = {...createRandomTransaction(0), transactionID: ''} as Transaction; + + // When we request merge candidates for it (offline path, which would otherwise write eligible transactions locally) + getTransactionsForMerging({ + isOffline: true, + targetTransaction, + transactions: {}, + policy: undefined, + report: undefined, + currentUserLogin: undefined, + }); + await waitForBatchedUpdates(); + + // Then no merge transaction entry is written for the empty key + const mergeTransaction = await getOnyxValue(`${ONYXKEYS.COLLECTION.MERGE_TRANSACTION}${targetTransaction.transactionID}`); + expect(mergeTransaction).toBeUndefined(); + }); +}); + describe('setupMergeTransactionData', () => { beforeEach(() => { return Onyx.clear().then(waitForBatchedUpdates);