Skip to content

Commit

Permalink
Cleanup all tests (Leaflet#7852)
Browse files Browse the repository at this point in the history
* Cleanup Map

* Cleanup Map.BoxZoom

* Cleanup Map.DragSpec

* Add SpecHelper function from commit before

* Cleanup Map.KeyboardSpec

* Cleanup Map.TapHoldSpec

* Cleanup Map.TouchZoomSpec

* Cleanup GeoJSONSpec

* Cleanup ImageOverlaySpec

* Cleanup PopupSpec

* Cleanup TooltipSpec

* Cleanup CanvasSpec

* Cleanup CircleMarkerSpec

* Cleanup CircleSpec

* Cleanup PathSpec

* Cleanup PolygonSpec

* Cleanup PolylineGeometrySpec

* Remove PolylineGeometrySpec

* Remove PolylineGeometrySpec from index.html

* Cleanup PolylineSpec

* Cleanup GridLayerSpec

* Cleanup TileLayerSpec

* Cleanup MarkerSpec

* Cleanup Marker.DragSpec

* Cleanup Icon.DefaultSpec

* Cleanup TransformationSpec

* Cleanup BoundsSpec

* Cleanup LineUtilSpec

* Cleanup PointSpec

* Cleanup PolyUtilSpec

* Cleanup ProjectionSpec

* Cleanup LatLngSpec

* Cleanup LatLngBoundsSpec

* Cleanup CRSSpec

* Cleanup ControlSpec

* Cleanup Control.LayersSpec

* Cleanup Control.AttributionSpec

* Fix some errors - global leaks & map.remove twice

* Fix lint

* Fix error

* Fix noConflict, tabs, createContainer with height & width

* noConflict was not working because rollup already exported the L var

* Replace createMapContainer
  • Loading branch information
Falke-Design authored Mar 18, 2022
1 parent 0ea4073 commit 7419c9c
Show file tree
Hide file tree
Showing 38 changed files with 919 additions and 963 deletions.
6 changes: 4 additions & 2 deletions spec/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"rules": {
"quotes": 0,
"no-console": 0,
"no-console": 0
},
"env": {
"mocha": true
Expand All @@ -11,6 +11,8 @@
"sinon": false,
"happen": false,
"Hand": false,
"touchEventType": false /* defined in SpecHelper.js */
"touchEventType": false, /* defined in SpecHelper.js */
"createContainer": false, /* defined in SpecHelper.js */
"removeMapContainer": false /* defined in SpecHelper.js */
}
}
20 changes: 17 additions & 3 deletions spec/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<title>Leaflet Spec Runner</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="../node_modules/mocha/mocha.css">
<link rel="stylesheet" type="text/css" href="../dist/leaflet.css">
<link rel="stylesheet" type="text/css" href="../dist/leaflet.css">
</head>
<body>
<div id="mocha"></div>
Expand All @@ -19,10 +19,23 @@
<script>
// Trick Leaflet into believing we have a touchscreen (for desktop)
if (window.TouchEvent) { window.ontouchstart = function(){} };
var oldL = window.L;
</script>

<script src="../dist/leaflet-src.js"></script>

<script>
// noConflict is defined in the rollup-config or in karma.conf.js, both are nor called while executing this
L.noConflict = function() {
window.L = oldL;
return this;
};

// sets the default imagePath to the project path like "/Leaflet/dist/images/"
// same as in after.js but not static
L.Icon.Default.imagePath = '/' + window.location.pathname.split('/')[1] + '/dist/images/';
</script>

<script class="mocha-init">
mocha.setup('bdd');
mocha.checkLeaks();
Expand Down Expand Up @@ -89,7 +102,6 @@
<script src="suites/layer/vector/PathSpec.js"></script>
<script src="suites/layer/vector/PolygonSpec.js"></script>
<script src="suites/layer/vector/PolylineSpec.js"></script>
<script src="suites/layer/vector/PolylineGeometrySpec.js"></script>
<script src="suites/layer/vector/CanvasSpec.js"></script>

