Files
TREK/client/tests/unit/mobile/collections/MLibraryPlaceSheet.test.tsx
T

297 lines
13 KiB
TypeScript

// FE-MOB-LIBPLACESHEET-001 to FE-MOB-LIBPLACESHEET-015
import userEvent from '@testing-library/user-event';
import type { PlacesLibraryDetailResponse } from '@trek/shared';
import type { ComponentProps } from 'react';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { type NavigationTarget, openNavigationTarget } from '../../../../src/components/Planner/placeNavigation';
import { useTranslation } from '../../../../src/i18n';
import MLibraryPlaceSheet from '../../../../src/mobile/screens/collections/MLibraryPlaceSheet';
import {
bothOrigins,
libraryDetail,
libraryEntry,
libraryInfo,
listDetail,
listOrigin,
tripDetail,
tripOrigin,
} from '../../../helpers/placesLibrary';
import { render, screen, within } from '../../../helpers/render';
import { resetAllStores } from '../../../helpers/store';
const nav = vi.hoisted(() => ({ targets: [] as NavigationTarget[] }));
/** The props each section below the origins was last handed; they read their own stores. */
const sections = vi.hoisted(() => ({}) as Record<string, Record<string, unknown>>);
const section = vi.hoisted(
() => (name: string) =>
function Section(props: Record<string, unknown>) {
sections[name] = props;
return <div data-testid={`section-${name}`} />;
}
);
vi.mock('../../../../src/mobile/screens/collections/MEntryTagsSection', () => ({ default: section('tags') }));
vi.mock('../../../../src/mobile/screens/collections/MOriginNotesSection', () => ({ default: section('notes') }));
vi.mock('../../../../src/mobile/screens/collections/MPlaceMediaSection', () => ({ default: section('media') }));
vi.mock('../../../../src/mobile/screens/collections/MVisitsSection', () => ({ default: section('visits') }));
vi.mock('../../../../src/mobile/screens/collections/MPlaceActivitiesSection', () => ({
default: section('activities'),
}));
vi.mock('../../../../src/mobile/screens/collections/MPlaceHistorySection', () => ({ default: section('history') }));
vi.mock('../../../../src/components/Planner/placeNavigation', () => ({
getNavigationTargets: () => nav.targets,
navigationTargetLabel: (target: NavigationTarget) => target.label,
openNavigationTarget: vi.fn(),
}));
vi.mock('../../../../src/components/shared/NavigationMenu', () => ({
NavigationMenu: ({
targets,
onClose,
title,
}: {
targets: NavigationTarget[];
onClose: () => void;
title?: string;
}) => (
<div role="menu" aria-label={title}>
{targets.map((target) => (
<span key={target.id}>{target.label}</span>
))}
<button type="button" onClick={onClose}>
menu-close
</button>
</div>
),
}));
const GOOGLE: NavigationTarget = { id: 'google', label: 'Google Maps', url: 'https://maps.example/g' };
const OSM: NavigationTarget = { id: 'osm', label: 'OpenStreetMap', url: 'https://maps.example/o' };
type Props = Omit<ComponentProps<typeof MLibraryPlaceSheet>, 't'>;
function Harness(props: Props) {
const { t } = useTranslation();
return <MLibraryPlaceSheet {...props} t={t} />;
}
/** The Colosseum in a read-only trip, planned and booked there, and in a list with labels and votes. */
function fullDetail(): PlacesLibraryDetailResponse {
const data = bothOrigins({ category: { id: 3, name: 'Restaurant', color: '#E8843D', icon: 'Utensils' } });
data.entry.origins = [tripOrigin({ read_only: true }), listOrigin({ status: 'visited' })];
data.details = [
tripDetail({
info: libraryInfo({ name: 'Colosseo', description: ' ' }),
notes: 'Book the underground tour',
days: [
{ day_id: 1, day_number: 2, date: null, title: null, time: null, end_time: null },
{ day_id: 2, day_number: 4, date: null, title: null, time: null, end_time: null },
],
has_accommodation: true,
has_reservation: true,
}),
listDetail({
info: libraryInfo({ name: 'Il Colosseo', description: 'Ancient arena' }),
labels: [{ id: 1, name: 'Must see', color: null }],
rating: { average: 4, count: 1, mine: 4 },
notes: 'Go at sunset',
}),
];
return data;
}
function setup(over: Partial<Props> = {}) {
const props: Props = {
open: true,
data: fullDetail(),
loading: false,
cover: null,
language: 'en',
onClose: vi.fn(),
onOpenList: vi.fn(),
onOpenTrip: vi.fn(),
onSaveToList: vi.fn(),
onCopyToTrip: vi.fn(),
onNotesSaved: vi.fn(),
...over,
};
const view = render(<Harness {...props} />);
return { ...view, props };
}
beforeEach(() => {
resetAllStores();
nav.targets = [];
vi.mocked(openNavigationTarget).mockClear();
for (const key of Object.keys(sections)) delete sections[key];
});
describe('MLibraryPlaceSheet: the sheet', () => {
it('FE-MOB-LIBPLACESHEET-001: closed it renders nothing', () => {
setup({ open: false });
expect(screen.queryByRole('dialog')).not.toBeInTheDocument();
});
it('FE-MOB-LIBPLACESHEET-002: while it loads it shows a spinner and nothing to do yet', () => {
setup({ data: null, loading: true });
const dialog = screen.getByRole('dialog', { name: 'All places' });
expect(dialog.querySelector('.animate-spin')).not.toBeNull();
expect(within(dialog).getAllByRole('button')).toHaveLength(1);
expect(within(dialog).queryByRole('heading')).not.toBeInTheDocument();
});
it('FE-MOB-LIBPLACESHEET-003: the head carries the cover, the category and the name, the body the address and description', () => {
setup({ cover: '/uploads/cover.jpg' });
const dialog = screen.getByRole('dialog', { name: 'Colosseum' });
expect(within(dialog).getByRole('heading', { name: 'Colosseum' })).toBeInTheDocument();
expect(within(dialog).getByText('Restaurant')).toBeInTheDocument();
expect(within(dialog).getByText('Piazza del Colosseo')).toBeInTheDocument();
expect(within(dialog).getByText('Ancient arena')).toBeInTheDocument();
expect(dialog.querySelector('img')).toHaveAttribute('src', '/uploads/cover.jpg');
expect(within(dialog).queryByText('No coordinates, so not on the map')).not.toBeInTheDocument();
});
it('FE-MOB-LIBPLACESHEET-004: without a cover, an address or coordinates it says why it is not on the map', () => {
const entry = libraryEntry({ lat: 41.89, lng: null, address: null });
setup({ data: libraryDetail(entry, [tripDetail()]) });
const dialog = screen.getByRole('dialog', { name: 'Colosseum' });
expect(dialog.querySelector('img')).toBeNull();
expect(within(dialog).getByText('No coordinates, so not on the map')).toBeInTheDocument();
expect(within(dialog).queryByText('Piazza del Colosseo')).not.toBeInTheDocument();
});
it('FE-MOB-LIBPLACESHEET-005: the close button closes it', async () => {
const user = userEvent.setup();
const { props } = setup();
await user.click(screen.getByRole('button', { name: 'Close' }));
expect(props.onClose).toHaveBeenCalled();
});
});
describe('MLibraryPlaceSheet: where the place sits', () => {
it('FE-MOB-LIBPLACESHEET-006: a trip shows its days, its name there, a stay and a booking, its notes go below', async () => {
const user = userEvent.setup();
const { props } = setup();
expect(screen.getByText('IN YOUR TRIPS')).toBeInTheDocument();
const card = screen.getByRole('button', { name: 'Open in the trip: Rome 2026' });
expect(within(card).getByText('Day 2 +1')).toBeInTheDocument();
expect(within(card).getByText('Read only')).toBeInTheDocument();
expect(within(card).getByText('Called Colosseo here')).toBeInTheDocument();
expect(within(card).getByText('Stay')).toBeInTheDocument();
expect(within(card).getByText('Booking')).toBeInTheDocument();
// The notes moved out of the card into the notes section under the origins.
expect(within(card).queryByText('Book the underground tour')).not.toBeInTheDocument();
await user.click(card);
expect(props.onOpenTrip).toHaveBeenCalledWith(5, 42);
});
it('FE-MOB-LIBPLACESHEET-007: a list shows its status, its name there, labels and votes', async () => {
const user = userEvent.setup();
const { props } = setup();
expect(screen.getByText('IN YOUR LISTS')).toBeInTheDocument();
const card = screen.getByRole('button', { name: 'Open in the list: Favourites' });
expect(within(card).getByText('Visited')).toBeInTheDocument();
expect(within(card).getByText('Called Il Colosseo here')).toBeInTheDocument();
expect(within(card).getByText('Must see')).toBeInTheDocument();
expect(within(card).getByText('4.0 from 1 vote')).toBeInTheDocument();
expect(within(card).queryByText('Go at sunset')).not.toBeInTheDocument();
await user.click(card);
expect(props.onOpenList).toHaveBeenCalledWith(9, 900);
});
it('FE-MOB-LIBPLACESHEET-008: a plain trip place shows its trip dates and no chips', () => {
const entry = libraryEntry({ origins: [tripOrigin({ trip_start: '2026-12-01', trip_end: null })] });
setup({ data: libraryDetail(entry, [tripDetail()]) });
const card = screen.getByRole('button', { name: 'Open in the trip: Rome 2026' });
expect(card).toHaveTextContent(/Dec/);
expect(within(card).queryByText('Stay')).not.toBeInTheDocument();
expect(within(card).queryByText('Read only')).not.toBeInTheDocument();
expect(screen.queryByText('IN YOUR LISTS')).not.toBeInTheDocument();
});
it('FE-MOB-LIBPLACESHEET-009: an origin the server sent no detail for is left out', () => {
const data = bothOrigins();
data.details = [tripDetail()];
setup({ data });
expect(screen.queryByRole('button', { name: 'Open in the list: Favourites' })).not.toBeInTheDocument();
expect(screen.queryByRole('button', { name: /Copy to trip/ })).not.toBeInTheDocument();
});
});
describe('MLibraryPlaceSheet: what to do with it', () => {
it('FE-MOB-LIBPLACESHEET-010: a trip place can be kept in a list, a saved one copied to a trip', async () => {
const user = userEvent.setup();
const { props } = setup();
await user.click(screen.getByRole('button', { name: /Save to Collection/ }));
expect(props.onSaveToList).toHaveBeenCalledWith(props.data?.entry.origins[0], props.data?.details[0]);
await user.click(screen.getByRole('button', { name: /Copy to trip/ }));
expect(props.onCopyToTrip).toHaveBeenCalledWith(900);
});
it('FE-MOB-LIBPLACESHEET-011: one map app opens straight away, none hides the button', async () => {
const user = userEvent.setup();
nav.targets = [GOOGLE];
const entry = libraryEntry({ key: 'list:900', origins: [listOrigin()] });
const { unmount } = setup({ data: libraryDetail(entry, [listDetail()]) });
expect(screen.queryByRole('button', { name: /Save to Collection/ })).not.toBeInTheDocument();
await user.click(screen.getByRole('button', { name: /Google Maps/ }));
expect(openNavigationTarget).toHaveBeenCalledWith(GOOGLE);
unmount();
nav.targets = [];
setup();
expect(screen.queryByRole('button', { name: /Navigation|Google Maps/ })).not.toBeInTheDocument();
});
it('FE-MOB-LIBPLACESHEET-012: several map apps open a titled menu that closes again', async () => {
const user = userEvent.setup();
nav.targets = [GOOGLE, OSM];
setup();
const button = screen.getByRole('button', { name: /Navigation/ });
await user.click(button);
const menu = screen.getByRole('menu', { name: 'Navigation' });
expect(within(menu).getByText('OpenStreetMap')).toBeInTheDocument();
await user.click(within(menu).getByRole('button', { name: 'menu-close' }));
expect(screen.queryByRole('menu')).not.toBeInTheDocument();
await user.click(button);
await user.click(button);
expect(screen.queryByRole('menu')).not.toBeInTheDocument();
expect(openNavigationTarget).not.toHaveBeenCalled();
});
});
describe('MLibraryPlaceSheet: the look of its origins and the sections below', () => {
it('FE-MOB-LIBPLACESHEET-013: a trip with a cover shows it, a list wears its own colour', () => {
const entry = libraryEntry({
origins: [tripOrigin({ trip_cover: '/uploads/covers/rome.jpg' }), listOrigin({ collection_color: '#ff0000' })],
});
setup({ data: libraryDetail(entry, [tripDetail(), listDetail()]) });
const trip = screen.getByRole('button', { name: 'Open in the trip: Rome 2026' });
expect(trip.querySelector('img')).toHaveAttribute('src', '/uploads/covers/rome.jpg');
const list = screen.getByRole('button', { name: 'Open in the list: Favourites' });
const tile = list.querySelector<HTMLElement>('[style*="color"]');
expect(tile?.style.color).toBe('rgb(255, 0, 0)');
});
it('FE-MOB-LIBPLACESHEET-014: tags, notes, media, visits, activities and history follow, wired to the place', () => {
const { props } = setup();
for (const name of ['tags', 'notes', 'media', 'visits', 'activities', 'history']) {
expect(screen.getByTestId(`section-${name}`)).toBeInTheDocument();
}
expect(sections.tags).toMatchObject({ data: props.data, onChanged: props.onNotesSaved });
expect(sections.notes).toMatchObject({ data: props.data, onSaved: props.onNotesSaved });
expect(sections.media).toMatchObject({ entry: props.data?.entry });
expect(sections.visits).toMatchObject({ entry: props.data?.entry, language: 'en' });
expect(sections.activities).toMatchObject({ entry: props.data?.entry, language: 'en' });
expect(sections.history).toMatchObject({ entry: props.data?.entry, language: 'en' });
});
it('FE-MOB-LIBPLACESHEET-015: while it loads no section is drawn', () => {
setup({ data: null, loading: true });
expect(screen.queryByTestId('section-notes')).toBeNull();
});
});