import { vi } from 'vitest'; import type { VisitStatus } from '@trek/shared'; import type { AtlasData, BucketItem } from '../../src/pages/atlas/atlasModel'; import type { AtlasCountryActionsOptions } from '../../src/pages/atlas/useAtlasCountryActions'; /** * Fixtures for the atlas surfaces. The phone country popup takes the country * actions' state and setters as one object, so tests build it here instead of * mounting the globe behind it. * * The state setters are spies that also write the new value back onto the * object (including updater callbacks), so a test can assert both "the * setter was called" and "this is the state it produced". */ /** What the country popup reads: the country actions' options, the confirm step and the atlas data. */ export type AtlasPopupState = AtlasCountryActionsOptions & { executeConfirmAction: () => void | Promise; data: AtlasData | null; }; export type VisitedRegions = Record< string, { code: string; name: string; placeCount: number; manuallyMarked?: boolean; status?: VisitStatus }[] >; const echoT = (key: string, params?: Record): string => params ? `${key}:${Object.values(params).join(',')}` : key; export function buildAtlasData(over: Partial = {}): AtlasData { return { countries: [{ code: 'FR', tripCount: 2, placeCount: 5, firstVisit: '2023-01-01', lastVisit: '2024-06-01', status: 'visited' }], stats: { totalTrips: 3, totalPlaces: 10, totalCountries: 1, totalDays: 14, totalCities: 3, totalCountriesPlanned: 0, totalCountriesIdea: 0 }, mostVisited: null, continents: { Europe: 1 }, continentsPlanned: {}, lastTrip: { id: 7, title: 'Paris Trip', countryCode: 'FR' }, nextTrip: null, streak: 2, firstYear: 2022, tripsThisYear: 1, ...over, }; } export function buildBucketItem(over: Partial = {}): BucketItem { return { id: 1, name: 'Kyoto', lat: null, lng: null, country_code: 'JP', notes: null, target_date: null, ...over, }; } /** state field → its setter. */ const SETTERS: Record = { data: 'setData', visitedRegions: 'setVisitedRegions', bucketList: 'setBucketList', confirmAction: 'setConfirmAction', }; export function buildAtlasController(over: Record = {}): AtlasPopupState { const base: Record = { t: echoT, data: buildAtlasData(), visitedRegions: {} as VisitedRegions, confirmAction: null, executeConfirmAction: vi.fn(async () => undefined), bucketList: [] as BucketItem[], handleDeleteBucketItem: vi.fn(async () => undefined), }; const ctrl: Record = { ...base, ...over }; for (const [field, setter] of Object.entries(SETTERS)) { if (setter in over) continue; ctrl[setter] = vi.fn((next: unknown) => { ctrl[field] = typeof next === 'function' ? (next as (prev: unknown) => unknown)(ctrl[field]) : next; }); } return ctrl as unknown as AtlasPopupState; }