mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
60 lines
2.5 KiB
TypeScript
60 lines
2.5 KiB
TypeScript
import type { StyleSpecification, TransformStyleFunction } from 'maplibre-gl';
|
|
import { describe, expect, it, vi } from 'vitest';
|
|
|
|
import { mapLibreBasemap, type GlStyle } from './glBasemap';
|
|
import { rememberMapLibreBasemap, swapMapLibreBasemap } from './glBasemapSwap';
|
|
|
|
const LIBERTY = 'https://tiles.openfreemap.org/styles/liberty';
|
|
const RASTER = mapLibreBasemap('trek:template', 'https://tile.openstreetmap.de/{z}/{x}/{y}.png', false, []).style;
|
|
|
|
const vectorStyle: StyleSpecification = {
|
|
version: 8,
|
|
sources: {},
|
|
layers: [{ id: 'land', type: 'background', paint: { 'background-color': '#fff' } }],
|
|
};
|
|
|
|
function fixture(initial: StyleSpecification, ceiling: number) {
|
|
let style = structuredClone(initial);
|
|
const listeners = new Map<string, Array<() => void>>();
|
|
const map = {
|
|
once: (name: string, listener: () => void) => {
|
|
const handlers = listeners.get(name) ?? [];
|
|
handlers.push(listener);
|
|
listeners.set(name, handlers);
|
|
},
|
|
fire: (name: string) => {
|
|
for (const listener of listeners.get(name) ?? []) listener();
|
|
listeners.delete(name);
|
|
},
|
|
getStyle: () => style,
|
|
getMaxZoom: () => ceiling,
|
|
setMaxZoom: vi.fn(),
|
|
setStyle: vi.fn((next: GlStyle, options: { transformStyle: TransformStyleFunction }) => {
|
|
style = options.transformStyle(style, typeof next === 'string' ? vectorStyle : next);
|
|
}),
|
|
};
|
|
return map;
|
|
}
|
|
|
|
describe('changing the MapLibre basemap in place', () => {
|
|
it('keeps the live trip data and restores the vector zoom ceiling after a raster style', () => {
|
|
expect(RASTER).toBeDefined();
|
|
if (!RASTER) return;
|
|
const map = fixture(vectorStyle, 22);
|
|
rememberMapLibreBasemap(map, LIBERTY);
|
|
map.fire('load');
|
|
const overlay = { type: 'geojson', data: { type: 'FeatureCollection', features: [] } } as const;
|
|
map.getStyle().sources.route = { ...overlay, data: { ...overlay.data, features: [] } };
|
|
map.getStyle().layers.push({ id: 'trip-route', type: 'line', source: 'route' });
|
|
swapMapLibreBasemap(map, RASTER);
|
|
expect(map.getStyle().sources.route).toEqual(overlay);
|
|
expect(map.getStyle().layers.map(({ id }) => id)).toEqual(['basemap', 'trip-route']);
|
|
expect(map.setMaxZoom).toHaveBeenLastCalledWith(19);
|
|
swapMapLibreBasemap(map, LIBERTY);
|
|
expect(map.getStyle().sources.route).toEqual(overlay);
|
|
expect(map.getStyle().layers.map(({ id }) => id)).toEqual(['land', 'trip-route']);
|
|
expect(map.setMaxZoom).toHaveBeenLastCalledWith(22);
|
|
map.fire('remove');
|
|
});
|
|
});
|