title: React unit testing Leaflet
body: <p>I have problem with unit testing Map component.</p>
  <p>My class component:</p>
  <pre><code>componentDidMount() {
      const { zoom, data, center } = this.props;
      const { activeButton } = this.state;
      if (data[activeButton] != null) {
        this.map = map('map', {
          center: data[activeButton].points ? data[activeButton].points[0] : center,
          zoom,
          layers: [tileLayer(...SETTINGS.mapTileLayer)],
        });
        if (data[activeButton].points != null) {
          this.addPolyline(data[activeButton].points);
        }
      }
    }
  
    addPolyline = (points: Points) =&gt; {
      if (this.polyline != null) {
        this.map.removeLayer(this.polyline);
        this.map.removeLayer(this.marker.start);
        this.map.removeLayer(this.marker.stop);
      }
  
      if (points != null &amp;&amp; points[0] != null) {
        this.polyline = new Polyline(points, SETTINGS.polyline);
  
        this.marker.start = new Marker(points[0]);
        this.marker.stop = new Marker(points[points.length - 1]);
  
        this.polyline.addTo(this.map);
        this.marker.start.addTo(this.map);
        this.marker.stop.addTo(this.map);
  
        this.map.panTo(points[0]);
      }
    };
  </code></pre>
  <p>Unit test with Jest and Enzyme</p>
  <pre><code>function getComponent(mockData) {
    return &lt;Map data={mockData} /&gt;;
  }
  
  const mockData = [
    { id: 0, name: '1', distance: '1', points: [[0, 0], [1, 1]] },
    { id: 1, name: '2', distance: '2', points: [[0, 0], [1, 1]] },
  ];
  
  describe('LastActivity component', () =&gt; {  
    it('button click', () =&gt; {
      const wrapper = mount(getComponent(mockData), { attachTo: document.body });
  
  ...etc...
  });
  </code></pre>
  <p>With this test I get</p>
  <blockquote>
  <p>TypeError: Cannot read property '_layerAdd' of null</p>
  <p>this.polyline.<strong>addTo</strong>(this.map);</p>
  </blockquote>
  <p>When I comment out line this line</p>
  <blockquote>
  <p>this.polyline.addTo(this.map);</p>
  </blockquote>
  <p>Everything works fine and tests pass</p>
  <p>package.json:</p>
  <pre><code>&quot;enzyme-adapter-react-16&quot;: &quot;1.2.0&quot;,
  &quot;enzyme&quot;: &quot;3.4.4&quot;,
  &quot;react&quot;: &quot;16.4.2&quot;,
  &quot;react-dom&quot;: &quot;16.4.2&quot;,
  &quot;leaflet&quot;: &quot;1.3.4&quot;,
  &quot;jest&quot;: &quot;23.5.0&quot;,
  &quot;jest-cli&quot;: &quot;23.5.0&quot;,
  </code></pre>