Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you unit test Leaflet JS maps?

How can you unit test Leaflet JS maps?

like image 997
SteveC Avatar asked Jun 29 '16 12:06

SteveC


1 Answers

I have been working a bit more on testing and using Leaflet.

Currently I am using QUnit to run the tests.

I currently have to open it in a browser to see if it is working, maybe someone else knows how to run QUnit via commandline.

Before I wrote and ran my tests I took a look at the Leafletjs docs page and just started exploring the different js objects with the developer tools console on google chrome.

Leaflet docs: http://leafletjs.com/reference-1.0.0.html

Example tests from my QUnit tests.js file:

QUnit.test("map default options", function( assert )    assert.equal(myMap.getCenter().toString(),
            "LatLng(0, 8.846)",
            "The map is centered at the ZMT's longitude, and the equator"
    );
    assert.equal(myMap.getZoom(),
            2,
            "The default zoom is set to 2"
    );
});

QUnit.test("baseLayer layerGroup", function( assert ) {
    assert.equal(baseLayer.getLayers().length,
            1,
            "There is just one layer in 'baseLayer' layerGroup"
    );
    assert.equal(baseLayer.getLayers()[0]._url,
            "http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
            "The url of the layer leads to the correct openstreet map tiles"
    );

    assert.equal(baseLayer.getLayers()[0].options.attribution,
            '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
            "The attribution for the layer is correct"
    );
    assert.equal(baseLayer.getLayers()[0].options.minZoom,
            0,
            "The default minimum zoom is set to 0"
    );
    assert.equal(baseLayer.getLayers()[0].options.maxZoom,
            19,
            "The default maximum zoom is set to 19"
    );
});
like image 95
patzmt Avatar answered Oct 24 '22 05:10

patzmt