// FE-COMP-PLACESHEET-001 to FE-COMP-PLACESHEET-005: everything the one place view shows // beside the place, for the desktop dialog and the phone sheet. import { act, renderHook } from '@testing-library/react'; import type { PlacesLibraryDetailResponse } from '@trek/shared'; import { beforeEach, describe, expect, it, vi } from 'vitest'; import { bothOrigins, libraryDetail, libraryEntry, tripDetail } from '../../../tests/helpers/placesLibrary'; import { PLACE_TAB_EMPTY, PLACE_TAB_ICON, PLACE_TAB_LABEL, PLACE_TABS, usePlaceSheet } from './usePlaceSheet'; /** What each part of the sheet holds; the parts have their own tests. */ const parts = vi.hoisted(() => ({ notes: { notes: [] as unknown[], targets: [] as unknown[] }, activities: { activities: [] as unknown[], canLink: false }, media: { photos: [] as unknown[], files: [] as unknown[], targets: [] as unknown[] }, history: { entries: [] as unknown[] }, visits: { count: 0 }, calls: { notes: [] as unknown[], visits: [] as unknown[], activities: [] as unknown[], history: [] as unknown[], media: [] as unknown[], }, })); vi.mock('./useOriginNotes', () => ({ useOriginNotes: (args: unknown) => { parts.calls.notes.push(args); return parts.notes; }, })); vi.mock('./usePlaceActivities', () => ({ usePlaceActivities: (args: unknown) => { parts.calls.activities.push(args); return parts.activities; }, })); vi.mock('./usePlaceHistory', () => ({ usePlaceHistory: (entry: unknown) => { parts.calls.history.push(entry); return parts.history; }, })); vi.mock('./usePlaceMedia', () => ({ usePlaceMedia: (entry: unknown) => { parts.calls.media.push(entry); return parts.media; }, })); vi.mock('./usePlaceVisits', () => ({ usePlaceVisits: (args: unknown) => { parts.calls.visits.push(args); return parts.visits; }, })); const t = (k: string) => k; /** The arguments a part was last read with. */ const last = (calls: unknown[]): unknown => calls[calls.length - 1]; function setup(data: PlacesLibraryDetailResponse | null, onChanged = vi.fn()) { const hook = renderHook((p: { data: PlacesLibraryDetailResponse | null }) => usePlaceSheet({ ...p, onChanged, t }), { initialProps: { data }, }); return { ...hook, onChanged }; } beforeEach(() => { parts.notes = { notes: [], targets: [] }; parts.activities = { activities: [], canLink: false }; parts.media = { photos: [], files: [], targets: [] }; parts.history = { entries: [] }; for (const key of Object.keys(parts.calls) as (keyof typeof parts.calls)[]) parts.calls[key] = []; }); describe('usePlaceSheet', () => { it('FE-COMP-PLACESHEET-001: the tabs come in one order, each with its word, and its empty text and symbol', () => { expect(PLACE_TABS).toEqual(['overview', 'notes', 'activities', 'media', 'history']); for (const tab of PLACE_TABS) expect(PLACE_TAB_LABEL[tab]).toBe(`placeSheet.tab.${tab}`); // Every tab but the overview can be empty, and says so with its own words and symbol. expect(PLACE_TAB_EMPTY.overview).toBe(''); for (const tab of PLACE_TABS.filter((x) => x !== 'overview')) { expect(PLACE_TAB_EMPTY[tab]).toBe(`placeSheet.empty.${tab}`); } expect(new Set(PLACE_TABS.map((tab) => PLACE_TAB_ICON[tab])).size).toBe(PLACE_TABS.length); }); it('FE-COMP-PLACESHEET-002: every part reads the open place, and a saved note reads the place again', () => { const data = bothOrigins(); const { result, onChanged } = setup(data); expect(result.current.entry).toBe(data.entry); expect(last(parts.calls.visits)).toEqual({ entry: data.entry }); expect(last(parts.calls.activities)).toEqual({ entry: data.entry, t }); expect(last(parts.calls.history)).toBe(data.entry); expect(last(parts.calls.media)).toBe(data.entry); expect(last(parts.calls.notes)).toEqual({ data, onSaved: onChanged, t }); expect(result.current.notes).toBe(parts.notes); expect(result.current.visits).toBe(parts.visits); }); it('FE-COMP-PLACESHEET-003: each tab counts what it holds, the files and pictures together', () => { parts.notes = { notes: [1, 2], targets: [] }; parts.activities = { activities: [1], canLink: false }; parts.media = { photos: [1, 2], files: [1], targets: [] }; parts.history = { entries: [1, 2, 3, 4] }; const { result } = setup(bothOrigins()); expect(result.current.counts).toEqual({ overview: 0, notes: 2, activities: 1, media: 3, history: 4 }); expect(result.current.empty).toEqual({ overview: false, notes: false, activities: false, media: false, history: false, }); }); it('FE-COMP-PLACESHEET-004: a tab with nothing in it reads empty, unless there is something to add', () => { const { result, rerender } = setup(bothOrigins()); expect(result.current.empty).toEqual({ overview: false, notes: true, activities: true, media: true, history: true, }); parts.notes = { notes: [], targets: [{ key: 'list:900' }] }; parts.activities = { activities: [], canLink: true }; rerender({ data: bothOrigins() }); expect(result.current.empty.notes).toBe(false); expect(result.current.empty.activities).toBe(false); expect(result.current.empty.media).toBe(true); }); it('FE-COMP-PLACESHEET-005: the picked tab stays while the place is read again, another place starts on its overview', () => { const first = bothOrigins(); const { result, rerender } = setup(first); expect(result.current.tab).toBe('overview'); act(() => result.current.setTab('media')); expect(result.current.tab).toBe('media'); // The same place read again after a save keeps its tab. rerender({ data: bothOrigins() }); expect(result.current.tab).toBe('media'); rerender({ data: libraryDetail(libraryEntry({ key: 'list:77' }), [tripDetail()]) }); expect(result.current.tab).toBe('overview'); rerender({ data: null }); expect(result.current.entry).toBeNull(); expect(result.current.tab).toBe('overview'); }); });