mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 00:43:15 +00:00
20e3d2d9ae
Tests for the vote by entry key in the api and the repository, the rating row and its voters, the rating filter and sort on both views, the edit form without a choice of copy, and the place view's sections, aligned with the renamed tag strings.
265 lines
11 KiB
TypeScript
265 lines
11 KiB
TypeScript
// FE-MOB-LIBPLACESHEET-001 to FE-MOB-LIBPLACESHEET-011: one place of All places on the phone,
|
|
// in the one place view a list's place opens too.
|
|
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 MLibraryPlaceSheet from '../../../../src/mobile/screens/collections/MLibraryPlaceSheet';
|
|
import { placesLibraryRepo } from '../../../../src/repo/placesLibraryRepo';
|
|
import {
|
|
bothOrigins,
|
|
libraryDetail,
|
|
libraryEntry,
|
|
libraryInfo,
|
|
listDetail,
|
|
tripDetail,
|
|
tripOrigin,
|
|
} from '../../../helpers/placesLibrary';
|
|
import { fireEvent, render, screen, waitFor, within } from '../../../helpers/render';
|
|
|
|
const stub = vi.hoisted(() => ({
|
|
nav: [] as unknown[],
|
|
sheetArgs: [] as unknown[],
|
|
ratingArgs: [] as unknown[],
|
|
}));
|
|
|
|
// The tabs' contents and the votes have their own tests; here they are what the sheet hands them.
|
|
vi.mock('../../../../src/components/Collections/usePlaceSheet', async (importOriginal) => {
|
|
const { placeSheetState } = await import('../../../helpers/placesLibrary');
|
|
return {
|
|
...(await importOriginal<typeof import('../../../../src/components/Collections/usePlaceSheet')>()),
|
|
usePlaceSheet: (args: { data: PlacesLibraryDetailResponse | null }) => {
|
|
stub.sheetArgs.push(args);
|
|
return placeSheetState({ entry: args.data?.entry ?? null });
|
|
},
|
|
};
|
|
});
|
|
vi.mock('../../../../src/components/Collections/usePlaceRating', () => ({
|
|
usePlaceRating: (args: unknown) => {
|
|
stub.ratingArgs.push(args);
|
|
return { target: null };
|
|
},
|
|
}));
|
|
vi.mock('../../../../src/components/Collections/PlaceRatingRow', () => ({
|
|
default: () => <div data-testid="rating-row" />,
|
|
}));
|
|
vi.mock('../../../../src/mobile/screens/collections/MVisitsSection', () => ({
|
|
MVisitsView: () => <div data-testid="visits" />,
|
|
}));
|
|
vi.mock('../../../../src/mobile/screens/collections/MOriginNotesSection', () => ({ MOriginNotesView: () => null }));
|
|
vi.mock('../../../../src/mobile/screens/collections/MPlaceActivitiesSection', () => ({
|
|
MPlaceActivitiesView: () => null,
|
|
}));
|
|
vi.mock('../../../../src/mobile/screens/collections/MPlaceMediaSection', () => ({ MPlaceMediaView: () => null }));
|
|
vi.mock('../../../../src/mobile/screens/collections/MPlaceHistorySection', () => ({ MPlaceHistoryView: () => null }));
|
|
|
|
const toast = vi.hoisted(() => ({ error: vi.fn(), success: vi.fn(), info: vi.fn(), warning: vi.fn() }));
|
|
vi.mock('../../../../src/components/shared/Toast', async (importOriginal) => ({
|
|
...(await importOriginal<typeof import('../../../../src/components/shared/Toast')>()),
|
|
useToast: () => toast,
|
|
}));
|
|
|
|
vi.mock('../../../../src/components/Planner/placeNavigation', () => ({
|
|
getNavigationTargets: () => stub.nav,
|
|
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 t = (k: string, p?: Record<string, unknown>) => (p ? `${k}:${JSON.stringify(p)}` : k);
|
|
|
|
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 = ComponentProps<typeof MLibraryPlaceSheet>;
|
|
|
|
/** The Colosseum in a trip and in the list "Favourites", which tags it "Must see" and describes it. */
|
|
function fullDetail(): PlacesLibraryDetailResponse {
|
|
const data = bothOrigins({ category: { id: 3, name: 'Restaurant', color: '#E8843D', icon: 'Utensils' } });
|
|
data.details = [
|
|
tripDetail(),
|
|
listDetail({
|
|
info: libraryInfo({ description: 'Ancient arena' }),
|
|
labels: [{ id: 1, name: 'Must see', color: null }],
|
|
available_tags: [{ id: 1, name: 'Must see', color: null }],
|
|
}),
|
|
];
|
|
return data;
|
|
}
|
|
|
|
function setup(over: Partial<Props> = {}) {
|
|
const props: Props = {
|
|
open: true,
|
|
data: fullDetail(),
|
|
loading: false,
|
|
cover: null,
|
|
language: 'en',
|
|
categories: [],
|
|
onClose: vi.fn(),
|
|
onOpenList: vi.fn(),
|
|
onOpenTrip: vi.fn(),
|
|
onSaveToList: vi.fn(),
|
|
onCopyToTrip: vi.fn(),
|
|
onNotesSaved: vi.fn(),
|
|
t,
|
|
...over,
|
|
};
|
|
const view = render(<MLibraryPlaceSheet {...props} />);
|
|
return { ...view, props };
|
|
}
|
|
|
|
beforeEach(() => {
|
|
vi.restoreAllMocks();
|
|
stub.nav = [];
|
|
stub.sheetArgs = [];
|
|
stub.ratingArgs = [];
|
|
vi.mocked(openNavigationTarget).mockClear();
|
|
for (const fn of Object.values(toast)) fn.mockReset();
|
|
});
|
|
|
|
describe('MLibraryPlaceSheet: the place', () => {
|
|
it('FE-MOB-LIBPLACESHEET-001: closed it renders nothing', () => {
|
|
setup({ open: false });
|
|
expect(screen.queryByRole('dialog')).toBeNull();
|
|
});
|
|
|
|
it('FE-MOB-LIBPLACESHEET-002: while it loads it shows a spinner and nothing to do yet', () => {
|
|
setup({ data: null, loading: true });
|
|
expect(document.querySelector('.animate-spin')).not.toBeNull();
|
|
expect(screen.queryByRole('tablist')).toBeNull();
|
|
expect(screen.queryByRole('button', { name: 'common.edit' })).toBeNull();
|
|
expect(screen.queryByTestId('rating-row')).toBeNull();
|
|
});
|
|
|
|
it('FE-MOB-LIBPLACESHEET-003: the cover names the place, its category and where it is; the votes, the description and the tabs follow', () => {
|
|
setup({ cover: '/uploads/cover.jpg' });
|
|
expect(screen.getByRole('dialog', { name: 'Colosseum' })).toBeInTheDocument();
|
|
expect(screen.getByRole('heading', { name: 'Colosseum' })).toBeInTheDocument();
|
|
expect(screen.getByText('Restaurant')).toBeInTheDocument();
|
|
expect(screen.getByText('Piazza del Colosseo')).toBeInTheDocument();
|
|
expect(screen.getByText('Ancient arena')).toBeInTheDocument();
|
|
expect(screen.getByTestId('rating-row')).toBeInTheDocument();
|
|
expect(screen.getAllByRole('tab')).toHaveLength(5);
|
|
expect(document.querySelector('[role="dialog"] img')).toHaveAttribute('src', '/uploads/cover.jpg');
|
|
});
|
|
|
|
it('FE-MOB-LIBPLACESHEET-004: the tabs, the tags and the votes read the open place, and a save there reads it again', () => {
|
|
const { props } = setup();
|
|
expect(stub.sheetArgs[stub.sheetArgs.length - 1]).toEqual({ data: props.data, onChanged: props.onNotesSaved, t });
|
|
expect(stub.ratingArgs[stub.ratingArgs.length - 1]).toEqual({
|
|
data: props.data,
|
|
onChanged: props.onNotesSaved,
|
|
t,
|
|
});
|
|
expect(screen.getByRole('region', { name: 'placeTags.title' })).toHaveTextContent('Must see');
|
|
expect(screen.getByTestId('visits')).toBeInTheDocument();
|
|
});
|
|
|
|
it('FE-MOB-LIBPLACESHEET-005: an origin card opens the place there, close closes the sheet', () => {
|
|
const { props } = setup();
|
|
fireEvent.click(screen.getByRole('button', { name: 'collections.library.openTrip: Rome 2026' }));
|
|
expect(props.onOpenTrip).toHaveBeenCalledWith(5, 42);
|
|
fireEvent.click(screen.getByRole('button', { name: 'collections.library.openList: Favourites' }));
|
|
expect(props.onOpenList).toHaveBeenCalledWith(9, 900);
|
|
fireEvent.click(screen.getByRole('button', { name: 'common.close' }));
|
|
expect(props.onClose).toHaveBeenCalledTimes(1);
|
|
});
|
|
});
|
|
|
|
describe('MLibraryPlaceSheet: what to do with it', () => {
|
|
it('FE-MOB-LIBPLACESHEET-006: a saved place is copied to a trip; a place only in a trip is kept in a list', () => {
|
|
const { props, unmount } = setup();
|
|
expect(screen.queryByRole('button', { name: /inspector\.saveToCollection/ })).toBeNull();
|
|
fireEvent.click(screen.getByRole('button', { name: /collections\.copyToTrip/ }));
|
|
expect(props.onCopyToTrip).toHaveBeenCalledWith(900);
|
|
unmount();
|
|
|
|
const tripOnly = libraryDetail(libraryEntry({ origins: [tripOrigin()] }), [tripDetail()]);
|
|
const trip = setup({ data: tripOnly });
|
|
fireEvent.click(screen.getByRole('button', { name: /inspector\.saveToCollection/ }));
|
|
expect(trip.props.onSaveToList).toHaveBeenCalledWith(tripOnly.entry?.origins[0], tripOnly.details[0]);
|
|
});
|
|
|
|
it('FE-MOB-LIBPLACESHEET-007: one map app opens straight away, several open a titled menu, none hides the button', () => {
|
|
stub.nav = [GOOGLE];
|
|
const single = setup();
|
|
fireEvent.click(screen.getByRole('button', { name: 'inspector.navigation' }));
|
|
expect(openNavigationTarget).toHaveBeenCalledWith(GOOGLE);
|
|
single.unmount();
|
|
|
|
stub.nav = [GOOGLE, OSM];
|
|
const several = setup();
|
|
fireEvent.click(screen.getByRole('button', { name: 'inspector.navigation' }));
|
|
const menu = screen.getByRole('menu', { name: 'inspector.navigation' });
|
|
expect(within(menu).getByText('OpenStreetMap')).toBeInTheDocument();
|
|
fireEvent.click(within(menu).getByRole('button', { name: 'menu-close' }));
|
|
expect(screen.queryByRole('menu')).toBeNull();
|
|
several.unmount();
|
|
|
|
stub.nav = [];
|
|
setup();
|
|
expect(screen.queryByRole('button', { name: 'inspector.navigation' })).toBeNull();
|
|
});
|
|
|
|
it('FE-MOB-LIBPLACESHEET-008: editing swaps the tabs for the form and saves the list copy', async () => {
|
|
const save = vi.spyOn(placesLibraryRepo, 'saveDetails').mockResolvedValue(undefined);
|
|
const { props } = setup();
|
|
fireEvent.click(screen.getByRole('button', { name: 'common.edit' }));
|
|
expect(screen.queryByRole('tablist')).toBeNull();
|
|
const name = screen.getByDisplayValue('Colosseum');
|
|
fireEvent.change(name, { target: { value: 'Arena' } });
|
|
fireEvent.click(screen.getByRole('button', { name: /common\.save/ }));
|
|
await waitFor(() => expect(props.onNotesSaved).toHaveBeenCalledTimes(1));
|
|
expect(save).toHaveBeenCalledWith(
|
|
{ kind: 'list', collection_place_id: 900 },
|
|
expect.objectContaining({ name: 'Arena' })
|
|
);
|
|
expect(screen.getByRole('tablist')).toBeInTheDocument();
|
|
});
|
|
|
|
it('FE-MOB-LIBPLACESHEET-009: cancel leaves the form without saving', () => {
|
|
const save = vi.spyOn(placesLibraryRepo, 'saveDetails');
|
|
setup();
|
|
fireEvent.click(screen.getByRole('button', { name: 'common.edit' }));
|
|
fireEvent.click(screen.getByRole('button', { name: 'common.cancel' }));
|
|
expect(screen.getByRole('tablist')).toBeInTheDocument();
|
|
expect(save).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('FE-MOB-LIBPLACESHEET-010: a place only in a read-only trip offers no edit', () => {
|
|
const entry = libraryEntry({ origins: [tripOrigin({ read_only: true })] });
|
|
setup({ data: libraryDetail(entry, [tripDetail()]) });
|
|
expect(screen.queryByRole('button', { name: 'common.edit' })).toBeNull();
|
|
});
|
|
|
|
it('FE-MOB-LIBPLACESHEET-011: a tag taken off in the overview is written to its list and reads the place again', async () => {
|
|
const setTags = vi.spyOn(placesLibraryRepo, 'setOriginTags').mockResolvedValue(undefined);
|
|
const { props } = setup();
|
|
fireEvent.click(screen.getAllByRole('button', { name: 'placeTags.add' })[0]);
|
|
// The picker opens on the list's group: Must see is on, a tap takes it off.
|
|
fireEvent.click(screen.getByRole('button', { name: 'Must see' }));
|
|
await waitFor(() => expect(props.onNotesSaved).toHaveBeenCalledTimes(1));
|
|
expect(setTags).toHaveBeenCalledWith({ kind: 'list', collection_id: 9, collection_place_id: 900 }, []);
|
|
});
|
|
});
|