<!-- /map -->
Expand All @@ -103,7 +115,9 @@
<script src="suites/map/handler/Map.TapHoldSpec.js"></script>

<script class="mocha-exec">
window.mocha.run();
window.mocha
.globals([ '_leaflet_events' ]) // acceptable globals - fixing gloabl leaks
.run();
</script>
</body>
</html>
24 changes: 24 additions & 0 deletions spec/suites/SpecHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,5 +79,29 @@ var touchEventType = L.Browser.touchNative ? 'touch' : 'pointer'; // eslint-disa
// Note: this override is needed to workaround prosthetic-hand fail,
// see https://github.com/Leaflet/prosthetic-hand/issues/14

function createContainer(width, height) { /* eslint-disable-line no-unused-vars */
width = width ? width : '400px';
height = height ? height : '400px';
var container = document.createElement("div");
container.style.position = 'absolute';
container.style.top = '0px';
container.style.left = '0px';
container.style.height = height;
container.style.width = width;
container.style.opacity = '0.4';
document.body.appendChild(container);

return container;
}

function removeMapContainer(map, container) { /* eslint-disable-line no-unused-vars */
if (map) {
map.remove();
}
if (container) {
document.body.removeChild(container);
}
}

console.log('L.Browser.pointer', L.Browser.pointer);
console.log('L.Browser.touchNative', L.Browser.touchNative);
54 changes: 30 additions & 24 deletions spec/suites/control/Control.AttributionSpec.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
describe("Control.Attribution", function () {
var map, control, container;
var map, control, container, controlContainer;

beforeEach(function () {
map = L.map(document.createElement('div'));
container = container = createContainer();
map = L.map(container);

control = new L.Control.Attribution({
prefix: 'prefix'
}).addTo(map);
map.setView([0, 0], 1);
container = control.getContainer();
controlContainer = control.getContainer();
});

afterEach(function () {
removeMapContainer(map, container);
});

function dummyLayer() {
Expand All @@ -18,25 +24,25 @@ describe("Control.Attribution", function () {
}

it("contains just prefix if no attributions added", function () {
expect(container.innerHTML).to.eql('prefix');
expect(controlContainer.innerHTML).to.eql('prefix');
});

describe('#addAttribution', function () {
it('adds one attribution correctly', function () {
control.addAttribution('foo');
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
});

it('adds no duplicate attributions', function () {
control.addAttribution('foo');
control.addAttribution('foo');
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
});

it('adds several attributions listed with comma', function () {
control.addAttribution('foo');
control.addAttribution('bar');
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo, bar');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo, bar');
});
});

Expand All @@ -45,7 +51,7 @@ describe("Control.Attribution", function () {
control.addAttribution('foo');
control.addAttribution('bar');
control.removeAttribution('foo');
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> bar');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> bar');
});
it('does nothing if removing attribution that was not present', function () {
control.addAttribution('foo');
Expand All @@ -54,14 +60,14 @@ describe("Control.Attribution", function () {
control.removeAttribution('baz');
control.removeAttribution('baz');
control.removeAttribution('');
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
});
});

describe('#setPrefix', function () {
it('changes prefix', function () {
control.setPrefix('bla');
expect(container.innerHTML).to.eql('bla');
expect(controlContainer.innerHTML).to.eql('bla');
});
});

Expand All @@ -81,20 +87,20 @@ describe("Control.Attribution", function () {
barLayer.getAttribution = function () { return 'bar'; };
bazLayer.getAttribution = function () { return 'baz'; };

expect(container.innerHTML).to.eql('prefix');
expect(controlContainer.innerHTML).to.eql('prefix');
map.addLayer(fooLayer);
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
map.addLayer(barLayer);
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo, bar');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo, bar');
map.addLayer(bazLayer);
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo, bar, baz');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo, bar, baz');

map.removeLayer(fooLayer);
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> bar, baz');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> bar, baz');
map.removeLayer(barLayer);
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> baz');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> baz');
map.removeLayer(bazLayer);
expect(container.innerHTML).to.eql('prefix');
expect(controlContainer.innerHTML).to.eql('prefix');
});

