Files
TREK/client/tests/helpers/atlasViewState.ts
T
Maurice 9018ee8980 test(atlas): cover the new Atlas from engine to hub views
SonarCloud measured 16.9% coverage on this PR's new code. Tests now cover the WebGL globe engine (boot, caching, input, camera, regions, prefetch) and renderer (against a fake WebGL2 context that reads the real shaders), the paint, labels, sky and pins, the central useAtlasGlobe hook and the hub hooks, the snapshot, account queue and Atlas repos, every desktop and phone hub view, the server's people, flights and mesh routes, the shared badge rules, and the new view transition, warm bags and kept planner maps.
2026-11-29 10:21:28 +01:00

94 lines
2.8 KiB
TypeScript

import type { Badge } from '@trek/shared';
import { vi } from 'vitest';
import type { AtlasGlobeState } from '../../src/pages/atlas/useAtlasGlobe';
import { buildAtlasData } from './atlas';
/**
* Fakes for the desktop globe Atlas surfaces (view, search, badges, wishlist,
* Dawarich tab, badge unlock). They all read one big state object from
* useAtlasGlobe; a test builds only the slice its surface reads, with fresh
* spies every call (the suite keeps mock history between tests).
*/
/** A translator that shows the key and its named params, so a test can read both. */
export function viewT(key: string, params?: Record<string, unknown>): string {
const parts = Object.entries(params ?? {}).map(([k, v]) => `${k}=${String(v)}`);
if (!parts.length) return key;
return `${key}(${parts.join(',')})`;
}
const NAMES: Record<string, string> = { FR: 'France', JP: 'Japan', DE: 'Germany', IT: 'Italy', US: 'United States' };
export function buildViewBadge(over: Partial<Badge> = {}): Badge {
return {
id: 'countries-5',
category: 'countries',
motif: 'countries',
tier: 'bronze',
titleKey: 'atlas.badge.countries.title',
descKey: 'atlas.badge.countries.desc',
earned: false,
current: 2,
target: 5,
...over,
};
}
/** The globe state with harmless defaults; `over` replaces any field. */
export function buildViewState(over: Record<string, unknown> = {}): AtlasGlobeState {
const base: Record<string, unknown> = {
t: vi.fn(viewT),
language: 'en',
data: buildAtlasData(),
manifest: { countries: [], attribution: [] },
regions: {},
badges: [] as Badge[],
me: { id: 7 },
people: null,
compareEnabled: false,
loadPeople: vi.fn(),
resolveName: vi.fn((a2: string) => NAMES[a2] ?? ''),
// search
query: '',
setQuery: vi.fn(),
results: [],
pickResult: vi.fn(),
placeHits: [],
placesLoading: false,
pickPlace: vi.fn(async () => undefined),
loadRegionSearch: vi.fn(),
// wishlist / dawarich
bucketState: {
bucketList: [],
showBucketAdd: false,
setShowBucketAdd: vi.fn(),
handleClearBucketVisit: vi.fn(async () => undefined),
handleDeleteBucketItem: vi.fn(async () => undefined),
},
dawarichEnabled: false,
dawarichSub: 'wishes',
openDawarich: vi.fn(),
reload: vi.fn(async () => undefined),
flyToPoint: vi.fn(),
// globe view
pins: [],
hover: null,
compareWith: null,
gaps: false,
setGaps: vi.fn(),
stopCompare: vi.fn(),
setError: vi.fn(),
zoomIn: vi.fn(),
zoomOut: vi.fn(),
fetchJson: vi.fn(),
labelFor: vi.fn(),
labelRegionsOf: vi.fn(),
paint: vi.fn(),
paintKey: 'k',
onHover: vi.fn(),
onSelect: vi.fn(),
onReady: vi.fn(),
};
return { ...base, ...over } as unknown as AtlasGlobeState;
}