0

I am looking to create unit tests for component below mainly on checking addLayer and removeLayer being called,

function App (()=>{
    const marker = useRef<L.Marker | undefined>( undefined);
    const fg : L.FeatureGroup<any> = fg(); 
    
    useEffect(()=>{
    var myIcon = L.divIcon({className:'my-div-icon',html:renderToStaticMarkup(<ArrowRight/>)});
    marker.current = L.marker([50.5, 30.5], {icon:myIcon});
     if(fg) fg.addLayer(marker.current);
      <div>Text</div>;
    
     return (){
    if(marker.current && fg)
    {fg.removeLayer(marker.current);}
   
    } 
    },[fg]);
    
    });

RTL test:

const mockOnClick = jest.fn();
const mockAddLayer = jest.fn();
describe("Test Leaflet methods",()=>{
it("Test add Layer",()=>{
const {wrapper} = render (<App/>);
expect(wrapper.getByText('Text')).toBeTruthy();
expect(mockAddLayer).toBeCalled();
});
});

The above test is failing with

expect(jest.fn()).toBeCalled() 
expected number of calls: >= 1 
received number of calls: 0 

It is possible to test fg.addLayer() and fg.removeLayer() are being called? I am a beginner in unit testing with leafletjs.

  • This question is not really GIS related and as such better suited for StackOverflow site. – TomazicM Oct 23 at 9:01
  • Can't you test it together with map events? When the layer is removed/added use the map event to listen for changes and check out the ones you want. It should give you information about the added/removed layer. The reference here: leafletjs.com/reference-1.7.1.html#map-event – Kumbra Oct 23 at 21:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.