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

Question about enzyme/mocha tests #65

Open
jstoebel opened this issue Apr 14, 2017 · 4 comments
Open

Question about enzyme/mocha tests #65

jstoebel opened this issue Apr 14, 2017 · 4 comments

Comments

@jstoebel
Copy link

I'm a little confused on how to write enzyme tests for a component that uses react-dimensions

The component looks like this:

const Show = React.createClass({

  // shortened for brevity.

  render () {
    var width = 0.4 * this.props.containerWidth,
      height = width,
      radius = 0.5 * width
    return (
      <div>
        <h1>{this.state.poll.name}</h1>
        <div className="container-fluid">
          // lots more stuff removed for brevity
        </div>
      </div>
    )
  }
})

export default Dimensions()(Show)

I'm trying to test it like this:

it("contains an h1 as first child", (done) => {
  const wrapper = mount(<Show />);
  expect(wrapper.find('h1')).to.have.length(1);
  done()
});

The above test fails with AssertionError: expected { Object (component, root, ...) } to have a length of 1 but got 0

I looked at the value of wrapper.html() and get <div style="width: 100%; height: 100%; padding: 0px; border: 0px;">0</div>. I believe this touches on #28, but as things work just fine in my browser I'm confused as to why it does not behave as I would expect in my tests. Any ideas? Is there any other code I can provide that would shed light on this situation? Thanks.

@tibbus
Copy link

tibbus commented Aug 4, 2017

There are more issues with this :

  1. You are trying to render mount(<Show />) without any parent wrapper, this lib is trying to read the parent dimensions and if it doesn't find it or if the height and width is 0 then will not render the Component.
  2. Even if you add a wrapper around that <Show /> it will not work because if you pass the inline style (ex : width: 500px ) the lib will not read it right as it's inline style.

@jimulle
Copy link

jimulle commented Aug 9, 2017

What do we need to set on the parent wrapper in enzyme tests to get dimensions to pick-up a height and width value. Is there a way we can mock this out for testing purposes?

@lferreira457
Copy link

lferreira457 commented Oct 25, 2018

Having similar issues as well with testing a library component that tries to measure wrapping parent dimensions, would be interested in knowing if this could be mocked?

@malykhinvi
Copy link

malykhinvi commented Nov 26, 2018

@jstoebel Don't know if it is still relevant, but you could mock it like this:

const config = process.env.NODE_ENV === 'test' ? {getWidth: () => 1920, getHeight: () => 1080} : {}
export default Dimensions(config)(Show);

Though it is not a best option, since source code is getting polluted with tests-related stuff.
cc @tibbus

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

5 participants