Skip to content

Commit 63bde36

Browse files
MelvinBotsituchan
andcommitted
Add unit test verifying splitExpensesTotal preserves report total on split deletion
Tests that passing correct splitExpensesTotal when deleting a split child transaction keeps the report total unchanged, preventing the double-counting bug that occurred when splitExpensesTotal was omitted. Co-authored-by: Situ Chandra Shil <situchan@users.noreply.github.com>
1 parent 75fe887 commit 63bde36

1 file changed

Lines changed: 68 additions & 0 deletions

File tree

tests/actions/IOUTest/SplitTest.ts

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4571,6 +4571,74 @@ describe('updateSplitTransactions', () => {
45714571
const updatedReportPreviewAction = getReportPreviewAction(chatReport?.reportID, expenseReport?.reportID);
45724572
expect(updatedReportPreviewAction?.childVisibleActionCount).toEqual(2);
45734573
});
4574+
4575+
it('should preserve report total when deleting a split with correct splitExpensesTotal', async () => {
4576+
// This tests the bug where useDeleteTransactions was not passing splitExpensesTotal,
4577+
// causing the report total to be incorrect after offline split deletion.
4578+
// Without splitExpensesTotal, changesInReportTotal = sum(remaining splits) - 0 = remaining total,
4579+
// which incorrectly subtracts that from the report total again.
4580+
const {expenseReport, originalTransactionID} = await createBaseExpense();
4581+
const reportID = expenseReport?.reportID ?? String(CONST.DEFAULT_NUMBER_ID);
4582+
const txID = originalTransactionID ?? String(CONST.DEFAULT_NUMBER_ID);
4583+
4584+
// Split into 4 parts for clean division (amount=10000, each split = 2500)
4585+
const splitIDs = await splitToN(4, expenseReport, txID);
4586+
4587+
// Capture the report total after the initial split
4588+
const reportAfterSplit = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
4589+
const totalAfterSplit = reportAfterSplit?.total ?? 0;
4590+
4591+
const {allTransactions, allReports, allReportNameValuePairs} = await getCollections();
4592+
const policyTags = await getPolicyTags(reportID);
4593+
const reports = getTransactionAndExpenseReports(reportID);
4594+
4595+
// "Delete" one split by calling updateSplitTransactions with only three remaining splits.
4596+
// Pass splitExpensesTotal = sum of remaining splits (this is what useDeleteTransactions now does).
4597+
const splitAmount = amount / 4;
4598+
const splitExpensesTotal = splitAmount * 3;
4599+
4600+
updateSplitTransactions({
4601+
allTransactionsList: allTransactions,
4602+
allReportsList: allReports,
4603+
allReportNameValuePairsList: allReportNameValuePairs,
4604+
transactionData: {
4605+
reportID,
4606+
originalTransactionID: txID,
4607+
splitExpenses: [
4608+
{transactionID: splitIDs[0], amount: splitAmount, created: DateUtils.getDBTime()},
4609+
{transactionID: splitIDs[1], amount: splitAmount, created: DateUtils.getDBTime()},
4610+
{transactionID: splitIDs[2], amount: splitAmount, created: DateUtils.getDBTime()},
4611+
],
4612+
splitExpensesTotal,
4613+
},
4614+
searchContext: {currentSearchHash: -2},
4615+
policyCategories: undefined,
4616+
policy: undefined,
4617+
policyRecentlyUsedCategories: [],
4618+
iouReport: expenseReport,
4619+
firstIOU: undefined,
4620+
isASAPSubmitBetaEnabled: false,
4621+
currentUserPersonalDetails,
4622+
transactionViolations: {},
4623+
policyRecentlyUsedCurrencies: [],
4624+
quickAction: undefined,
4625+
iouReportNextStep: undefined,
4626+
betas: [CONST.BETAS.ALL],
4627+
policyTags,
4628+
personalDetails: {[RORY_ACCOUNT_ID]: {accountID: RORY_ACCOUNT_ID, login: RORY_EMAIL}},
4629+
transactionReport: reports.transactionReport,
4630+
expenseReport: reports.expenseReport,
4631+
isOffline: false,
4632+
});
4633+
await waitForBatchedUpdates();
4634+
4635+
// With correct splitExpensesTotal: changesInReportTotal = remainingAmount - splitExpensesTotal = 0,
4636+
// so the report total should not change from the post-split value.
4637+
// Without splitExpensesTotal (the bug): changesInReportTotal = remainingAmount - 0 = remainingAmount,
4638+
// which would incorrectly subtract that from the total, resulting in totalAfterSplit + remainingAmount.
4639+
const updatedReport = await getOnyxValue(`${ONYXKEYS.COLLECTION.REPORT}${reportID}`);
4640+
expect(updatedReport?.total).toBe(totalAfterSplit);
4641+
});
45744642
});
45754643

45764644
describe('initSplitExpense', () => {

0 commit comments

Comments
 (0)