Leaflet/spec/suites/map/MapSpec.js

368 lines
11 KiB
JavaScript
Raw Normal View History

describe("Map", function () {
var map,
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),
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),
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'),
map = new L.Map(container);
expect(function () {
new 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 () {
new 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),
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();
});
});
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);
});
});
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("#getMinZoom and #getMaxZoom", function () {
2013-02-27 03:31:23 +08:00
it("minZoom and maxZoom options overrides any minZoom and maxZoom set on layers", function () {
var c = document.createElement('div'),
map = L.map(c, { minZoom: 5, maxZoom: 10 });
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 }).addTo(map);
L.tileLayer("{z}{x}{y}", { minZoom:5, maxZoom: 15 }).addTo(map);
2013-03-02 05:49:20 +08:00
expect(map.getMinZoom()).to.be(5);
expect(map.getMaxZoom()).to.be(10);
2013-02-27 03:31:23 +08:00
});
});
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();
});
it("does not call layer.onAdd if the layer is removed before the map becomes ready", function () {
var layer = { onAdd: sinon.spy(), onRemove: sinon.spy() };
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 = { onAdd: sinon.spy() },
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 = { onAdd: sinon.spy() },
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 = { onAdd: sinon.spy(), onRemove: sinon.spy() },
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 = { onAdd: sinon.spy(), onRemove: sinon.spy() };
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();
2013-02-27 03:31:23 +08:00
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 }).addTo(map);
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
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();
2013-02-27 03:31:23 +08:00
L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 }).addTo(map);
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
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();
2013-02-27 03:31:23 +08:00
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 = { 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();
});
it("fires a layerremove event if the map is ready", function () {
var layer = { onAdd: sinon.spy(), onRemove: sinon.spy() },
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 = { onAdd: sinon.spy(), onRemove: sinon.spy() },
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 = { onAdd: sinon.spy(), onRemove: sinon.spy() },
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 = { onAdd: sinon.spy(), onRemove: sinon.spy() };
map.on('layerremove', function() {
expect(map.hasLayer(layer)).not.to.be.ok();
done();
});
map.setView([0, 0], 0);
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();
2013-02-27 03:31:23 +08:00
var tl = L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 }).addTo(map);
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(),
tl = L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 10 }).addTo(map),
2013-02-27 03:31:23 +08:00
t2 = L.tileLayer("{z}{x}{y}", { minZoom:0, maxZoom: 15 }).addTo(map);
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(){
2013-02-27 03:31:23 +08:00
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),
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),
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
});
});
});