Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map container not found when testing Map parent component #246

Closed
5 tasks done
thomasthiebaud opened this issue Nov 2, 2016 · 8 comments
Closed
5 tasks done

Map container not found when testing Map parent component #246

thomasthiebaud opened this issue Nov 2, 2016 · 8 comments

Comments

@thomasthiebaud
Copy link

Please make sure to check the following boxes before submitting an issue. Thanks!

  • Check that all peer dependencies are installed: React, ReactDOM and Leaflet.
  • Check that you are using a supported version of React (v0.14 or v15).
  • Check that you are using a supported version of Leaflet (v.0.7)
grep -E (react|leaflet) package.json
"leaflet": "0.7.7",
"react": "15.3.2",
"react-dom": "15.3.2",
"react-leaflet": "0.12.3",

Expected behavior

Inside a mocha test using enzyme I expect to be able to mount the wrapper a component of my map without having an Map container not found.

Actual behavior

Map container not found.

Steps to reproduce

Given this component

export class Container extends React.Component {
  render () {
    return (
      <div id="map">
        <SimpleExample /> // SimpleExample map from jsfiddle
      </div>
    )
  }
}

I would like to mount the component using enzyme.mount function like this

import React from 'react'
import { mount } from 'enzyme'

describe('(Component) Container', () => {
  let _wrapper

  it('Should render a <Container>', () => {
    _wrapper = mount(<Container />)
    expect(_wrapper.is('div')).to.be.true
  })
})

without having the Map container not found. error.

I already checked this issue #204, but this is not the same because I trying a parent component, not directly the map component.

@evansiroky
Copy link

@thomasthiebaud it won't matter where the react-leaflet map component is nested in your structure. The only way to get react-leaflet map components to mount with enzyme is by attaching it to an existing element which has been inserted in the document body in jsdom or some other document-like thing.

@dkreft I did some research and it appears that the core issue is that Leaflet scans the document body to try to find the element to modify, but doesn't find it because of the following: enzyme attempts to mount by calling react-addons-test-utils.renderIntoDocument which creates a blank div, but does not insert it into a document body. Therefore, when you mount with the attachTo method and the thing you're attaching to has been inserted into the document body, then everything contained within that element is now searchable and thus Leaflet can find it.

@PaulLeCam
Copy link
Owner

This seem to be an issue with mocha/enzyme, same as in #204, as it works as expected in browsers.
If you have a fix for it please open a PR but I won't spend time on this myself.

@thomasthiebaud
Copy link
Author

Ok, thank for the answer. I will try to investigate.

@simeonackermann
Copy link

Based on #246 (comment) the solution could be to use attach as mount option.
For example the Parent component renders the map, in the test I attach the Parent to an empty div:

import React from 'react';
import {mount} from 'enzyme';

it('test map', () => {
   const div = global.document.createElement('div');
   global.document.body.appendChild(div);
   
   const wrapper = mount(<Parent />, {attachTo: div});
});

@skipjack
Copy link

skipjack commented Oct 9, 2019

I see the same behavior with jest out of the box. Meaning, when only using react-test-renderer I get the same Map container not found. error:

describe('the `Map` component', () => {
    let component = Renderer.create(<Map />),
        tree = component.toJSON()

    test('renders correctly with nothing passed', () => {
        expect(tree).toMatchSnapshot()
    })
})

I still have to try @simeonackermann's workaround with enzyme but if that's the "recommended" approach, can I submit a PR to document it?

@skipjack
Copy link

skipjack commented Oct 9, 2019

@simeonackermann your example worked, thanks! @PaulLeCam would you accept a PR to add documentation on this? Any advice on an existing document to edit from here would be appreciated:

https://github.com/PaulLeCam/react-leaflet/tree/master/docs

Otherwise, I can just add a new file called testing.md.

skyqrose added a commit to mbta/skate that referenced this issue Dec 31, 2019
Asana Task:
Dev cleanup: Use react-leaflet package
https://app.asana.com/0/1152340551558956/1153855588392657

It's mostly done and working
But tests are failing with "Map container not found"
See PaulLeCam/react-leaflet#246

I don't know how to fix that and need to move on to other tasks
so leaving this unfinished for now.
@eric-burel
Copy link

For lost googlers: you may encounter similar questions when using Jest+Storyshots. I am looking for fix in this precise scenario.

@eric-burel
Copy link

eric-burel commented Dec 10, 2020

I see the same behavior with jest out of the box. Meaning, when only using react-test-renderer I get the same Map container not found. error:

describe('the `Map` component', () => {
    let component = Renderer.create(<Map />),
        tree = component.toJSON()

    test('renders correctly with nothing passed', () => {
        expect(tree).toMatchSnapshot()
    })
})

I still have to try @simeonackermann's workaround with enzyme but if that's the "recommended" approach, can I submit a PR to document it?

Did you manage to fix this with Snapshots? That explains the issue I experience with Storyshots but I am not sure how far I can customize it, since most of the snapshot operations are automated.

Edit: alternate solution for Storyshots using React Testing Library renderer, works like a charm. Add this mock for SVG rendering and you're good to go: https://stackoverflow.com/questions/54382414/fixing-react-leaflet-testing-error-cannot-read-property-layeradd-of-null/54384719#54384719

KaylaBrady added a commit to mbta/skate that referenced this issue Sep 19, 2022
Note: For the tests related to drawing markers / shapes on the map, I intended to introduce snapshot tests.
However, I kept running into "Map container not found" (issue)[PaulLeCam/react-leaflet#246]. Opted to keep the test as finding the specified classname in the given HTML as that is what the original tests were doing anyway.
KaylaBrady added a commit to mbta/skate that referenced this issue Sep 20, 2022
…testing library (#1726)

* refactor(LadderPage.test): convert enzyme tests to react testing library

* refactor(Ladder.test): replace enzyme tests with react-testing-library

* refactor: A few more straightforward enzyme conversions

* refactor: a few more enzyme tests

* revert swings test changes

* refactor: use title intead of test-id for buttons

* fix: title case for titles

* refactor: more enzyme => react-testing-library tests

* refactor: header tests

* refactor: settings page tests

* refactor: routeVariantName tests

* refactor: Shuttle picker tests

* refactor: searchPage enzyme tests => react testing library

* refactor: map tests using react-testing library

Note: For the tests related to drawing markers / shapes on the map, I intended to introduce snapshot tests.
However, I kept running into "Map container not found" (issue)[PaulLeCam/react-leaflet#246]. Opted to keep the test as finding the specified classname in the given HTML as that is what the original tests were doing anyway.

* refactor: late view tests using react-testing-library

* fix: resolve console warnings in minischedule tests

* fix: searchPage tests no longer log console warnings

I'm not really sure why simply reordering the tests did the trick. I suspect is has something to do with how the now last test uses the StateDispatchProvider or something that wasn't clearing properly between tests, but I'm not totally sure.

* refactor: cleanup based on feedback

* doc: add comments for lateView tests

* fix(ladderPage.test): use title instead of testId

* fix(lateView.test): make sure to test that all boxes are unselected

* refactor: toBeTruthy => toBeInTheDocument for affectedwq react-testing-library tests

* test: lateView.test updates

Co-authored-by: Brady <kbrady@CTD24877KBRADY.local>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants