/** * 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 { mapLibreBasemap } from './glBasemap'; 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'; const RASTER = mapLibreBasemap('trek:template', 'https://tile.openstreetmap.de/{z}/{x}/{y}.png', false, []).style; function tripMap(provider: 'mapbox-gl' | 'maplibre-gl', quality: boolean, style: unknown = 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', ]); }); it('FE-MAP-GLOPTIONS-011: a raster template stops at the zoom Leaflet stops at', () => { const options = tripMap('maplibre-gl', false, RASTER); expect(options.style).toBe(RASTER); expect(options.maxZoom).toBe(19); }); it('FE-MAP-GLOPTIONS-012: a style document never moves the Mapbox ceiling', () => { expect(tripMap('mapbox-gl', false, RASTER)).not.toHaveProperty('maxZoom'); }); }); 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[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-026: a raster template style is left as it is', () => { const map = fakeMap(); applyGl3d(map as unknown as GlMap, RASTER ?? '', true, 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(); }); });