mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-03 09:38:54 +00:00
feat(costs): let a trip keep its costs as totals only (#1714)
Travellers who track what a trip costs but never split it saw an Unfinished warning on every expense and a settle-up they had no use for. A trip now has a costs mode (appended migration, trips.costs_mode, split by default); totals leaves payers, shares and settling up out of Costs on desktop and phone through the same gate as a trip of one, and saves new expenses without a payer. PUT /api/trips/:id and update_trip take it on budget_edit. The split chips move into their own component.
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
// FE-BUDGET-COSTSMODE-001 to FE-BUDGET-COSTSMODE-003
|
||||
import { act, renderHook } from '@testing-library/react';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { buildTrip } from '../../../tests/helpers/factories';
|
||||
import { resetAllStores } from '../../../tests/helpers/store';
|
||||
import { useTripStore, type TripStoreState } from '../../store/tripStore';
|
||||
import { useCostsMode } from './useCostsMode';
|
||||
|
||||
let updateTrip: ReturnType<typeof vi.fn>;
|
||||
let addToast: ReturnType<typeof vi.fn>;
|
||||
|
||||
const trip = (costsMode?: 'split' | 'totals') => buildTrip({ id: 7, ...(costsMode ? { costs_mode: costsMode } : {}) });
|
||||
|
||||
beforeEach(() => {
|
||||
resetAllStores();
|
||||
updateTrip = vi.fn(async () => buildTrip({ id: 7 }));
|
||||
useTripStore.setState({ updateTrip } as unknown as Partial<TripStoreState>);
|
||||
addToast = vi.fn();
|
||||
(window as unknown as { __addToast: unknown }).__addToast = addToast;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
delete (window as unknown as { __addToast?: unknown }).__addToast;
|
||||
});
|
||||
|
||||
describe('useCostsMode (#1714)', () => {
|
||||
it('FE-BUDGET-COSTSMODE-001: a trip splits its costs until it says otherwise, and keeps totals only when it does', () => {
|
||||
expect(renderHook(() => useCostsMode(trip(), 2, true)).result.current.splitting).toBe(true);
|
||||
expect(renderHook(() => useCostsMode(trip('split'), 2, true)).result.current.splitting).toBe(true);
|
||||
expect(renderHook(() => useCostsMode(trip('totals'), 2, true)).result.current.splitting).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-BUDGET-COSTSMODE-002: the switch is offered to whoever may edit the costs, on a trip of more than one', () => {
|
||||
expect(renderHook(() => useCostsMode(trip(), 2, true)).result.current.offered).toBe(true);
|
||||
expect(renderHook(() => useCostsMode(trip(), 1, true)).result.current.offered).toBe(false);
|
||||
expect(renderHook(() => useCostsMode(trip(), 3, false)).result.current.offered).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-BUDGET-COSTSMODE-003: switching saves the mode on the trip, and a failure says so', async () => {
|
||||
const { result } = renderHook(() => useCostsMode(trip(), 2, true));
|
||||
await act(async () => {
|
||||
await result.current.setSplitting(false);
|
||||
});
|
||||
expect(updateTrip).toHaveBeenCalledWith(7, { costs_mode: 'totals' });
|
||||
expect(result.current.saving).toBe(false);
|
||||
|
||||
updateTrip.mockRejectedValueOnce(new Error('offline'));
|
||||
await act(async () => {
|
||||
await result.current.setSplitting(true);
|
||||
});
|
||||
expect(updateTrip).toHaveBeenLastCalledWith(7, { costs_mode: 'split' });
|
||||
expect(addToast.mock.calls.map((c) => c[1])).toContain('error');
|
||||
expect(result.current.saving).toBe(false);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user