Files
TREK/client/src/components/Map/placeNameLabels.test.ts
T
Maurice 01d327b502 feat(places): Places replaces Collections, with All places, place sheets and a calmer list
- Collections is Places now: /places opens "All places", every place of the
  trips and lists with where it comes from, the same filter row (where from,
  status, category, order), map bar (search, lock, list toggle) and place sheet
  as a list; /places/saved and /places/:id are the lists, and the old
  /collections links still land there. On the phone the same, with its own
  sheet. A place sheet shows each trip (planned day or trip dates, stay,
  booking, notes) and each list (status, labels, rating, notes) as one row
  that opens it.
- The rail: a calm "New list" with its plus tile, All places and All saved as
  views, a "My lists" heading, every name on one edge. Lists can carry one of
  24 symbols, shown in their colour in the rail and the editor.
- A saved place's row keeps to two lines: its category as an icon before the
  name, its average vote as a star on the picture. Map pins wear the same star.
- The place forms put the category in front of the name as a tile that opens
  the category list, instead of a row of chips.
- All saved gets a subtitle; the import dialog opens centred.
- Fixes: a pasted coordinate pair splits from either field of the place form;
  "more" in All places frees itself when a filter changes mid-load; place
  names on the trip map and the rating badge follow the reading direction.
2026-11-29 15:49:35 +01:00

131 lines
4.3 KiB
TypeScript

// 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 = <T extends Node>(node: T): T => {
if (node instanceof HTMLElement) place(node, pin.label);
return original(node);
};
}
return container;
}
const labels = (c: HTMLElement) => Array.from(c.querySelectorAll<HTMLElement>('.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: '<b>Senso-ji</b>',
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('<b>Senso-ji</b>');
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([]);
});
});