Skip to content
Open
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
9 changes: 2 additions & 7 deletions src/pages/AddExistingExpense.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import React, {useCallback, useEffect, useMemo, useRef, useState} from 'react';
import {View} from 'react-native';

import canAddTransactionAmountToReport from './canAddTransactionAmountToReport';
import UnreportedExpenseListItem from './UnreportedExpenseListItem';

type AddExistingExpensePageType = PlatformStackScreenProps<AddExistingExpensesParamList, typeof SCREENS.ADD_EXISTING_EXPENSES_ROOT>;
Expand Down Expand Up @@ -121,13 +122,7 @@ function AddExistingExpense({route}: AddExistingExpensePageType) {

const transactionAmount = getTransactionDetails(item)?.amount ?? 0;

// Only block negative amounts for unreported expenses.
if (transactionAmount < 0 && isUnreported) {
return false;
}

// Zero amount expenses are not allowed in IOU reports
if (isIOU && transactionAmount === 0) {
if (!canAddTransactionAmountToReport(transactionAmount, isIOU)) {
return false;
}

Expand Down
5 changes: 5 additions & 0 deletions src/pages/canAddTransactionAmountToReport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function canAddTransactionAmountToReport(transactionAmount: number, isIOU: boolean): boolean {
return !isIOU || transactionAmount > 0;
}

export default canAddTransactionAmountToReport;
20 changes: 20 additions & 0 deletions tests/unit/AddExistingExpenseTest.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import {getTransactionDetails} from '@libs/ReportUtils';
import {createUnreportedExpenses} from '@libs/TransactionUtils';

import canAddTransactionAmountToReport from '@pages/canAddTransactionAmountToReport';

import CONST from '@src/CONST';
import type Transaction from '@src/types/onyx/Transaction';

Expand All @@ -25,6 +28,23 @@ function generateTransaction(values: Partial<Transaction> = {}): Transaction {
}

describe('AddExistingExpense', () => {
describe('canAddTransactionAmountToReport', () => {
const getUnreportedTransactionAmount = (storedAmount: number) => getTransactionDetails(generateTransaction({amount: storedAmount}))?.amount ?? 0;

test.each([
{amountType: 'negative', storedAmount: 1000, isIOU: false, expected: true},
{amountType: 'zero', storedAmount: 0, isIOU: false, expected: true},
{amountType: 'positive', storedAmount: -1000, isIOU: false, expected: true},
{amountType: 'negative', storedAmount: 1000, isIOU: true, expected: false},
{amountType: 'zero', storedAmount: 0, isIOU: true, expected: false},
{amountType: 'positive', storedAmount: -1000, isIOU: true, expected: true},
])('should handle a $amountType unreported expense with isIOU=$isIOU', ({storedAmount, isIOU, expected}) => {
const transactionAmount = getUnreportedTransactionAmount(storedAmount);

expect(canAddTransactionAmountToReport(transactionAmount, isIOU)).toBe(expected);
});
});

describe('createUnreportedExpenses', () => {
it('should mark transactions with DELETE pendingAction as disabled', () => {
const normalTransaction = generateTransaction({
Expand Down
Loading