Files
TREK/client/tests/helpers/placesLibrary.ts
T

227 lines
5.9 KiB
TypeScript

import type {
PlacesLibraryDetail,
PlacesLibraryDetailResponse,
PlacesLibraryDuplicate,
PlacesLibraryDuplicateSide,
PlacesLibraryEntry,
PlacesLibraryInfo,
PlacesLibraryListDetail,
PlacesLibraryListOrigin,
PlacesLibraryPageResponse,
PlacesLibraryTripDetail,
PlacesLibraryTripOrigin,
} from '@trek/shared';
import { vi } from 'vitest';
import type { usePlacesLibrary } from '../../src/components/Collections/usePlacesLibrary';
/*
* Builders for "All places" (PLC-07, PLC-08): an entry with its origins, a page of
* them, and what each origin keeps of a place. One trip place 42 in "Rome 2026"
* and one saved place 900 in "Favourites", both the Colosseum, unless told otherwise.
*/
export function tripOrigin(over: Partial<PlacesLibraryTripOrigin> = {}): PlacesLibraryTripOrigin {
return {
kind: 'trip',
trip_id: 5,
trip_title: 'Rome 2026',
trip_start: null,
trip_end: null,
place_id: 42,
can_edit: true,
read_only: false,
...over,
};
}
export function listOrigin(over: Partial<PlacesLibraryListOrigin> = {}): PlacesLibraryListOrigin {
return {
kind: 'list',
collection_id: 9,
collection_name: 'Favourites',
collection_place_id: 900,
status: 'want',
can_edit: true,
...over,
};
}
export function libraryInfo(over: Partial<PlacesLibraryInfo> = {}): PlacesLibraryInfo {
return {
name: 'Colosseum',
address: 'Piazza del Colosseo',
lat: 41.89,
lng: 12.49,
description: null,
website: null,
phone: null,
image_url: null,
category: null,
...over,
};
}
export function tripDetail(over: Partial<PlacesLibraryTripDetail> = {}): PlacesLibraryTripDetail {
return {
kind: 'trip',
place_id: 42,
info: libraryInfo(),
notes: null,
rating: { average: null, count: 0, mine: null },
days: [],
has_accommodation: false,
has_reservation: false,
...over,
};
}
export function listDetail(over: Partial<PlacesLibraryListDetail> = {}): PlacesLibraryListDetail {
return {
kind: 'list',
collection_place_id: 900,
info: libraryInfo(),
notes: null,
labels: [],
rating: { average: null, count: 0, mine: null },
...over,
};
}
export function libraryEntry(over: Partial<PlacesLibraryEntry> = {}): PlacesLibraryEntry {
return {
key: 'trip:42',
name: 'Colosseum',
address: 'Piazza del Colosseo',
lat: 41.89,
lng: 12.49,
category: null,
image_url: null,
updated_at: null,
origins: [tripOrigin()],
...over,
};
}
export function libraryPage(
entries: PlacesLibraryEntry[],
over: Partial<PlacesLibraryPageResponse> = {}
): PlacesLibraryPageResponse {
return {
entries,
total: entries.length,
counts: { all: entries.length, trip: entries.length, list: 0 },
next_cursor: null,
...over,
};
}
/** An entry with one detail per origin, in the order of its origins. */
export function libraryDetail(entry: PlacesLibraryEntry, details: PlacesLibraryDetail[]): PlacesLibraryDetailResponse {
return { entry, details };
}
/** The Colosseum in the trip and in a list, with what both keep of it. */
export function bothOrigins(over: Partial<PlacesLibraryEntry> = {}): PlacesLibraryDetailResponse {
const entry = libraryEntry({ origins: [tripOrigin(), listOrigin()], ...over });
return libraryDetail(entry, [tripDetail(), listDetail()]);
}
/** What usePlacesLibrary hands a view, for the view tests that mock the hook. */
export type PlacesLibraryState = ReturnType<typeof usePlacesLibrary>;
/** An idle "All places" with nothing loaded, every setter and action a mock. */
export function libraryState(over: Partial<PlacesLibraryState> = {}): PlacesLibraryState {
return {
search: '',
setSearch: vi.fn(),
kind: 'all',
setKind: vi.fn(),
categoryId: 'all',
setCategoryId: vi.fn(),
sort: 'name',
setSort: vi.fn(),
status: 'all',
setStatus: vi.fn(),
tagIds: [],
toggleTag: vi.fn(),
filtered: false,
resetFilters: vi.fn(),
entries: [],
mappable: [],
total: 0,
counts: { all: 0, trip: 0, list: 0 },
hasMore: false,
loading: false,
loadingMore: false,
loadMore: vi.fn(() => Promise.resolve()),
error: null,
offline: false,
reload: vi.fn(),
selectedKey: null,
open: vi.fn(),
show: vi.fn(),
close: vi.fn(),
detail: null,
detailLoading: false,
cover: null,
...over,
};
}
/** Three places: two in trips with a position, one saved without. */
export function threeEntries(): PlacesLibraryEntry[] {
return [
libraryEntry({
key: 'trip:1',
name: 'Pantheon',
address: 'Piazza della Rotonda',
origins: [tripOrigin({ place_id: 1 })],
}),
libraryEntry({
key: 'trip:2',
name: 'Trevi Fountain',
address: null,
origins: [tripOrigin({ place_id: 2 }), listOrigin({ collection_place_id: 20 })],
}),
libraryEntry({
key: 'list:30',
name: 'Secret garden',
address: null,
lat: null,
lng: null,
origins: [listOrigin({ collection_place_id: 30, collection_name: 'Wishlist' })],
}),
];
}
/** One side of a suggested pair: the Colosseum as one trip place, with its provider ids unset. */
export function duplicateSide(over: Partial<PlacesLibraryDuplicateSide> = {}): PlacesLibraryDuplicateSide {
return {
key: 'trip:42',
identity_id: null,
name: 'Colosseum',
address: 'Piazza del Colosseo',
lat: 41.89,
lng: 12.49,
category: null,
google_place_id: null,
google_ftid: null,
osm_id: null,
origins: [tripOrigin()],
...over,
};
}
/** A suggested pair: the trip's Colosseum and a saved "Colosseo", the same name nearby, mergeable. */
export function duplicatePair(over: Partial<PlacesLibraryDuplicate> = {}): PlacesLibraryDuplicate {
return {
a: duplicateSide(),
b: duplicateSide({ key: 'list:900', name: 'Colosseo', origins: [listOrigin()] }),
reasons: ['name_nearby'],
differing: ['name'],
can_merge: true,
...over,
};
}