Skip to content

Commit

Permalink
warn if invalid 'before' argument is passed to Map#addLayer
Browse files Browse the repository at this point in the history
  • Loading branch information
Molly Lloyd committed Oct 4, 2017
1 parent 64e9841 commit 429c8c9
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,10 @@ class Style extends Evented {

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

const index = before ? this._order.indexOf(before) : this._order.length;
const beforeIndex = before ? this._order.indexOf(before) : 0;
const index = before && beforeIndex !== -1 ? beforeIndex : this._order.length;
if (before && beforeIndex === -1) util.warnOnce(`Layer with id "${before}" does not exist on this map. Adding layer to the top of the stack.`);

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('warns 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'};
t.stub(util, 'warnOnce');

style.on('style.load', () => {
style.addLayer(layer, 'z');
t.deepEqual(style._order, ['a', 'b', 'c']);
t.ok(util.warnOnce.calledOnce);
t.end();
});
});

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 429c8c9

Please sign in to comment.