mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-01 17:03:15 +00:00
ddfb7ad7ad
The trip PDF writes an outline's d into an SVG string as it stands. A detail file that carries anything but moves, lines and closes in numbers (a stale cache, a proxy in between) is now refused, and the coarse outline is drawn.
115 lines
4.6 KiB
TypeScript
115 lines
4.6 KiB
TypeScript
/**
|
|
* FE-STUDIO-DETAIL-001 to FE-STUDIO-DETAIL-005
|
|
*
|
|
* The finer country outlines (#3056): read only when they are a shape, loaded
|
|
* once per country, never failing a map that has to fall back to the coarse
|
|
* outline, and never holding the print view past its ceiling.
|
|
*
|
|
* The loaded outlines are kept for the page's life, so every test here asks
|
|
* for countries no other test in the file loads.
|
|
*/
|
|
import { renderHook, waitFor } from '@testing-library/react';
|
|
import { http, HttpResponse } from 'msw';
|
|
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
|
|
import { server } from '../../../tests/helpers/msw/server';
|
|
import {
|
|
countryDetailSettled,
|
|
countryDetailShapes,
|
|
preloadCountryDetail,
|
|
readCountryDetail,
|
|
useCountryDetail,
|
|
} from './countryDetail';
|
|
|
|
const SHAPE = { d: 'M0 0L10 0L10 10Z', w: 100, h: 80, b: [-160, -23, -154, -18] };
|
|
|
|
/** Answers the files of these countries with a shape, and counts the requests. */
|
|
function serve(codes: string[], answer: (code: string) => Response = () => HttpResponse.json(SHAPE)) {
|
|
const asked: string[] = [];
|
|
server.use(
|
|
http.get('/country-detail/:file', ({ params }) => {
|
|
const code = String(params.file).replace('.json', '');
|
|
asked.push(code);
|
|
return codes.includes(code) ? answer(code) : new HttpResponse(null, { status: 404 });
|
|
})
|
|
);
|
|
return asked;
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.useRealTimers();
|
|
});
|
|
|
|
describe('a file', () => {
|
|
it('FE-STUDIO-DETAIL-001: reads as a shape only when it is one', () => {
|
|
expect(readCountryDetail(SHAPE)).toEqual(SHAPE);
|
|
expect(readCountryDetail(null)).toBeNull();
|
|
expect(readCountryDetail('M0 0')).toBeNull();
|
|
expect(readCountryDetail({ ...SHAPE, d: '<svg/>' })).toBeNull();
|
|
expect(readCountryDetail({ ...SHAPE, d: 'M0 0"/><script>alert(1)</script><path d="M0 0' })).toBeNull();
|
|
expect(readCountryDetail({ ...SHAPE, d: 'M0 0C1 1 2 2 3 3Z' })).toBeNull();
|
|
expect(readCountryDetail({ ...SHAPE, w: '100' })).toBeNull();
|
|
expect(readCountryDetail({ ...SHAPE, b: [1, 2, 3] })).toBeNull();
|
|
expect(readCountryDetail({ ...SHAPE, b: [1, 2, 3, Number.NaN] })).toBeNull();
|
|
expect(readCountryDetail({ ...SHAPE, b: 'box' })).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('loading', () => {
|
|
it('FE-STUDIO-DETAIL-002: loads each country once, and only the countries the coarse set knows', async () => {
|
|
const asked = serve(['FJ']);
|
|
expect(countryDetailSettled('FJ')).toBe(false);
|
|
await Promise.all([preloadCountryDetail(['fj', 'FJ']), preloadCountryDetail(['FJ', 'XX', '1A', 'toolong'])]);
|
|
expect(asked).toEqual(['FJ']);
|
|
expect(countryDetailShapes(['FJ', 'XX']).get('FJ')).toEqual(SHAPE);
|
|
expect(countryDetailShapes(['FJ', 'XX']).has('XX')).toBe(false);
|
|
expect(countryDetailSettled('fj')).toBe(true);
|
|
// A code without a file of its own is never waited for.
|
|
expect(countryDetailSettled('XX')).toBe(true);
|
|
await preloadCountryDetail(['FJ']);
|
|
expect(asked).toEqual(['FJ']);
|
|
});
|
|
|
|
it('FE-STUDIO-DETAIL-003: settles without a shape when a file is missing or is not one, and the map stays coarse', async () => {
|
|
serve(['TO'], () => HttpResponse.json({ not: 'a shape' }));
|
|
await preloadCountryDetail(['TO', 'WS']);
|
|
expect(countryDetailSettled('TO')).toBe(true);
|
|
expect(countryDetailSettled('WS')).toBe(true);
|
|
expect(countryDetailShapes(['TO', 'WS']).size).toBe(0);
|
|
});
|
|
|
|
it('FE-STUDIO-DETAIL-004: stops waiting at the ceiling, while the file may still come', async () => {
|
|
let release: () => void = () => undefined;
|
|
const held = new Promise<void>((resolve) => {
|
|
release = resolve;
|
|
});
|
|
server.use(
|
|
http.get('/country-detail/VU.json', async () => {
|
|
await held;
|
|
return HttpResponse.json(SHAPE);
|
|
})
|
|
);
|
|
const started = Date.now();
|
|
await preloadCountryDetail(['VU'], 30);
|
|
expect(Date.now() - started).toBeLessThan(5000);
|
|
expect(countryDetailSettled('VU')).toBe(false);
|
|
release();
|
|
await waitFor(() => expect(countryDetailSettled('VU')).toBe(true));
|
|
expect(countryDetailShapes(['VU']).get('VU')).toEqual(SHAPE);
|
|
});
|
|
});
|
|
|
|
describe('useCountryDetail', () => {
|
|
it('FE-STUDIO-DETAIL-005: hands a map the finer outlines as they arrive, and asks for nothing when given none', async () => {
|
|
const asked = serve(['SB']);
|
|
const { result, rerender } = renderHook(({ codes }) => useCountryDetail(codes), {
|
|
initialProps: { codes: [] as string[] },
|
|
});
|
|
expect(result.current.size).toBe(0);
|
|
expect(asked).toEqual([]);
|
|
rerender({ codes: ['SB'] });
|
|
await waitFor(() => expect(result.current.get('SB')).toEqual(SHAPE));
|
|
expect(asked).toEqual(['SB']);
|
|
});
|
|
});
|