Don't call Layer#onRemove if Layer#onAdd was never called

This commit is contained in:
John Firebaugh 2013-06-01 21:48:41 -07:00
parent 01bede20d1
commit 055b3393da
2 changed files with 40 additions and 1 deletions

View File

@ -132,6 +132,21 @@ describe("Map", function () {
}); });
describe("#addLayer", function () { describe("#addLayer", function () {
it("calls layer.onAdd immediately if the map is ready", function () {
var layer = { onAdd: sinon.spy() };
map.setView([0, 0], 0);
map.addLayer(layer);
expect(layer.onAdd.called).to.be.ok();
});
it("calls layer.onAdd when the map becomes ready", function () {
var layer = { onAdd: sinon.spy() };
map.addLayer(layer);
expect(layer.onAdd.called).not.to.be.ok();
map.setView([0, 0], 0);
expect(layer.onAdd.called).to.be.ok();
});
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();
@ -168,6 +183,28 @@ describe("Map", function () {
}); });
describe("#removeLayer", function () { describe("#removeLayer", function () {
it("calls layer.onRemove if the map is ready", function () {
var layer = { onAdd: sinon.spy(), onRemove: sinon.spy() };
map.setView([0, 0], 0);
map.addLayer(layer);
map.removeLayer(layer);
expect(layer.onRemove.called).to.be.ok();
});
it("does not call layer.onRemove if the layer was not added", function () {
var layer = { onAdd: sinon.spy(), onRemove: sinon.spy() };
map.setView([0, 0], 0);
map.removeLayer(layer);
expect(layer.onRemove.called).not.to.be.ok();
});
it("does not call layer.onRemove if the map is not ready", function () {
var layer = { onAdd: sinon.spy(), onRemove: sinon.spy() };
map.addLayer(layer);
map.removeLayer(layer);
expect(layer.onRemove.called).not.to.be.ok();
});
describe("when the last tile layer on a map is removed", function () { describe("when the last tile layer on a map is removed", function () {
it("fires a zoomlevelschange event", function () { it("fires a zoomlevelschange event", function () {
map.whenReady(function(){ map.whenReady(function(){

View File

@ -203,7 +203,9 @@ L.Map = L.Class.extend({
if (!this._layers[id]) { return; } if (!this._layers[id]) { return; }
layer.onRemove(this); if (this._loaded) {
layer.onRemove(this);
}
delete this._layers[id]; delete this._layers[id];
if (this._zoomBoundLayers[id]) { if (this._zoomBoundLayers[id]) {