Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error if invalid 'before' argument is passed to Map#addLayer #5401

Merged
merged 2 commits into from
Oct 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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