mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-23 21:33:19 +00:00
fix(costs): count only balances read in the shown currency in the owe and owed cards
After a currency switch the cards kept summing the flows of the previous currency, offline or while the new read ran, and printed them in the new one.
This commit is contained in:
@@ -16,7 +16,7 @@ import type { CostsSettlementResponse } from './costsModel';
|
||||
import { useCostsLedger } from './useCostsLedger';
|
||||
import { useFreezeMissingRates } from './useFreezeMissingRates';
|
||||
|
||||
// FE-BUDGET-LEDGER-001 to FE-BUDGET-LEDGER-021
|
||||
// FE-BUDGET-LEDGER-001 to FE-BUDGET-LEDGER-022
|
||||
|
||||
const fx = vi.hoisted(() => ({
|
||||
// One stable converter, the way useExchangeRates hands out a memoised one: USD is half a euro.
|
||||
@@ -394,6 +394,26 @@ describe('useCostsLedger: another currency for this trip (#1683)', () => {
|
||||
expect(createMock()).not.toHaveBeenCalled();
|
||||
localStorage.removeItem('trek:costs-currency:7');
|
||||
});
|
||||
it('FE-BUDGET-LEDGER-022: the owe and owed cards count only flows read in the currency shown', async () => {
|
||||
localStorage.removeItem('trek:costs-currency:7');
|
||||
const { result } = setup();
|
||||
await waitFor(() => expect(result.current.totals.owe).toBe(15));
|
||||
expect(result.current.totals.owed).toBe(5);
|
||||
settlementMock().mockRejectedValueOnce(new Error('Settlement not available offline'));
|
||||
act(() => result.current.setBase('JPY'));
|
||||
await waitFor(() => expect(result.current.settlementError).toBe(true));
|
||||
expect(result.current.totals.owe).toBe(0);
|
||||
expect(result.current.totals.owed).toBe(0);
|
||||
settlementMock().mockResolvedValueOnce({ ...SETTLEMENT, currency: 'EUR' });
|
||||
act(() => result.current.loadSettlement());
|
||||
await waitFor(() => expect(result.current.settlementError).toBe(false));
|
||||
expect(result.current.totals.owe).toBe(0);
|
||||
settlementMock().mockResolvedValueOnce({ ...SETTLEMENT, currency: 'JPY' });
|
||||
act(() => result.current.loadSettlement());
|
||||
await waitFor(() => expect(result.current.totals.owe).toBe(15));
|
||||
expect(result.current.totals.owed).toBe(5);
|
||||
localStorage.removeItem('trek:costs-currency:7');
|
||||
});
|
||||
});
|
||||
|
||||
describe('useCostsLedger: writes', () => {
|
||||
|
||||
@@ -216,7 +216,8 @@ export function useCostsLedger({
|
||||
// A settle-up books the flows in the display currency, so only flows read in it
|
||||
// may be booked: not after a failed read, and not while they are in another one.
|
||||
// And only by someone who may edit the costs at all.
|
||||
const canSettle = canEdit && !settlementError && settledIn === base && flows.length > 0;
|
||||
const flowsInBase = !settlementError && settledIn === base;
|
||||
const canSettle = canEdit && flowsInBase && flows.length > 0;
|
||||
const splitting = useMemo(
|
||||
() => showsSplitting(trip?.costs_mode, memberCount, budgetItems, settlement, me),
|
||||
[trip?.costs_mode, memberCount, budgetItems, settlement, me]
|
||||
@@ -227,9 +228,10 @@ export function useCostsLedger({
|
||||
const knownNames = useMemo(() => knownPersonNames(budgetItems, settlement), [budgetItems, settlement]);
|
||||
// `ctx` carries the rates. They can land after the expenses, and without it in the
|
||||
// deps the cards kept the sums they were first added up with while the rows moved on.
|
||||
// The owe and owed cards print in the display currency too, so they count the same flows.
|
||||
const totals = useMemo(
|
||||
() => computeTotals(budgetItems, flows, ctx, shareOf, leftOutCats),
|
||||
[budgetItems, flows, ctx, shareOf, leftOutCats]
|
||||
() => computeTotals(budgetItems, flowsInBase ? flows : [], ctx, shareOf, leftOutCats),
|
||||
[budgetItems, flowsInBase, flows, ctx, shareOf, leftOutCats]
|
||||
);
|
||||
const filtered = useMemo(
|
||||
() =>
|
||||
|
||||
Reference in New Issue
Block a user