import {Style} from './style'; import {SourceCache} from '../source/source_cache'; import {StyleLayer} from './style_layer'; import {Transform} from '../geo/transform'; import {extend} from '../util/util'; import {RequestManager} from '../util/request_manager'; import {Event, Evented} from '../util/evented'; import {RGBAImage} from '../util/image'; import {rtlMainThreadPluginFactory} from '../source/rtl_text_plugin_main_thread'; import {browser} from '../util/browser'; import {OverscaledTileID} from '../source/tile_id'; import {fakeServer, type FakeServer} from 'nise'; import {EvaluationParameters} from './evaluation_parameters'; import {LayerSpecification, GeoJSONSourceSpecification, FilterSpecification, SourceSpecification, StyleSpecification, SymbolLayerSpecification, TerrainSpecification, SkySpecification} from '@maplibre/maplibre-gl-style-spec'; import {GeoJSONSource} from '../source/geojson_source'; import {sleep} from '../util/test/util'; import {RTLPluginLoadedEventName} from '../source/rtl_text_plugin_status'; import {MessageType} from '../util/actor_messages'; function createStyleJSON(properties?): StyleSpecification { return extend({ 'version': 8, 'sources': {}, 'layers': [] }, properties); } function createSource() { return { type: 'vector', minzoom: 1, maxzoom: 10, attribution: 'MapLibre', tiles: ['http://example.com/{z}/{x}/{y}.png'] } as any as SourceSpecification; } function createGeoJSONSource() { return { 'type': 'geojson', 'data': { 'type': 'FeatureCollection', 'features': [] } }; } class StubMap extends Evented { style: Style; transform: Transform; private _requestManager: RequestManager; _terrain: TerrainSpecification; constructor() { super(); this.transform = new Transform(); this._requestManager = new RequestManager(); } _getMapId() { return 1; } getPixelRatio() { return 1; } setTerrain(terrain) { this._terrain = terrain; } getTerrain() { return this._terrain; } } const getStubMap = () => new StubMap() as any; function createStyle(map = getStubMap()) { const style = new Style(map); map.style = style; return style; } let server: FakeServer; let mockConsoleError: jest.SpyInstance; beforeEach(() => { global.fetch = null; server = fakeServer.create(); mockConsoleError = jest.spyOn(console, 'error').mockImplementation(() => { }); }); afterEach(() => { server.restore(); mockConsoleError.mockRestore(); }); describe('Style', () => { test('RTL plugin load reloads vector source but not raster source', async() => { const map = getStubMap(); const style = new Style(map); map.style = style; style.loadJSON({ 'version': 8, 'sources': { 'raster': { type: 'raster', tiles: ['http://tiles.server'] }, 'vector': { type: 'vector', tiles: ['http://tiles.server'] } }, 'layers': [{ 'id': 'raster', 'type': 'raster', 'source': 'raster' }] }); await style.once('style.load'); jest.spyOn(style.sourceCaches['raster'], 'reload'); jest.spyOn(style.sourceCaches['vector'], 'reload'); rtlMainThreadPluginFactory().fire(new Event(RTLPluginLoadedEventName)); expect(style.sourceCaches['raster'].reload).not.toHaveBeenCalled(); expect(style.sourceCaches['vector'].reload).toHaveBeenCalled(); }); }); describe('Style#loadURL', () => { test('fires "dataloading"', () => { const style = new Style(getStubMap()); const spy = jest.fn(); style.on('dataloading', spy); style.loadURL('style.json'); expect(spy).toHaveBeenCalledTimes(1); expect(spy.mock.calls[0][0].target).toBe(style); expect(spy.mock.calls[0][0].dataType).toBe('style'); }); test('transforms style URL before request', () => { const map = getStubMap(); const spy = jest.spyOn(map._requestManager, 'transformRequest'); const style = new Style(map); style.loadURL('style.json'); expect(spy).toHaveBeenCalledTimes(1); expect(spy.mock.calls[0][0]).toBe('style.json'); expect(spy.mock.calls[0][1]).toBe('Style'); }); test('validates the style', done => { const style = new Style(getStubMap()); style.on('error', ({error}) => { expect(error).toBeTruthy(); expect(error.message).toMatch(/version/); done(); }); style.loadURL('style.json'); server.respondWith(JSON.stringify(createStyleJSON({version: 'invalid'}))); server.respond(); }); test('cancels pending requests if removed', () => { const style = new Style(getStubMap()); style.loadURL('style.json'); style._remove(); expect((server.lastRequest as any).aborted).toBe(true); }); test('does not fire an error if removed', async () => { const style = new Style(getStubMap()); const spy = jest.fn(); style.on('error', spy); style.loadURL('style.json'); style._remove(); await sleep(0); expect(spy).not.toHaveBeenCalled(); }); test('fires an error if the request fails', async () => { const style = new Style(getStubMap()); const errorStatus = 400; const promise = style.once('error'); style.loadURL('style.json'); server.respondWith(request => request.respond(errorStatus)); server.respond(); const {error} = await promise; expect(error).toBeTruthy(); expect(error.status).toBe(errorStatus); }); }); describe('Style#loadJSON', () => { test('serialize() returns undefined until style is loaded', async () => { const style = new Style(getStubMap()); style.loadJSON(createStyleJSON()); expect(style.serialize()).toBeUndefined(); await style.once('style.load'); expect(style.serialize()).toEqual(createStyleJSON()); }); test('fires "dataloading" (synchronously)', () => { const style = new Style(getStubMap()); const spy = jest.fn(); style.on('dataloading', spy); style.loadJSON(createStyleJSON()); expect(spy).toHaveBeenCalledTimes(1); expect(spy.mock.calls[0][0].target).toBe(style); expect(spy.mock.calls[0][0].dataType).toBe('style'); }); test('fires "data" (asynchronously)', async () => { const style = new Style(getStubMap()); style.loadJSON(createStyleJSON()); const e = await style.once('data'); expect(e.target).toBe(style); expect(e.dataType).toBe('style'); }); test('fires "data" when the sprite finishes loading', async () => { // Stubbing to bypass Web APIs that supported by jsdom: // * `URL.createObjectURL` in ajax.getImage (https://github.com/tmpvar/jsdom/issues/1721) // * `canvas.getContext('2d')` in browser.getImageData jest.spyOn(browser, 'getImageData'); // stub Image so we can invoke 'onload' // https://github.com/jsdom/jsdom/commit/58a7028d0d5b6aacc5b435daee9fd8f9eacbb14c server.respondWith('GET', 'http://example.com/sprite.png', new ArrayBuffer(8)); server.respondWith('GET', 'http://example.com/sprite.json', '{}'); const style = new Style(getStubMap()); style.loadJSON({ 'version': 8, 'sources': {}, 'layers': [], 'sprite': 'http://example.com/sprite' }); style.once('error', (e) => expect(e).toBeFalsy()); const e = await style.once('data'); expect(e.target).toBe(style); expect(e.dataType).toBe('style'); const promise = style.once('data'); server.respond(); await promise; expect(e.target).toBe(style); expect(e.dataType).toBe('style'); }); test('Validate sprite image extraction', async () => { // Stubbing to bypass Web APIs that supported by jsdom: // * `URL.createObjectURL` in ajax.getImage (https://github.com/tmpvar/jsdom/issues/1721) // * `canvas.getContext('2d')` in browser.getImageData jest.spyOn(browser, 'getImageData'); // stub Image so we can invoke 'onload' // https://github.com/jsdom/jsdom/commit/58a7028d0d5b6aacc5b435daee9fd8f9eacbb14c server.respondWith('GET', 'http://example.com/sprite.png', new ArrayBuffer(8)); server.respondWith('GET', 'http://example.com/sprite.json', '{"image1": {"width": 1, "height": 1, "x": 0, "y": 0, "pixelRatio": 1.0}}'); const style = new Style(getStubMap()); style.loadJSON({ 'version': 8, 'sources': {}, 'layers': [], 'sprite': 'http://example.com/sprite' }); const firstDataEvent = await style.once('data'); expect(firstDataEvent.target).toBe(style); expect(firstDataEvent.dataType).toBe('style'); const secondDataPromise = style.once('data'); server.respond(); const secondDateEvent = await secondDataPromise; expect(secondDateEvent.target).toBe(style); expect(secondDateEvent.dataType).toBe('style'); const response = await style.imageManager.getImages(['image1']); const image = response['image1']; expect(image.data).toBeInstanceOf(RGBAImage); expect(image.data.width).toBe(1); expect(image.data.height).toBe(1); expect(image.pixelRatio).toBe(1); }); test('validates the style', async () => { const style = new Style(getStubMap()); const promise = style.once('error'); style.loadJSON(createStyleJSON({version: 'invalid'})); const {error} = await promise; expect(error).toBeTruthy(); expect(error.message).toMatch(/version/); }); test('creates sources', async () => { const style = createStyle(); style.loadJSON(extend(createStyleJSON(), { 'sources': { 'mapLibre': { 'type': 'vector', 'tiles': [] } } })); await style.once('style.load'); expect(style.sourceCaches['mapLibre'] instanceof SourceCache).toBeTruthy(); }); test('creates layers', async () => { const style = createStyle(); style.loadJSON({ 'version': 8, 'sources': { 'foo': { 'type': 'vector' } }, 'layers': [{ 'id': 'fill', 'source': 'foo', 'source-layer': 'source-layer', 'type': 'fill' }] }); await style.once('style.load'); expect(style.getLayer('fill') instanceof StyleLayer).toBeTruthy(); }); test('transforms sprite json and image URLs before request', async () => { const map = getStubMap(); const transformSpy = jest.spyOn(map._requestManager, 'transformRequest'); const style = createStyle(map); style.loadJSON(extend(createStyleJSON(), { 'sprite': 'http://example.com/sprites/bright-v8' })); await style.once('style.load'); expect(transformSpy).toHaveBeenCalledTimes(2); expect(transformSpy.mock.calls[0][0]).toBe('http://example.com/sprites/bright-v8.json'); expect(transformSpy.mock.calls[0][1]).toBe('SpriteJSON'); expect(transformSpy.mock.calls[1][0]).toBe('http://example.com/sprites/bright-v8.png'); expect(transformSpy.mock.calls[1][1]).toBe('SpriteImage'); }); test('emits an error on non-existant vector source layer', done => { const style = createStyle(); style.loadJSON(createStyleJSON({ sources: { '-source-id-': {type: 'vector', tiles: []} }, layers: [] })); style.on('style.load', () => { style.removeSource('-source-id-'); const source = createSource(); source['vector_layers'] = [{id: 'green'}]; style.addSource('-source-id-', source); style.addLayer({ 'id': '-layer-id-', 'type': 'circle', 'source': '-source-id-', 'source-layer': '-source-layer-' }); style.update({} as EvaluationParameters); }); style.on('error', (event) => { const err = event.error; expect(err).toBeTruthy(); expect(err.toString().indexOf('-source-layer-') !== -1).toBeTruthy(); expect(err.toString().indexOf('-source-id-') !== -1).toBeTruthy(); expect(err.toString().indexOf('-layer-id-') !== -1).toBeTruthy(); done(); }); }); test('sets up layer event forwarding', done => { const style = new Style(getStubMap()); style.loadJSON(createStyleJSON({ layers: [{ id: 'background', type: 'background' }] })); style.on('error', (e) => { expect(e.layer).toEqual({id: 'background'}); expect(e.mapLibre).toBeTruthy(); done(); }); style.on('style.load', () => { style._layers.background.fire(new Event('error', {mapLibre: true})); }); }); test('sets terrain if defined', async () => { const map = getStubMap(); const style = new Style(map); map.setTerrain = jest.fn(); style.loadJSON(createStyleJSON({ sources: {'source-id': createGeoJSONSource()}, terrain: {source: 'source-id', exaggeration: 0.33} })); await style.once('style.load'); expect(style.map.setTerrain).toHaveBeenCalled(); }); test('applies transformStyle function', async () => { const previousStyle = createStyleJSON({ sources: { base: { type: 'geojson', data: {type: 'FeatureCollection', features: []} } }, layers: [{ id: 'layerId0', type: 'circle', source: 'base' }, { id: 'layerId1', type: 'circle', source: 'base' }] }); const style = new Style(getStubMap()); style.loadJSON(createStyleJSON(), { transformStyle: (prevStyle, nextStyle) => ({ ...nextStyle, sources: { ...nextStyle.sources, base: prevStyle.sources.base }, layers: [ ...nextStyle.layers, prevStyle.layers[0] ] }) }, previousStyle); await style.once('style.load'); expect('base' in style.stylesheet.sources).toBeTruthy(); expect(style.stylesheet.layers[0].id).toBe(previousStyle.layers[0].id); expect(style.stylesheet.layers).toHaveLength(1); }); }); describe('Style#_load', () => { test('initiates sprite loading when it\'s present', () => { const style = new Style(getStubMap()); const prevStyleSpec = createStyleJSON({ sprite: 'https://example.com/test1' }); const nextStyleSpec = createStyleJSON({ sprite: 'https://example.com/test2' }); const _loadSpriteSpyOn = jest.spyOn(style, '_loadSprite'); style._load(nextStyleSpec, {}, prevStyleSpec); expect(_loadSpriteSpyOn).toHaveBeenCalledTimes(1); }); test('does not initiate sprite loading when it\'s absent (undefined)', () => { const style = new Style(getStubMap()); const prevStyleSpec = createStyleJSON({ sprite: 'https://example.com/test1' }); const nextStyleSpec = createStyleJSON({sprite: undefined}); const _loadSpriteSpyOn = jest.spyOn(style, '_loadSprite'); style._load(nextStyleSpec, {}, prevStyleSpec); expect(_loadSpriteSpyOn).not.toHaveBeenCalled(); }); test('layers are broadcasted to worker', () => { const style = new Style(getStubMap()); let dispatchType: MessageType; let dispatchData; const styleSpec = createStyleJSON({ layers: [{ id: 'background', type: 'background' }] }); const _broadcastSpyOn = jest.spyOn(style.dispatcher, 'broadcast') .mockImplementation((type, data) => { dispatchType = type; dispatchData = data; return Promise.resolve({} as any); }); style._load(styleSpec, {}); expect(_broadcastSpyOn).toHaveBeenCalled(); expect(dispatchType).toBe(MessageType.setLayers); expect(dispatchData).toHaveLength(1); expect(dispatchData[0].id).toBe('background'); // cleanup _broadcastSpyOn.mockRestore(); }); test('validate style when validate option is true', () => { const style = new Style(getStubMap()); const styleSpec = createStyleJSON({ layers: [{ id: 'background', type: 'background' }, { id: 'custom', type: 'custom' }] }); const stub = jest.spyOn(console, 'error'); style._load(styleSpec, {validate: true}); // 1. layers[1]: missing required property "source" // 2. layers[1].type: expected one of [fill, line, symbol, circle, heatmap, fill-extrusion, raster, hillshade, background], "custom" found expect(stub).toHaveBeenCalledTimes(2); // cleanup stub.mockReset(); }); test('layers are NOT serialized immediately after creation', () => { const style = new Style(getStubMap()); const styleSpec = createStyleJSON({ layers: [{ id: 'background', type: 'background' }, { id: 'custom', type: 'custom' }] }); style._load(styleSpec, {validate: false}); expect(style._serializedLayers).toBeNull(); }); }); describe('Style#_remove', () => { test('removes cache sources and clears their tiles', async () => { const style = new Style(getStubMap()); style.loadJSON(createStyleJSON({ sources: {'source-id': createGeoJSONSource()} })); await style.once('style.load'); const sourceCache = style.sourceCaches['source-id']; jest.spyOn(sourceCache, 'setEventedParent'); jest.spyOn(sourceCache, 'onRemove'); jest.spyOn(sourceCache, 'clearTiles'); style._remove(); expect(sourceCache.setEventedParent).toHaveBeenCalledWith(null); expect(sourceCache.onRemove).toHaveBeenCalledWith(style.map); expect(sourceCache.clearTiles).toHaveBeenCalled(); }); test('deregisters plugin listener', async () => { const style = new Style(getStubMap()); style.loadJSON(createStyleJSON()); jest.spyOn(rtlMainThreadPluginFactory(), 'off'); await style.once('style.load'); style._remove(); expect(rtlMainThreadPluginFactory().off).toHaveBeenCalled(); }); }); describe('Style#update', () => { test('on error', done => { const style = createStyle(); style.loadJSON({ 'version': 8, 'sources': { 'source': { 'type': 'vector' } }, 'layers': [{ 'id': 'second', 'source': 'source', 'source-layer': 'source-layer', 'type': 'fill' }] }); style.on('error', (error) => { expect(error).toBeFalsy(); }); style.on('style.load', () => { style.addLayer({id: 'first', source: 'source', type: 'fill', 'source-layer': 'source-layer'}, 'second'); style.addLayer({id: 'third', source: 'source', type: 'fill', 'source-layer': 'source-layer'}); style.removeLayer('second'); style.dispatcher.broadcast = (key, value) => { expect(key).toBe(MessageType.updateLayers); expect(value['layers'].map((layer) => { return layer.id; })).toEqual(['first', 'third']); expect(value['removedIds']).toEqual(['second']); done(); return Promise.resolve({} as any); }; style.update({} as EvaluationParameters); }); }); }); describe('Style#setState', () => { test('throw before loaded', () => { const style = new Style(getStubMap()); expect(() => style.setState(createStyleJSON())).toThrow(/load/i); }); test('do nothing if there are no changes', async () => { const style = createStyle(); style.loadJSON(createStyleJSON()); const spys = []; spys.push(jest.spyOn(style, 'addLayer').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'removeLayer').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setPaintProperty').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setLayoutProperty').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setFilter').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'addSource').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'removeSource').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setGeoJSONSourceData').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setLayerZoomRange').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setLight').mockImplementation((() => {}) as any)); await style.once('style.load'); const didChange = style.setState(createStyleJSON()); expect(didChange).toBeFalsy(); for (const spy of spys) { expect(spy).not.toHaveBeenCalled(); } }); test('do operations if there are changes', async () => { const style = createStyle(); const styleJson = createStyleJSON({ layers: [{ id: 'layerId0', type: 'symbol', source: 'sourceId0', 'source-layer': '123' }, { id: 'layerId1', type: 'circle', source: 'sourceId1', 'source-layer': '' }], sources: { sourceId0: createGeoJSONSource(), sourceId1: createGeoJSONSource(), }, light: { anchor: 'viewport' } }); style.loadJSON(styleJson); await style.once('style.load'); const spys = []; spys.push(jest.spyOn(style, 'addLayer').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'removeLayer').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setPaintProperty').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setLayoutProperty').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setFilter').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'addSource').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'removeSource').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setLayerZoomRange').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setLight').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setGeoJSONSourceData').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setGlyphs').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setSprite').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setSky').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style.map, 'setTerrain').mockImplementation((() => {}) as any)); const newStyle = JSON.parse(JSON.stringify(styleJson)) as StyleSpecification; newStyle.layers[0].paint = {'text-color': '#7F7F7F',}; newStyle.layers[0].layout = {'text-size': 16,}; newStyle.layers[0].minzoom = 2; (newStyle.layers[0] as SymbolLayerSpecification).filter = ['==', 'id', 1]; newStyle.layers.splice(1, 1); newStyle.sources['foo'] = createSource(); delete newStyle.sources['sourceId1']; newStyle.light = { anchor: 'map' }; newStyle.layers.push({ id: 'layerId2', type: 'circle', source: 'sourceId0' }); ((newStyle.sources.sourceId0 as GeoJSONSourceSpecification).data as GeoJSON.FeatureCollection).features.push({} as any); newStyle.glyphs = 'https://example.com/{fontstack}/{range}.pbf'; newStyle.sprite = 'https://example.com'; newStyle.terrain = { source: 'foo', exaggeration: 0.5 }; newStyle.zoom = 2; newStyle.sky = { 'fog-color': '#000001', 'sky-color': '#000002', 'horizon-fog-blend': 0.5, }; const didChange = style.setState(newStyle); expect(didChange).toBeTruthy(); for (const spy of spys) { expect(spy).toHaveBeenCalled(); } }); test('change transition doesn\'t change the style, but is considered a change', async () => { const style = createStyle(); const styleJson = createStyleJSON(); style.loadJSON(styleJson); await style.once('style.load'); const spys = []; spys.push(jest.spyOn(style, 'addLayer').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'removeLayer').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setPaintProperty').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setLayoutProperty').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setFilter').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'addSource').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'removeSource').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setLayerZoomRange').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setLight').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setGeoJSONSourceData').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setGlyphs').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style, 'setSprite').mockImplementation((() => {}) as any)); spys.push(jest.spyOn(style.map, 'setTerrain').mockImplementation((() => {}) as any)); const newStyleJson = createStyleJSON(); newStyleJson.transition = {duration: 5}; const didChange = style.setState(newStyleJson); expect(didChange).toBeTruthy(); for (const spy of spys) { expect(spy).not.toHaveBeenCalled(); } }); test('Issue #3893: compare new source options against originally provided options rather than normalized properties', async () => { server.respondWith('/tilejson.json', JSON.stringify({ tiles: ['http://tiles.server'] })); const initial = createStyleJSON(); initial.sources.mySource = { type: 'raster', url: '/tilejson.json' }; const style = new Style(getStubMap()); style.loadJSON(initial); const promise = style.once('style.load'); server.respond(); await promise; const spyRemove = jest.spyOn(style, 'removeSource').mockImplementation((() => {}) as any); const spyAdd = jest.spyOn(style, 'addSource').mockImplementation((() => {}) as any); style.setState(initial); expect(spyRemove).not.toHaveBeenCalled(); expect(spyAdd).not.toHaveBeenCalled(); }); test('return true if there is a change', async () => { const initialState = createStyleJSON(); const nextState = createStyleJSON({ sources: { foo: { type: 'geojson', data: {type: 'FeatureCollection', features: []} } } }); const style = new Style(getStubMap()); style.loadJSON(initialState); await style.once('style.load'); const didChange = style.setState(nextState); expect(didChange).toBeTruthy(); expect(style.stylesheet).toEqual(nextState); }); test('sets GeoJSON source data if different', async () => { const initialState = createStyleJSON({ 'sources': {'source-id': createGeoJSONSource()} }); const geoJSONSourceData = { 'type': 'FeatureCollection', 'features': [ { 'type': 'Feature', 'geometry': { 'type': 'Point', 'coordinates': [125.6, 10.1] } } ] }; const nextState = createStyleJSON({ 'sources': { 'source-id': { 'type': 'geojson', 'data': geoJSONSourceData } } }); const style = new Style(getStubMap()); style.loadJSON(initialState); await style.once('style.load'); const geoJSONSource = style.sourceCaches['source-id'].getSource() as GeoJSONSource; const mockStyleSetGeoJSONSourceDate = jest.spyOn(style, 'setGeoJSONSourceData'); const mockGeoJSONSourceSetData = jest.spyOn(geoJSONSource, 'setData'); const didChange = style.setState(nextState); expect(mockStyleSetGeoJSONSourceDate).toHaveBeenCalledWith('source-id', geoJSONSourceData); expect(mockGeoJSONSourceSetData).toHaveBeenCalledWith(geoJSONSourceData); expect(didChange).toBeTruthy(); expect(style.stylesheet).toEqual(nextState); }); test('updates stylesheet according to applied transformStyle function', async () => { const initialState = createStyleJSON({ sources: { base: { type: 'geojson', data: {type: 'FeatureCollection', features: []} } }, layers: [{ id: 'layerId0', type: 'circle', source: 'base' }, { id: 'layerId1', type: 'circle', source: 'base' }] }); const nextState = createStyleJSON(); const style = new Style(getStubMap()); style.loadJSON(initialState); await style.once('style.load'); const didChange = style.setState(nextState, { transformStyle: (prevStyle, nextStyle) => ({ ...nextStyle, sources: { ...nextStyle.sources, base: prevStyle.sources.base }, layers: [ ...nextStyle.layers, prevStyle.layers[0] ] }) }); expect(didChange).toBeTruthy(); expect('base' in style.stylesheet.sources).toBeTruthy(); expect(style.stylesheet.layers[0].id).toBe(initialState.layers[0].id); expect(style.stylesheet.layers).toHaveLength(1); }); test('Style#setState skips validateStyle when validate false', async () => { const style = new Style(getStubMap()); const styleSpec = createStyleJSON(); style.loadJSON(styleSpec); await style.once('style.load'); style.addSource('abc', createSource()); const nextState = {...styleSpec}; nextState.sources['def'] = {type: 'geojson'} as GeoJSONSourceSpecification; const didChange = style.setState(nextState, {validate: false}); expect(didChange).toBeTruthy(); }); }); describe('Style#addSource', () => { test('throw before loaded', () => { const style = new Style(getStubMap()); expect(() => style.addSource('source-id', createSource())).toThrow(/load/i); }); test('throw if missing source type', async () => { const style = new Style(getStubMap()); style.loadJSON(createStyleJSON()); const source = createSource(); delete source.type; await style.once('style.load'); expect(() => style.addSource('source-id', source)).toThrow(/type/i); }); test('fires "data" event', async () => { const style = createStyle(); style.loadJSON(createStyleJSON()); const source = createSource(); const dataPromise = style.once('data'); style.on('style.load', () => { style.addSource('source-id', source); style.update({} as EvaluationParameters); }); await dataPromise; }); test('throws on duplicates', async () => { const style = createStyle(); style.loadJSON(createStyleJSON()); const source = createSource(); await style.once('style.load'); style.addSource('source-id', source); expect(() => { style.addSource('source-id', source); }).toThrow(/Source "source-id" already exists./); }); test('sets up source event forwarding', async () => { const promisesResolve = {} as any; const promises = [ new Promise((resolve) => { promisesResolve.error = resolve; }), new Promise((resolve) => { promisesResolve.metadata = resolve; }), new Promise((resolve) => { promisesResolve.content = resolve; }), new Promise((resolve) => { promisesResolve.other = resolve; }), ]; const style = createStyle(); style.loadJSON(createStyleJSON({ layers: [{ id: 'background', type: 'background' }] })); const source = createSource(); await style.once('style.load'); style.on('error', () => { promisesResolve.error(); }); style.on('data', (e) => { if (e.sourceDataType === 'metadata' && e.dataType === 'source') { promisesResolve.metadata(); } else if (e.sourceDataType === 'content' && e.dataType === 'source') { promisesResolve.content(); } else { promisesResolve.other(); } }); style.addSource('source-id', source); // fires data twice style.sourceCaches['source-id'].fire(new Event('error')); style.sourceCaches['source-id'].fire(new Event('data')); await expect(Promise.all(promises)).resolves.toBeDefined(); }); }); describe('Style#removeSource', () => { test('throw before loaded', () => { const style = new Style(getStubMap()); expect(() => style.removeSource('source-id')).toThrow(/load/i); }); test('fires "data" event', async () => { const style = new Style(getStubMap()); style.loadJSON(createStyleJSON()); const source = createSource(); const dataPromise = style.once('data'); style.on('style.load', () => { style.addSource('source-id', source); style.removeSource('source-id'); style.update({} as EvaluationParameters); }); await dataPromise; }); test('clears tiles', async () => { const style = new Style(getStubMap()); style.loadJSON(createStyleJSON({ sources: {'source-id': createGeoJSONSource()} })); await style.once('style.load'); const sourceCache = style.sourceCaches['source-id']; jest.spyOn(sourceCache, 'clearTiles'); style.removeSource('source-id'); expect(sourceCache.clearTiles).toHaveBeenCalledTimes(1); }); test('throws on non-existence', async () => { const style = new Style(getStubMap()); style.loadJSON(createStyleJSON()); await style.once('style.load'); expect(() => { style.removeSource('source-id'); }).toThrow(/There is no source with this ID/); }); async function createStyleAndLoad(): Promise