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(); 
    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);
     return (){
    if(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/>);

The above test is failing with

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.