mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +00:00
feat(trips): shortening a trip says first which days go and what is on them
The trip dialog on the desktop and the trip sheet on a phone now ask before a save whose new dates remove days with something on them. The question names those days the way the day list does and lists what goes with them in the same rows the delete day question uses: planned places stay in the place list, notes and day texts are deleted, bookings stay under Bookings, and a stay that checks in or out on a removed day goes as a whole while its booking and expense remain, shown in the danger tone. A moved start gets both questions in one step, with the booking hint following the chosen shift mode, and a save that only drops empty days asks nothing. The rule that lays the days out moved into shared as planDayGrid, with resolveDayGridRange deciding whether the rows are rebuilt at all. generateDays on the server now carries that plan out in one transaction, so the warning names exactly the days the save takes, and a seeded fuzz test holds the new rebuild to the old one over 300 random day grids. The update_trip tool says in its description what shortening takes and lists removed_days in its answer. Alongside, the dialog sends a day count only when it changed, so a stale count no longer trims a day just added, stays are reloaded after every date change including the one made on a phone, and the date shift tip now points to adding a day under Reorder days.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
// FE-COMP-TRIPFORM-001 to FE-COMP-TRIPFORM-084
|
||||
// FE-COMP-TRIPFORM-001 to FE-COMP-TRIPFORM-095
|
||||
import type { Mock } from 'vitest';
|
||||
import { render, screen, waitFor, fireEvent, within } from '../../../tests/helpers/render';
|
||||
import userEvent from '@testing-library/user-event';
|
||||
@@ -8,9 +8,9 @@ import { useTripStore } from '../../store/tripStore';
|
||||
import { useSettingsStore } from '../../store/settingsStore';
|
||||
import { usePermissionsStore } from '../../store/permissionsStore';
|
||||
import { resetAllStores, seedStore } from '../../../tests/helpers/store';
|
||||
import { buildUser, buildTrip } from '../../../tests/helpers/factories';
|
||||
import { buildUser, buildTrip, buildDay, buildAssignment, buildPlace, buildReservation } from '../../../tests/helpers/factories';
|
||||
import { server } from '../../../tests/helpers/msw/server';
|
||||
import type { Trip } from '../../types';
|
||||
import type { Accommodation, Reservation, Trip } from '../../types';
|
||||
import { MAX_TRIP_DAYS } from '@trek/shared';
|
||||
import TripFormModal from './TripFormModal';
|
||||
|
||||
@@ -1290,4 +1290,169 @@ describe('TripFormModal', () => {
|
||||
expect.objectContaining({ title: 'Renamed', start_date: '2020-01-01', end_date: '2030-01-01' })
|
||||
));
|
||||
});
|
||||
|
||||
// ── Shortening: the warning before days go ────────────────────────────────
|
||||
|
||||
/** A week in October with a place on its last two days, and the trip's bookings and stays. */
|
||||
const shrinkableTrip = () => buildTrip({ id: 1, title: 'Coast week', start_date: '2026-10-01', end_date: '2026-10-07' });
|
||||
const seedTripDays = (extras: { reservations?: Reservation[]; accommodations?: Accommodation[]; empty?: boolean } = {}) => {
|
||||
const days = Array.from({ length: 7 }, (_, i) => buildDay({
|
||||
id: 300 + i,
|
||||
trip_id: 1,
|
||||
day_number: i + 1,
|
||||
date: `2026-10-0${i + 1}`,
|
||||
assignments: !extras.empty && i >= 5 ? [buildAssignment({ day_id: 300 + i, place: buildPlace({ id: 400 + i }) })] : [],
|
||||
}));
|
||||
server.use(
|
||||
http.get('/api/trips/:id/days', () => HttpResponse.json({ days })),
|
||||
http.get('/api/trips/:id/reservations', () => HttpResponse.json({ reservations: extras.reservations ?? [] })),
|
||||
http.get('/api/trips/:id/accommodations', () => HttpResponse.json({ accommodations: extras.accommodations ?? [] })),
|
||||
);
|
||||
};
|
||||
const changeEndDate = async (user: ReturnType<typeof userEvent.setup>, iso: string) => {
|
||||
await user.click(screen.getAllByRole('button', { name: 'Enter date manually' })[1]);
|
||||
const input = screen.getByPlaceholderText('DD.MM.YYYY');
|
||||
fireEvent.change(input, { target: { value: iso } });
|
||||
fireEvent.keyDown(input, { key: 'Enter' });
|
||||
};
|
||||
|
||||
it('FE-COMP-TRIPFORM-088: an earlier end asks first and names the days it takes, with what is on them', async () => {
|
||||
seedTripDays({
|
||||
accommodations: [{ id: 9, trip_id: 1, start_day_id: 304, end_day_id: 306, place_name: 'Harbour Hotel' } as Accommodation],
|
||||
reservations: [buildReservation({ id: 70, day_id: 304, type: 'hotel', title: 'Harbour, 2 nights', accommodation_id: 9 })],
|
||||
});
|
||||
const user = userEvent.setup();
|
||||
const onSave = vi.fn().mockResolvedValue({});
|
||||
render(<TripFormModal {...defaultProps} trip={shrinkableTrip()} onSave={onSave} />);
|
||||
|
||||
await changeEndDate(user, '2026-10-05');
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
|
||||
const list = await screen.findByRole('list', { name: 'Remove days?' });
|
||||
expect(screen.getAllByText('Remove days?').length).toBeGreaterThan(0);
|
||||
expect(screen.getByText('Saving the new dates removes these days:')).toBeInTheDocument();
|
||||
expect(within(list).getByText('Tue, Oct 6')).toBeInTheDocument();
|
||||
expect(within(list).getByText('Wed, Oct 7')).toBeInTheDocument();
|
||||
expect(within(list).getByText('Stay at Harbour Hotel').closest('li')).toHaveAttribute('data-tone', 'danger');
|
||||
expect(within(list).getByText(/Its booking "Harbour, 2 nights" and its expense stay under Bookings/)).toBeInTheDocument();
|
||||
expect(within(list).getByText('Planned places: 2')).toBeInTheDocument();
|
||||
// No start moved, so no shift question.
|
||||
expect(screen.queryByText('Keep bookings on their dates')).not.toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: 'Remove days and save' })).toHaveClass('bg-danger');
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-COMP-TRIPFORM-089: a later start that also shortens asks both in one step', async () => {
|
||||
seedTripDays();
|
||||
const user = userEvent.setup();
|
||||
render(<TripFormModal {...defaultProps} trip={shrinkableTrip()} onSave={vi.fn()} />);
|
||||
|
||||
// Moving the start keeps the length, so the end is pulled back afterwards.
|
||||
await changeStartDate(user, '2026-10-03');
|
||||
await changeEndDate(user, '2026-10-07');
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
|
||||
await screen.findByText('Keep bookings on their dates');
|
||||
expect(screen.getByText('New start date')).toBeInTheDocument();
|
||||
const list = screen.getByRole('list', { name: 'Remove days?' });
|
||||
expect(within(list).getByText('The last days go, not the first')).toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: 'Remove days and save' })).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-TRIPFORM-090: a longer trip, or a shorter one that loses only empty days, saves without asking', async () => {
|
||||
seedTripDays({ empty: true });
|
||||
const user = userEvent.setup();
|
||||
const onSave = vi.fn().mockResolvedValue({});
|
||||
render(<TripFormModal {...defaultProps} trip={shrinkableTrip()} onSave={onSave} />);
|
||||
|
||||
await changeEndDate(user, '2026-10-05');
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
await waitFor(() => expect(onSave).toHaveBeenCalledWith(expect.objectContaining({ end_date: '2026-10-05' })));
|
||||
expect(onSave).toHaveBeenCalledWith(expect.not.objectContaining({ date_shift_mode: expect.anything() }));
|
||||
expect(screen.queryByRole('list', { name: 'Remove days?' })).not.toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-COMP-TRIPFORM-091: confirming saves the held payload, with the shift mode only when it was asked', async () => {
|
||||
seedTripDays();
|
||||
const user = userEvent.setup();
|
||||
const onSave = vi.fn().mockResolvedValue({});
|
||||
render(<TripFormModal {...defaultProps} trip={shrinkableTrip()} onSave={onSave} />);
|
||||
|
||||
await changeEndDate(user, '2026-10-05');
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
await user.click(await screen.findByRole('button', { name: 'Remove days and save' }));
|
||||
|
||||
await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1));
|
||||
expect(onSave).toHaveBeenCalledWith(expect.objectContaining({ start_date: '2026-10-01', end_date: '2026-10-05' }));
|
||||
expect(onSave).toHaveBeenCalledWith(expect.not.objectContaining({ date_shift_mode: expect.anything() }));
|
||||
});
|
||||
|
||||
it('FE-COMP-TRIPFORM-092: the booking hint follows the shift mode picked above the list', async () => {
|
||||
seedTripDays({ reservations: [buildReservation({ id: 71, day_id: 306, title: 'Ferry' })] });
|
||||
const user = userEvent.setup();
|
||||
const onSave = vi.fn().mockResolvedValue({});
|
||||
render(<TripFormModal {...defaultProps} trip={shrinkableTrip()} onSave={onSave} />);
|
||||
|
||||
await changeStartDate(user, '2026-10-02');
|
||||
await changeEndDate(user, '2026-10-06');
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
|
||||
const list = await screen.findByRole('list', { name: 'Remove days?' });
|
||||
expect(within(list).getByText(/One whose date is still part of the trip goes back onto that day/)).toBeInTheDocument();
|
||||
await user.click(screen.getByRole('radio', { name: /Shift everything/i }));
|
||||
expect(within(list).getByText('They stay under Bookings, without a day.')).toBeInTheDocument();
|
||||
|
||||
await user.click(screen.getByRole('button', { name: 'Remove days and save' }));
|
||||
await waitFor(() => expect(onSave).toHaveBeenCalledWith(expect.objectContaining({ date_shift_mode: 'shift_all' })));
|
||||
});
|
||||
|
||||
it('FE-COMP-TRIPFORM-093: Back from the warning returns to the form and saves nothing', async () => {
|
||||
seedTripDays();
|
||||
const user = userEvent.setup();
|
||||
const onSave = vi.fn();
|
||||
render(<TripFormModal {...defaultProps} trip={shrinkableTrip()} onSave={onSave} />);
|
||||
|
||||
await changeEndDate(user, '2026-10-05');
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
await screen.findByRole('list', { name: 'Remove days?' });
|
||||
|
||||
await user.click(screen.getByRole('button', { name: /Back/i }));
|
||||
await waitFor(() => expect(screen.queryByRole('list', { name: 'Remove days?' })).not.toBeInTheDocument());
|
||||
expect(screen.getByDisplayValue('Coast week')).toBeInTheDocument();
|
||||
expect(onSave).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-COMP-TRIPFORM-094: when the days cannot be read it still warns, in general terms', async () => {
|
||||
const spy = vi.spyOn(console, 'error').mockImplementation(() => {});
|
||||
server.use(http.get('/api/trips/:id/days', () => HttpResponse.json({ error: 'boom' }, { status: 500 })));
|
||||
const user = userEvent.setup();
|
||||
render(<TripFormModal {...defaultProps} trip={shrinkableTrip()} onSave={vi.fn()} />);
|
||||
|
||||
await changeEndDate(user, '2026-10-05');
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
|
||||
expect(await screen.findByRole('alert')).toHaveTextContent('The days of this trip could not be checked.');
|
||||
expect(screen.queryByRole('list', { name: 'Remove days?' })).not.toBeInTheDocument();
|
||||
expect(screen.getByRole('button', { name: 'Remove days and save' })).toBeInTheDocument();
|
||||
spy.mockRestore();
|
||||
});
|
||||
|
||||
it('FE-COMP-TRIPFORM-095: a trip without dates sends its day count only when it changed', async () => {
|
||||
const user = userEvent.setup();
|
||||
const onSave = vi.fn().mockResolvedValue({});
|
||||
const undated = buildTrip({ id: 1, title: 'Someday', start_date: null, end_date: null, day_count: 4 });
|
||||
const { unmount } = render(<TripFormModal {...defaultProps} trip={undated} onSave={onSave} />);
|
||||
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
await waitFor(() => expect(onSave).toHaveBeenCalledTimes(1));
|
||||
expect(onSave.mock.calls[0][0]).not.toHaveProperty('day_count');
|
||||
unmount();
|
||||
|
||||
render(<TripFormModal {...defaultProps} trip={undated} onSave={onSave} />);
|
||||
const dayInput = document.querySelector(`input[max="${MAX_TRIP_DAYS}"]`) as HTMLInputElement;
|
||||
fireEvent.change(dayInput, { target: { value: '6' } });
|
||||
await user.click(screen.getByRole('button', { name: /Update/i }));
|
||||
await waitFor(() => expect(onSave).toHaveBeenCalledTimes(2));
|
||||
expect(onSave.mock.calls[1][0]).toMatchObject({ day_count: 6 });
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user