2013-06-12 11:55:03 +08:00
|
|
|
describe("Marker", function () {
|
2013-06-29 04:22:20 +08:00
|
|
|
var map,
|
2015-09-25 18:55:37 +08:00
|
|
|
spy,
|
|
|
|
div,
|
|
|
|
icon1,
|
|
|
|
icon2;
|
2013-09-12 18:14:00 +08:00
|
|
|
|
2013-06-12 11:55:03 +08:00
|
|
|
beforeEach(function () {
|
2015-03-03 01:33:39 +08:00
|
|
|
div = document.createElement('div');
|
|
|
|
div.style.height = '100px';
|
|
|
|
document.body.appendChild(div);
|
|
|
|
|
|
|
|
map = L.map(div).setView([0, 0], 0);
|
2013-09-12 18:14:00 +08:00
|
|
|
icon1 = new L.Icon.Default();
|
|
|
|
icon2 = new L.Icon.Default({
|
2016-08-22 22:40:03 +08:00
|
|
|
iconUrl: icon1.options.iconUrl + '?2',
|
|
|
|
shadowUrl: icon1.options.shadowUrl + '?2'
|
2013-09-12 18:14:00 +08:00
|
|
|
});
|
2013-06-12 11:55:03 +08:00
|
|
|
});
|
|
|
|
|
2015-03-03 01:33:39 +08:00
|
|
|
afterEach(function () {
|
|
|
|
document.body.removeChild(div);
|
|
|
|
});
|
|
|
|
|
2013-06-12 11:55:03 +08:00
|
|
|
describe("#setIcon", function () {
|
2016-06-01 22:24:24 +08:00
|
|
|
|
|
|
|
it("set the correct x and y size attributes", function () {
|
|
|
|
var expectedX = 96;
|
|
|
|
var expectedY = 100;
|
|
|
|
var sizedIcon = new L.Icon.Default({
|
2016-09-27 22:54:46 +08:00
|
|
|
iconUrl: icon1.options.iconUrl + '?3',
|
2016-06-01 22:24:24 +08:00
|
|
|
iconSize: [expectedX, expectedY]
|
|
|
|
});
|
|
|
|
|
|
|
|
var marker = new L.Marker([0, 0], {icon: sizedIcon});
|
|
|
|
map.addLayer(marker);
|
|
|
|
|
|
|
|
var icon = marker._icon;
|
|
|
|
|
|
|
|
expect(icon.style.width).to.be(expectedX + 'px');
|
|
|
|
expect(icon.style.height).to.be(expectedY + 'px');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("set the correct x and y size attributes passing only one value", function () {
|
|
|
|
var expectedXY = 96;
|
|
|
|
var sizedIcon = new L.Icon.Default({
|
2016-09-27 22:54:46 +08:00
|
|
|
iconUrl: icon1.options.iconUrl + '?3',
|
2016-06-01 22:24:24 +08:00
|
|
|
iconSize: expectedXY
|
|
|
|
});
|
|
|
|
|
|
|
|
var marker = new L.Marker([0, 0], {icon: sizedIcon});
|
|
|
|
map.addLayer(marker);
|
|
|
|
|
|
|
|
var icon = marker._icon;
|
|
|
|
|
|
|
|
expect(icon.style.width).to.be(expectedXY + 'px');
|
|
|
|
expect(icon.style.height).to.be(expectedXY + 'px');
|
|
|
|
});
|
|
|
|
|
2016-06-10 21:36:07 +08:00
|
|
|
it("set the correct x and y size attributes passing a L.Point instance", function () {
|
|
|
|
var expectedXY = 96;
|
|
|
|
var sizedIcon = new L.Icon.Default({
|
2016-09-27 22:54:46 +08:00
|
|
|
iconUrl: icon1.options.iconUrl + '?3',
|
2016-06-10 21:36:07 +08:00
|
|
|
iconSize: L.point(expectedXY, expectedXY)
|
|
|
|
});
|
|
|
|
|
|
|
|
var marker = new L.Marker([0, 0], {icon: sizedIcon});
|
|
|
|
map.addLayer(marker);
|
|
|
|
|
|
|
|
var icon = marker._icon;
|
|
|
|
|
|
|
|
expect(icon.style.width).to.be(expectedXY + 'px');
|
|
|
|
expect(icon.style.height).to.be(expectedXY + 'px');
|
|
|
|
});
|
|
|
|
|
2017-06-25 20:11:57 +08:00
|
|
|
it("changes the icon to another image while re-using the IMG element", function () {
|
2013-09-12 18:14:00 +08:00
|
|
|
var marker = new L.Marker([0, 0], {icon: icon1});
|
2013-06-12 11:55:03 +08:00
|
|
|
map.addLayer(marker);
|
2013-09-12 18:14:00 +08:00
|
|
|
|
2013-06-12 11:55:03 +08:00
|
|
|
var beforeIcon = marker._icon;
|
2013-09-12 18:14:00 +08:00
|
|
|
marker.setIcon(icon2);
|
2013-06-12 11:55:03 +08:00
|
|
|
var afterIcon = marker._icon;
|
2013-09-12 18:14:00 +08:00
|
|
|
|
2017-06-25 20:11:57 +08:00
|
|
|
expect(beforeIcon).to.be(afterIcon); // Check that the <IMG> element is re-used
|
2013-09-12 18:14:00 +08:00
|
|
|
expect(afterIcon.src).to.contain(icon2._getIconUrl('icon'));
|
2013-06-12 11:55:03 +08:00
|
|
|
});
|
|
|
|
|
2014-06-30 07:27:04 +08:00
|
|
|
it("preserves draggability", function () {
|
|
|
|
var marker = new L.Marker([0, 0], {icon: icon1});
|
|
|
|
map.addLayer(marker);
|
|
|
|
|
|
|
|
marker.dragging.disable();
|
|
|
|
marker.setIcon(icon2);
|
|
|
|
|
|
|
|
expect(marker.dragging.enabled()).to.be(false);
|
|
|
|
|
|
|
|
marker.dragging.enable();
|
|
|
|
|
|
|
|
marker.setIcon(icon1);
|
|
|
|
|
|
|
|
expect(marker.dragging.enabled()).to.be(true);
|
2015-01-20 02:07:04 +08:00
|
|
|
|
2015-02-14 00:53:23 +08:00
|
|
|
map.removeLayer(marker);
|
2015-01-20 02:07:04 +08:00
|
|
|
map.addLayer(marker);
|
|
|
|
|
|
|
|
expect(marker.dragging.enabled()).to.be(true);
|
2015-02-14 00:53:23 +08:00
|
|
|
|
|
|
|
map.removeLayer(marker);
|
|
|
|
// Dragging is still enabled, we should be able to disable it,
|
|
|
|
// even if marker is off the map.
|
2017-02-02 17:14:39 +08:00
|
|
|
expect(marker.dragging).to.be(undefined);
|
|
|
|
marker.options.draggable = false;
|
2015-02-14 00:53:23 +08:00
|
|
|
map.addLayer(marker);
|
2017-02-02 17:14:39 +08:00
|
|
|
|
|
|
|
map.removeLayer(marker);
|
|
|
|
|
|
|
|
// We should also be able to enable dragging while off the map
|
|
|
|
expect(marker.dragging).to.be(undefined);
|
|
|
|
marker.options.draggable = true;
|
|
|
|
|
|
|
|
map.addLayer(marker);
|
|
|
|
expect(marker.dragging.enabled()).to.be(true);
|
2014-06-30 07:27:04 +08:00
|
|
|
});
|
|
|
|
|
2017-06-25 20:11:57 +08:00
|
|
|
it("changes the DivIcon to another DivIcon, while re-using the DIV element", function () {
|
2015-01-29 01:32:27 +08:00
|
|
|
var marker = new L.Marker([0, 0], {icon: new L.DivIcon({html: 'Inner1Text'})});
|
2013-06-12 11:55:03 +08:00
|
|
|
map.addLayer(marker);
|
2013-09-12 18:14:00 +08:00
|
|
|
|
2013-06-12 11:55:03 +08:00
|
|
|
var beforeIcon = marker._icon;
|
2015-01-29 01:32:27 +08:00
|
|
|
marker.setIcon(new L.DivIcon({html: 'Inner2Text'}));
|
2013-06-12 11:55:03 +08:00
|
|
|
var afterIcon = marker._icon;
|
2013-09-12 18:14:00 +08:00
|
|
|
|
2017-06-25 20:11:57 +08:00
|
|
|
expect(beforeIcon).to.be(afterIcon); // Check that the <DIV> element is re-used
|
2013-06-12 11:55:03 +08:00
|
|
|
expect(afterIcon.innerHTML).to.contain('Inner2Text');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("removes text when changing to a blank DivIcon", function () {
|
2015-01-29 01:32:27 +08:00
|
|
|
var marker = new L.Marker([0, 0], {icon: new L.DivIcon({html: 'Inner1Text'})});
|
2013-06-12 11:55:03 +08:00
|
|
|
map.addLayer(marker);
|
2013-09-12 18:14:00 +08:00
|
|
|
|
2013-06-12 11:55:03 +08:00
|
|
|
marker.setIcon(new L.DivIcon());
|
|
|
|
var afterIcon = marker._icon;
|
2013-09-12 18:14:00 +08:00
|
|
|
|
2013-06-12 11:55:03 +08:00
|
|
|
expect(marker._icon.innerHTML).to.not.contain('Inner1Text');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("changes a DivIcon to an image", function () {
|
2015-01-29 01:32:27 +08:00
|
|
|
var marker = new L.Marker([0, 0], {icon: new L.DivIcon({html: 'Inner1Text'})});
|
2013-06-12 11:55:03 +08:00
|
|
|
map.addLayer(marker);
|
2013-06-12 12:18:20 +08:00
|
|
|
var oldIcon = marker._icon;
|
2013-09-12 18:14:00 +08:00
|
|
|
|
|
|
|
marker.setIcon(icon1);
|
|
|
|
|
2017-06-25 20:11:57 +08:00
|
|
|
expect(oldIcon).to.not.be(marker._icon); // Check that the _icon is NOT re-used
|
2013-06-12 12:18:20 +08:00
|
|
|
expect(oldIcon.parentNode).to.be(null);
|
2013-09-12 18:14:00 +08:00
|
|
|
|
2014-03-20 02:58:19 +08:00
|
|
|
if (L.Browser.retina) {
|
|
|
|
expect(marker._icon.src).to.contain('marker-icon-2x.png');
|
|
|
|
} else {
|
|
|
|
expect(marker._icon.src).to.contain('marker-icon.png');
|
|
|
|
}
|
2013-06-12 12:18:20 +08:00
|
|
|
expect(marker._icon.parentNode).to.be(map._panes.markerPane);
|
2013-06-12 11:55:03 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
it("changes an image to a DivIcon", function () {
|
2013-09-12 18:14:00 +08:00
|
|
|
var marker = new L.Marker([0, 0], {icon: icon1});
|
2013-06-12 11:55:03 +08:00
|
|
|
map.addLayer(marker);
|
2013-06-12 12:18:20 +08:00
|
|
|
var oldIcon = marker._icon;
|
|
|
|
|
2015-01-29 01:32:27 +08:00
|
|
|
marker.setIcon(new L.DivIcon({html: 'Inner1Text'}));
|
2013-09-12 18:14:00 +08:00
|
|
|
|
2017-06-25 20:11:57 +08:00
|
|
|
expect(oldIcon).to.not.be(marker._icon); // Check that the _icon is NOT re-used
|
2013-06-12 12:18:20 +08:00
|
|
|
expect(oldIcon.parentNode).to.be(null);
|
2013-09-12 18:14:00 +08:00
|
|
|
|
2013-06-12 12:05:42 +08:00
|
|
|
expect(marker._icon.innerHTML).to.contain('Inner1Text');
|
2013-06-12 12:18:20 +08:00
|
|
|
expect(marker._icon.parentNode).to.be(map._panes.markerPane);
|
2013-06-12 11:55:03 +08:00
|
|
|
});
|
|
|
|
|
2013-06-24 04:54:40 +08:00
|
|
|
it("reuses the icon/shadow when changing icon", function () {
|
2015-01-29 01:32:27 +08:00
|
|
|
var marker = new L.Marker([0, 0], {icon: icon1});
|
2013-06-24 04:54:40 +08:00
|
|
|
map.addLayer(marker);
|
|
|
|
var oldIcon = marker._icon;
|
|
|
|
var oldShadow = marker._shadow;
|
|
|
|
|
2013-09-12 18:14:00 +08:00
|
|
|
marker.setIcon(icon2);
|
2013-06-24 04:54:40 +08:00
|
|
|
|
|
|
|
expect(oldIcon).to.be(marker._icon);
|
|
|
|
expect(oldShadow).to.be(marker._shadow);
|
|
|
|
|
|
|
|
expect(marker._icon.parentNode).to.be(map._panes.markerPane);
|
|
|
|
expect(marker._shadow.parentNode).to.be(map._panes.shadowPane);
|
|
|
|
});
|
2018-01-01 17:30:16 +08:00
|
|
|
|
|
|
|
it("sets the alt attribute to an empty string when no alt text is passed", function () {
|
|
|
|
var marker = L.marker([0, 0], {icon: icon1});
|
|
|
|
map.addLayer(marker);
|
|
|
|
var icon = marker._icon;
|
|
|
|
expect(icon.hasAttribute('alt')).to.be(true);
|
|
|
|
expect(icon.alt).to.be('');
|
|
|
|
});
|
|
|
|
|
|
|
|
it("doesn't set the alt attribute for DivIcons", function () {
|
|
|
|
var marker = L.marker([0, 0], {icon: L.divIcon(), alt: 'test'});
|
|
|
|
map.addLayer(marker);
|
|
|
|
var icon = marker._icon;
|
|
|
|
expect(icon.hasAttribute('alt')).to.be(false);
|
|
|
|
});
|
2013-06-12 11:55:03 +08:00
|
|
|
});
|
2014-01-29 06:33:08 +08:00
|
|
|
|
|
|
|
describe("#setLatLng", function () {
|
|
|
|
it("fires a move event", function () {
|
|
|
|
|
2015-01-29 01:32:27 +08:00
|
|
|
var marker = new L.Marker([0, 0], {icon: icon1});
|
2014-01-29 06:33:08 +08:00
|
|
|
map.addLayer(marker);
|
|
|
|
|
|
|
|
var beforeLatLng = marker._latlng;
|
|
|
|
var afterLatLng = new L.LatLng(1, 2);
|
|
|
|
|
|
|
|
var eventArgs = null;
|
|
|
|
marker.on('move', function (e) {
|
|
|
|
eventArgs = e;
|
|
|
|
});
|
|
|
|
|
|
|
|
marker.setLatLng(afterLatLng);
|
|
|
|
|
|
|
|
expect(eventArgs).to.not.be(null);
|
2014-01-29 09:51:55 +08:00
|
|
|
expect(eventArgs.oldLatLng).to.be(beforeLatLng);
|
2014-01-29 06:33:08 +08:00
|
|
|
expect(eventArgs.latlng).to.be(afterLatLng);
|
|
|
|
expect(marker.getLatLng()).to.be(afterLatLng);
|
|
|
|
});
|
|
|
|
});
|
2015-03-03 01:33:39 +08:00
|
|
|
|
|
|
|
describe('events', function () {
|
|
|
|
it('fires click event when clicked', function () {
|
|
|
|
var spy = sinon.spy();
|
|
|
|
|
|
|
|
var marker = L.marker([0, 0]).addTo(map);
|
|
|
|
|
|
|
|
marker.on('click', spy);
|
|
|
|
happen.click(marker._icon);
|
|
|
|
|
|
|
|
expect(spy.called).to.be.ok();
|
|
|
|
});
|
2015-03-07 19:28:16 +08:00
|
|
|
|
|
|
|
it('fires click event when clicked with DivIcon', function () {
|
|
|
|
var spy = sinon.spy();
|
|
|
|
|
|
|
|
var marker = L.marker([0, 0], {icon: new L.DivIcon()}).addTo(map);
|
|
|
|
|
|
|
|
marker.on('click', spy);
|
|
|
|
happen.click(marker._icon);
|
|
|
|
|
|
|
|
expect(spy.called).to.be.ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fires click event when clicked on DivIcon child element', function () {
|
|
|
|
var spy = sinon.spy();
|
|
|
|
|
|
|
|
var marker = L.marker([0, 0], {icon: new L.DivIcon({html: '<img src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" />'})}).addTo(map);
|
|
|
|
|
|
|
|
marker.on('click', spy);
|
|
|
|
|
|
|
|
happen.click(marker._icon);
|
|
|
|
expect(spy.called).to.be.ok();
|
|
|
|
|
|
|
|
happen.click(marker._icon.querySelector('img'));
|
|
|
|
expect(spy.calledTwice).to.be.ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
it('fires click event when clicked on DivIcon child element set using setIcon', function () {
|
|
|
|
var spy = sinon.spy();
|
|
|
|
|
|
|
|
var marker = L.marker([0, 0]).addTo(map);
|
|
|
|
marker.setIcon(new L.DivIcon({html: '<img src="data:image/gif;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" />'}));
|
|
|
|
|
|
|
|
marker.on('click', spy);
|
|
|
|
|
|
|
|
happen.click(marker._icon);
|
|
|
|
expect(spy.called).to.be.ok();
|
|
|
|
|
|
|
|
happen.click(marker._icon.querySelector('img'));
|
|
|
|
expect(spy.calledTwice).to.be.ok();
|
|
|
|
});
|
|
|
|
|
2015-07-08 17:34:19 +08:00
|
|
|
it("do not propagate click event", function () {
|
|
|
|
var spy = sinon.spy();
|
|
|
|
var spy2 = sinon.spy();
|
|
|
|
var mapSpy = sinon.spy();
|
|
|
|
var marker = new L.Marker(new L.LatLng(55.8, 37.6));
|
|
|
|
map.addLayer(marker);
|
|
|
|
marker.on('click', spy);
|
|
|
|
marker.on('click', spy2);
|
|
|
|
map.on('click', mapSpy);
|
|
|
|
happen.click(marker._icon);
|
|
|
|
expect(spy.called).to.be.ok();
|
|
|
|
expect(spy2.called).to.be.ok();
|
|
|
|
expect(mapSpy.called).not.to.be.ok();
|
|
|
|
});
|
|
|
|
|
|
|
|
it("do not propagate dblclick event", function () {
|
|
|
|
var spy = sinon.spy();
|
|
|
|
var spy2 = sinon.spy();
|
|
|
|
var mapSpy = sinon.spy();
|
|
|
|
var marker = new L.Marker(new L.LatLng(55.8, 37.6));
|
|
|
|
map.addLayer(marker);
|
|
|
|
marker.on('dblclick', spy);
|
|
|
|
marker.on('dblclick', spy2);
|
|
|
|
map.on('dblclick', mapSpy);
|
|
|
|
happen.dblclick(marker._icon);
|
|
|
|
expect(spy.called).to.be.ok();
|
|
|
|
expect(spy2.called).to.be.ok();
|
|
|
|
expect(mapSpy.called).not.to.be.ok();
|
|
|
|
});
|
|
|
|
|
2016-07-01 17:14:22 +08:00
|
|
|
it("do not catch event if it does not listen to it", function (done) {
|
2015-07-10 05:00:52 +08:00
|
|
|
var marker = new L.Marker([55, 37]);
|
|
|
|
map.addLayer(marker);
|
|
|
|
marker.once('mousemove', function (e) {
|
|
|
|
// It should be the marker coordinates
|
2016-07-01 17:14:22 +08:00
|
|
|
expect(e.latlng.equals(marker.getLatLng())).to.be.equal(true);
|
2015-07-10 05:00:52 +08:00
|
|
|
});
|
|
|
|
happen.mousemove(marker._icon);
|
2016-07-01 17:14:22 +08:00
|
|
|
|
2015-07-10 05:00:52 +08:00
|
|
|
map.once('mousemove', function (e) {
|
|
|
|
// It should be the mouse coordinates, not the marker ones
|
2016-07-01 17:14:22 +08:00
|
|
|
expect(e.latlng.equals(marker.getLatLng())).to.be.equal(false);
|
|
|
|
done();
|
2015-07-10 05:00:52 +08:00
|
|
|
});
|
|
|
|
happen.mousemove(marker._icon);
|
|
|
|
});
|
|
|
|
|
2015-03-03 01:33:39 +08:00
|
|
|
});
|
2013-09-12 18:14:00 +08:00
|
|
|
});
|