Leaflet/spec/suites/map/MapSpec.js

979 lines
30 KiB
JavaScript
Raw Normal View History

describe("Map", function () {
var map,
2015-09-25 18:55:37 +08:00
spy;
beforeEach(function () {
map = L.map(document.createElement('div'));
});
describe("#remove", function () {
it("fires an unload event if loaded", function () {
var container = document.createElement('div'),
2013-03-02 05:49:20 +08:00
map = new L.Map(container).setView([0, 0], 0),
2015-09-25 18:55:37 +08:00
spy = sinon.spy();
map.on('unload', spy);
map.remove();
2013-03-02 05:49:20 +08:00
expect(spy.called).to.be.ok();
});
it("fires no unload event if not loaded", function () {
var container = document.createElement('div'),
2013-03-02 05:49:20 +08:00
map = new L.Map(container),
2015-09-25 18:55:37 +08:00
spy = sinon.spy();
map.on('unload', spy);
map.remove();
2013-03-02 05:49:20 +08:00
expect(spy.called).not.to.be.ok();
});
describe("corner case checking", function () {
it("throws an exception upon reinitialization", function () {
var container = document.createElement('div'),
2015-09-25 18:55:37 +08:00
map = new L.Map(container);
expect(function () {
L.map(container);
}).to.throwException(function (e) {
expect(e.message).to.eql("Map container is already initialized.");
});
map.remove();
});
it("throws an exception if a container is not found", function () {
expect(function () {
L.map('nonexistentdivelement');
}).to.throwException(function (e) {
expect(e.message).to.eql("Map container not found.");
});
map.remove();
});
});
it("undefines container._leaflet", function () {
var container = document.createElement('div'),
map = new L.Map(container);
map.remove();
2013-03-02 05:49:20 +08:00
expect(container._leaflet).to.be(undefined);
});
it("unbinds events", function () {
var container = document.createElement('div'),
2013-03-02 05:49:20 +08:00
map = new L.Map(container).setView([0, 0], 1),
2015-09-25 18:55:37 +08:00
spy = sinon.spy();
map.on('click dblclick mousedown mouseup mousemove', spy);
map.remove();
happen.click(container);
happen.dblclick(container);
happen.mousedown(container);
happen.mouseup(container);
happen.mousemove(container);
2013-03-02 05:49:20 +08:00
expect(spy.called).to.not.be.ok();
});
});
describe('#getCenter', function () {
it('throws if not set before', function () {
expect(function () {
map.getCenter();
2013-03-02 05:49:20 +08:00
}).to.throwError();
});
it('returns a precise center when zoomed in after being set (#426)', function () {
var center = L.latLng(10, 10);
map.setView(center, 1);
map.setZoom(19);
expect(map.getCenter()).to.eql(center);
});
it('returns correct center after invalidateSize (#1919)', function () {
map.setView(L.latLng(10, 10), 1);
map.invalidateSize();
expect(map.getCenter()).not.to.eql(L.latLng(10, 10));
});
});
2012-10-13 03:49:12 +08:00
describe("#whenReady", function () {
describe("when the map has not yet been loaded", function () {
it("calls the callback when the map is loaded", function () {
2013-03-02 05:49:20 +08:00
var spy = sinon.spy();
2012-10-13 03:49:12 +08:00
map.whenReady(spy);
2013-03-02 05:49:20 +08:00
expect(spy.called).to.not.be.ok();
2012-10-13 03:49:12 +08:00
map.setView([0, 0], 1);
2013-03-02 05:49:20 +08:00
expect(spy.called).to.be.ok();
});
2012-10-13 03:49:12 +08:00
});
describe("when the map has already been loaded", function () {
it("calls the callback immediately", function () {
2013-03-02 05:49:20 +08:00
var spy = sinon.spy();
2012-10-13 03:49:12 +08:00
map.setView([0, 0], 1);
map.whenReady(spy);
2013-03-02 05:49:20 +08:00
expect(spy.called).to.be.ok();
2012-10-13 03:49:12 +08:00
});
});
});
describe("#setView", function () {
2013-02-20 01:25:49 +08:00
it("sets the view of the map", function () {
2013-03-02 05:49:20 +08:00
expect(map.setView([51.505, -0.09], 13)).to.be(map);
expect(map.getZoom()).to.be(13);
expect(map.getCenter().distanceTo([51.505, -0.09])).to.be.lessThan(5);
});
2014-11-06 07:44:23 +08:00
it("can be passed without a zoom specified", function () {
map.setZoom(13);
expect(map.setView([51.605, -0.11])).to.be(map);
expect(map.getZoom()).to.be(13);
expect(map.getCenter().distanceTo([51.605, -0.11])).to.be.lessThan(5);
});
2014-11-06 07:44:23 +08:00
it("limits initial zoom when no zoom specified", function () {
map.options.maxZoom = 20;
map.setZoom(100);
expect(map.setView([51.605, -0.11])).to.be(map);
expect(map.getZoom()).to.be(20);
expect(map.getCenter().distanceTo([51.605, -0.11])).to.be.lessThan(5);
});
2014-11-06 07:44:23 +08:00
it("defaults to zoom passed as map option", function () {
map = L.map(document.createElement('div'), {zoom: 13});
expect(map.setView([51.605, -0.11])).to.be(map);
expect(map.getZoom()).to.be(13);
});
it("passes duration option to panBy", function () {
map = L.map(document.createElement('div'), {zoom: 13, center: [0, 0]});
map.panBy = sinon.spy();
map.setView([51.605, -0.11], 13, {animate: true, duration: 13});
expect(map.panBy.callCount).to.eql(1);
expect(map.panBy.args[0][1].duration).to.eql(13);
});
});
describe("#getBounds", function () {
2013-02-27 03:31:23 +08:00
it("is safe to call from within a moveend callback during initial load (#1027)", function () {
map.on("moveend", function () {
2013-02-27 03:31:23 +08:00
map.getBounds();
});
map.setView([51.505, -0.09], 13);
});
});
describe('#setMaxBounds', function () {
it("aligns pixel-wise map view center with maxBounds center if it cannot move view bounds inside maxBounds (#1908)", function () {
var container = map.getContainer();
// large view, cannot fit within maxBounds
container.style.width = container.style.height = "1000px";
document.body.appendChild(container);
// maxBounds
var bounds = L.latLngBounds([51.5, -0.05], [51.55, 0.05]);
map.setMaxBounds(bounds, {animate: false});
// set view outside
map.setView(L.latLng([53.0, 0.15]), 12, {animate: false});
// get center of bounds in pixels
var boundsCenter = map.project(bounds.getCenter()).round();
expect(map.project(map.getCenter()).round()).to.eql(boundsCenter);
document.body.removeChild(container);
});
it("moves map view within maxBounds by changing one coordinate", function () {
var container = map.getContainer();
// small view, can fit within maxBounds
container.style.width = container.style.height = "200px";
document.body.appendChild(container);
// maxBounds
var bounds = L.latLngBounds([51, -0.2], [52, 0.2]);
map.setMaxBounds(bounds, {animate: false});
// set view outside maxBounds on one direction only
// leaves untouched the other coordinate (that is not already centered)
var initCenter = [53.0, 0.1];
map.setView(L.latLng(initCenter), 16, {animate: false});
// one pixel coordinate hasn't changed, the other has
var pixelCenter = map.project(map.getCenter()).round();
var pixelInit = map.project(initCenter).round();
expect(pixelCenter.x).to.eql(pixelInit.x);
expect(pixelCenter.y).not.to.eql(pixelInit.y);
// the view is inside the bounds
expect(bounds.contains(map.getBounds())).to.be(true);
document.body.removeChild(container);
});
});
describe("#getMinZoom and #getMaxZoom", function () {
describe('#getMinZoom', function () {
it('returns 0 if not set by Map options or TileLayer options', function () {
var map = L.map(document.createElement('div'));
expect(map.getMinZoom()).to.be(0);
});
});
2013-02-27 03:31:23 +08:00
it("minZoom and maxZoom options overrides any minZoom and maxZoom set on layers", function () {
var map = L.map(document.createElement('div'), {minZoom: 2, maxZoom: 20});
L.tileLayer("{z}{x}{y}", {minZoom: 4, maxZoom: 10}).addTo(map);
L.tileLayer("{z}{x}{y}", {minZoom: 6, maxZoom: 17}).addTo(map);
L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 22}).addTo(map);
expect(map.getMinZoom()).to.be(2);
expect(map.getMaxZoom()).to.be(20);
2013-02-27 03:31:23 +08:00
});
});
describe("#getMinZoom and #getMaxZoom", function () {
describe('#getMinZoom', function () {
it('returns 0 if not set by Map options or TileLayer options', function () {
var map = L.map(document.createElement('div'));
expect(map.getMinZoom()).to.be(0);
});
});
it("minZoom and maxZoom options overrides any minZoom and maxZoom set on layers", function () {
var map = L.map(document.createElement('div'), {minZoom: 2, maxZoom: 20});
L.tileLayer("{z}{x}{y}", {minZoom: 4, maxZoom: 10}).addTo(map);
L.tileLayer("{z}{x}{y}", {minZoom: 6, maxZoom: 17}).addTo(map);
L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 22}).addTo(map);
expect(map.getMinZoom()).to.be(2);
expect(map.getMaxZoom()).to.be(20);
});
});
2013-12-14 06:51:37 +08:00
describe("#hasLayer", function () {
it("returns false when passed undefined, null, or false", function () {
var map = L.map(document.createElement('div'));
expect(map.hasLayer(undefined)).to.equal(false);
expect(map.hasLayer(null)).to.equal(false);
expect(map.hasLayer(false)).to.equal(false);
});
});
function layerSpy() {
var layer = new L.Layer();
layer.onAdd = sinon.spy();
layer.onRemove = sinon.spy();
return layer;
}
describe("#addLayer", function () {
it("calls layer.onAdd immediately if the map is ready", function () {
var layer = layerSpy();
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 = layerSpy();
map.addLayer(layer);
expect(layer.onAdd.called).not.to.be.ok();
map.setView([0, 0], 0);
expect(layer.onAdd.called).to.be.ok();
});
it("does not call layer.onAdd if the layer is removed before the map becomes ready", function () {
var layer = layerSpy();
map.addLayer(layer);
map.removeLayer(layer);
map.setView([0, 0], 0);
expect(layer.onAdd.called).not.to.be.ok();
});
it("fires a layeradd event immediately if the map is ready", function () {
var layer = layerSpy(),
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 = layerSpy(),
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 = layerSpy(),
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();
});
it("adds the layer before firing layeradd", function (done) {
var layer = layerSpy();
map.on('layeradd', function () {
expect(map.hasLayer(layer)).to.be.ok();
done();
});
map.setView([0, 0], 0);
map.addLayer(layer);
});
describe("When the first layer is added to a map", function () {
it("fires a zoomlevelschange event", function () {
2013-03-02 05:49:20 +08:00
var spy = sinon.spy();
map.on("zoomlevelschange", spy);
2013-03-02 05:49:20 +08:00
expect(spy.called).not.to.be.ok();
L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 10}).addTo(map);
2013-03-02 05:49:20 +08:00
expect(spy.called).to.be.ok();
});
});
2013-02-27 03:31:23 +08:00
describe("when a new layer with greater zoomlevel coverage than the current layer is added to a map", function () {
2013-02-27 03:31:23 +08:00
it("fires a zoomlevelschange event", function () {
2013-03-02 05:49:20 +08:00
var spy = sinon.spy();
L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 10}).addTo(map);
2013-02-27 03:31:23 +08:00
map.on("zoomlevelschange", spy);
2013-03-02 05:49:20 +08:00
expect(spy.called).not.to.be.ok();
L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 15}).addTo(map);
2013-03-02 05:49:20 +08:00
expect(spy.called).to.be.ok();
2013-02-27 03:31:23 +08:00
});
});
2013-02-27 03:31:23 +08:00
describe("when a new layer with the same or lower zoomlevel coverage as the current layer is added to a map", function () {
it("fires no zoomlevelschange event", function () {
2013-03-02 05:49:20 +08:00
var spy = sinon.spy();
L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 10}).addTo(map);
2013-02-27 03:31:23 +08:00
map.on("zoomlevelschange", spy);
2013-03-02 05:49:20 +08:00
expect(spy.called).not.to.be.ok();
L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 10}).addTo(map);
2013-03-02 05:49:20 +08:00
expect(spy.called).not.to.be.ok();
L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 5}).addTo(map);
2013-03-02 05:49:20 +08:00
expect(spy.called).not.to.be.ok();
2013-02-27 03:31:23 +08:00
});
});
});
2013-02-27 03:31:23 +08:00
describe("#removeLayer", function () {
it("calls layer.onRemove if the map is ready", function () {
var layer = layerSpy();
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 = layerSpy();
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 = layerSpy();
map.addLayer(layer);
map.removeLayer(layer);
expect(layer.onRemove.called).not.to.be.ok();
});
it("fires a layerremove event if the map is ready", function () {
var layer = layerSpy(),
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 = layerSpy(),
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 = layerSpy(),
spy = sinon.spy();
map.on('layerremove', spy);
map.addLayer(layer);
map.removeLayer(layer);
expect(spy.called).not.to.be.ok();
});
it("removes the layer before firing layerremove", function (done) {
var layer = layerSpy();
map.on('layerremove', function () {
expect(map.hasLayer(layer)).not.to.be.ok();
done();
});
map.setView([0, 0], 0);
map.addLayer(layer);
map.removeLayer(layer);
});
it("supports adding and removing a tile layer without initializing the map", function () {
var layer = L.tileLayer("{z}{x}{y}");
map.addLayer(layer);
map.removeLayer(layer);
});
it("supports adding and removing a tile layer without initializing the map", function () {
map.setView([0, 0], 18);
2014-10-23 18:52:19 +08:00
var layer = L.gridLayer();
map.addLayer(layer);
map.removeLayer(layer);
});
describe("when the last tile layer on a map is removed", function () {
2013-02-27 03:31:23 +08:00
it("fires a zoomlevelschange event", function () {
map.whenReady(function () {
2013-03-02 05:49:20 +08:00
var spy = sinon.spy();
var tl = L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 10}).addTo(map);
2013-02-27 03:31:23 +08:00
map.on("zoomlevelschange", spy);
2013-03-02 05:49:20 +08:00
expect(spy.called).not.to.be.ok();
2013-02-27 03:31:23 +08:00
map.removeLayer(tl);
2013-03-02 05:49:20 +08:00
expect(spy.called).to.be.ok();
2013-02-27 03:31:23 +08:00
});
});
});
2013-02-27 03:31:23 +08:00
describe("when a tile layer is removed from a map and it had greater zoom level coverage than the remainding layer", function () {
2013-02-27 03:31:23 +08:00
it("fires a zoomlevelschange event", function () {
map.whenReady(function () {
2013-03-02 05:49:20 +08:00
var spy = sinon.spy(),
2015-09-25 18:55:37 +08:00
tl = L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 10}).addTo(map),
t2 = L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 15}).addTo(map);
2013-02-27 03:31:23 +08:00
map.on("zoomlevelschange", spy);
2013-03-02 05:49:20 +08:00
expect(spy.called).to.not.be.ok();
2013-02-27 03:31:23 +08:00
map.removeLayer(t2);
2013-03-02 05:49:20 +08:00
expect(spy.called).to.be.ok();
2013-02-27 03:31:23 +08:00
});
});
});
2013-02-27 03:31:23 +08:00
describe("when a tile layer is removed from a map it and it had lesser or the sa,e zoom level coverage as the remainding layer(s)", function () {
2013-02-27 03:31:23 +08:00
it("fires no zoomlevelschange event", function () {
map.whenReady(function () {
var tl = L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 10}).addTo(map),
t2 = L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 10}).addTo(map),
t3 = L.tileLayer("{z}{x}{y}", {minZoom: 0, maxZoom: 5}).addTo(map);
map.on("zoomlevelschange", spy);
expect(spy).not.toHaveBeenCalled();
map.removeLayer(t2);
expect(spy).not.toHaveBeenCalled();
map.removeLayer(t3);
expect(spy).not.toHaveBeenCalled();
});
});
});
});
2013-02-27 03:39:00 +08:00
describe("#eachLayer", function () {
it("returns self", function () {
2013-03-02 05:49:20 +08:00
expect(map.eachLayer(function () {})).to.be(map);
2013-02-27 03:39:00 +08:00
});
it("calls the provided function for each layer", function () {
var t1 = L.tileLayer("{z}{x}{y}").addTo(map),
2013-03-02 05:49:20 +08:00
t2 = L.tileLayer("{z}{x}{y}").addTo(map),
2015-09-25 18:55:37 +08:00
spy = sinon.spy();
2013-02-27 03:39:00 +08:00
map.eachLayer(spy);
2013-03-02 05:49:20 +08:00
expect(spy.callCount).to.eql(2);
expect(spy.firstCall.args).to.eql([t1]);
expect(spy.secondCall.args).to.eql([t2]);
2013-02-27 03:39:00 +08:00
});
it("calls the provided function with the provided context", function () {
2013-03-02 05:49:20 +08:00
var t1 = L.tileLayer("{z}{x}{y}").addTo(map),
2015-09-25 18:55:37 +08:00
spy = sinon.spy();
2013-02-27 03:39:00 +08:00
map.eachLayer(spy, map);
2013-03-02 05:49:20 +08:00
expect(spy.thisValues[0]).to.eql(map);
2013-02-27 03:39:00 +08:00
});
});
describe("#invalidateSize", function () {
var container,
2013-11-14 03:50:02 +08:00
origWidth = 100,
2015-09-25 18:55:37 +08:00
clock;
beforeEach(function () {
container = map.getContainer();
container.style.width = origWidth + "px";
document.body.appendChild(container);
map.setView([0, 0], 0);
map.invalidateSize({pan: false});
clock = sinon.useFakeTimers();
});
afterEach(function () {
document.body.removeChild(container);
clock.restore();
});
it("pans by the right amount when growing in 1px increments", function () {
container.style.width = (origWidth + 1) + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(1);
container.style.width = (origWidth + 2) + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(1);
container.style.width = (origWidth + 3) + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(2);
});
it("pans by the right amount when shrinking in 1px increments", function () {
container.style.width = (origWidth - 1) + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(0);
container.style.width = (origWidth - 2) + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(-1);
container.style.width = (origWidth - 3) + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(-1);
});
it("pans back to the original position after growing by an odd size and back", function () {
container.style.width = (origWidth + 5) + "px";
map.invalidateSize();
container.style.width = origWidth + "px";
map.invalidateSize();
expect(map._getMapPanePos().x).to.be(0);
});
it("emits no move event if the size has not changed", function () {
var spy = sinon.spy();
map.on("move", spy);
map.invalidateSize();
expect(spy.called).not.to.be.ok();
});
it("emits a move event if the size has changed", function () {
var spy = sinon.spy();
map.on("move", spy);
2013-11-14 03:56:48 +08:00
container.style.width = (origWidth + 5) + "px";
map.invalidateSize();
expect(spy.called).to.be.ok();
});
it("emits a moveend event if the size has changed", function () {
var spy = sinon.spy();
map.on("moveend", spy);
2013-11-14 03:56:48 +08:00
container.style.width = (origWidth + 5) + "px";
map.invalidateSize();
expect(spy.called).to.be.ok();
});
it("debounces the moveend event if the debounceMoveend option is given", function () {
var spy = sinon.spy();
map.on("moveend", spy);
2013-11-14 03:56:48 +08:00
container.style.width = (origWidth + 5) + "px";
map.invalidateSize({debounceMoveend: true});
expect(spy.called).not.to.be.ok();
clock.tick(200);
expect(spy.called).to.be.ok();
});
});
2015-03-08 19:21:43 +08:00
describe('#flyTo', function () {
2015-03-09 17:13:42 +08:00
it('move to requested center and zoom, and call zoomend once', function (done) {
2015-03-08 19:21:43 +08:00
var spy = sinon.spy(),
2015-09-25 18:55:37 +08:00
newCenter = new L.LatLng(10, 11),
newZoom = 12;
var callback = function () {
expect(map.getCenter()).to.eql(newCenter);
expect(map.getZoom()).to.eql(newZoom);
spy();
expect(spy.calledOnce).to.be.ok();
done();
};
2015-03-08 19:21:43 +08:00
map.setView([0, 0], 0);
map.once('zoomend', callback).flyTo(newCenter, newZoom);
2015-03-08 19:21:43 +08:00
});
2015-06-08 23:47:08 +08:00
});
describe('#zoomIn and #zoomOut', function () {
var center = L.latLng(22, 33);
beforeEach(function () {
map.setView(center, 10);
});
it('zoomIn zooms by 1 zoom level by default', function (done) {
2015-09-28 18:22:48 +08:00
map.once('zoomend', function () {
2015-06-08 23:47:08 +08:00
expect(map.getZoom()).to.eql(11);
expect(map.getCenter()).to.eql(center);
done();
});
map.zoomIn(null, {animate: false});
});
it('zoomOut zooms by 1 zoom level by default', function (done) {
2015-09-28 18:22:48 +08:00
map.once('zoomend', function () {
2015-06-08 23:47:08 +08:00
expect(map.getZoom()).to.eql(9);
expect(map.getCenter()).to.eql(center);
done();
});
map.zoomOut(null, {animate: false});
});
it('zoomIn ignores the zoomDelta option on non-any3d browsers', function (done) {
L.Browser.any3d = false;
map.options.zoomSnap = 0.25;
map.options.zoomDelta = 0.25;
2015-09-28 18:22:48 +08:00
map.once('zoomend', function () {
2015-06-08 23:47:08 +08:00
expect(map.getZoom()).to.eql(11);
expect(map.getCenter()).to.eql(center);
done();
});
map.zoomIn(null, {animate: false});
});
it('zoomIn respects the zoomDelta option on any3d browsers', function (done) {
L.Browser.any3d = true;
map.options.zoomSnap = 0.25;
map.options.zoomDelta = 0.25;
map.setView(center, 10);
2015-09-28 18:22:48 +08:00
map.once('zoomend', function () {
2015-06-08 23:47:08 +08:00
expect(map.getZoom()).to.eql(10.25);
expect(map.getCenter()).to.eql(center);
done();
});
map.zoomIn(null, {animate: false});
});
it('zoomOut respects the zoomDelta option on any3d browsers', function (done) {
L.Browser.any3d = true;
map.options.zoomSnap = 0.25;
map.options.zoomDelta = 0.25;
map.setView(center, 10);
2015-09-28 18:22:48 +08:00
map.once('zoomend', function () {
2015-06-08 23:47:08 +08:00
expect(map.getZoom()).to.eql(9.75);
expect(map.getCenter()).to.eql(center);
done();
});
map.zoomOut(null, {animate: false});
});
it('zoomIn snaps to zoomSnap on any3d browsers', function (done) {
map.options.zoomSnap = 0.25;
map.setView(center, 10);
2015-09-28 18:22:48 +08:00
map.once('zoomend', function () {
2015-06-08 23:47:08 +08:00
expect(map.getZoom()).to.eql(10.25);
expect(map.getCenter()).to.eql(center);
done();
});
L.Browser.any3d = true;
map.zoomIn(0.22, {animate: false});
});
it('zoomOut snaps to zoomSnap on any3d browsers', function (done) {
map.options.zoomSnap = 0.25;
map.setView(center, 10);
2015-09-28 18:22:48 +08:00
map.once('zoomend', function () {
2015-06-08 23:47:08 +08:00
expect(map.getZoom()).to.eql(9.75);
expect(map.getCenter()).to.eql(center);
done();
});
L.Browser.any3d = true;
map.zoomOut(0.22, {animate: false});
});
});
describe('#fitBounds', 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);
map.setView(center, 10);
});
2015-09-28 18:22:48 +08:00
afterEach(function () {
2015-06-08 23:47:08 +08:00
document.body.removeChild(map.getContainer());
});
it('Snaps zoom level to integer by default', function (done) {
2015-09-28 18:22:48 +08:00
map.once('zoomend', function () {
2015-06-08 23:47:08 +08:00
expect(map.getZoom()).to.eql(2);
expect(map.getCenter().equals(boundsCenter, 0.05)).to.eql(true);
done();
});
map.fitBounds(bounds, {animate: false});
});
it('Snaps zoom to zoomSnap on any3d browsers', function (done) {
map.options.zoomSnap = 0.25;
L.Browser.any3d = true;
2015-09-28 18:22:48 +08:00
map.once('zoomend', function () {
2015-06-08 23:47:08 +08:00
expect(map.getZoom()).to.eql(2.75);
expect(map.getCenter().equals(boundsCenter, 0.05)).to.eql(true);
done();
});
map.fitBounds(bounds, {animate: false});
});
it('Ignores zoomSnap on non-any3d browsers', function (done) {
map.options.zoomSnap = 0.25;
L.Browser.any3d = false;
2015-09-28 18:22:48 +08:00
map.once('zoomend', function () {
2015-06-08 23:47:08 +08:00
expect(map.getZoom()).to.eql(2);
expect(map.getCenter().equals(boundsCenter, 0.05)).to.eql(true);
done();
});
map.fitBounds(bounds, {animate: false});
});
2015-03-08 19:21:43 +08:00
});
2016-02-09 16:56:33 +08:00
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();
});
});
2015-03-14 05:08:50 +08:00
describe('#DOM events', function () {
var c, map;
beforeEach(function () {
c = document.createElement('div');
c.style.width = '400px';
c.style.height = '400px';
map = new L.Map(c);
map.setView(new L.LatLng(0, 0), 0);
document.body.appendChild(c);
});
afterEach(function () {
document.body.removeChild(c);
});
it("DOM events propagate from polygon to map", function () {
var spy = sinon.spy();
map.on("mousemove", spy);
var layer = new L.Polygon([[1, 2], [3, 4], [5, 6]]).addTo(map);
happen.mousemove(layer._path);
expect(spy.calledOnce).to.be.ok();
});
it("DOM events propagate from marker to map", function () {
var spy = sinon.spy();
map.on("mousemove", spy);
var layer = new L.Marker([1, 2]).addTo(map);
happen.mousemove(layer._icon);
expect(spy.calledOnce).to.be.ok();
});
it("DOM events fired on marker can be cancelled before being caught by the map", function () {
var mapSpy = sinon.spy();
var layerSpy = sinon.spy();
map.on("mousemove", mapSpy);
var layer = new L.Marker([1, 2]).addTo(map);
layer.on("mousemove", L.DomEvent.stopPropagation).on("mousemove", layerSpy);
happen.mousemove(layer._icon);
expect(layerSpy.calledOnce).to.be.ok();
expect(mapSpy.called).not.to.be.ok();
});
it("DOM events fired on polygon can be cancelled before being caught by the map", function () {
var mapSpy = sinon.spy();
var layerSpy = sinon.spy();
map.on("mousemove", mapSpy);
var layer = new L.Polygon([[1, 2], [3, 4], [5, 6]]).addTo(map);
layer.on("mousemove", L.DomEvent.stopPropagation).on("mousemove", layerSpy);
happen.mousemove(layer._path);
expect(layerSpy.calledOnce).to.be.ok();
expect(mapSpy.called).not.to.be.ok();
});
it("mouseout is forwarded if fired on the original target", function () {
2015-03-14 05:08:50 +08:00
var mapSpy = sinon.spy(),
2015-09-25 18:55:37 +08:00
layerSpy = sinon.spy(),
otherSpy = sinon.spy();
2015-03-14 05:08:50 +08:00
var layer = new L.Polygon([[1, 2], [3, 4], [5, 6]]).addTo(map);
var other = new L.Polygon([[10, 20], [30, 40], [50, 60]]).addTo(map);
map.on("mouseout", mapSpy);
layer.on("mouseout", layerSpy);
other.on("mouseout", otherSpy);
happen.mouseout(layer._path, {relatedTarget: map._container});
2015-03-14 05:08:50 +08:00
expect(mapSpy.called).not.to.be.ok();
expect(otherSpy.called).not.to.be.ok();
expect(layerSpy.calledOnce).to.be.ok();
});
it("mouseout is forwarded when using a DivIcon", function () {
var icon = L.divIcon({
html: "<p>this is text in a child element</p>",
iconSize: [100, 100]
});
var mapSpy = sinon.spy(),
layerSpy = sinon.spy(),
layer = L.marker([1, 2], {icon: icon}).addTo(map);
map.on("mouseout", mapSpy);
layer.on("mouseout", layerSpy);
happen.mouseout(layer._icon, {relatedTarget: map._container});
expect(mapSpy.called).not.to.be.ok();
expect(layerSpy.calledOnce).to.be.ok();
});
it("mouseout is not forwarded if relatedTarget is a target's child", function () {
var icon = L.divIcon({
html: "<p>this is text in a child element</p>",
iconSize: [100, 100]
});
var mapSpy = sinon.spy(),
layerSpy = sinon.spy(),
layer = L.marker([1, 2], {icon: icon}).addTo(map),
child = layer._icon.querySelector('p');
map.on("mouseout", mapSpy);
layer.on("mouseout", layerSpy);
happen.mouseout(layer._icon, {relatedTarget: child});
expect(mapSpy.called).not.to.be.ok();
expect(layerSpy.calledOnce).not.to.be.ok();
});
it("mouseout is not forwarded if fired on target's child", function () {
var icon = L.divIcon({
html: "<p>this is text in a child element</p>",
iconSize: [100, 100]
});
var mapSpy = sinon.spy(),
layerSpy = sinon.spy(),
layer = L.marker([1, 2], {icon: icon}).addTo(map),
child = layer._icon.querySelector('p');
map.on("mouseout", mapSpy);
layer.on("mouseout", layerSpy);
happen.mouseout(child, {relatedTarget: layer._icon});
expect(mapSpy.called).not.to.be.ok();
expect(layerSpy.calledOnce).not.to.be.ok();
});
it("mouseout is not forwarded to layers if fired on the map", function () {
2015-03-14 05:08:50 +08:00
var mapSpy = sinon.spy(),
2015-09-25 18:55:37 +08:00
layerSpy = sinon.spy(),
otherSpy = sinon.spy();
2015-03-14 05:08:50 +08:00
var layer = new L.Polygon([[1, 2], [3, 4], [5, 6]]).addTo(map);
var other = new L.Polygon([[10, 20], [30, 40], [50, 60]]).addTo(map);
map.on("mouseout", mapSpy);
layer.on("mouseout", layerSpy);
other.on("mouseout", otherSpy);
happen.mouseout(map._container);
expect(otherSpy.called).not.to.be.ok();
expect(layerSpy.called).not.to.be.ok();
expect(mapSpy.calledOnce).to.be.ok();
});
2015-06-24 14:31:59 +08:00
it("preclick is fired before click on marker and map", function () {
var called = 0;
var layer = new L.Marker([1, 2]).addTo(map);
layer.on("preclick", function (e) {
expect(called++).to.eql(0);
expect(e.latlng).to.ok();
});
layer.on("click", function (e) {
expect(called++).to.eql(2);
expect(e.latlng).to.ok();
});
map.on("preclick", function (e) {
expect(called++).to.eql(1);
expect(e.latlng).to.ok();
});
map.on("click", function (e) {
expect(called++).to.eql(3);
expect(e.latlng).to.ok();
});
happen.click(layer._icon);
});
2015-03-14 05:08:50 +08:00
});
2015-09-25 18:40:21 +08:00
describe('#getScaleZoom && #getZoomScale', function () {
it("convert zoom to scale and viceversa and return the same values", function () {
var toZoom = 6.25;
var fromZoom = 8.5;
var scale = map.getScaleZoom(toZoom, fromZoom);
expect(Math.round(map.getZoomScale(scale, fromZoom) * 100) / 100).to.eql(toZoom);
});
});
2016-02-09 16:56:33 +08:00
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);
});
});
});