Specifying maxBounds causes mocha tests to explode with "Invalid LatLng object: (NaN, NaN)"
See original GitHub issueUsing Leaflet 0.7.7 and jsdom 9.4.2, I’m unable to successfully execute a unit test on L.map
.
I’m not using a test runner–just plain ol’ mocha under Node.
$ mocha --require test/support/bugReportSetup.js --compilers js:babel-register --require babel-polyfill test/unit/components/MaxBoundsBugTest.jsx
MaxBoundsBug
when maxBounds is provided
1) mounts without problems
0 passing (898ms)
1 failing
1) MaxBoundsBug when maxBounds is provided mounts without problems:
Error: Invalid LatLng object: (NaN, NaN)
at Object.L.LatLng (node_modules/leaflet/dist/leaflet-src.js:1122:9)
at Object.L.Projection.SphericalMercator.unproject (node_modules/leaflet/dist/leaflet-src.js:1403:10)
at Object.L.CRS.pointToLatLng (node_modules/leaflet/dist/leaflet-src.js:1439:26)
at L.Map.L.Class.extend.unproject (node_modules/leaflet/dist/leaflet-src.js:1930:27)
at L.Map.L.Class.extend._limitCenter (node_modules/leaflet/dist/leaflet-src.js:2279:15)
at L.Map.include.setView (node_modules/leaflet/dist/leaflet-src.js:8687:17)
at L.Map.L.Class.extend.initialize (node_modules/leaflet/dist/leaflet-src.js:1545:9)
at new NewClass (node_modules/leaflet/dist/leaflet-src.js:229:20)
at Object.L.map (node_modules/leaflet/dist/leaflet-src.js:2318:9)
at Context.<anonymous> (MaxBoundsBugTest.jsx:13:9)
Here are the gory details…
$ npm show leaflet version
0.7.7
$ npm show jsdom version
9.4.2
// File: MaxBoundsBugTest.jsx
import { expect } from 'chai'
import L from 'leaflet'
describe('MaxBoundsBug', () => {
context('when maxBounds is provided', () => {
const maxBounds = [
[-80, -180], // SW
[90, 180], // NE
]
it('mounts without problems', () => {
L.map('test', {
center: [30, 0],
zoom: 13,
maxBounds: maxBounds
});
})
})
})
// File: bugReportSetup.js
// From https://github.com/airbnb/enzyme/blob/master/docs/guides/jsdom.md
import { jsdom } from 'jsdom'
const EXPOSED_PROPERTIES = ['window', 'navigator', 'document'];
MAIN: {
global.document = jsdom('<div id="test"></div>')
global.window = document.defaultView
Object.keys(document.defaultView).forEach((property) => {
if ( global[property] === void 0 ) {
EXPOSED_PROPERTIES.push(property)
global[property] = document.defaultView[property]
}
})
}
Issue Analytics
- State:
- Created 7 years ago
- Reactions:2
- Comments:12 (6 by maintainers)
Top Results From Across the Web
I had error "Invalid LatLng object: (NaN, 86.06925048939979 ...
I had error "Invalid LatLng object: (NaN, 86.06925048939979)" when using proj4leaflet. Im using react leaflet for an interactive map. for ...
Read more >tests.html · master · Thomas Louail / voronoize · GitLab
// Retrieves runtime widget sizing information for an element. 413. // The return value is either null, or an object with fill, padding,....
Read more >https://zenodo.org/record/4572636/files/meta_inter...
Or, can be an array of objects with "code" and "data" // properties; ... new Error("Invalid LatLng object: ("+t+", "+i+")");this.lat=+t,this.lng=+i,void ...
Read more >Data Visualization with JavaScript - Web Mechanic
This chapter shows off the Underscore.js library, which makes it easy to prepare the data that drives our visualizations.
Read more >https://raw.githubusercontent.com/geanders/RProgra...
{3}/.test(i.background)},r.backgroundsize=function(){return I("backgroundSize")} ... a[0]-f[0]||f[1]-a[1]});f=[];j=[NaN,NaN];for(c=0;c<b.length;++c)i=b[c] ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Pretty stupid solution actually. Just set
clientWidth
andclientHeight
for the container element you are passing to the map to some valid values:I just had a very similar issue myself and wanted to provide some extra info.
I’m also running automated tests with jsdom and Mocha. jsdom provides only the DOM for testing, i.e., it doesn’t actually render the document. Element properties such as
clientWidth
andclientHeight
, therefore, are not provided. Those properties, however, are checked inL.getSize
in order to calculate the center of the map’s containing element.A workaround like the one @Scarysize provided works fine enough, e.g.,
It might be useful, however, if
getSize
through an error ifclientWidth
orclientHeight
can’t be checked, or if they gracefully fell back to0
(which seems reasonable since those properties would presumably only be absent if the document wasn’t actually being rendered in a browser).