[Pkg-javascript-commits] [leaflet-markercluster] 130/219: Converted removeOutsideVisibleBoundsSpec test suite
Jonas Smedegaard
dr at jones.dk
Sat May 7 09:39:27 UTC 2016
This is an automated email from the git hooks/post-receive script.
js pushed a commit to branch master
in repository leaflet-markercluster.
commit 69e142d65849f3d1e529fa2dd59152f37655b225
Author: ghybs <ghybs1 at gmail.com>
Date: Thu Oct 15 23:13:31 2015 +0400
Converted removeOutsideVisibleBoundsSpec test suite
Following re-work of RefreshSpec test suite, also converted this suite to
avoid use of `beforeEach` and `afterEach` hooks which seem to create
problems with PhantomJS when total number of tests increases.
This commit actually implements a wrapper for `it` function which calls
`beforeEach2` and `afterEach2` user defined functions.
---
spec/suites/removeOutsideVisibleBoundsSpec.js | 225 +++++++++++++++++---------
1 file changed, 153 insertions(+), 72 deletions(-)
diff --git a/spec/suites/removeOutsideVisibleBoundsSpec.js b/spec/suites/removeOutsideVisibleBoundsSpec.js
index be97928..e7a633a 100644
--- a/spec/suites/removeOutsideVisibleBoundsSpec.js
+++ b/spec/suites/removeOutsideVisibleBoundsSpec.js
@@ -1,104 +1,185 @@
describe('Option removeOutsideVisibleBounds', function () {
- var map, div;
-
- /*var marker1 = new L.Marker([1.5, -0.4]), // 2 screens width away.
- marker2 = new L.Marker([1.5, 0.6]), // 1 screen width away.
- marker3 = new L.Marker([1.5, 1.5]), // In view port.
- marker4 = new L.Marker([1.5, 2.4]), // 1 screen width away.
- marker5 = new L.Marker([1.5, 3.4]), // 2 screens width away.
- markers = [marker1, marker2, marker3, marker4, marker5],
- group,
- previousMobileValue = L.Browser.mobile;*/
-
- function centerMapView() {
- // Corresponds to zoom level 8 for the above div dimensions.
- map.fitBounds(new L.LatLngBounds([
- [1, 1],
- [2, 2]
- ]));
+
+ /**
+ * Wrapper for Mocha's `it` function, to avoid using `beforeEach` and `afterEach`
+ * which create problems with PhantomJS when total number of tests (across all suites)
+ * increases. Might be due to use of promises for which PhantomJS performs badly?
+ * @param testDescription string
+ * @param testInstructions function
+ * @param testFinally function to be executed just before afterEach2, in the `finally` block.
+ */
+ function it2(testDescription, testInstructions, testFinally) {
+
+ it(testDescription, function () {
+
+ // Before each test.
+ if (typeof beforeEach2 === "function") {
+ beforeEach2();
+ }
+
+ try {
+
+ // Perform the actual test instructions.
+ testInstructions();
+
+ } catch (e) {
+
+ // Re-throw the exception so that Mocha sees the failed test.
+ throw e;
+
+ } finally {
+
+ // If specific final instructions are provided.
+ if (typeof testFinally === "function") {
+ testFinally();
+ }
+
+ // After each test.
+ if (typeof afterEach2 === "function") {
+ afterEach2();
+ }
+
+ }
+ });
}
- beforeEach(function () {
- clock = sinon.useFakeTimers();
- div = document.createElement('div');
- div.style.width = '200px';
- div.style.height = '200px';
- document.body.appendChild(div);
+ /////////////////////////////
+ // SETUP FOR EACH TEST
+ /////////////////////////////
- map = L.map(div, { maxZoom: 18 });
- });
+ /**
+ * Instructions to be executed before each test called with `it2`.
+ */
+ function beforeEach2() {
- afterEach(function () {
- clock.restore();
+ // Nothing for this test suite.
- document.body.removeChild(div);
- });
+ }
- it('removes objects more than 1 screen away from view port by default', function () {
+ /**
+ * Instructions to be executed after each test called with `it2`.
+ */
+ function afterEach2() {
- centerMapView();
+ if (group instanceof L.MarkerClusterGroup) {
+ group.removeLayers(group.getLayers());
+ map.removeLayer(group);
+ }
- var marker1 = new L.Marker([1.5, -0.4]), // 2 screens width away.
- marker2 = new L.Marker([1.5, 0.6]), // 1 screen width away.
- marker3 = new L.Marker([1.5, 1.5]), // In view port.
- marker4 = new L.Marker([1.5, 2.4]), // 1 screen width away.
- marker5 = new L.Marker([1.5, 3.4]), // 2 screens width away.
- markers = [marker1, marker2, marker3, marker4, marker5],
- group = L.markerClusterGroup().addTo(map);
+ // Throw away group as it can be assigned with different configurations between tests.
+ group = null;
+ }
- group.addLayers(markers);
- expect(marker1._icon).to.be(undefined);
- expect(map._panes.markerPane.childNodes.length).to.be(3); // markers 2, 3 and 4.
- expect(marker5._icon).to.be(undefined);
+ /////////////////////////////
+ // PREPARATION CODE
+ /////////////////////////////
- });
+ var marker1 = L.marker([1.5, -0.4]), // 2 screens width away.
+ marker2 = L.marker([1.5, 0.6]), // 1 screen width away.
+ marker3 = L.marker([1.5, 1.5]), // In view port.
+ marker4 = L.marker([1.5, 2.4]), // 1 screen width away.
+ marker5 = L.marker([1.5, 3.4]), // 2 screens width away.
+ div, map, group, previousMobileSetting;
+
+ div = document.createElement('div');
+ div.style.width = '200px';
+ div.style.height = '200px';
+ document.body.appendChild(div);
- it('removes objects out of view port by default for mobile device', function () {
+ map = L.map(div, { maxZoom: 18 });
- // Fool Leaflet
- var previous = L.Browser.mobile;
- L.Browser.mobile = true;
+ // Corresponds to zoom level 8 for the above div dimensions.
+ map.fitBounds(new L.LatLngBounds([
+ [1, 1],
+ [2, 2]
+ ]));
- centerMapView();
+ // Add all markers once to map then remove them immediately so that their icon is null (instead of undefined).
+ map.removeLayer(marker1.addTo(map));
+ map.removeLayer(marker2.addTo(map));
+ map.removeLayer(marker3.addTo(map));
+ map.removeLayer(marker4.addTo(map));
+ map.removeLayer(marker5.addTo(map));
- var marker1 = new L.Marker([1.5, -0.4]), // 2 screens width away.
- marker2 = new L.Marker([1.5, 0.6]), // 1 screen width away.
- marker3 = new L.Marker([1.5, 1.5]), // In view port.
- marker4 = new L.Marker([1.5, 2.4]), // 1 screen width away.
- marker5 = new L.Marker([1.5, 3.4]), // 2 screens width away.
- markers = [marker1, marker2, marker3, marker4, marker5],
- group = L.markerClusterGroup().addTo(map);
- group.addLayers(markers);
+ function prepareGroup() {
- expect(marker1._icon).to.be(undefined);
- expect(marker2._icon).to.be(undefined);
- expect(map._panes.markerPane.childNodes.length).to.be(1); // marker 3 only.
- expect(marker4._icon).to.be(undefined);
- expect(marker5._icon).to.be(undefined);
+ // Group should be assigned with a Marker Cluster Group before calling this function.
+ group.addTo(map);
- L.Browser.mobile = previous;
+ // Add markers 1 by 1 to make sure we do not create an async process.
+ marker1.addTo(group);
+ marker2.addTo(group);
+ marker3.addTo(group);
+ marker4.addTo(group);
+ marker5.addTo(group);
+ }
+
+
+ /////////////////////////////
+ // TESTS
+ /////////////////////////////
+
+ it2('removes objects more than 1 screen away from view port by default', function () {
+
+ group = L.markerClusterGroup();
+
+ prepareGroup();
+
+ expect(marker1._icon).to.be(null);
+ expect(map._panes.markerPane.childNodes.length).to.be(3); // markers 2, 3 and 4.
+ expect(marker5._icon).to.be(null);
});
- it('leaves all objects on map when set to false', function () {
+ it2(
+ 'removes objects out of view port by default for mobile device',
+
+ function () {
- centerMapView();
+ // Fool Leaflet, make it thinks it runs on a mobile device.
+ previousMobileSetting = L.Browser.mobile;
+ L.Browser.mobile = true;
- var marker1 = new L.Marker([1.5, -0.4]), // 2 screens width away.
- marker2 = new L.Marker([1.5, 0.6]), // 1 screen width away.
- marker3 = new L.Marker([1.5, 1.5]), // In view port.
- marker4 = new L.Marker([1.5, 2.4]), // 1 screen width away.
- marker5 = new L.Marker([1.5, 3.4]), // 2 screens width away.
- markers = [marker1, marker2, marker3, marker4, marker5],
- group = L.markerClusterGroup({removeOutsideVisibleBounds: false}).addTo(map);
+ group = L.markerClusterGroup();
- group.addLayers(markers);
+ prepareGroup();
+
+ expect(marker1._icon).to.be(null);
+ expect(marker2._icon).to.be(null);
+ expect(map._panes.markerPane.childNodes.length).to.be(1); // marker 3 only.
+ expect(marker4._icon).to.be(null);
+ expect(marker5._icon).to.be(null);
+
+ },
+
+ // Extra final instruction to be called even on failure.
+ function () {
+ // Restore original setting, so that next tests are unaffected.
+ L.Browser.mobile = previousMobileSetting;
+ }
+ );
+
+ it2('leaves all objects on map when set to false', function () {
+
+ group = L.markerClusterGroup({
+ removeOutsideVisibleBounds: false
+ });
+
+ prepareGroup();
expect(map._panes.markerPane.childNodes.length).to.be(5); // All 5 markers.
});
+
+ /////////////////////////////
+ // CLEAN UP CODE
+ /////////////////////////////
+
+ map.remove();
+ document.body.removeChild(div);
+
});
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/leaflet-markercluster.git
More information about the Pkg-javascript-commits
mailing list