Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/libs/actions/IOU.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3373,6 +3373,10 @@ function calculateDiffAmount(
const currentAmount = getAmount(transaction, isExpenseReportLocal);
const updatedAmount = getAmount(updatedTransaction, isExpenseReportLocal);

if (updatedCurrency === currentCurrency && currentAmount === updatedAmount) {
return 0;
}

if (updatedCurrency === iouReport.currency && currentCurrency === iouReport.currency) {
// Calculate the diff between the updated amount and the current amount if the currency of the updated and current transactions have the same currency as the report
return updatedAmount - currentAmount;
Expand Down Expand Up @@ -10132,5 +10136,6 @@ export {
getNavigationUrlAfterTrackExpenseDelete,
canSubmitReport,
submitPerDiemExpense,
calculateDiffAmount,
};
export type {GPSPoint as GpsPoint, IOURequestType};
80 changes: 80 additions & 0 deletions tests/actions/IOUTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import isEqual from 'lodash/isEqual';
import type {OnyxCollection, OnyxEntry, OnyxInputValue} from 'react-native-onyx';
import Onyx from 'react-native-onyx';
import {
calculateDiffAmount,
canApproveIOU,
canCancelPayment,
cancelPayment,
Expand Down Expand Up @@ -4791,4 +4792,83 @@ describe('actions/IOU', () => {
expect(canIOUBePaid(fakeReport, policyChat, fakePolicy)).toBeFalsy();
});
});

describe('calculateDiffAmount', () => {
it('should return 0 if iouReport is undefined', () => {
const fakeTransaction: Transaction = {
...createRandomTransaction(1),
reportID: '1',
amount: 100,
currency: 'USD',
};

expect(calculateDiffAmount(undefined, fakeTransaction, fakeTransaction)).toBe(0);
});

it('should return 0 when the currency and amount of the transactions are the same', () => {
const fakeReport: Report = {
...createRandomReport(1),
type: CONST.REPORT.TYPE.EXPENSE,
policyID: '1',
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.APPROVED,
managerID: RORY_ACCOUNT_ID,
};
const fakeTransaction: Transaction = {
...createRandomTransaction(1),
reportID: fakeReport.reportID,
amount: 100,
currency: 'USD',
};

expect(calculateDiffAmount(fakeReport, fakeTransaction, fakeTransaction)).toBe(0);
});

it('should return the difference between the updated amount and the current amount when the currency of the updated and current transactions have the same currency', () => {
const fakeReport: Report = {
...createRandomReport(1),
type: CONST.REPORT.TYPE.EXPENSE,
policyID: '1',
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.APPROVED,
managerID: RORY_ACCOUNT_ID,
currency: 'USD',
};
const fakeTransaction: Transaction = {
...createRandomTransaction(1),
amount: 100,
currency: 'USD',
};
const updatedTransaction = {
...fakeTransaction,
amount: 200,
currency: 'USD',
};

expect(calculateDiffAmount(fakeReport, updatedTransaction, fakeTransaction)).toBe(-100);
});

it('should return null when the currency of the updated and current transactions have different values', () => {
const fakeReport: Report = {
...createRandomReport(1),
type: CONST.REPORT.TYPE.EXPENSE,
policyID: '1',
stateNum: CONST.REPORT.STATE_NUM.APPROVED,
statusNum: CONST.REPORT.STATUS_NUM.APPROVED,
managerID: RORY_ACCOUNT_ID,
};
const fakeTransaction: Transaction = {
...createRandomTransaction(1),
amount: 100,
currency: 'USD',
};
const updatedTransaction = {
...fakeTransaction,
amount: 200,
currency: 'EUR',
};

expect(calculateDiffAmount(fakeReport, updatedTransaction, fakeTransaction)).toBeNull();
});
});
});