// FE-COMP-PLACESLIBVIEW-001 to FE-COMP-PLACESLIBVIEW-020 import userEvent from '@testing-library/user-event'; import React from 'react'; import { libraryDetail, libraryEntry, libraryInfo, libraryState, type PlacesLibraryState, threeEntries, tripDetail, tripOrigin, } from '../../../tests/helpers/placesLibrary'; import { act, fireEvent, render, screen, within } from '../../../tests/helpers/render'; import { resetAllStores } from '../../../tests/helpers/store'; import { useTranslation } from '../../i18n/TranslationContext'; import { useCollectionStore } from '../../store/collectionStore'; import { useSaveToCollectionStore } from '../../store/saveToCollectionStore'; import type CollectionMapPanel from './CollectionMapPanel'; import type CopyToTripModal from './CopyToTripModal'; import type LibraryPlaceDetail from './LibraryPlaceDetail'; import PlacesLibraryView from './PlacesLibraryView'; type MapProps = React.ComponentProps; type DetailProps = React.ComponentProps; type CopyProps = React.ComponentProps; const seen = vi.hoisted(() => ({ lib: null as PlacesLibraryState | null, map: null as MapProps | null, detail: null as DetailProps | null, copy: null as CopyProps | null, rect: { left: 300, width: 600 } as { left: number; width: number } | null, })); vi.mock('./usePlacesLibrary', () => ({ usePlacesLibrary: () => seen.lib, })); // The review of possible duplicates has its own tests; here it has none to offer. vi.mock('./usePlaceDuplicates', () => ({ usePlaceDuplicates: () => ({ pairs: [], hasMore: false, loading: false, busy: null, lastMerge: null, merge: vi.fn(), dismiss: vi.fn(), undo: vi.fn(), reload: vi.fn(), }), })); // The map stack does not render in jsdom; the panel's contract is what the view hands it. vi.mock('./CollectionMapPanel', () => ({ default: (props: MapProps) => { seen.map = props; return
{`markers:${props.places.length}`}
; }, })); vi.mock('./LibraryPlaceDetail', () => ({ default: (props: DetailProps) => { seen.detail = props; return
; }, })); vi.mock('./CopyToTripModal', () => ({ default: (props: CopyProps) => { seen.copy = props; return
{`copy:${props.placeIds.join(',')}`}
; }, })); vi.mock('../shared/PlaceAvatar', () => ({ default: () => })); vi.mock('../../hooks/useElementRect', () => ({ useElementRect: () => ({ ref: () => undefined, rect: seen.rect }), })); function captured(value: T | null): T { if (value == null) throw new Error('not rendered'); return value; } type ViewProps = Omit, 't'>; function Harness(props: ViewProps): React.ReactElement { const { t } = useTranslation(); return ; } const openSave = vi.fn<(target: Parameters['open']>[0]) => void>(); const copyToTrip = vi.fn['copyToTrip']>(); function setup(lib: Partial = {}, over: Partial = {}) { seen.lib = libraryState(lib); const props: ViewProps = { categories: [], dark: false, isWide: false, language: 'en', view: 'list', onView: vi.fn(), mapLocked: false, onToggleMapLocked: vi.fn(), navigate: vi.fn(), ...over, }; const view = render(); return { ...view, props, lib: captured(seen.lib) }; } const rows = () => Array.from(document.querySelectorAll('.col-lrow')); beforeEach(() => { resetAllStores(); seen.lib = null; seen.map = null; seen.detail = null; seen.copy = null; seen.rect = { left: 300, width: 600 }; openSave.mockReset(); copyToTrip.mockReset(); copyToTrip.mockResolvedValue({ copied: 1, skipped: [] }); useSaveToCollectionStore.setState({ open: openSave }); useCollectionStore.setState({ copyToTrip }); }); describe('PlacesLibraryView: the rows', () => { it('FE-COMP-PLACESLIBVIEW-001: one row per place with its name, address and where it comes from', () => { setup({ entries: threeEntries() }); expect(rows()).toHaveLength(3); const [first, second] = rows(); expect(within(first).getByText('Pantheon')).toBeInTheDocument(); expect(within(first).getByText('Piazza della Rotonda')).toBeInTheDocument(); expect(within(first).getByText('Rome 2026')).toBeInTheDocument(); expect(within(second).getByText('Favourites')).toBeInTheDocument(); expect(screen.getAllByTestId('avatar')).toHaveLength(3); }); it('FE-COMP-PLACESLIBVIEW-002: a click, Enter or Space opens a place, other keys do not', () => { const { lib } = setup({ entries: threeEntries() }); const [first, second, third] = rows(); fireEvent.click(first); fireEvent.keyDown(second, { key: 'Enter' }); fireEvent.keyDown(third, { key: ' ' }); fireEvent.keyDown(third, { key: 'a' }); expect(lib.open).toHaveBeenNthCalledWith(1, 'trip:1'); expect(lib.open).toHaveBeenNthCalledWith(2, 'trip:2'); expect(lib.open).toHaveBeenNthCalledWith(3, 'list:30'); expect(lib.open).toHaveBeenCalledTimes(3); }); it('FE-COMP-PLACESLIBVIEW-003: the open place is marked and its sheet shows', () => { setup({ entries: threeEntries(), selectedKey: 'trip:2' }); expect(rows().map((r) => r.classList.contains('sel'))).toEqual([false, true, false]); expect(screen.getByTestId('place-detail')).toBeInTheDocument(); }); it('FE-COMP-PLACESLIBVIEW-004: "more" counts what is left, loads it and waits while it loads', async () => { const user = userEvent.setup(); const { lib, unmount } = setup({ entries: threeEntries(), total: 8, hasMore: true }); await user.click(screen.getByRole('button', { name: 'Show 5 more' })); expect(lib.loadMore).toHaveBeenCalledTimes(1); unmount(); setup({ entries: threeEntries(), total: 8, hasMore: true, loadingMore: true }); expect(screen.getByRole('button', { name: 'Show 5 more' })).toBeDisabled(); }); it('FE-COMP-PLACESLIBVIEW-005: no "more" once everything is there, and no sheet without a place open', () => { setup({ entries: threeEntries(), total: 3 }); expect(screen.queryByRole('button', { name: /more/ })).not.toBeInTheDocument(); expect(screen.queryByTestId('place-detail')).not.toBeInTheDocument(); }); }); describe('PlacesLibraryView: states', () => { it('FE-COMP-PLACESLIBVIEW-006: offline it says it needs a connection', () => { setup({ offline: true, entries: threeEntries() }); expect(screen.getByText('All places needs a connection.')).toBeInTheDocument(); expect(rows()).toHaveLength(0); }); it('FE-COMP-PLACESLIBVIEW-007: a failure shows as an alert', () => { setup({ error: 'Library is down' }); expect(screen.getByRole('alert')).toHaveTextContent('Library is down'); }); it('FE-COMP-PLACESLIBVIEW-008: the first load shows a spinner, a later one keeps the rows', () => { const { container, unmount } = setup({ loading: true }); expect(container.querySelector('.col-loading')).not.toBeNull(); unmount(); const again = setup({ loading: true, entries: threeEntries() }); expect(again.container.querySelector('.col-loading')).toBeNull(); expect(rows()).toHaveLength(3); }); it('FE-COMP-PLACESLIBVIEW-009: with no places at all it says so and offers nothing to clear', () => { setup(); expect(screen.getByText('No places yet')).toBeInTheDocument(); expect(screen.queryByRole('button', { name: 'Clear filters' })).not.toBeInTheDocument(); }); it('FE-COMP-PLACESLIBVIEW-010: a filter that matches nothing offers to clear the filters', async () => { const user = userEvent.setup(); const { lib } = setup({ filtered: true }); expect(screen.getByText('No matches')).toBeInTheDocument(); await user.click(screen.getByRole('button', { name: 'Clear filters' })); expect(lib.resetFilters).toHaveBeenCalledTimes(1); }); }); describe('PlacesLibraryView: search, filters and the map', () => { it('FE-COMP-PLACESLIBVIEW-011: on a narrow screen the search sits above the places and there is no map', () => { const { lib } = setup({ entries: threeEntries(), search: 'pan' }); const search = screen.getByRole('textbox', { name: 'Search all places' }); expect(search).toHaveValue('pan'); fireEvent.change(search, { target: { value: 'pant' } }); expect(lib.setSearch).toHaveBeenCalledWith('pant'); expect(screen.queryByTestId('map-panel')).not.toBeInTheDocument(); }); it('FE-COMP-PLACESLIBVIEW-012: the filter row reaches the hook', async () => { const user = userEvent.setup(); const { lib, container } = setup({ entries: threeEntries(), counts: { all: 3, trip: 2, list: 2 } }); const kindTrigger = container.querySelector('.col-filter-btn'); await user.click(captured(kindTrigger)); await user.click(screen.getByRole('option', { name: /Trips/ })); expect(lib.setKind).toHaveBeenCalledWith('trip'); }); it('FE-COMP-PLACESLIBVIEW-013: a wide screen splits into the list and a map of the places with a position', () => { const { container } = setup({ entries: threeEntries(), selectedKey: 'trip:2' }, { isWide: true }); expect(container.querySelector('.col-split')).not.toBeNull(); expect(screen.getByTestId('map-panel')).toHaveTextContent('markers:2'); expect(screen.queryByRole('textbox', { name: 'Search all places' })).not.toBeInTheDocument(); const map = captured(seen.map); expect(map.places.map((p) => p.name)).toEqual(['Pantheon', 'Trevi Fountain']); expect(map.selectedPlaceId).toBe(2); expect(map.overlay).toBe(true); }); it('FE-COMP-PLACESLIBVIEW-014: a pin opens its place, the map closes it, the map search is the same search', () => { const { lib } = setup({ entries: threeEntries() }, { isWide: true }); const map = captured(seen.map); expect(map.selectedPlaceId).toBeNull(); act(() => map.onSelect(2)); expect(lib.show).toHaveBeenCalledWith('trip:2'); act(() => map.onSelect(99)); expect(lib.show).toHaveBeenCalledTimes(1); act(() => map.onDeselect()); expect(lib.close).toHaveBeenCalledTimes(1); act(() => map.onSearch('trevi')); expect(lib.setSearch).toHaveBeenCalledWith('trevi'); }); it('FE-COMP-PLACESLIBVIEW-015: going to the map alone closes the place, coming back does not', () => { const { lib, props, unmount } = setup({ entries: threeEntries() }, { isWide: true, view: 'list' }); act(() => captured(seen.map).onToggleView()); expect(lib.close).toHaveBeenCalledTimes(1); expect(props.onView).toHaveBeenCalledWith('map'); unmount(); const back = setup({ entries: threeEntries() }, { isWide: true, view: 'map' }); expect(back.container.querySelector('.col-split.map-full')).not.toBeNull(); act(() => captured(seen.map).onToggleView()); expect(back.lib.close).not.toHaveBeenCalled(); expect(back.props.onView).toHaveBeenCalledWith('list'); }); it('FE-COMP-PLACESLIBVIEW-016: a wide screen without any position keeps the plain list', () => { const unplaced = threeEntries().map((e) => ({ ...e, lat: null, lng: null })); const { container } = setup({ entries: unplaced }, { isWide: true }); expect(container.querySelector('.col-split')).toBeNull(); expect(screen.getByRole('textbox', { name: 'Search all places' })).toBeInTheDocument(); }); }); describe('PlacesLibraryView: the place sheet', () => { it('FE-COMP-PLACESLIBVIEW-017: the sheet docks over the list, over the map alone, or nowhere without the split', () => { const split = setup({ entries: threeEntries(), selectedKey: 'trip:1' }, { isWide: true }); expect(captured(seen.detail).anchorRect).toEqual({ left: 300, width: 600 }); split.unmount(); const mapAlone = setup({ entries: threeEntries(), selectedKey: 'trip:1' }, { isWide: true, view: 'map' }); expect(captured(seen.detail).anchorRect).toEqual({ left: 312, width: 420 }); mapAlone.unmount(); seen.rect = { left: 0, width: 300 }; const narrowMap = setup({ entries: threeEntries(), selectedKey: 'trip:1' }, { isWide: true, view: 'map' }); expect(captured(seen.detail).anchorRect).toEqual({ left: 12, width: 276 }); narrowMap.unmount(); seen.rect = null; const unmeasured = setup({ entries: threeEntries(), selectedKey: 'trip:1' }, { isWide: true, view: 'map' }); expect(captured(seen.detail).anchorRect).toBeNull(); unmeasured.unmount(); setup({ entries: threeEntries(), selectedKey: 'trip:1' }); expect(captured(seen.detail).anchorRect).toBeNull(); }); it('FE-COMP-PLACESLIBVIEW-018: the sheet gets what the hook loaded and opens a list or a trip by its route', () => { const detail = libraryDetail(libraryEntry(), [tripDetail()]); const { lib, props } = setup({ entries: threeEntries(), selectedKey: 'trip:42', detail, detailLoading: true, cover: '/uploads/c.jpg', }); const sheet = captured(seen.detail); expect(sheet.data).toBe(detail); expect(sheet.loading).toBe(true); expect(sheet.cover).toBe('/uploads/c.jpg'); expect(sheet.language).toBe('en'); act(() => sheet.onOpenList(3, 30)); expect(props.navigate).toHaveBeenLastCalledWith('/places/3?place=30'); act(() => sheet.onOpenTrip(5, 42)); expect(props.navigate).toHaveBeenLastCalledWith('/trips/5?place=42'); act(() => sheet.onClose()); expect(lib.close).toHaveBeenCalledTimes(1); }); it('FE-COMP-PLACESLIBVIEW-019: keeping a trip place in a list hands over what the trip knows of it', () => { const entry = libraryEntry({ category: { id: 3, name: 'Restaurant', color: null, icon: null } }); setup({ entries: [entry], selectedKey: 'trip:42', detail: libraryDetail(entry, [tripDetail()]) }); const info = libraryInfo({ name: 'Colosseo', description: 'Arena', website: 'https://colosseo.example', phone: '+39 06', image_url: '/uploads/c.jpg', }); act(() => captured(seen.detail).onSaveToList(tripOrigin(), tripDetail({ info }))); expect(openSave).toHaveBeenCalledWith({ name: 'Colosseo', source_trip_id: 5, source_place_id: 42, description: 'Arena', lat: 41.89, lng: 12.49, address: 'Piazza del Colosseo', category_id: 3, image_url: '/uploads/c.jpg', website: 'https://colosseo.example', phone: '+39 06', }); }); it('FE-COMP-PLACESLIBVIEW-020: copying a saved place opens the trip picker, which copies it and closes', async () => { setup({ entries: threeEntries(), selectedKey: 'list:30' }); expect(screen.queryByTestId('copy-modal')).not.toBeInTheDocument(); act(() => captured(seen.detail).onCopyToTrip(900)); expect(screen.getByTestId('copy-modal')).toHaveTextContent('copy:900'); const modal = captured(seen.copy); expect(modal.isOpen).toBe(true); const carry = { include_notes: true } as Parameters[1]; await act(async () => { await modal.onCopy(7, carry); }); expect(copyToTrip).toHaveBeenCalledWith(7, [900], undefined, carry); act(() => modal.onClose()); expect(screen.queryByTestId('copy-modal')).not.toBeInTheDocument(); }); });