mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-10-19 13:36:48 +00:00
188d867aee
useDayPlanSidebar returned about 145 names, and DayPlanSidebar destructured the same list again, the largest copied block in the client. Most of it was the component's own props passed straight back, plus values nothing read any more. The optional props' fallbacks move into withDayPlanDefaults, which the hook and the view both read the props through, so they are written once. The hook now returns only what it computes. State that only the view reads (the context menu, the permission flags, the hover and expansion states, the pending note deletion) is declared in the view, and the day note handlers come back as one notes object. The values no one read are gone, which also drops their unused variable warnings. No behaviour changes: every value keeps its source and its fallback.
81 lines
2.8 KiB
TypeScript
81 lines
2.8 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest';
|
|
import { buildReservation, buildTrip } from '../../../tests/helpers/factories';
|
|
import type { DayPlanSidebarProps } from './DayPlanSidebar';
|
|
import { withDayPlanDefaults } from './DayPlanSidebar.defaults';
|
|
|
|
function required(): DayPlanSidebarProps {
|
|
return {
|
|
tripId: 1,
|
|
trip: buildTrip({ id: 1 }),
|
|
days: [],
|
|
places: [],
|
|
categories: [],
|
|
assignments: {},
|
|
selectedDayId: null,
|
|
selectedPlaceId: null,
|
|
selectedAssignmentId: null,
|
|
onSelectDay: vi.fn(),
|
|
onPlaceClick: vi.fn(),
|
|
onDayDetail: vi.fn(),
|
|
onReorder: vi.fn(),
|
|
onUpdateDayTitle: vi.fn(),
|
|
onRouteCalculated: vi.fn(),
|
|
onAssignToDay: vi.fn(),
|
|
onRemoveAssignment: vi.fn(),
|
|
onEditPlace: vi.fn(),
|
|
onDeletePlace: vi.fn(),
|
|
onAddReservation: vi.fn(),
|
|
};
|
|
}
|
|
|
|
describe('withDayPlanDefaults', () => {
|
|
it('fills in every optional prop the plan reads without a value', () => {
|
|
const resolved = withDayPlanDefaults(required());
|
|
expect(resolved.accommodations).toEqual([]);
|
|
expect(resolved.reservations).toEqual([]);
|
|
expect(resolved.visibleConnectionIds).toEqual([]);
|
|
expect(resolved.allConnectionsShown).toBe(false);
|
|
expect(resolved.routeShown).toBe(false);
|
|
expect(resolved.routeProfile).toBe('driving');
|
|
expect(resolved.canUndo).toBe(false);
|
|
expect(resolved.lastActionLabel).toBeNull();
|
|
expect(resolved.showRouteToolsWhenExpanded).toBe(false);
|
|
expect(resolved.isMobile).toBe(false);
|
|
});
|
|
|
|
it('keeps the values that are given, falsy ones included', () => {
|
|
const reservations = [buildReservation({ id: 5 })];
|
|
const resolved = withDayPlanDefaults({
|
|
...required(),
|
|
reservations,
|
|
routeShown: true,
|
|
routeProfile: 'walking',
|
|
canUndo: true,
|
|
lastActionLabel: '',
|
|
isMobile: true,
|
|
});
|
|
expect(resolved.reservations).toBe(reservations);
|
|
expect(resolved.routeShown).toBe(true);
|
|
expect(resolved.routeProfile).toBe('walking');
|
|
expect(resolved.canUndo).toBe(true);
|
|
expect(resolved.lastActionLabel).toBe('');
|
|
expect(resolved.isMobile).toBe(true);
|
|
});
|
|
|
|
it('passes the other props through untouched', () => {
|
|
const props = required();
|
|
const onClearDay = vi.fn();
|
|
const resolved = withDayPlanDefaults({ ...props, onClearDay });
|
|
expect(resolved.tripId).toBe(1);
|
|
expect(resolved.trip).toBe(props.trip);
|
|
expect(resolved.onSelectDay).toBe(props.onSelectDay);
|
|
expect(resolved.onClearDay).toBe(onClearDay);
|
|
});
|
|
|
|
it('treats an explicit undefined as missing, like a default parameter', () => {
|
|
const resolved = withDayPlanDefaults({ ...required(), accommodations: undefined, routeProfile: undefined });
|
|
expect(resolved.accommodations).toEqual([]);
|
|
expect(resolved.routeProfile).toBe('driving');
|
|
});
|
|
});
|