Files
TREK/client/tests/unit/mobile/collections/MLibraryPlaceSheet.test.tsx
T
Maurice 01d327b502 feat(places): Places replaces Collections, with All places, place sheets and a calmer list
- Collections is Places now: /places opens "All places", every place of the
  trips and lists with where it comes from, the same filter row (where from,
  status, category, order), map bar (search, lock, list toggle) and place sheet
  as a list; /places/saved and /places/:id are the lists, and the old
  /collections links still land there. On the phone the same, with its own
  sheet. A place sheet shows each trip (planned day or trip dates, stay,
  booking, notes) and each list (status, labels, rating, notes) as one row
  that opens it.
- The rail: a calm "New list" with its plus tile, All places and All saved as
  views, a "My lists" heading, every name on one edge. Lists can carry one of
  24 symbols, shown in their colour in the rail and the editor.
- A saved place's row keeps to two lines: its category as an icon before the
  name, its average vote as a star on the picture. Map pins wear the same star.
- The place forms put the category in front of the name as a tile that opens
  the category list, instead of a row of chips.
- All saved gets a subtitle; the import dialog opens centred.
- Fixes: a pasted coordinate pair splits from either field of the place form;
  "more" in All places frees itself when a filter changes mid-load; place
  names on the trip map and the rating badge follow the reading direction.
2026-11-29 15:49:35 +01:00

244 lines
10 KiB
TypeScript

// FE-MOB-LIBPLACESHEET-001 to FE-MOB-LIBPLACESHEET-012
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[] }));
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(),
...over,
};
const view = render(<Harness {...props} />);
return { ...view, props };
}
beforeEach(() => {
resetAllStores();
nav.targets = [];
vi.mocked(openNavigationTarget).mockClear();
});
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, a booking and its notes', 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();
expect(within(card).getByText('Book the underground tour')).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, votes and notes', 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).getByText('Go at sunset')).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();
});
});