// FE-MAP-PLACENAMES-001 to FE-MAP-PLACENAMES-005: the place names beside the trip map's pins (#2796). import { afterEach, describe, expect, it } from 'vitest'; import { controlBoxes, layoutPlaceNames, tagPlaceName } from './placeNameLabels'; type Rect = { x: number; y: number; w: number; h: number }; /** jsdom lays nothing out, so every element answers the box it was given. */ function place(el: Element, r: Rect): void { el.getBoundingClientRect = () => ({ x: r.x, y: r.y, width: r.w, height: r.h, toJSON: () => ({}) }) as DOMRect; } function mapWith(pins: { name: string; priority: 0 | 1 | 2; at: Rect; label: Rect }[]): HTMLElement { const container = document.createElement('div'); place(container, { x: 0, y: 0, w: 800, h: 600 }); document.body.appendChild(container); for (const pin of pins) { const el = document.createElement('div'); place(el, pin.at); tagPlaceName(el, pin.name, pin.priority); container.appendChild(el); // The label is made on the first layout; it gets its box the moment it is added. const original = el.appendChild.bind(el); el.appendChild = (node: T): T => { if (node instanceof HTMLElement) place(node, pin.label); return original(node); }; } return container; } const labels = (c: HTMLElement) => Array.from(c.querySelectorAll('.trek-place-name')); afterEach(() => { document.body.innerHTML = ''; }); describe('place names on the map', () => { it('FE-MAP-PLACENAMES-001: every tagged pin gets its name as text, nothing as markup', () => { const c = mapWith([ { name: 'Senso-ji', priority: 0, at: { x: 10, y: 10, w: 20, h: 20 }, label: { x: 36, y: 12, w: 80, h: 16 }, }, ]); layoutPlaceNames(c, true); expect(labels(c)).toHaveLength(1); expect(labels(c)[0].textContent).toBe('Senso-ji'); expect(c.querySelector('b')).toBeNull(); }); it('FE-MAP-PLACENAMES-002: of two colliding names the selected one stays', () => { const c = mapWith([ { name: 'Plain', priority: 0, at: { x: 10, y: 10, w: 20, h: 20 }, label: { x: 36, y: 12, w: 80, h: 16 }, }, { name: 'Picked', priority: 2, at: { x: 10, y: 60, w: 20, h: 20 }, label: { x: 40, y: 14, w: 80, h: 16 }, }, ]); layoutPlaceNames(c, true); const [plain, picked] = labels(c); expect(picked.hasAttribute('data-hidden')).toBe(false); expect(plain.hasAttribute('data-hidden')).toBe(true); }); it('FE-MAP-PLACENAMES-003: a name that would cover another pin, a control or run off the map is hidden', () => { const c = mapWith([ { name: 'Covers a pin', priority: 0, at: { x: 10, y: 10, w: 20, h: 20 }, label: { x: 36, y: 12, w: 80, h: 16 }, }, { name: 'Neighbour', priority: 0, at: { x: 60, y: 8, w: 20, h: 20 }, label: { x: 300, y: 300, w: 40, h: 16 }, }, { name: 'Edge', priority: 0, at: { x: 760, y: 200, w: 20, h: 20 }, label: { x: 786, y: 202, w: 80, h: 16 }, }, { name: 'Under control', priority: 0, at: { x: 400, y: 500, w: 20, h: 20 }, label: { x: 426, y: 502, w: 80, h: 16 }, }, ]); layoutPlaceNames(c, true, [{ x0: 450, y0: 490, x1: 520, y1: 530 }]); const hidden = labels(c).map((l) => l.hasAttribute('data-hidden')); expect(hidden).toEqual([true, false, true, true]); }); it('FE-MAP-PLACENAMES-004: switched off, the names go away again', () => { const c = mapWith([ { name: 'Senso-ji', priority: 0, at: { x: 10, y: 10, w: 20, h: 20 }, label: { x: 36, y: 12, w: 80, h: 16 }, }, ]); layoutPlaceNames(c, true); layoutPlaceNames(c, false); expect(labels(c)).toHaveLength(0); layoutPlaceNames(null, true); }); it('FE-MAP-PLACENAMES-005: control boxes are read off the elements the selector finds', () => { const c = document.createElement('div'); const btn = document.createElement('button'); place(btn, { x: 5, y: 6, w: 30, h: 30 }); c.appendChild(btn); expect(controlBoxes(c, 'button')).toEqual([{ x0: 5, y0: 6, x1: 35, y1: 36 }]); expect(controlBoxes(null, 'button')).toEqual([]); }); });