A local-first personal finance app
Fix Scheduled Transactions Not Being Included in Selected Balance (#7274)
Ianc5fe29d50cc9
diff --git a/packages/desktop-client/src/components/accounts/Balance.test.tsx b/packages/desktop-client/src/components/accounts/Balance.test.tsxnew file mode 100644index 0000000000..c7738c80a1--- /dev/null+++ b/packages/desktop-client/src/components/accounts/Balance.test.tsx@@ -0,0 +1,121 @@+import React from 'react';++import { render, screen } from '@testing-library/react';++import type { ScheduleEntity } from 'loot-core/types/models';++import { SelectedBalance } from './Balance';++import { useCachedSchedules } from '@desktop-client/hooks/useCachedSchedules';+import { useSelectedItems } from '@desktop-client/hooks/useSelected';+import { useSheetValue } from '@desktop-client/hooks/useSheetValue';+import { TestProviders } from '@desktop-client/mocks';++vi.mock('@desktop-client/hooks/useSelected', () => ({+ useSelectedItems: vi.fn(),+}));++vi.mock('@desktop-client/hooks/useSheetValue', () => ({+ useSheetValue: vi.fn(),+}));++vi.mock('@desktop-client/hooks/useCachedSchedules', () => ({+ useCachedSchedules: vi.fn(),+}));++function makeSchedule(+ id: string,+ amount: number,+ accountId: string,+): ScheduleEntity {+ return {+ id,+ rule: 'rule-1',+ next_date: '2026-03-24',+ completed: false,+ posts_transaction: false,+ tombstone: false,+ _payee: 'payee-1',+ _account: accountId,+ _amount: amount,+ _amountOp: 'is',+ _date: '2026-03-24',+ _conditions: [],+ _actions: [],+ } satisfies ScheduleEntity;+}++describe('SelectedBalance – normal transactions', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ vi.mocked(useCachedSchedules).mockReturnValue({+ isLoading: false,+ schedules: [],+ });+ });++ test('shows balance for selected normal transactions', () => {+ vi.mocked(useSheetValue)+ .mockReturnValueOnce(null)+ .mockReturnValueOnce(-5000);++ render(+ <TestProviders>+ <SelectedBalance selectedItems={new Set(['tx-123'])} />+ </TestProviders>,+ );++ expect(screen.getByText('Selected balance:')).toBeInTheDocument();+ expect(screen.getByText('-50.00')).toBeInTheDocument();+ });+});++describe('SelectedBalance – preview (scheduled) transactions', () => {+ beforeEach(() => {+ vi.clearAllMocks();+ vi.mocked(useSheetValue).mockReturnValue(null);+ });++ test('includes the schedule amount when a preview transaction is selected', () => {+ const scheduleId = 'schedule-abc';++ vi.mocked(useSelectedItems).mockReturnValue(+ new Set([`preview/${scheduleId}/2026-03-24`]),+ );+ vi.mocked(useCachedSchedules).mockReturnValue({+ isLoading: false,+ schedules: [makeSchedule(scheduleId, -5000, 'account-1')],+ });++ render(+ <TestProviders>+ <SelectedBalance+ selectedItems={new Set([`preview/${scheduleId}/2026-03-24`])}+ />+ </TestProviders>,+ );++ expect(screen.getByText('Selected balance:')).toBeInTheDocument();+ });++ test('counts each selected occurrence of the same schedule independently', () => {+ const scheduleId = 'schedule-abc';+ const previewId1 = `preview/${scheduleId}/2026-03-24`;+ const previewId2 = `preview/${scheduleId}/2026-04-24`;+ const selectedItems = new Set([previewId1, previewId2]);++ vi.mocked(useSelectedItems).mockReturnValue(selectedItems);+ vi.mocked(useCachedSchedules).mockReturnValue({+ isLoading: false,+ schedules: [makeSchedule(scheduleId, -5000, 'account-1')],+ });++ render(+ <TestProviders>+ <SelectedBalance selectedItems={selectedItems} />+ </TestProviders>,+ );++ expect(screen.getByText('-100.00')).toBeInTheDocument();+ });+});diff --git a/packages/desktop-client/src/components/accounts/Balance.tsx b/packages/desktop-client/src/components/accounts/Balance.tsxindex b48caf4d52..5ed1d62424 100644--- a/packages/desktop-client/src/components/accounts/Balance.tsx+++ b/packages/desktop-client/src/components/accounts/Balance.tsx@@ -64,7 +64,10 @@ type SelectedBalanceProps = {account?: AccountEntity;};-function SelectedBalance({ selectedItems, account }: SelectedBalanceProps) {+export function SelectedBalance({+ selectedItems,+ account,+}: SelectedBalanceProps) {const { t } = useTranslation();const name = `selected-balance-${[...selectedItems].join('-')}`;@@ -97,22 +100,22 @@ function SelectedBalance({ selectedItems, account }: SelectedBalanceProps) {return null;}- const previewIds = [...selectedItems]- .filter(id => isPreviewId(id))- .map(id => id.slice(8));let isExactBalance = true;- for (const s of schedules) {- if (previewIds.includes(s.id)) {+ for (const id of [...selectedItems].filter(isPreviewId)) {+ // Preview IDs are in the format `preview/<schedule_id>/<date>`+ const scheduleId = id.slice(8).split('/')[0];+ const schedule = schedules.find(s => s.id === scheduleId);+ if (schedule) {// If a schedule is `between X and Y` then we calculate the average- if (s._amountOp === 'isbetween') {+ if (schedule._amountOp === 'isbetween') {isExactBalance = false;}- if (!account || account.id === s._account) {- scheduleBalance += getScheduledAmount(s._amount);+ if (!account || account.id === schedule._account) {+ scheduleBalance += getScheduledAmount(schedule._amount);} else {- scheduleBalance -= getScheduledAmount(s._amount);+ scheduleBalance -= getScheduledAmount(schedule._amount);}}}diff --git a/upcoming-release-notes/7274.md b/upcoming-release-notes/7274.mdnew file mode 100644index 0000000000..a475ab2233--- /dev/null+++ b/upcoming-release-notes/7274.md@@ -0,0 +1,6 @@+---+category: Bugfixes+authors: [iaewing]+---++Fixes a bug that prevented scheduled transactions from being included in the selected balance when selecting multiple transactions