import { TextEncoder, TextDecoder } from 'util'; import { PickupPointCards, PickupPointChoice } from '../checkout-pickup-point/pickup-point-cards'; let renderToStaticMarkup: (element: JSX.Element) => string; beforeAll(async () => { // react-dom/server needs these, which the jsdom test environment lacks. Object.assign(globalThis, { TextEncoder, TextDecoder }); ({ renderToStaticMarkup } = await import('react-dom/server')); }); const point = (id: number) => ({ id, name: `Locker ${id}`, distance: '0.3 miles', details: [`${id} Station Road, Leeds, LS2 7AU`, 'Open today 10:00 to 18:00'], }); const choice = (count: number, overrides: Partial = {}): PickupPointChoice => ({ package: 0, provider: 'INPOST', kind: 'locker', legend: 'Choose a locker near LS2 7HY', selected: 1, shownFirst: 3, points: Array.from({ length: count }, (_, i) => point(i + 1)), unavailable: '', ...overrides, }); const html = (c: PickupPointChoice, selected = c.selected) => renderToStaticMarkup( undefined} />); describe('PickupPointCards', () => { // Positive — nearest three, the rest behind "Show 2 more", the selected one checked it('shows the nearest three first and the rest behind Show more', () => { const out = html(choice(5)); expect(out).toContain('Choose a locker near LS2 7HY'); expect(out.match(/type="radio"/g)).toHaveLength(5); expect(out.match(/checked=""/g)).toHaveLength(1); expect(out).toMatch(/checked="" value="1"/); expect(out).toContain('Show 2 more'); expect(out.indexOf('value="3"')).toBeLessThan(out.indexOf('p2g-pickup-more')); expect(out.indexOf('value="4"')).toBeGreaterThan(out.indexOf('p2g-pickup-more')); expect(out).toContain('Open today 10:00 to 18:00'); }); // Positive — distance, address and hours sit behind each card's More info, after the name it('puts the details behind More info', () => { const out = html(choice(2)); expect(out.match(/
More info<\/summary>/g)).toHaveLength(2); expect(out).toContain( 'Locker 1
More info0.3 miles' ); }); // Negative — a place with no distance or details has no More info it('has no More info without details', () => { const c = choice(1); c.points[0] = { ...c.points[0], distance: '', details: [] }; expect(html(c)).not.toContain('p2g-pickup-info'); }); // Positive — the customer's pick further down opens the list it('opens the list when the selected place is further down', () => { const out = html(choice(5), 5); expect(out).toContain('
'); expect(out).toMatch(/checked="" value="5"/); }); // Negative — three or fewer places have no Show more; names are escaped it('has no Show more for three places and escapes names', () => { const c = choice(1); c.points[0].name = 'x'; const out = html(c); expect(out).not.toContain('p2g-pickup-more'); expect(out).not.toContain('x'); expect(out).toContain('<b>x</b>'); }); // Negative — no places: the server's message, or nothing it('shows the unavailable message when there are no places', () => { expect(html(choice(0, { unavailable: 'Pickup points are not available right now.' }))).toBe( '

Pickup points are not available right now.

' ); expect(html(choice(0))).toBe(''); }); });