mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-21 19:40:34 +00:00
feat(assistant): booking and stay cards, and a read of the bookings with their details
This commit is contained in:
@@ -0,0 +1,62 @@
|
||||
import { renderHook } from '@testing-library/react';
|
||||
import type { Reservation } from '@trek/shared';
|
||||
import { describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import type { DeepLinkProps } from './deepLinkTypes';
|
||||
import { useBookingDeepLink } from './useBookingDeepLink';
|
||||
|
||||
/**
|
||||
* FE-PLANNER-BOOKLINK-001 to FE-PLANNER-BOOKLINK-003
|
||||
*
|
||||
* The desktop planner's way into a booking the chat points at: its detail, or
|
||||
* its route drawn on the plan, a route already drawn left as it is and its
|
||||
* departure day picked.
|
||||
*/
|
||||
|
||||
const FLIGHT = { id: 7, trip_id: 5, type: 'flight', day_id: 31 } as Reservation;
|
||||
|
||||
function setup(query: string, onMap = false) {
|
||||
const searchParams = new URLSearchParams(query);
|
||||
const setSearchParams = vi.fn((update: (params: URLSearchParams) => URLSearchParams) => update(searchParams));
|
||||
const planner = {
|
||||
tripId: 5,
|
||||
reservations: [FLIGHT],
|
||||
isLoading: false,
|
||||
trip: { id: 5 },
|
||||
openBookingDetail: vi.fn(),
|
||||
showBookingOnMap: vi.fn(),
|
||||
isBookingOnMap: vi.fn(() => onMap),
|
||||
handleSelectDay: vi.fn(),
|
||||
};
|
||||
renderHook(() =>
|
||||
useBookingDeepLink({
|
||||
planner: planner as unknown as DeepLinkProps['planner'],
|
||||
workspace: {} as DeepLinkProps['workspace'],
|
||||
searchParams,
|
||||
setSearchParams: setSearchParams as never,
|
||||
})
|
||||
);
|
||||
return { planner, searchParams };
|
||||
}
|
||||
|
||||
describe('useBookingDeepLink', () => {
|
||||
it('FE-PLANNER-BOOKLINK-001: ?booking= opens the booking detail and leaves the URL', () => {
|
||||
const { planner, searchParams } = setup('booking=7');
|
||||
expect(planner.openBookingDetail).toHaveBeenCalledWith(FLIGHT);
|
||||
expect(planner.showBookingOnMap).not.toHaveBeenCalled();
|
||||
expect(searchParams.has('booking')).toBe(false);
|
||||
});
|
||||
|
||||
it('FE-PLANNER-BOOKLINK-002: with map=1 the route is drawn on the plan', () => {
|
||||
const { planner } = setup('booking=7&map=1');
|
||||
expect(planner.showBookingOnMap).toHaveBeenCalledWith(FLIGHT);
|
||||
expect(planner.openBookingDetail).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-PLANNER-BOOKLINK-003: a route already drawn is not switched off again; the plan goes to its day', () => {
|
||||
const { planner, searchParams } = setup('booking=7&map=1', true);
|
||||
expect(planner.showBookingOnMap).not.toHaveBeenCalled();
|
||||
expect(planner.handleSelectDay).toHaveBeenCalledWith(31);
|
||||
expect(searchParams.has('map')).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -1,9 +1,27 @@
|
||||
import { useBookingLink } from '../../../hooks/useBookingLink';
|
||||
import type { DeepLinkProps } from './deepLinkTypes';
|
||||
|
||||
/**
|
||||
* Where the bookings open a booking the chat points at. Nothing yet: the booking cards
|
||||
* fill it in, and until then the planner keeps its own links.
|
||||
* A booking the chat points at, on the desktop planner: its detail opens, the
|
||||
* same view a click on it in the plan or in its tab opens; with `&map=1` its
|
||||
* route is drawn on the plan instead. A route already shown stays shown, and
|
||||
* the plan goes to the day it departs.
|
||||
*/
|
||||
export function useBookingDeepLink(_links: DeepLinkProps): void {
|
||||
// Filled in by the booking cards.
|
||||
export function useBookingDeepLink({ planner, searchParams, setSearchParams }: DeepLinkProps): void {
|
||||
const { tripId, reservations, isLoading, trip, openBookingDetail, showBookingOnMap, isBookingOnMap } = planner;
|
||||
useBookingLink({
|
||||
searchParams,
|
||||
setSearchParams,
|
||||
tripId,
|
||||
reservations,
|
||||
loaded: !isLoading && trip?.id === tripId,
|
||||
take: (booking, onMap) => {
|
||||
if (!onMap) openBookingDetail(booking);
|
||||
else if (!isBookingOnMap(booking)) showBookingOnMap(booking);
|
||||
else {
|
||||
const dayId = booking.day_id ?? booking.end_day_id ?? null;
|
||||
if (dayId !== null) planner.handleSelectDay(dayId);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user