Files
TREK/client/src/components/Collections/usePlaceActivities.test.ts
T

229 lines
9.7 KiB
TypeScript

// FE-COMP-PLACEACTIVITIES-001 to FE-COMP-PLACEACTIVITIES-013: the tours and bookings of a
// place of All places (PLC-18), behind the desktop section and the phone section.
import { act, renderHook, waitFor } from '@testing-library/react';
import type { PlaceLibraryActivitiesResponse, PlacesLibraryEntry } from '@trek/shared';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { activityCandidate, listSource, listTour, placeActivity, tripSource } from '../../../tests/helpers/placeExtras';
import { libraryEntry, listOrigin } from '../../../tests/helpers/placesLibrary';
import { placeActivitiesRepo } from '../../repo/placeActivitiesRepo';
import type { TranslationFn } from '../../types';
import { activityHref, usePlaceActivities } from './usePlaceActivities';
const toastError = vi.hoisted(() => vi.fn());
vi.mock('../shared/Toast', () => {
const toast = { error: toastError, success: vi.fn(), warning: vi.fn(), info: vi.fn() };
return { useToast: () => toast };
});
vi.mock('../../repo/placeActivitiesRepo', () => ({
placeActivitiesRepo: {
libraryEntry: vi.fn(),
tripCandidates: vi.fn(),
savedCandidates: vi.fn(),
linkTrip: vi.fn(),
linkSaved: vi.fn(),
unlink: vi.fn(),
},
}));
const repo = vi.mocked(placeActivitiesRepo);
const t: TranslationFn = (k) => k;
const EMPTY = { activities: [], can_link: true };
function mount(entry: PlacesLibraryEntry | null = libraryEntry()) {
return renderHook(({ e }: { e: PlacesLibraryEntry | null }) => usePlaceActivities({ entry: e, t }), {
initialProps: { e: entry },
});
}
async function mountLoaded(res: PlaceLibraryActivitiesResponse, entry = libraryEntry()) {
repo.libraryEntry.mockResolvedValue(res);
const view = mount(entry);
await waitFor(() => expect(repo.libraryEntry).toHaveBeenCalled());
await waitFor(() => expect(view.result.current.activities).toEqual(res.activities));
return view;
}
beforeEach(() => {
vi.clearAllMocks();
});
describe('activityHref', () => {
it('FE-COMP-PLACEACTIVITIES-001: a booking and a tour open in their trip, a kept tour in its list', () => {
expect(activityHref(placeActivity())).toBe('/trips/5?booking=7');
expect(activityHref({ target: { kind: 'tour', trip_id: 5, tour_id: 8 } })).toBe('/trips/5?tour=8');
expect(activityHref(listTour())).toMatch(/\/9$/);
});
});
describe('usePlaceActivities', () => {
it('FE-COMP-PLACEACTIVITIES-002: reads the entry through its first origin and says whether it may link', async () => {
const view = await mountLoaded({ activities: [placeActivity()], sources: [tripSource()] });
expect(repo.libraryEntry).toHaveBeenCalledWith('trip', 42, expect.any(AbortSignal));
await waitFor(() => expect(view.result.current.canLink).toBe(true));
expect(view.result.current.picking).toBeNull();
expect(view.result.current.busy).toBe(false);
});
it('FE-COMP-PLACEACTIVITIES-003: a list origin reads through its saved place; no origin reads nothing', async () => {
await mountLoaded({ activities: [], sources: [] }, libraryEntry({ origins: [listOrigin()] }));
expect(repo.libraryEntry).toHaveBeenCalledWith('list', 900, expect.any(AbortSignal));
repo.libraryEntry.mockClear();
const empty = mount(libraryEntry({ origins: [] }));
expect(repo.libraryEntry).not.toHaveBeenCalled();
expect(empty.result.current.activities).toEqual([]);
expect(empty.result.current.canLink).toBe(false);
mount(null);
expect(repo.libraryEntry).not.toHaveBeenCalled();
});
it('FE-COMP-PLACEACTIVITIES-004: a failed read shows nothing and offers no link', async () => {
repo.libraryEntry.mockRejectedValue(new Error('offline'));
const view = mount();
await waitFor(() => expect(repo.libraryEntry).toHaveBeenCalled());
expect(view.result.current.activities).toEqual([]);
expect(view.result.current.canLink).toBe(false);
});
it('FE-COMP-PLACEACTIVITIES-005: an origin without the right to link does not count', async () => {
const view = await mountLoaded({ activities: [placeActivity()], sources: [tripSource({ can_link: false })] });
expect(view.result.current.canLink).toBe(false);
});
it('FE-COMP-PLACEACTIVITIES-006: another entry hides the old answer until its own arrives', async () => {
const view = await mountLoaded({ activities: [placeActivity()], sources: [] });
repo.libraryEntry.mockReturnValue(new Promise(() => undefined));
view.rerender({ e: libraryEntry({ origins: [listOrigin()] }) });
expect(view.result.current.activities).toEqual([]);
});
it('FE-COMP-PLACEACTIVITIES-007: the picker reads the candidates of every origin that takes links', async () => {
const view = await mountLoaded({
activities: [],
sources: [
tripSource(),
listSource(),
tripSource({ source: { kind: 'trip', trip_id: 6, place_id: 43 }, can_link: false }),
],
});
const booking = activityCandidate();
const tour = activityCandidate({ key: 'collection_tour:3' });
repo.tripCandidates.mockResolvedValue({ candidates: [booking] });
repo.savedCandidates.mockResolvedValue({ candidates: [tour] });
await act(async () => {
await view.result.current.openPicker();
});
expect(repo.tripCandidates).toHaveBeenCalledTimes(1);
expect(repo.tripCandidates).toHaveBeenCalledWith(5, 42);
expect(repo.savedCandidates).toHaveBeenCalledWith(900);
expect(view.result.current.picking).toEqual([
{ source: tripSource(), candidates: [booking] },
{ source: listSource(), candidates: [tour] },
]);
// Opening reads the entry again.
await waitFor(() => expect(repo.libraryEntry).toHaveBeenCalledTimes(2));
act(() => view.result.current.closePicker());
expect(view.result.current.picking).toBeNull();
});
it('FE-COMP-PLACEACTIVITIES-008: a failed picker read toasts the server message and opens nothing', async () => {
const view = await mountLoaded({ activities: [], sources: [tripSource()] });
repo.tripCandidates.mockRejectedValue({ response: { data: { error: 'No access' } } });
await act(async () => {
await view.result.current.openPicker();
});
expect(toastError).toHaveBeenCalledWith('No access');
expect(view.result.current.picking).toBeNull();
expect(view.result.current.busy).toBe(false);
});
it('FE-COMP-PLACEACTIVITIES-009: linking a booking or a tour to a trip place sends its own id and closes the picker', async () => {
const view = await mountLoaded({ activities: [], sources: [tripSource()] });
repo.tripCandidates.mockResolvedValue({ candidates: [activityCandidate()] });
repo.linkTrip.mockResolvedValue(EMPTY);
await act(async () => {
await view.result.current.openPicker();
});
await act(async () => {
await view.result.current.link(tripSource(), activityCandidate());
});
expect(repo.linkTrip).toHaveBeenLastCalledWith(5, 42, { reservation_id: 7 });
expect(view.result.current.picking).toBeNull();
await act(async () => {
await view.result.current.link(
tripSource(),
activityCandidate({ type: 'tour', target: { kind: 'tour', trip_id: 5, tour_id: 8 } })
);
});
expect(repo.linkTrip).toHaveBeenLastCalledWith(5, 42, { tour_id: 8 });
});
it('FE-COMP-PLACEACTIVITIES-010: a kept tour links to the saved place, never to a trip place', async () => {
const view = await mountLoaded({ activities: [], sources: [tripSource(), listSource()] });
repo.linkSaved.mockResolvedValue(EMPTY);
const keptTour = activityCandidate({
target: { kind: 'collection_tour', collection_id: 9, collection_tour_id: 3 },
});
await act(async () => {
await view.result.current.link(listSource(), keptTour);
});
expect(repo.linkSaved).toHaveBeenCalledWith(900, 3);
await act(async () => {
await view.result.current.link(tripSource(), keptTour);
});
// A booking never reaches a saved place either.
await act(async () => {
await view.result.current.link(listSource(), activityCandidate());
});
expect(repo.linkTrip).not.toHaveBeenCalled();
expect(repo.linkSaved).toHaveBeenCalledTimes(1);
});
it('FE-COMP-PLACEACTIVITIES-011: unlinking takes the link off and reads the entry again', async () => {
const act1 = placeActivity();
const view = await mountLoaded({ activities: [act1], sources: [tripSource()] });
repo.unlink.mockResolvedValue(EMPTY);
await act(async () => {
await view.result.current.unlink(act1);
});
expect(repo.unlink).toHaveBeenCalledWith(act1);
await waitFor(() => expect(repo.libraryEntry).toHaveBeenCalledTimes(2));
});
it('FE-COMP-PLACEACTIVITIES-012: a refused write without a message toasts the general error', async () => {
const view = await mountLoaded({ activities: [placeActivity()], sources: [] });
repo.unlink.mockRejectedValue(new Error('boom'));
await act(async () => {
await view.result.current.unlink(placeActivity());
});
expect(toastError).toHaveBeenCalledWith('common.error');
expect(repo.libraryEntry).toHaveBeenCalledTimes(1);
});
it('FE-COMP-PLACEACTIVITIES-013: a second write while one runs is ignored', async () => {
const view = await mountLoaded({ activities: [placeActivity()], sources: [] });
let finish: (v: typeof EMPTY) => void = () => undefined;
repo.unlink.mockReturnValue(
new Promise((res) => {
finish = res;
})
);
let first: Promise<void> | undefined;
act(() => {
first = view.result.current.unlink(placeActivity());
});
expect(view.result.current.busy).toBe(true);
await act(async () => {
await view.result.current.unlink(placeActivity());
});
expect(repo.unlink).toHaveBeenCalledTimes(1);
await act(async () => {
finish(EMPTY);
await first;
});
expect(view.result.current.busy).toBe(false);
});
});