Files
TREK/server/tests/unit/nest/trips.helpers.test.ts
T
Maurice a70eea5bf2 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.
2026-11-13 10:17:03 +01:00

49 lines
2.1 KiB
TypeScript

/**
* The pure helpers of the trips domain: the date range a trip may span and the
* feed token a trip row loses on its way out, and the rights a trip update
* needs. TRIP-HELPERS-001 to 004.
*/
import { assertTripSpan, tripUpdatePermissions, withoutFeedToken } from '../../../src/nest/trips/trips.helpers';
import { MAX_TRIP_DAYS, addIsoDays } from '@trek/shared';
import { describe, expect, it } from 'vitest';
describe('assertTripSpan', () => {
it('TRIP-HELPERS-001: takes a range from a single day up to the limit', () => {
const lastAllowed = addIsoDays('2025-07-01', MAX_TRIP_DAYS - 1);
expect(() => assertTripSpan('2025-07-01', '2025-07-01')).not.toThrow();
expect(() => assertTripSpan('2025-07-01', lastAllowed)).not.toThrow();
});
it('TRIP-HELPERS-002: refuses an inverted range and one past the limit', () => {
const tooLate = addIsoDays('2025-07-01', MAX_TRIP_DAYS);
expect(() => assertTripSpan('2025-07-02', '2025-07-01')).toThrow('End date must be after start date');
expect(() => assertTripSpan('2025-07-01', tooLate)).toThrow(`A trip can span at most ${MAX_TRIP_DAYS} days`);
});
});
describe('withoutFeedToken', () => {
it('TRIP-HELPERS-003: drops feed_token from the row it was given and passes anything else through', () => {
const row = { id: 1, title: 'Japan', feed_token: 'feed-1' };
expect(withoutFeedToken(row)).toBe(row);
expect(row).toEqual({ id: 1, title: 'Japan' });
expect(row).not.toHaveProperty('feed_token');
expect(withoutFeedToken(undefined)).toBeUndefined();
expect(withoutFeedToken(null)).toBeNull();
expect(withoutFeedToken(42)).toBe(42);
});
});
describe('tripUpdatePermissions', () => {
it('TRIP-HELPERS-004: the costs mode asks for budget_edit, not trip_edit (#1714)', () => {
expect(tripUpdatePermissions({ costs_mode: 'totals' }).map((p) => p.action)).toEqual(['budget_edit']);
expect(tripUpdatePermissions({ costs_mode: 'split', title: 'Japan' }).map((p) => p.action)).toEqual([
'budget_edit',
'trip_edit',
]);
expect(tripUpdatePermissions({ title: 'Japan', costs_mode: undefined }).map((p) => p.action)).toEqual([
'trip_edit',
]);
});
});