mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
test(components): cover map, collections, settings, journey and admin
This commit is contained in:
@@ -1,6 +1,11 @@
|
||||
import { describe, it, expect, vi } from 'vitest'
|
||||
import { ReservationMapboxOverlay } from './reservationsMapbox'
|
||||
import type { Reservation } from '../../types'
|
||||
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
||||
import {
|
||||
ReservationMapboxOverlay,
|
||||
RESERVATION_SOURCE_ID,
|
||||
RESERVATION_LINE_LAYER_ID,
|
||||
type ReservationOverlayOptions,
|
||||
} from './reservationsMapbox'
|
||||
import type { Reservation, ReservationEndpoint } from '../../types'
|
||||
|
||||
// A minimal mapbox-gl stand-in: a persistent source that records the last
|
||||
// setData, and project() spreading points far enough apart to pass the
|
||||
@@ -134,3 +139,577 @@ describe('ReservationMapboxOverlay transit routes (#1570)', () => {
|
||||
expect(data.features).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
// ── FE-COMP-RESGL-001 to FE-COMP-RESGL-023 ────────────────────────────────────
|
||||
// The fake above always hands back a source, which makes the constructor skip
|
||||
// setupLayer. This one starts empty like a real freshly-styled map, records its
|
||||
// event handlers so the tests can fire them, and lets a test swap the projection
|
||||
// mid-flight to simulate a zoom.
|
||||
|
||||
type Handler = () => void
|
||||
type Projection = (c: [number, number]) => { x: number; y: number }
|
||||
|
||||
const spread: Projection = ([lng, lat]) => ({ x: lng * 1000, y: lat * 1000 })
|
||||
const collapsed: Projection = () => ({ x: 0, y: 0 })
|
||||
|
||||
function freshMap(projection: Projection = spread) {
|
||||
const sources = new Map<string, { setData: ReturnType<typeof vi.fn> }>()
|
||||
const layers = new Map<string, unknown>()
|
||||
const handlers: Record<string, Handler[]> = {}
|
||||
return {
|
||||
projection,
|
||||
_sources: sources,
|
||||
_layers: layers,
|
||||
getSource: (id: string) => sources.get(id),
|
||||
addSource: vi.fn((id: string) => { sources.set(id, { setData: vi.fn() }) }),
|
||||
addLayer: vi.fn((layer: { id: string }) => { layers.set(layer.id, layer) }),
|
||||
getLayer: (id: string) => layers.get(id),
|
||||
removeLayer: vi.fn((id: string) => { layers.delete(id) }),
|
||||
removeSource: vi.fn((id: string) => { sources.delete(id) }),
|
||||
on: vi.fn(function (this: void, event: string, fn: Handler) { (handlers[event] ||= []).push(fn) }),
|
||||
off: vi.fn((event: string, fn: Handler) => { handlers[event] = (handlers[event] || []).filter(h => h !== fn) }),
|
||||
getZoom: () => 12,
|
||||
project(this: { projection: Projection }, c: [number, number]) { return this.projection(c) },
|
||||
fire: (event: string) => { (handlers[event] || []).forEach(h => h()) },
|
||||
listeners: (event: string) => (handlers[event] || []).length,
|
||||
}
|
||||
}
|
||||
|
||||
type MarkerRecord = { el: HTMLElement; remove: ReturnType<typeof vi.fn>; lngLat: [number, number] | null }
|
||||
let glMarkers: MarkerRecord[] = []
|
||||
|
||||
// Unlike FakeMarker above this keeps the element it was handed, so the tests can
|
||||
// read the rendered badge and dispatch real clicks on it.
|
||||
const TrackingMarker = vi.fn(function (options?: { element?: HTMLElement }) {
|
||||
const record: MarkerRecord = {
|
||||
el: options?.element ?? document.createElement('div'),
|
||||
remove: vi.fn(),
|
||||
lngLat: null,
|
||||
}
|
||||
glMarkers.push(record)
|
||||
const marker = {
|
||||
setLngLat: (lngLat: [number, number]) => { record.lngLat = lngLat; return marker },
|
||||
addTo: () => marker,
|
||||
remove: record.remove,
|
||||
getElement: () => record.el,
|
||||
}
|
||||
return marker
|
||||
}) as unknown as new (options?: { element?: HTMLElement; anchor?: string }) => unknown
|
||||
|
||||
function endpoint(over: Partial<ReservationEndpoint> = {}): ReservationEndpoint {
|
||||
return {
|
||||
role: 'from', sequence: 0, name: 'A', code: null,
|
||||
lat: 48, lng: 2, timezone: null, local_time: null, local_date: null,
|
||||
...over,
|
||||
} as unknown as ReservationEndpoint
|
||||
}
|
||||
|
||||
function booking(over: Partial<Reservation> = {}): Reservation {
|
||||
return {
|
||||
id: 100, type: 'flight', status: 'confirmed',
|
||||
endpoints: [
|
||||
endpoint({ role: 'from', sequence: 0, name: 'Vienna', code: 'VIE', lat: 48, lng: 16 }),
|
||||
endpoint({ role: 'to', sequence: 1, name: 'London', code: 'LHR', lat: 51.5, lng: -0.5 }),
|
||||
],
|
||||
...over,
|
||||
} as unknown as Reservation
|
||||
}
|
||||
|
||||
const labelled = { showConnections: true, showStats: false, showEndpointLabels: true }
|
||||
|
||||
function features(map: ReturnType<typeof freshMap>) {
|
||||
const src = map._sources.get(RESERVATION_SOURCE_ID)
|
||||
const calls = src?.setData.mock.calls ?? []
|
||||
const data = calls[calls.length - 1]?.[0] as {
|
||||
features: { properties: Record<string, unknown>; geometry: { coordinates: [number, number][] } }[]
|
||||
} | undefined
|
||||
return data?.features ?? []
|
||||
}
|
||||
|
||||
function attach(map: ReturnType<typeof freshMap>, options: ReservationOverlayOptions = opts) {
|
||||
return new ReservationMapboxOverlay(map as never, options, TrackingMarker as never)
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
glMarkers = []
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe('ReservationMapboxOverlay lifecycle', () => {
|
||||
it('FE-COMP-RESGL-001: sets up the geojson source and both line layers on a fresh map', () => {
|
||||
const map = freshMap()
|
||||
attach(map)
|
||||
expect(map.addSource).toHaveBeenCalledWith(RESERVATION_SOURCE_ID, expect.objectContaining({ type: 'geojson' }))
|
||||
expect(map._layers.has(RESERVATION_LINE_LAYER_ID)).toBe(true)
|
||||
expect(map._layers.has(`${RESERVATION_LINE_LAYER_ID}-transit-casing`)).toBe(true)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-002: a second overlay on the same map reuses the existing source', () => {
|
||||
const map = freshMap()
|
||||
attach(map)
|
||||
map.addSource.mockClear()
|
||||
map.addLayer.mockClear()
|
||||
attach(map)
|
||||
expect(map.addSource).not.toHaveBeenCalled()
|
||||
expect(map.addLayer).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-003: destroy unhooks every listener and removes the markers, layer and source', () => {
|
||||
const map = freshMap()
|
||||
const overlay = attach(map, { ...opts, onEndpointClick: vi.fn() })
|
||||
overlay.update([booking()], opts)
|
||||
expect(glMarkers).toHaveLength(2)
|
||||
|
||||
overlay.destroy()
|
||||
expect(glMarkers.every(m => m.remove.mock.calls.length > 0)).toBe(true)
|
||||
expect(map.listeners('zoomend')).toBe(0)
|
||||
expect(map.listeners('moveend')).toBe(0)
|
||||
expect(map.listeners('render')).toBe(0)
|
||||
expect(map.removeLayer).toHaveBeenCalledWith(RESERVATION_LINE_LAYER_ID)
|
||||
expect(map.removeSource).toHaveBeenCalledWith(RESERVATION_SOURCE_ID)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-004: destroy survives a map whose style is already gone', () => {
|
||||
const map = freshMap()
|
||||
const overlay = attach(map)
|
||||
map.removeLayer.mockImplementation(() => { throw new Error('style gone') })
|
||||
expect(() => overlay.destroy()).not.toThrow()
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-005: a camera move redraws — a hop that was decluttered appears once zoomed in', () => {
|
||||
const map = freshMap(collapsed)
|
||||
const overlay = attach(map)
|
||||
overlay.update([booking({ type: 'train' })], opts)
|
||||
expect(features(map)).toHaveLength(0)
|
||||
|
||||
map.projection = spread
|
||||
map.fire('zoomend')
|
||||
expect(features(map)).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-006: the redraw and rotation handlers are inert once the overlay is destroyed', () => {
|
||||
const map = freshMap()
|
||||
const overlay = attach(map)
|
||||
const handlerFor = (event: string) =>
|
||||
map.on.mock.calls.find(c => c[0] === event)?.[1] as Handler
|
||||
const rerender = handlerFor('moveend')
|
||||
const rotate = handlerFor('render')
|
||||
|
||||
overlay.update([booking()], opts)
|
||||
const drawsBefore = map._sources.get(RESERVATION_SOURCE_ID)!.setData.mock.calls.length
|
||||
overlay.destroy()
|
||||
|
||||
rerender()
|
||||
rotate()
|
||||
expect(map._sources.get(RESERVATION_SOURCE_ID)).toBeUndefined()
|
||||
expect(drawsBefore).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-007: an update after the style dropped the source draws nothing', () => {
|
||||
const map = freshMap()
|
||||
const overlay = attach(map)
|
||||
map._sources.clear()
|
||||
overlay.update([booking()], opts)
|
||||
expect(glMarkers).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-008: a projection failure keeps the booking rather than hiding it', () => {
|
||||
const map = freshMap(() => { throw new Error('not projectable') })
|
||||
const overlay = attach(map)
|
||||
overlay.update([booking()], labelled)
|
||||
expect(features(map)).toHaveLength(1)
|
||||
// The label threshold cannot be measured either, so no labels are drawn.
|
||||
expect(glMarkers[0].el.textContent).toBe('')
|
||||
})
|
||||
})
|
||||
|
||||
describe('ReservationMapboxOverlay item filtering', () => {
|
||||
it('FE-COMP-RESGL-009: ignores reservations that are not transport', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([booking({ type: 'hotel' as Reservation['type'] })], opts)
|
||||
expect(features(map)).toHaveLength(0)
|
||||
expect(glMarkers).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-010: ignores a booking with fewer than two waypoints', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([booking({ endpoints: [endpoint({ role: 'from', sequence: 0 })] })], opts)
|
||||
expect(features(map)).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-011: ignores endpoints that are neither from, to nor stop', () => {
|
||||
const map = freshMap()
|
||||
const res = booking({
|
||||
endpoints: [
|
||||
endpoint({ role: 'from', sequence: 0, lat: 48, lng: 16 }),
|
||||
endpoint({ role: 'via' as ReservationEndpoint['role'], sequence: 1, lat: 49, lng: 17 }),
|
||||
],
|
||||
})
|
||||
attach(map).update([res], opts)
|
||||
expect(features(map)).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-012: orders waypoints by sequence and draws one arc per leg', () => {
|
||||
const map = freshMap()
|
||||
const res = booking({
|
||||
type: 'train',
|
||||
endpoints: [
|
||||
endpoint({ role: 'to', sequence: 2, name: 'C', lat: 51, lng: 16 }),
|
||||
endpoint({ role: 'from', sequence: 0, name: 'A', lat: 48, lng: 16 }),
|
||||
endpoint({ role: 'stop', sequence: 1, name: 'B', lat: 49.5, lng: 16 }),
|
||||
],
|
||||
})
|
||||
attach(map).update([res], opts)
|
||||
const drawn = features(map)
|
||||
expect(drawn).toHaveLength(2)
|
||||
// GeoJSON is [lng, lat]; legs run A→B and B→C.
|
||||
expect(drawn[0].geometry.coordinates).toEqual([[16, 48], [16, 49.5]])
|
||||
expect(drawn[1].geometry.coordinates).toEqual([[16, 49.5], [16, 51]])
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-013: a booking without a status is drawn as pending', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([booking({ status: undefined })], opts)
|
||||
expect(features(map)[0].properties.status).toBe('pending')
|
||||
})
|
||||
})
|
||||
|
||||
describe('ReservationMapboxOverlay endpoint markers', () => {
|
||||
it('FE-COMP-RESGL-014: places one marker per waypoint, titled with the stop name', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([booking()], opts)
|
||||
expect(glMarkers).toHaveLength(2)
|
||||
expect(glMarkers.map(m => m.lngLat)).toEqual([[16, 48], [-0.5, 51.5]])
|
||||
expect(glMarkers.map(m => m.el.title)).toEqual(['Vienna', 'London'])
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-015: no label is rendered while endpoint labels are off', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([booking()], opts)
|
||||
expect(glMarkers[0].el.textContent).toBe('')
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-016: labels show the endpoint code when there is one', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([booking()], labelled)
|
||||
expect(glMarkers.map(m => m.el.textContent)).toEqual(['VIE', 'LHR'])
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-017: a code-less stop falls back to its name with the parenthetical stripped', () => {
|
||||
const map = freshMap()
|
||||
const res = booking({
|
||||
type: 'train',
|
||||
endpoints: [
|
||||
endpoint({ role: 'from', sequence: 0, name: 'Wien Hbf (Bahnsteig 3)', lat: 48, lng: 16 }),
|
||||
endpoint({ role: 'to', sequence: 1, name: 'München Hbf', lat: 49, lng: 16 }),
|
||||
],
|
||||
})
|
||||
attach(map).update([res], labelled)
|
||||
expect(glMarkers.map(m => m.el.textContent)).toEqual(['Wien Hbf', 'München Hbf'])
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-018: a short hop draws its line but not its labels', () => {
|
||||
const map = freshMap()
|
||||
// 300 px apart: past the 200 px line threshold, under the 400 px label one.
|
||||
const res = booking({
|
||||
type: 'train',
|
||||
endpoints: [
|
||||
endpoint({ role: 'from', sequence: 0, name: 'Wien', code: 'VIE', lat: 48, lng: 16 }),
|
||||
endpoint({ role: 'to', sequence: 1, name: 'Graz', code: 'GRZ', lat: 48.3, lng: 16 }),
|
||||
],
|
||||
})
|
||||
attach(map).update([res], labelled)
|
||||
expect(features(map)).toHaveLength(1)
|
||||
expect(glMarkers.map(m => m.el.textContent)).toEqual(['', ''])
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-019: a redraw replaces the previous markers instead of stacking them', () => {
|
||||
const map = freshMap()
|
||||
const overlay = attach(map)
|
||||
overlay.update([booking()], opts)
|
||||
const first = [...glMarkers]
|
||||
|
||||
overlay.update([booking()], opts)
|
||||
expect(first.every(m => m.remove.mock.calls.length > 0)).toBe(true)
|
||||
expect(glMarkers).toHaveLength(4)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-020: hiding connections drops the markers and the lines', () => {
|
||||
const map = freshMap()
|
||||
const overlay = attach(map)
|
||||
overlay.update([booking()], opts)
|
||||
const shown = [...glMarkers]
|
||||
|
||||
overlay.update([booking()], { ...opts, showConnections: false })
|
||||
expect(shown.every(m => m.remove.mock.calls.length > 0)).toBe(true)
|
||||
expect(features(map)).toHaveLength(0)
|
||||
expect(glMarkers).toHaveLength(2) // the removed ones; none were added
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-021: clicking an endpoint reports its reservation and does not reach the map', () => {
|
||||
const onEndpointClick = vi.fn()
|
||||
const clickable = { ...opts, onEndpointClick }
|
||||
const map = freshMap()
|
||||
attach(map, clickable).update([booking({ id: 77 })], clickable)
|
||||
|
||||
const wrapper = document.createElement('div')
|
||||
const onMapClick = vi.fn()
|
||||
wrapper.addEventListener('click', onMapClick)
|
||||
wrapper.appendChild(glMarkers[0].el)
|
||||
glMarkers[0].el.dispatchEvent(new MouseEvent('click', { bubbles: true }))
|
||||
|
||||
expect(onEndpointClick).toHaveBeenCalledWith(77)
|
||||
expect(onMapClick).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-022: without a click handler the marker stays inert', () => {
|
||||
const map = freshMap()
|
||||
attach(map, { ...opts, onEndpointClick: undefined }).update([booking()], opts)
|
||||
expect(() => glMarkers[0].el.dispatchEvent(new MouseEvent('click', { bubbles: true }))).not.toThrow()
|
||||
})
|
||||
})
|
||||
|
||||
// computeDuration feeds the stats badge, which the GL overlay no longer draws —
|
||||
// but it still runs on every update, and a throw in there blanked whole trips
|
||||
// once (#1620). These pin down that every time shape stays survivable and the
|
||||
// connection keeps being drawn.
|
||||
describe('ReservationMapboxOverlay duration math', () => {
|
||||
function timed(from: Partial<ReservationEndpoint>, to: Partial<ReservationEndpoint>, res: Partial<Reservation> = {}) {
|
||||
return booking({
|
||||
endpoints: [
|
||||
endpoint({ role: 'from', sequence: 0, name: 'Vienna', code: 'VIE', lat: 48, lng: 16, ...from }),
|
||||
endpoint({ role: 'to', sequence: 1, name: 'London', code: 'LHR', lat: 51.5, lng: -0.5, ...to }),
|
||||
],
|
||||
...res,
|
||||
})
|
||||
}
|
||||
|
||||
it('FE-COMP-RESGL-023: a well-formed cross-timezone flight renders', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([timed(
|
||||
{ timezone: 'Europe/Vienna', local_date: '2024-01-15', local_time: '09:20' },
|
||||
{ timezone: 'Europe/London', local_date: '2024-01-15', local_time: '11:05' },
|
||||
)], opts)
|
||||
expect(features(map)).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-024: an overnight flight whose arrival reads earlier than departure renders', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([timed(
|
||||
{ timezone: 'Europe/Vienna', local_date: '2024-01-15', local_time: '22:30' },
|
||||
{ timezone: 'America/New_York', local_date: '2024-01-15', local_time: '02:10' },
|
||||
)], opts)
|
||||
expect(features(map)).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-025: a time-only endpoint borrows the date from the other side', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([timed(
|
||||
{ local_date: '2024-01-15', local_time: '09:20' },
|
||||
{},
|
||||
{ reservation_end_time: '17:30' } as Partial<Reservation>,
|
||||
)], opts)
|
||||
expect(features(map)).toHaveLength(1)
|
||||
|
||||
const other = freshMap()
|
||||
attach(other).update([timed(
|
||||
{},
|
||||
{ local_date: '2024-01-15', local_time: '17:30' },
|
||||
{ reservation_time: '09:20' } as Partial<Reservation>,
|
||||
)], opts)
|
||||
expect(features(other)).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-026: two date-less times render without a duration', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([timed({}, {}, {
|
||||
reservation_time: '09:20', reservation_end_time: '17:30',
|
||||
} as Partial<Reservation>)], opts)
|
||||
expect(features(map)).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-027: a booking without time zones uses the plain reservation times', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([timed({}, {}, {
|
||||
reservation_time: '2024-01-15T09:20', reservation_end_time: '2024-01-15T17:30',
|
||||
} as Partial<Reservation>)], opts)
|
||||
expect(features(map)).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-028: an implausibly long gap is rendered without a duration', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([timed({}, {}, {
|
||||
reservation_time: '2024-01-15T09:20', reservation_end_time: '2024-01-19T17:30',
|
||||
} as Partial<Reservation>)], opts)
|
||||
expect(features(map)).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-029: a booking with no times at all still draws its connection', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([timed({}, {})], opts)
|
||||
expect(features(map)).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-030: a hop shorter than an hour renders', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([timed({}, {}, {
|
||||
reservation_time: '2024-01-15T09:20', reservation_end_time: '2024-01-15T09:50',
|
||||
} as Partial<Reservation>)], opts)
|
||||
expect(features(map)).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-031: a stored timestamp that carries no clock time renders', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([timed(
|
||||
{ timezone: 'Europe/Vienna' },
|
||||
{ timezone: 'Europe/London' },
|
||||
{ reservation_time: '2024-01-15T', reservation_end_time: '2024-01-15T18:00' } as Partial<Reservation>,
|
||||
)], opts)
|
||||
expect(features(map)).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe('ReservationMapboxOverlay declutter floors', () => {
|
||||
// At the fake projection one degree of latitude is 1000 px, so `dLat` is the
|
||||
// on-screen gap between the two endpoints divided by 1000.
|
||||
const hop = (id: number, type: string, dLat: number) => booking({
|
||||
id, type: type as Reservation['type'],
|
||||
endpoints: [
|
||||
endpoint({ role: 'from', sequence: 0, name: 'Genoa', code: 'GOA', lat: 48, lng: 16 }),
|
||||
endpoint({ role: 'to', sequence: 1, name: 'Palma', code: 'PMI', lat: 48 + dLat, lng: 16 }),
|
||||
],
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-032: a cruise needs 150 px before it is drawn', () => {
|
||||
const near = freshMap()
|
||||
attach(near, labelled).update([hop(1, 'cruise', 0.1)], labelled)
|
||||
expect(features(near)).toHaveLength(0)
|
||||
|
||||
const far = freshMap()
|
||||
attach(far, labelled).update([hop(2, 'cruise', 0.2)], labelled)
|
||||
expect(features(far)).toHaveLength(1)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-033: a cruise keeps its endpoint labels off below 300 px', () => {
|
||||
const map = freshMap()
|
||||
attach(map, labelled).update([hop(1, 'cruise', 0.2)], labelled)
|
||||
expect(glMarkers.map(m => m.el.textContent)).toEqual(['', ''])
|
||||
|
||||
glMarkers = []
|
||||
const wide = freshMap()
|
||||
attach(wide, labelled).update([hop(2, 'cruise', 0.4)], labelled)
|
||||
expect(glMarkers.map(m => m.el.textContent)).toEqual(['GOA', 'PMI'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('ReservationMapboxOverlay waypoint edge cases', () => {
|
||||
it('FE-COMP-RESGL-034: a booking without an endpoints array is skipped', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([booking({ endpoints: undefined })], opts)
|
||||
expect(features(map)).toHaveLength(0)
|
||||
expect(glMarkers).toHaveLength(0)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-035: waypoints without a sequence keep their array order', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([booking({
|
||||
type: 'train',
|
||||
endpoints: [
|
||||
endpoint({ role: 'from', sequence: undefined, name: 'A', lat: 48, lng: 16 }),
|
||||
endpoint({ role: 'stop', sequence: undefined, name: 'B', lat: 49.5, lng: 16 }),
|
||||
endpoint({ role: 'to', sequence: undefined, name: 'C', lat: 51, lng: 16 }),
|
||||
],
|
||||
})], opts)
|
||||
const drawn = features(map)
|
||||
expect(drawn).toHaveLength(2)
|
||||
expect(drawn[0].geometry.coordinates).toEqual([[16, 48], [16, 49.5]])
|
||||
expect(drawn[1].geometry.coordinates).toEqual([[16, 49.5], [16, 51]])
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-036: a leg whose stops share coordinates degrades to a straight segment', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([booking({
|
||||
endpoints: [
|
||||
endpoint({ role: 'from', sequence: 0, name: 'Vienna', code: 'VIE', lat: 48, lng: 16 }),
|
||||
endpoint({ role: 'stop', sequence: 1, name: 'Vienna Gate', code: 'VIE', lat: 48, lng: 16 }),
|
||||
endpoint({ role: 'to', sequence: 2, name: 'London', code: 'LHR', lat: 51.5, lng: 16 }),
|
||||
],
|
||||
})], opts)
|
||||
const drawn = features(map)
|
||||
expect(drawn).toHaveLength(2)
|
||||
expect(drawn[0].geometry.coordinates).toHaveLength(2)
|
||||
expect(drawn[1].geometry.coordinates.length).toBeGreaterThan(2)
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-037: a code-less stop between two coded ends falls back to its name', () => {
|
||||
const map = freshMap()
|
||||
attach(map, labelled).update([booking({
|
||||
type: 'train',
|
||||
endpoints: [
|
||||
endpoint({ role: 'from', sequence: 0, name: 'Wien', code: 'VIE', lat: 48, lng: 16 }),
|
||||
endpoint({ role: 'stop', sequence: 1, name: 'Zuerich HB (Gleis 5)', code: null, lat: 50, lng: 16 }),
|
||||
endpoint({ role: 'to', sequence: 2, name: 'London', code: 'LHR', lat: 51.5, lng: 16 }),
|
||||
],
|
||||
})], labelled)
|
||||
expect(glMarkers.map(m => m.el.textContent)).toEqual(['VIE', 'Zuerich HB', 'LHR'])
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-038: an endpoint without a name gets an empty marker title', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([booking({
|
||||
endpoints: [
|
||||
endpoint({ role: 'from', sequence: 0, name: '', lat: 48, lng: 16 }),
|
||||
endpoint({ role: 'to', sequence: 1, name: 'London', lat: 51.5, lng: 16 }),
|
||||
],
|
||||
})], opts)
|
||||
expect(glMarkers.map(m => m.el.title)).toEqual(['', 'London'])
|
||||
})
|
||||
})
|
||||
|
||||
describe('ReservationMapboxOverlay transit styling', () => {
|
||||
const GEOMETRY = '__upzA_gayB_af@_pR_ry@~oR'
|
||||
|
||||
const transit = (legs: Record<string, unknown>[], over: Partial<Reservation> = {}) => booking({
|
||||
id: 200,
|
||||
type: 'transit' as Reservation['type'],
|
||||
endpoints: [
|
||||
endpoint({ role: 'from', sequence: 0, name: 'Stop A', lat: 48, lng: 2 }),
|
||||
endpoint({ role: 'to', sequence: 1, name: 'Stop B', lat: 48.05, lng: 2 }),
|
||||
],
|
||||
metadata: { transit: { legs } },
|
||||
...over,
|
||||
} as unknown as Partial<Reservation>)
|
||||
|
||||
it('FE-COMP-RESGL-039: a walking leg is grey and flagged as a walk', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([transit([{ geometry: GEOMETRY, mode: 'WALK' }])], opts)
|
||||
expect(features(map)[0].properties).toMatchObject({ transitPath: true, walk: true, color: '#64748b' })
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-040: a leg without a line colour falls back to the transit violet', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([transit([{ geometry: GEOMETRY, mode: 'BUS' }])], opts)
|
||||
expect(features(map)[0].properties).toMatchObject({ walk: false, color: '#7c3aed' })
|
||||
})
|
||||
|
||||
it('FE-COMP-RESGL-041: a transit journey without a status is drawn as pending', () => {
|
||||
const map = freshMap()
|
||||
attach(map).update([transit([{ geometry: GEOMETRY, mode: 'BUS' }], { status: undefined })], opts)
|
||||
expect(features(map)[0].properties.status).toBe('pending')
|
||||
})
|
||||
})
|
||||
|
||||
describe('ReservationMapboxOverlay frame handler', () => {
|
||||
it('FE-COMP-RESGL-042: a paint frame rotates only — it does not redraw the overlay', () => {
|
||||
const map = freshMap()
|
||||
const overlay = attach(map)
|
||||
overlay.update([booking()], opts)
|
||||
const drawsBefore = map._sources.get(RESERVATION_SOURCE_ID)!.setData.mock.calls.length
|
||||
|
||||
map.fire('render')
|
||||
|
||||
expect(map._sources.get(RESERVATION_SOURCE_ID)!.setData.mock.calls).toHaveLength(drawsBefore)
|
||||
expect(glMarkers).toHaveLength(2)
|
||||
expect(glMarkers.every(m => m.remove.mock.calls.length === 0)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user