mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 16:33:16 +00:00
refactor(maps): build every GL map from one set of options
The trip map, the journey map and the settings preview each assembled their own constructor options and their own 3D setup on load, and the copies had drifted: the preview laid terrain under every style outside the Standard family, while the trip map only under the satellite and outdoor styles, so the preview showed a map the trip never drew. glMapOptions builds the options for all three and applyGl3d sets up terrain, buildings and the flattened Standard DEM. For Mapbox the options are exactly the ones each map built before, key for key and in the same order, which a test pins; the preview now follows the trip map's terrain rule. The next steps tune MapLibre in this one place. MapViewGL.tsx shrinks, and its size baseline follows.
This commit is contained in:
@@ -0,0 +1,205 @@
|
||||
/**
|
||||
* The construction options of every GL map (FE-MAP-GLOPTIONS-*).
|
||||
*
|
||||
* Mapbox GL is TREK's 3D map and its options are pinned here exactly as the
|
||||
* trip map, the journey map and the settings preview built them before they
|
||||
* shared this builder: every key, every value, nothing more. A change to the
|
||||
* Mapbox branch has to change this test on purpose.
|
||||
*/
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { applyGl3d, glMapOptions } from './glMapOptions';
|
||||
|
||||
const env = vi.hoisted(() => ({ tileStore: false }));
|
||||
const transformRequest = vi.hoisted(() => () => ({ url: '' }));
|
||||
|
||||
vi.mock('../../native/tileCache', () => ({
|
||||
tileCacheActive: () => env.tileStore,
|
||||
mapboxTileCacheRequest: transformRequest,
|
||||
}));
|
||||
|
||||
const container = document.createElement('div');
|
||||
const STREETS = 'mapbox://styles/mapbox/streets-v12';
|
||||
const STANDARD = 'mapbox://styles/mapbox/standard';
|
||||
const SATELLITE = 'mapbox://styles/mapbox/satellite-v9';
|
||||
const LIBERTY = 'https://tiles.openfreemap.org/styles/liberty';
|
||||
|
||||
function tripMap(provider: 'mapbox-gl' | 'maplibre-gl', quality: boolean, style = STREETS) {
|
||||
return glMapOptions({
|
||||
provider,
|
||||
container,
|
||||
style,
|
||||
center: [2.35, 48.85],
|
||||
zoom: 12,
|
||||
pitch: 45,
|
||||
attributionControl: true,
|
||||
quality,
|
||||
appTileStore: true,
|
||||
});
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
env.tileStore = false;
|
||||
});
|
||||
|
||||
describe('glMapOptions: Mapbox, byte for byte as before', () => {
|
||||
/** What the trip map built, key for key. */
|
||||
const tripMapOptions = {
|
||||
container,
|
||||
style: STREETS,
|
||||
center: [2.35, 48.85],
|
||||
zoom: 12,
|
||||
pitch: 45,
|
||||
attributionControl: true,
|
||||
antialias: false,
|
||||
projection: 'mercator',
|
||||
};
|
||||
|
||||
it('FE-MAP-GLOPTIONS-001: the trip map', () => {
|
||||
expect(tripMap('mapbox-gl', false)).toStrictEqual(tripMapOptions);
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-002: the quality mode antialiases and draws the globe', () => {
|
||||
expect(tripMap('mapbox-gl', true)).toStrictEqual({ ...tripMapOptions, antialias: true, projection: 'globe' });
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-003: in the iOS app with its tile store, Mapbox resources go through it', () => {
|
||||
env.tileStore = true;
|
||||
expect(tripMap('mapbox-gl', false)).toStrictEqual({ ...tripMapOptions, transformRequest });
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-004: the journey map, with its compact credit', () => {
|
||||
env.tileStore = true;
|
||||
const lngLat = { lng: 9.1, lat: 45.4 };
|
||||
const options = glMapOptions({
|
||||
provider: 'mapbox-gl',
|
||||
container,
|
||||
style: STANDARD,
|
||||
center: lngLat,
|
||||
zoom: 2,
|
||||
pitch: 0,
|
||||
attributionControl: { compact: true },
|
||||
quality: false,
|
||||
appTileStore: true,
|
||||
});
|
||||
expect(options).toStrictEqual({
|
||||
...tripMapOptions,
|
||||
style: STANDARD,
|
||||
center: lngLat,
|
||||
zoom: 2,
|
||||
pitch: 0,
|
||||
attributionControl: { compact: true },
|
||||
transformRequest,
|
||||
});
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-005: the settings preview never goes through the tile store', () => {
|
||||
env.tileStore = true;
|
||||
const options = glMapOptions({
|
||||
provider: 'mapbox-gl',
|
||||
container,
|
||||
style: STREETS,
|
||||
center: [2.3, 48.8],
|
||||
zoom: 16,
|
||||
pitch: 0,
|
||||
attributionControl: true,
|
||||
quality: true,
|
||||
});
|
||||
expect(options).toStrictEqual({
|
||||
...tripMapOptions,
|
||||
center: [2.3, 48.8],
|
||||
zoom: 16,
|
||||
pitch: 0,
|
||||
antialias: true,
|
||||
projection: 'globe',
|
||||
});
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-006: keys come in the order they always had', () => {
|
||||
env.tileStore = true;
|
||||
expect(Object.keys(tripMap('mapbox-gl', false))).toEqual([...Object.keys(tripMapOptions), 'transformRequest']);
|
||||
});
|
||||
});
|
||||
|
||||
describe('glMapOptions: MapLibre', () => {
|
||||
it('FE-MAP-GLOPTIONS-010: rotates around the pointer and has no projection or tile store', () => {
|
||||
env.tileStore = true;
|
||||
const options = tripMap('maplibre-gl', false, LIBERTY);
|
||||
expect(options).toMatchObject({ style: LIBERTY, antialias: false, aroundCenter: false });
|
||||
expect(Object.keys(options)).toEqual([
|
||||
'container',
|
||||
'style',
|
||||
'center',
|
||||
'zoom',
|
||||
'pitch',
|
||||
'attributionControl',
|
||||
'antialias',
|
||||
'aroundCenter',
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
describe('applyGl3d', () => {
|
||||
function fakeMap() {
|
||||
return {
|
||||
getLayer: vi.fn(() => undefined),
|
||||
getSource: vi.fn(() => undefined),
|
||||
addSource: vi.fn(),
|
||||
addLayer: vi.fn(),
|
||||
setTerrain: vi.fn(),
|
||||
getStyle: vi.fn(() => ({ layers: [] })),
|
||||
};
|
||||
}
|
||||
type GlMap = Parameters<typeof applyGl3d>[0];
|
||||
|
||||
it('FE-MAP-GLOPTIONS-020: a satellite style in 3D gets terrain and buildings', () => {
|
||||
const map = fakeMap();
|
||||
applyGl3d(map as unknown as GlMap, SATELLITE, true, false);
|
||||
expect(map.setTerrain).toHaveBeenCalledWith({ source: 'mapbox-dem', exaggeration: 1.2 });
|
||||
expect(map.addLayer).toHaveBeenCalledWith(expect.objectContaining({ id: 'trek-3d-buildings' }), undefined);
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-021: a flat vector style in 3D gets buildings and no terrain', () => {
|
||||
const map = fakeMap();
|
||||
applyGl3d(map as unknown as GlMap, STREETS, true, true);
|
||||
expect(map.setTerrain).not.toHaveBeenCalled();
|
||||
expect(map.addLayer).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
id: 'trek-3d-buildings',
|
||||
paint: expect.objectContaining({ 'fill-extrusion-color': '#3b3b3f' }),
|
||||
}),
|
||||
undefined
|
||||
);
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-022: Mapbox Standard brings its own 3D and has its terrain flattened, 3D or not', () => {
|
||||
for (const enable3d of [true, false]) {
|
||||
const map = fakeMap();
|
||||
applyGl3d(map as unknown as GlMap, STANDARD, enable3d, false);
|
||||
expect(map.addLayer).not.toHaveBeenCalled();
|
||||
expect(map.setTerrain).toHaveBeenCalledWith(null);
|
||||
}
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-023: a style not ready for terrain does not break the load', () => {
|
||||
const map = fakeMap();
|
||||
map.setTerrain.mockImplementation(() => {
|
||||
throw new Error('style not loaded');
|
||||
});
|
||||
expect(() => applyGl3d(map as unknown as GlMap, STANDARD, false, false)).not.toThrow();
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-024: with 3D off a non-Standard style is left as it is', () => {
|
||||
const map = fakeMap();
|
||||
applyGl3d(map as unknown as GlMap, SATELLITE, false, false);
|
||||
expect(map.addLayer).not.toHaveBeenCalled();
|
||||
expect(map.setTerrain).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('FE-MAP-GLOPTIONS-025: a MapLibre style is left as it is', () => {
|
||||
const map = fakeMap();
|
||||
applyGl3d(map as unknown as GlMap, LIBERTY, false, false);
|
||||
expect(map.addLayer).not.toHaveBeenCalled();
|
||||
expect(map.setTerrain).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user