diff --git a/src/Layer.js b/src/Layer.js index 494eb2a82..56c07ae4c 100644 --- a/src/Layer.js +++ b/src/Layer.js @@ -378,9 +378,15 @@ export default class Layer { } _addToMGLMap(map, beforeLayerID) { - if (map.isStyleLoaded()) { + const STYLE_ERROR_REGEX = /Style is not done loading/; + + try { this._onMapLoaded(map, beforeLayerID); - } else { + } catch (error) { + if (!STYLE_ERROR_REGEX.test(error)) { + throw new Error(error); + } + map.on('load', () => { this._onMapLoaded(map, beforeLayerID); }); diff --git a/test/unit/layer.test.js b/test/unit/layer.test.js index 23010b973..b9b40482b 100644 --- a/test/unit/layer.test.js +++ b/test/unit/layer.test.js @@ -129,48 +129,27 @@ describe('api/layer', () => { describe('.addTo', () => { describe('._addToMGLMap', () => { let layer; + + const loadEvent = {}; + + const mapMock = { + isStyleLoaded: jasmine.createSpy('isStyleLoaded').and.returnValue(true), + on: (id, callback) => { + if (id === 'load') { + callback.call(layer, loadEvent); + } + } + }; + beforeEach(() => { layer = new Layer('layer0', source, viz); - layer._onMapLoaded = () => { }; + layer._onMapLoaded = () => {}; spyOn(layer, '_onMapLoaded'); }); it('should call onMapLoaded when the map is loaded', () => { - const mapMock = { isStyleLoaded: () => true }; - layer._addToMGLMap(mapMock); - expect(layer._onMapLoaded).toHaveBeenCalledWith(mapMock, undefined); - }); - - it('should not call onMapLoaded when the map is not loaded', () => { - const mapMock = { isStyleLoaded: () => false, on: () => { } }; - layer._addToMGLMap(mapMock); - expect(layer._onMapLoaded).not.toHaveBeenCalled(); - }); - - it('should call onMapLoaded when the map `load` event is triggered', () => { - const mapMock = { - isStyleLoaded: () => false, - on: (id, callback) => { - if (id === 'load') { - callback(); - } - } - }; - layer._addToMGLMap(mapMock); - expect(layer._onMapLoaded).toHaveBeenCalledWith(mapMock, undefined); - }); - - it('should not call onMapLoaded when other the map event is triggered', () => { - const mapMock = { - isStyleLoaded: () => false, - on: (id, callback) => { - if (id === 'other') { - callback(); - } - } - }; layer._addToMGLMap(mapMock); - expect(layer._onMapLoaded).not.toHaveBeenCalled(); + expect(layer._onMapLoaded).toHaveBeenCalled(); }); }); });