zcreativelabs / react-simple-maps

Beautiful React SVG maps with d3-geo and topojson using a declarative api.
https://www.react-simple-maps.io/
MIT License
3.04k stars 424 forks source link

Cannot read property 'baseVal' of undefined when testing #245

Open shanytc opened 3 years ago

shanytc commented 3 years ago

Hey, I am trying to test the map using jest/enzyme, I am getting this error whenever I try to mount() my component.

It fails on d3-zoom.js when trying to access the viewBox

if (e.hasAttribute("viewBox")) {
      e = e.viewBox.baseVal;  <---- here
      return [[e.x, e.y], [e.x + e.width, e.y + e.height]];
    }

react code

<ComposableMap
   width={width}
   height={height}
   projectionConfig={{
     scale: scale,
     center: center,
   }}
 >
    <ZoomableGroup zoom={defaultZoom} center={center}>
        Geographies geography={topography}>......</Geographies>
    </ZoomableGroup>
</ComposableMap>
kopach commented 3 years ago

I ended up mocking react-simple-maps and mocking ZoomableGroup. Something like this

  jest.mock('react-simple-maps', () => ({
    ...jest.requireActual('react-simple-maps'),
    ZoomableGroup: ({ children }: ZoomableGroupProps): JSX.Element => <>{children}</>,
  }))