Skip to content

Commit

Permalink
error if invalid 'before' argument is passed to Map#addLayer (#5401)
Browse files Browse the repository at this point in the history
error if invalid 'before' argument is passed to Map#addLayer
  • Loading branch information
mollymerp authored Oct 5, 2017
1 parent 5f5a1f6 commit dff1fa4
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,13 @@ class Style extends Evented {

layer.setEventedParent(this, {layer: {id: id}});


const index = before ? this._order.indexOf(before) : this._order.length;
if (before && index === -1) {
this.fire('error', { message: new Error(`Layer with id "${before}" does not exist on this map.`)});
return;
}

this._order.splice(index, 0, id);

this._layers[id] = layer;
Expand Down
22 changes: 22 additions & 0 deletions test/unit/style/style.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,28 @@ test('Style#addLayer', (t) => {
});
});

t.test('fire error if before layer does not exist', (t) => {
const style = new Style(new StubMap());
style.loadJSON(createStyleJSON({
layers: [{
id: 'a',
type: 'background'
}, {
id: 'b',
type: 'background'
}]
}));
const layer = {id: 'c', type: 'background'};

style.on('style.load', () => {
style.on('error', (error)=>{
t.match(error.message, /does not exist on this map/);
t.end();
});
style.addLayer(layer, 'z');
});
});

t.test('fires an error on non-existant source layer', (t) => {
const style = new Style(new StubMap());
style.loadJSON(util.extend(createStyleJSON(), {
Expand Down

0 comments on commit dff1fa4

Please sign in to comment.