diff --git a/src/libs/actions/IOU/index.ts b/src/libs/actions/IOU/index.ts index 4caa9400bc6f..1697ccae3b4b 100644 --- a/src/libs/actions/IOU/index.ts +++ b/src/libs/actions/IOU/index.ts @@ -12952,6 +12952,7 @@ function prepareRejectMoneyRequestData( existingTransactionThreadReportID: childReportID, shouldGenerateTransactionThreadReport: false, }); + createdIOUReportActionID = iouAction.reportActionID; optimisticData.push( { diff --git a/tests/actions/IOUTest.ts b/tests/actions/IOUTest.ts index 307ba204dea4..5130f3d62b65 100644 --- a/tests/actions/IOUTest.ts +++ b/tests/actions/IOUTest.ts @@ -11131,6 +11131,58 @@ describe('actions/IOU', () => { ]), ); }); + + it('should the createdIOUReportActionID parameter not be undefined when rejecting an expense to an open report', async () => { + // Mock API.write for this test + // eslint-disable-next-line rulesdir/no-multiple-api-calls + const writeSpy = jest.spyOn(API, 'write').mockImplementation(jest.fn()); + + const openingReport = { + ...createRandomReport(3, undefined), + type: CONST.REPORT.TYPE.EXPENSE, + ownerAccountID: TEST_USER_ACCOUNT_ID, + managerID: MANAGER_ACCOUNT_ID, + total: 0, + currency: CONST.CURRENCY.USD, + stateNum: CONST.REPORT.STATE_NUM.OPEN, + statusNum: CONST.REPORT.STATUS_NUM.OPEN, + policyID: policy?.id, + chatReportID: chatReport?.reportID, + }; + + const secondTransaction = { + ...createRandomTransaction(2), + reportID: iouReport?.reportID, + amount, + currency: CONST.CURRENCY.USD, + merchant: 'Test Merchant', + transactionID: '2', + }; + + // Given: An expense report (not IOU) + const expenseReport = {...iouReport, type: CONST.REPORT.TYPE.EXPENSE, total: amount * 2}; + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${expenseReport.reportID}`, expenseReport); + await Onyx.set(`${ONYXKEYS.COLLECTION.REPORT}${openingReport.reportID}`, openingReport); + await Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${secondTransaction.transactionID}`, secondTransaction); + await waitForBatchedUpdates(); + + // When: Reject the money request + if (!transaction?.transactionID || !iouReport?.reportID) { + throw new Error('Required transaction or report data is missing'); + } + rejectMoneyRequest(transaction.transactionID, iouReport.reportID, comment, policy); + await waitForBatchedUpdates(); + + // Then: createdIOUReportActionID shouldn't be undefined + expect(writeSpy).toHaveBeenCalledWith( + expect.anything(), + expect.not.objectContaining({ + createdIOUReportActionID: undefined, + }), + expect.anything(), + ); + writeSpy.mockRestore(); + }); }); describe('markRejectViolationAsResolved', () => {