it('keeps count of duplicated attributions', function () {
Expand All @@ -105,20 +111,20 @@ describe("Control.Attribution", function () {
fo2Layer.getAttribution = function () { return 'foo'; };
fo3Layer.getAttribution = function () { return 'foo'; };

expect(container.innerHTML).to.eql('prefix');
expect(controlContainer.innerHTML).to.eql('prefix');
map.addLayer(fooLayer);
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
map.addLayer(fo2Layer);
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
map.addLayer(fo3Layer);
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');

map.removeLayer(fooLayer);
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
map.removeLayer(fo2Layer);
expect(container.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
expect(controlContainer.innerHTML).to.eql('prefix <span aria-hidden="true">|</span> foo');
map.removeLayer(fo3Layer);
expect(container.innerHTML).to.eql('prefix');
expect(controlContainer.innerHTML).to.eql('prefix');
});
});
});
25 changes: 12 additions & 13 deletions spec/suites/control/Control.LayersSpec.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
describe("Control.Layers", function () {
var div, map;
var container, map;

beforeEach(function () {
div = document.createElement('div');
document.body.appendChild(div);
map = L.map(div);
container = container = createContainer();
map = L.map(container);

map.setView([0, 0], 14);
});

afterEach(function () {
map.remove();
document.body.removeChild(div);
removeMapContainer(map, container);
});

describe("baselayerchange event", function () {
Expand Down Expand Up @@ -242,8 +241,8 @@ describe("Control.Layers", function () {
// Need to create a DIV with specified height and insert it into DOM, so that the browser
// gives it an actual size.
map.remove();
div.style.height = div.style.width = '200px';
map = L.map(div);
container.style.height = container.style.width = '200px';
map = L.map(container);

for (; i < 20; i += 1) {
// Default text size: 12px => 12 * 20 = 240px height (not even considering padding/margin).
Expand All @@ -252,8 +251,8 @@ describe("Control.Layers", function () {

layersCtrl.addTo(map);

expect(div.clientHeight).to.be.greaterThan(0); // Make sure first that the map container has a height, otherwise this test is useless.
expect(div.clientHeight).to.be.greaterThan(layersCtrl._container.clientHeight);
expect(container.clientHeight).to.be.greaterThan(0); // Make sure first that the map container has a height, otherwise this test is useless.
expect(container.clientHeight).to.be.greaterThan(layersCtrl._container.clientHeight);
expect(layersCtrl._section.classList.contains('leaflet-control-layers-scrollbar')).to.be(true);
});

Expand All @@ -264,8 +263,8 @@ describe("Control.Layers", function () {
// Need to create a DIV with specified height and insert it into DOM, so that the browser
// gives it an actual size.
map.remove();
div.style.height = div.style.width = '200px';
map = L.map(div);
container.style.height = container.style.width = '200px';
map = L.map(container);

layersCtrl.addTo(map);
expect(layersCtrl._section.classList.contains('leaflet-control-layers-scrollbar')).to.be(false);
Expand All @@ -275,7 +274,7 @@ describe("Control.Layers", function () {
layersCtrl.addOverlay(L.marker([0, 0]), i);
}

expect(div.clientHeight).to.be.greaterThan(layersCtrl._container.clientHeight);
expect(container.clientHeight).to.be.greaterThan(layersCtrl._container.clientHeight);
expect(layersCtrl._section.classList.contains('leaflet-control-layers-scrollbar')).to.be(true);
});
});
Expand Down
10 changes: 5 additions & 5 deletions spec/suites/control/ControlSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@ describe("Control", function () {
control;

beforeEach(function () {
container = document.createElement('div');
document.body.appendChild(container);
map = L.map(container).setView([0, 0], 1);
container = container = createContainer();
map = L.map(container);

map.setView([0, 0], 1);
control = new L.Control();
control.onAdd = onAdd;
control.addTo(map);
});

afterEach(function () {
map.remove();
document.body.removeChild(container);
removeMapContainer(map, container);
});

describe("#addTo", function () {
Expand Down
Loading

0 comments on commit 7419c9c

Please sign in to comment.