Throw if argument to addLayer is not actually a layer (#5689)

Fixes #5225.
This commit is contained in:
Per Liedman 2017-08-06 08:36:10 +02:00 committed by GitHub
parent ce107a9f1b
commit 54ce147f3d
2 changed files with 11 additions and 0 deletions

View File

@ -419,6 +419,13 @@ describe("Map", function () {
map.addLayer(layer); map.addLayer(layer);
}); });
it("throws if adding something which is not a layer", function () {
var control = L.control.layers();
expect(function () {
map.addLayer(control);
}).to.throwError();
});
describe("When the first layer is added to a map", function () { describe("When the first layer is added to a map", function () {
it("fires a zoomlevelschange event", function () { it("fires a zoomlevelschange event", function () {
var spy = sinon.spy(); var spy = sinon.spy();

View File

@ -155,6 +155,10 @@ Map.include({
// @method addLayer(layer: Layer): this // @method addLayer(layer: Layer): this
// Adds the given layer to the map // Adds the given layer to the map
addLayer: function (layer) { addLayer: function (layer) {
if (!layer._layerAdd) {
throw new Error('The provided object is not a Layer.');
}
var id = Util.stamp(layer); var id = Util.stamp(layer);
if (this._layers[id]) { return this; } if (this._layers[id]) { return this; }
this._layers[id] = layer; this._layers[id] = layer;