From 8dee2d35914096b4f8879c308ecf50f47e6c3800 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20S=C3=A1nchez=20Ortega?= Date: Tue, 9 Feb 2016 09:56:33 +0100 Subject: [PATCH] Fix for #4208 with extra unit tests --- spec/suites/map/MapSpec.js | 48 ++++++++++++++++++++++++++++++++++++++ src/map/Map.js | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/spec/suites/map/MapSpec.js b/spec/suites/map/MapSpec.js index 57e26de8..593feff2 100644 --- a/spec/suites/map/MapSpec.js +++ b/spec/suites/map/MapSpec.js @@ -763,6 +763,43 @@ describe("Map", function () { }); + + describe('#fitBounds after layers set', function () { + var center = L.latLng(22, 33), + bounds = L.latLngBounds(L.latLng(1, 102), L.latLng(11, 122)), + boundsCenter = bounds.getCenter(); + + beforeEach(function () { + // fitBounds needs a map container with non-null area + var container = map.getContainer(); + container.style.width = container.style.height = "100px"; + document.body.appendChild(container); + }); + + afterEach(function () { + document.body.removeChild(map.getContainer()); + }); + + it('Snaps to a number after adding tile layer', function (done) { + L.Browser.any3d = true; + map.addLayer(L.tileLayer('file:///dev/null')); + expect(map.getZoom()).to.be(undefined); + map.fitBounds(bounds); + expect(map.getZoom()).to.be(2); + done(); + }); + + it('Snaps to a number after adding marker', function (done) { + L.Browser.any3d = true; + map.addLayer(L.marker(center)); + expect(map.getZoom()).to.be(undefined); + map.fitBounds(bounds); + expect(map.getZoom()).to.be(2); + done(); + }); + + }); + describe('#DOM events', function () { var c, map; @@ -927,4 +964,15 @@ describe("Map", function () { expect(Math.round(map.getZoomScale(scale, fromZoom) * 100) / 100).to.eql(toZoom); }); }); + + describe('#getZoom', function () { + it("returns undefined if map not initialized", function () { + expect(map.getZoom()).to.be(undefined); + }); + + it("returns undefined if map not initialized but layers added", function () { + map.addLayer(L.tileLayer('file:///dev/null')); + expect(map.getZoom()).to.be(undefined); + }); + }); }); diff --git a/src/map/Map.js b/src/map/Map.js index 5ea9df19..c87183ca 100644 --- a/src/map/Map.js +++ b/src/map/Map.js @@ -333,7 +333,7 @@ L.Map = L.Evented.extend({ bounds = L.latLngBounds(bounds); padding = L.point(padding || [0, 0]); - var zoom = this.getZoom(), + var zoom = this.getZoom() || 0, min = this.getMinZoom(), max = this.getMaxZoom(), nw = bounds.getNorthWest(),