1

I am trying to unit test onCreated event handler of the React-leaflet-draw's Edit control component using react testing library/jest. I tried the following without any luck:

test('Polygon draw functionality', async () => {
  const drawHandler = jest.fn();
  const { getByText, getByTestId, getAllByTestId, queryAllByTestId } = render(
  <Map
    data={mapData}
    onDraw={drawHandler}
  ></Map>,
  {}
);
  let e = document.createEvent('Event');
  e.initEvent('click', true, true);
  let cb = document.getElementsByClassName('leaflet-draw-draw-polygon');
  cb[0].dispatchEvent(e);
  expect(drawHandler).toHaveBeenCalled();
});

Map.js

...
<FeatureGroup>
        <EditControl
          position='topright'
          onCreated={props.onDraw}
          draw={{
            polyline: false,
            polygon: true,
            rectangle: false,
            circle: false,
            marker: false,
            circlemarker: false,
          }}
        />
      </FeatureGroup>
...

Is there any better way to programmatically trigger onCreated event?

Premshankar Tiwari
  • 3,006
  • 3
  • 24
  • 30

0 Answers0