Leaflet/spec/suites/layer/marker/MarkerSpec.js

118 lines
3.3 KiB
JavaScript
Raw Normal View History

describe("Marker", function () {
var map,
2013-09-12 18:14:00 +08:00
spy,
icon1,
icon2;
beforeEach(function () {
map = L.map(document.createElement('div')).setView([0, 0], 0);
2013-09-12 18:14:00 +08:00
icon1 = new L.Icon.Default();
icon2 = new L.Icon.Default({
iconUrl: icon1._getIconUrl('icon') + '?2',
shadowUrl: icon1._getIconUrl('shadow') + '?2'
});
});
describe("#setIcon", function () {
it("changes the icon to another image", function () {
2013-09-12 18:14:00 +08:00
var marker = new L.Marker([0, 0], {icon: icon1});
map.addLayer(marker);
2013-09-12 18:14:00 +08:00
var beforeIcon = marker._icon;
2013-09-12 18:14:00 +08:00
marker.setIcon(icon2);
var afterIcon = marker._icon;
2013-09-12 18:14:00 +08:00
expect(beforeIcon).to.be(afterIcon);
2013-09-12 18:14:00 +08:00
expect(afterIcon.src).to.contain(icon2._getIconUrl('icon'));
});
it("changes the icon to another DivIcon", function () {
var marker = new L.Marker([0, 0], {icon: new L.DivIcon({html: 'Inner1Text' }) });
map.addLayer(marker);
2013-09-12 18:14:00 +08:00
var beforeIcon = marker._icon;
marker.setIcon(new L.DivIcon({html: 'Inner2Text' }));
var afterIcon = marker._icon;
2013-09-12 18:14:00 +08:00
expect(beforeIcon).to.be(afterIcon);
expect(afterIcon.innerHTML).to.contain('Inner2Text');
});
it("removes text when changing to a blank DivIcon", function () {
var marker = new L.Marker([0, 0], {icon: new L.DivIcon({html: 'Inner1Text' }) });
map.addLayer(marker);
2013-09-12 18:14:00 +08:00
marker.setIcon(new L.DivIcon());
var afterIcon = marker._icon;
2013-09-12 18:14:00 +08:00
expect(marker._icon.innerHTML).to.not.contain('Inner1Text');
});
it("changes a DivIcon to an image", function () {
var marker = new L.Marker([0, 0], {icon: new L.DivIcon({html: 'Inner1Text' }) });
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);
2013-06-12 12:18:20 +08:00
expect(oldIcon).to.not.be(marker._icon);
expect(oldIcon.parentNode).to.be(null);
2013-09-12 18:14:00 +08:00
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);
});
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});
map.addLayer(marker);
2013-06-12 12:18:20 +08:00
var oldIcon = marker._icon;
marker.setIcon(new L.DivIcon({html: 'Inner1Text' }));
2013-09-12 18:14:00 +08:00
2013-06-12 12:18:20 +08:00
expect(oldIcon).to.not.be(marker._icon);
expect(oldIcon.parentNode).to.be(null);
2013-09-12 18:14:00 +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);
});
it("reuses the icon/shadow when changing icon", function () {
2013-09-12 18:14:00 +08:00
var marker = new L.Marker([0, 0], { icon: icon1});
map.addLayer(marker);
var oldIcon = marker._icon;
var oldShadow = marker._shadow;
2013-09-12 18:14:00 +08:00
marker.setIcon(icon2);
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);
});
});
describe("#setLatLng", function () {
it("fires a move event", function () {
var marker = new L.Marker([0, 0], { icon: icon1 });
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);
expect(eventArgs.oldLatLng).to.be(beforeLatLng);
expect(eventArgs.latlng).to.be(afterLatLng);
expect(marker.getLatLng()).to.be(afterLatLng);
});
});
2013-09-12 18:14:00 +08:00
});