diff --git a/spec/suites/map/MapSpec.js b/spec/suites/map/MapSpec.js index 662f0afb..0a479eeb 100644 --- a/spec/suites/map/MapSpec.js +++ b/spec/suites/map/MapSpec.js @@ -155,6 +155,35 @@ describe("Map", function () { expect(layer.onAdd.called).not.to.be.ok(); }); + it("fires a layeradd event immediately if the map is ready", function () { + var layer = { onAdd: sinon.spy() }, + spy = sinon.spy(); + map.on('layeradd', spy); + map.setView([0, 0], 0); + map.addLayer(layer); + expect(spy.called).to.be.ok(); + }); + + it("fires a layeradd event when the map becomes ready", function () { + var layer = { onAdd: sinon.spy() }, + spy = sinon.spy(); + map.on('layeradd', spy); + map.addLayer(layer); + expect(spy.called).not.to.be.ok(); + map.setView([0, 0], 0); + expect(spy.called).to.be.ok(); + }); + + it("does not fire a layeradd event if the layer is removed before the map becomes ready", function () { + var layer = { onAdd: sinon.spy(), onRemove: sinon.spy() }, + spy = sinon.spy(); + map.on('layeradd', spy); + map.addLayer(layer); + map.removeLayer(layer); + map.setView([0, 0], 0); + expect(spy.called).not.to.be.ok(); + }); + describe("When the first layer is added to a map", function () { it("fires a zoomlevelschange event", function () { var spy = sinon.spy(); @@ -213,6 +242,34 @@ describe("Map", function () { expect(layer.onRemove.called).not.to.be.ok(); }); + it("fires a layerremove event if the map is ready", function () { + var layer = { onAdd: sinon.spy(), onRemove: sinon.spy() }, + spy = sinon.spy(); + map.on('layerremove', spy); + map.setView([0, 0], 0); + map.addLayer(layer); + map.removeLayer(layer); + expect(spy.called).to.be.ok(); + }); + + it("does not fire a layerremove if the layer was not added", function () { + var layer = { onAdd: sinon.spy(), onRemove: sinon.spy() }, + spy = sinon.spy(); + map.on('layerremove', spy); + map.setView([0, 0], 0); + map.removeLayer(layer); + expect(spy.called).not.to.be.ok(); + }); + + it("does not fire a layerremove if the map is not ready", function () { + var layer = { onAdd: sinon.spy(), onRemove: sinon.spy() }, + spy = sinon.spy(); + map.on('layerremove', spy); + map.addLayer(layer); + map.removeLayer(layer); + expect(spy.called).not.to.be.ok(); + }); + describe("when the last tile layer on a map is removed", function () { it("fires a zoomlevelschange event", function () { map.whenReady(function(){ diff --git a/src/map/Map.js b/src/map/Map.js index d93082ea..a122e7fa 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -204,6 +204,7 @@ L.Map = L.Class.extend({ if (this._loaded) { layer.onRemove(this); + this.fire('layerremove', {layer: layer}); } delete this._layers[id]; @@ -219,7 +220,7 @@ L.Map = L.Class.extend({ layer.off('load', this._onTileLayerLoad, this); } - return this.fire('layerremove', {layer: layer}); + return this; }, hasLayer: function (layer) {