Fixes Layer.isPopupOpen when no popup is bound (#5106)

* Adds initial tests for L.Layer#popup

* Fixes Layer.isPopupOpen when no popup is bound

This fixes uncaught exception when checking whether a popup isOpen(),
when the layer has no popup bound to it.
This commit is contained in:
Gert Van Gool 2016-11-29 10:33:54 +01:00 committed by Per Liedman
parent 188362098b
commit 9d6e6fcdf4
2 changed files with 65 additions and 1 deletions

View File

@ -328,3 +328,67 @@ describe("L.Map#openPopup", function () {
}); });
}); });
describe('L.Layer#_popup', function () {
var c, map, marker;
beforeEach(function () {
c = document.createElement('div');
c.style.width = '400px';
c.style.height = '400px';
map = new L.Map(c);
map.setView(new L.LatLng(55.8, 37.6), 6);
marker = L.marker(L.latLng(55.8, 37.6)).addTo(map);
});
afterEach(function () {
if (document.body.contains(c)) {
document.body.removeChild(c);
}
});
it("only adds a popup to the map when opened", function () {
marker.bindPopup("new layer");
expect(map.hasLayer(marker.getPopup())).to.be(false);
marker.openPopup();
expect(map.hasLayer(marker.getPopup())).to.be(true);
});
it("keeps an open popup on the map when it's unbound from the layer", function () {
marker.bindPopup("new layer").openPopup();
var popup = marker.getPopup();
marker.unbindPopup();
expect(map.hasLayer(popup)).to.be(true);
});
it("should not give an error when the marker has no popup", function () {
expect(function () {
marker.isPopupOpen();
}).to.not.throwException();
expect(marker.isPopupOpen()).to.be(false);
});
it("should show a popup as closed if it's never opened", function () {
marker.bindPopup("new layer");
expect(marker.isPopupOpen()).to.be(false);
});
it("should show a popup as opend if it's opened", function () {
marker.bindPopup("new layer").openPopup();
expect(marker.isPopupOpen()).to.be(true);
});
it("should show a popup as closed if it's opened and closed", function () {
marker.bindPopup("new layer").openPopup().closePopup();
expect(marker.isPopupOpen()).to.be(false);
});
it("should show the popup as closed if it's unbound", function () {
marker.bindPopup("new layer").openPopup().unbindPopup();
expect(function () {
marker.isPopupOpen();
}).to.not.throwException();
expect(marker.isPopupOpen()).to.be(false);
});
});

View File

@ -453,7 +453,7 @@ L.Layer.include({
// @method isPopupOpen(): boolean // @method isPopupOpen(): boolean
// Returns `true` if the popup bound to this layer is currently open. // Returns `true` if the popup bound to this layer is currently open.
isPopupOpen: function () { isPopupOpen: function () {
return this._popup.isOpen(); return (this._popup ? this._popup.isOpen() : false);
}, },
// @method setPopupContent(content: String|HTMLElement|Popup): this // @method setPopupContent(content: String|HTMLElement|Popup): this