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 1 commit
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: 5 additions & 1 deletion src/style/style.js
Original file line number Diff line number Diff line change
Expand Up @@ -568,7 +568,11 @@ 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) : -1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this._order.length can never be -1, so the conditionals can be simplified:

const index = before ? this._order.indexOf(before) : this._order.length;
if (index === -1) {
    // error
}

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah good point! I still had to use if (before && index === -1) for flow to allow me to include before in the error message.

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.`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's report this via this.fire('error', ...) for consistency with non-existent layer IDs in moveLayer, removeLayer, etc.


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