// FE-COMP-PLACEPOPUP-001 to FE-COMP-PLACEPOPUP-021 import { describe, it, expect } from 'vitest' import { createElement } from 'react' import { Coffee, MapPin, Signpost, type LucideIcon } from 'lucide-react' import { renderIconMarkup } from '../../utils/iconMarkup' import { buildPlacePopupHtml, buildPoiPopupHtml } from './placePopup' import { buildPlace } from '../../../tests/helpers/factories' import type { Poi } from './poiCategories' import type { Place } from '../../types' type PopupPlace = Place & { category_color?: string | null category_icon?: string | null category_name?: string | null } function popupPlace(overrides: Partial = {}): PopupPlace { return { ...buildPlace(), category_color: null, category_icon: null, category_name: null, ...overrides, } as PopupPlace } function poi(overrides: Partial = {}): Poi { return { osm_id: 'node/1', name: 'Café Central', lat: 48.21, lng: 16.36, category: 'cafe', poi_type: 'cafe', address: null, website: null, phone: null, opening_hours: null, cuisine: null, source: 'openstreetmap', ...overrides, } } describe('buildPlacePopupHtml', () => { it('FE-COMP-PLACEPOPUP-001: renders the name and nothing else for a bare place', () => { const html = buildPlacePopupHtml(popupPlace({ name: 'Louvre', address: null }), null) expect(html).toContain('Louvre') expect(html).not.toContain(' { const html = buildPlacePopupHtml(popupPlace({ name: undefined as never, address: null }), null) // The name line is there and empty. expect(html).toContain('white-space:nowrap;">') expect(html).not.toContain('undefined') }) it('FE-COMP-PLACEPOPUP-002: escapes HTML metacharacters in the name', () => { const html = buildPlacePopupHtml(popupPlace({ name: 'Bar & "Grill" x' }), null) expect(html).toContain('Bar & "Grill" <b>x</b>') expect(html).not.toContain('x') }) it('FE-COMP-PLACEPOPUP-003: embeds a data: thumbnail as an img', () => { const html = buildPlacePopupHtml(popupPlace(), 'data:image/jpeg;base64,AAAA') expect(html).toContain(' { const html = buildPlacePopupHtml(popupPlace(), '/api/maps/place-photo/abc123') expect(html).toContain(' { const html = buildPlacePopupHtml(popupPlace(), 'https://lh3.googleusercontent.com/p/AF1') expect(html).not.toContain(' { const html = buildPlacePopupHtml( popupPlace({ category_name: 'Museum', category_icon: 'Landmark', category_color: '#6366F1' }), null, ) expect(html).toContain('Museum') expect(html).toContain(' { const known = buildPlacePopupHtml(popupPlace({ category_name: 'X', category_icon: 'MapPin' }), null) const unknown = buildPlacePopupHtml(popupPlace({ category_name: 'X', category_icon: 'NoSuchIcon' }), null) const svgOf = (s: string) => s.slice(s.indexOf('')) expect(svgOf(unknown)).toBe(svgOf(known)) }) it('FE-COMP-PLACEPOPUP-008: a category without an icon draws no category row', () => { const html = buildPlacePopupHtml(popupPlace({ category_name: 'Museum', category_icon: null }), null) expect(html).not.toContain('Museum') expect(html).not.toContain(' { const html = buildPlacePopupHtml(popupPlace({ category_name: null, category_icon: 'Landmark' }), null) expect(html).not.toContain(' { const html = buildPlacePopupHtml(popupPlace({ address: 'Rue & B' }), null) expect(html).toContain('Rue <A> & B') }) it('FE-COMP-PLACEPOPUP-011: a category icon with no colour falls back to grey', () => { const html = buildPlacePopupHtml( popupPlace({ category_name: 'Museum', category_icon: 'Landmark', category_color: null }), null, ) expect(html).toContain('#6b7280') }) }) describe('buildPoiPopupHtml', () => { it('FE-COMP-PLACEPOPUP-012: renders the POI name with its category-coloured icon', () => { const html = buildPoiPopupHtml(poi({ name: 'Café Central', category: 'cafe' })) expect(html).toContain('Café Central') expect(html).toContain(' { const html = buildPoiPopupHtml(poi({ category: 'not-a-category', name: 'Somewhere' })) expect(html).not.toContain(' { expect(buildPoiPopupHtml(poi({ address: null }))).not.toContain('margin-top:3px') expect(buildPoiPopupHtml(poi({ address: 'Herrengasse 14 & 16' }))).toContain('Herrengasse 14 & 16') }) }) describe('buildPoiPopupHtml looks and plugin details', () => { const PLUGIN_KEY = 'plugin:trail-finder/trailheads' const icon = (Icon: LucideIcon, color: string) => renderIconMarkup(createElement(Icon, { size: 12, color, strokeWidth: 2 })) const parse = (html: string) => { const box = document.createElement('div') box.innerHTML = html return box } it('FE-COMP-PLACEPOPUP-015: a core POI card is the one it always was, with no details block', () => { const html = buildPoiPopupHtml(poi({ name: 'Café Central', category: 'cafe', address: 'Herrengasse 14' })) expect(html).toBe( '
' + `
${icon(Coffee, '#B45309')}` + 'Café Central
' + '
Herrengasse 14
', ) }) it('FE-COMP-PLACEPOPUP-016: shows the icon and colour the POI carries and its detail rows in order', () => { const html = buildPoiPopupHtml(poi({ category: PLUGIN_KEY, name: 'Trailhead', color: '#2f855a', icon: 'Signpost', details: [{ label: 'Length', value: '12.4 km' }, { label: 'Step-free', value: 'Yes' }], })) expect(html).toContain(icon(Signpost, '#2f855a')) const cells = [...parse(html).querySelectorAll('div[style*="grid-template-columns"] > span')].map(el => el.textContent) expect(cells).toEqual(['Length', '12.4 km', 'Step-free', 'Yes']) }) it('FE-COMP-PLACEPOPUP-017: markup in the name, the address and the details stays text', () => { const html = buildPoiPopupHtml(poi({ category: PLUGIN_KEY, name: '', address: '', color: '#2f855a', icon: 'Signpost', details: [{ label: 'Fee', value: '">' }], })) const box = parse(html) expect(box.querySelectorAll('img, script, b')).toHaveLength(0) // The only svg is the category glyph, never one a value smuggled in. expect(box.querySelectorAll('svg')).toHaveLength(1) expect(box.textContent).toContain('') expect(box.textContent).toContain('Fee') expect(box.textContent).toContain('">') }) it('FE-COMP-PLACEPOPUP-018: a colour or icon that fails the checks is drawn grey with a pin', () => { const html = buildPoiPopupHtml(poi({ category: PLUGIN_KEY, color: 'red;background:url(https://evil.example/x)', icon: '', })) expect(html).toContain(icon(MapPin, '#6b7280')) expect(html).not.toContain('evil.example') expect(parse(html).querySelectorAll('img')).toHaveLength(0) }) it('FE-COMP-PLACEPOPUP-019: shows at most the six rows the contract allows', () => { const details = Array.from({ length: 9 }, (_, i) => ({ label: `Row ${i}`, value: String(i) })) const html = buildPoiPopupHtml(poi({ category: PLUGIN_KEY, color: '#2f855a', icon: 'Signpost', details })) expect(parse(html).querySelectorAll('div[style*="grid-template-columns"] > span')).toHaveLength(12) expect(html).not.toContain('Row 6') }) it('FE-COMP-PLACEPOPUP-020: a long label wraps inside a capped column and leaves its value the wider share', () => { const html = buildPoiPopupHtml(poi({ category: PLUGIN_KEY, color: '#2f855a', icon: 'Signpost', details: [{ label: 'Wheelchair accessible toilet, ground fl', value: 'Step-free via the side door on the left' }], })) const grid = parse(html).querySelector('div[style*="grid-template-columns"]')! expect(grid.style.gridTemplateColumns).toBe('fit-content(45%) 1fr') const [label, value] = [...grid.querySelectorAll(':scope > span')] expect(label.style.overflowWrap).toBe('anywhere') expect(value.style.overflowWrap).toBe('anywhere') }) })