I want to test if FeatureLayer is removed from the map instance, but somehow
instead of expect(removeCallCount).toBe(1)
I have 0. What can be the problem?
test("FeatureLayer removes itself from map instance when no longer in map", async () => {
const scope = createMapScope();
const capture = jest.fn();
mockAddLayer.mockReset();
expect(mockAddLayer.mock.calls.length).toBe(0);
const { rerender } = render(<MapInstanceScope scope={scope}>
<Map scope={scope}>
<FeatureLayer>
<FeatureLayerContext.Consumer>
{capture}
</FeatureLayerContext.Consumer>
</FeatureLayer>
</Map>
</MapInstanceScope>);
const { featureLayer } = capture.mock.calls[capture.mock.calls.length - 1][0] as { featureLayer: L.FeatureGroup };
let removeCallCount = 0;
jest.spyOn(featureLayer, "remove").mockImplementation(function(){removeCallCount = removeCallCount + 1; return Leaflet.L.featureGroup(); });
rerender(<MapInstanceScope scope={scope}>
<Map scope={scope} />
</MapInstanceScope>);
expect(removeCallCount).toBe(1);
});
Instead of jest.spyOn(featureLayer, "remove")
I also tried such events as removeLayer, removeOn, but none of these worked.
This is a part of the code that is responsible for layer's removal (just in case):
export const useMapLayerManager = (layer?: L.Layer, layerProps?: MapLayersContextLayerPropsShape) => {
const { addLayer, removeLayer } = useContext(MapLayersContext);
const memoizedLayerProps = useMemo(() => ({
label: layerProps?.label
}), [
layerProps?.label
])
useEffect(() => {
if(layer){
addLayer(layer, layerProps);
return () => {
removeLayer(layer);
}
}
}, [layer, memoizedLayerProps]); // eslint-disable-line react-hooks/exhaustive-deps
}