diff --git a/spec/index.html b/spec/index.html index 9d9aa9a5..5c94956e 100644 --- a/spec/index.html +++ b/spec/index.html @@ -30,6 +30,7 @@ + diff --git a/spec/suites/control/ControlSpec.js b/spec/suites/control/ControlSpec.js new file mode 100644 index 00000000..6332a62d --- /dev/null +++ b/spec/suites/control/ControlSpec.js @@ -0,0 +1,29 @@ +describe("Control", function () { + var map; + + beforeEach(function () { + map = L.map(document.createElement('div')); + }); + + function onAdd() { + return L.DomUtil.create('div', 'leaflet-test-control'); + } + + describe("#addTo", function () { + it("adds the container to the map", function () { + var control = new L.Control(); + control.onAdd = onAdd; + control.addTo(map); + expect(map.getContainer().querySelector('.leaflet-test-control')).to.equal(control.getContainer()); + }); + + it("removes the control from any existing map", function () { + var control = new L.Control(); + control.onAdd = onAdd; + control.addTo(map); + control.addTo(map); + expect(map.getContainer().querySelectorAll('.leaflet-test-control').length).to.equal(1); + expect(map.getContainer().querySelector('.leaflet-test-control')).to.equal(control.getContainer()); + }); + }); +}); diff --git a/src/control/Control.js b/src/control/Control.js index d8750fc3..4bae4401 100644 --- a/src/control/Control.js +++ b/src/control/Control.js @@ -37,6 +37,10 @@ L.Control = L.Class.extend({ }, addTo: function (map) { + if (this._map) { + this.remove(); + } + this._map = map; var container = this._container = this.onAdd(map),