Skip to content

A local-first personal finance app

Fix amount resetting to zero (#6056) (#8733)

parapsychicfa3cced1d163
diff --git a/packages/desktop-client/src/components/transactions/TransactionsTable.test.tsx b/packages/desktop-client/src/components/transactions/TransactionsTable.test.tsx
index ac8414aea2..0ac8736c2d 100644
--- a/packages/desktop-client/src/components/transactions/TransactionsTable.test.tsx
+++ b/packages/desktop-client/src/components/transactions/TransactionsTable.test.tsx
@@ -28,6 +28,7 @@ import {
renderHook,
screen,
waitFor,
+ within,
} from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { format as formatDate, parse as parseDate } from 'date-fns';
@@ -163,6 +164,10 @@ type LiveTransactionTableProps = {
isAdding: boolean;
onTransactionsChange?: (newTrans: TransactionEntity[]) => void;
onCloseAddTransaction?: () => void;
+ onApplyRules?: (
+ transaction: TransactionEntity,
+ updatedFieldName?: string | null,
+ ) => Promise<TransactionEntity>;
};
function LiveTransactionTable(props: LiveTransactionTableProps) {
@@ -1062,6 +1067,30 @@ describe('Transactions', () => {
expect(queryNewField(container, 'debit').textContent).toBe('0.00');
});
+ test('clicking cleared while editing the amount keeps the amount', async () => {
+ const { container, updateProps } = renderTransactions({
+ // Rules run over the backend, so saving a new transaction only lands in
+ // state a tick later. Clicking another cell in the meantime must not
+ // read back the pre-save amount.
+ onApplyRules: async transaction => transaction,
+ });
+ updateProps({ isAdding: true });
+
+ const input = await editNewField(container, 'debit');
+ await userEvent.clear(input);
+ await userEvent.type(input, '100');
+
+ // Click "cleared" directly, without tabbing/entering out of the amount
+ // field first
+ await userEvent.click(
+ within(queryNewField(container, 'cleared')).getByTestId('cell-button'),
+ );
+
+ await waitFor(() =>
+ expect(queryNewField(container, 'debit').textContent).toBe('100.00'),
+ );
+ });
+
test('adding a new split transaction works', async () => {
const { container, getTransactions, updateProps } = renderTransactions();
updateProps({ isAdding: true });
diff --git a/packages/desktop-client/src/components/transactions/table/utils.test.ts b/packages/desktop-client/src/components/transactions/table/utils.test.ts
index 449e03e75c..0c881c1f77 100644
--- a/packages/desktop-client/src/components/transactions/table/utils.test.ts
+++ b/packages/desktop-client/src/components/transactions/table/utils.test.ts
@@ -1,4 +1,7 @@
-import { shouldApplyRuleChange } from './utils';
+import type { TransactionEntity } from '@actual-app/core/types/models';
+
+import type { SerializedTransaction } from './utils';
+import { deserializeTransaction, shouldApplyRuleChange } from './utils';
describe('shouldApplyRuleChange', () => {
test('applies rule changes to empty fields', () => {
@@ -63,3 +66,65 @@ describe('shouldApplyRuleChange', () => {
);
});
});
+
+describe('deserializeTransaction', () => {
+ const originalTransaction: TransactionEntity = {
+ id: 'temp',
+ account: 'account-id',
+ date: '2017-01-01',
+ amount: 0,
+ cleared: false,
+ };
+
+ function serialized(
+ overrides: Partial<SerializedTransaction>,
+ ): SerializedTransaction {
+ return {
+ ...originalTransaction,
+ debit: '',
+ credit: '',
+ ...overrides,
+ };
+ }
+
+ test('reads the amount from the debit field', () => {
+ expect(
+ deserializeTransaction(serialized({ debit: '100.00' }), {
+ ...originalTransaction,
+ amount: -2500,
+ }).amount,
+ ).toBe(-10000);
+ });
+
+ test('reads the amount from the credit field', () => {
+ expect(
+ deserializeTransaction(serialized({ credit: '100.00' }), {
+ ...originalTransaction,
+ amount: -2500,
+ }).amount,
+ ).toBe(10000);
+ });
+
+ test('keeps the in-progress amount when both amount fields are blank', () => {
+ // Editing a non-amount field (e.g. toggling cleared) blanks both debit and
+ // credit, so the amount has to come from elsewhere. `originalTransaction`
+ // can still be showing the pre-save amount while a save is in flight, so
+ // the amount already on the row wins over the parent's copy.
+ const result = deserializeTransaction(
+ serialized({ amount: -10000, cleared: true }),
+ { ...originalTransaction, amount: 0 },
+ );
+
+ expect(result.amount).toBe(-10000);
+ expect(result.cleared).toBe(true);
+ });
+
+ test('falls back to the original amount when the row has none', () => {
+ expect(
+ deserializeTransaction(
+ serialized({ amount: null as unknown as TransactionEntity['amount'] }),
+ { ...originalTransaction, amount: -2500 },
+ ).amount,
+ ).toBe(-2500);
+ });
+});
diff --git a/packages/desktop-client/src/components/transactions/table/utils.ts b/packages/desktop-client/src/components/transactions/table/utils.ts
index e6ab845263..5bbfa67e95 100644
--- a/packages/desktop-client/src/components/transactions/table/utils.ts
+++ b/packages/desktop-client/src/components/transactions/table/utils.ts
@@ -81,7 +81,9 @@ export function deserializeTransaction(
}
amount =
- amount != null ? amountToInteger(amount) : originalTransaction.amount;
+ amount != null
+ ? amountToInteger(amount)
+ : (realTransaction.amount ?? originalTransaction.amount);
let date = originalDate;
if (date == null) {
date = originalTransaction.date || currentDay();
diff --git a/upcoming-release-notes/fix-amount-reset-when-clicking-cleared.md b/upcoming-release-notes/fix-amount-reset-when-clicking-cleared.md
new file mode 100644
index 0000000000..949e4a744c
--- /dev/null
+++ b/upcoming-release-notes/fix-amount-reset-when-clicking-cleared.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [ParaPsychic]
+---
+
+Fix an issue where an entered amount reset to zero when the Cleared icon was clicked while the amount field was focused.