react-simple-maps icon indicating copy to clipboard operation
react-simple-maps copied to clipboard

Cannot read property 'baseVal' of undefined when testing

Open shanytc opened this issue 5 years ago • 1 comments

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>

shanytc avatar Jan 19 '21 09:01 shanytc

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}</>,
  }))

kopach avatar Jul 01 '21 14:07 kopach