nkbt/react-collapse

Does not work with react-test-renderer

Closed this issue · 6 comments

Hi, we have some components that use react-collapse (thanks!) but trying to test them:

import Collapse from 'react-collapse'
import renderer from 'react-test-renderer'

const tree = renderer.create(<Collapse isOpened={true}><div>stuff</div></Collapse>).toJSON()

...I'm hitting the following error:

     TypeError: Cannot read property 'clientHeight' of null
      at componentDidMount (node_modules/react-height/src/ReactHeight.js:31:20)
      at node_modules/react-test-renderer/lib/ReactCompositeComponent.js:265:25
      at measureLifeCyclePerf (node_modules/react-test-renderer/lib/ReactCompositeComponent.js:75:12)
      at node_modules/react-test-renderer/lib/ReactCompositeComponent.js:264:11
      at CallbackQueue.notifyAll (node_modules/react-test-renderer/lib/CallbackQueue.js:76:22)
      at ReactTestReconcileTransaction.close (node_modules/react-test-renderer/lib/ReactTestReconcileTransaction.js:36:26)
      at ReactTestReconcileTransaction.closeAll (node_modules/react-test-renderer/lib/Transaction.js:206:25)
      at ReactTestReconcileTransaction.perform (node_modules/react-test-renderer/lib/Transaction.js:153:16)
      at batchedMountComponentIntoNode (node_modules/react-test-renderer/lib/ReactTestMount.js:69:27)
      at ReactDefaultBatchingStrategyTransaction.perform (node_modules/react-test-renderer/lib/Transaction.js:140:20)
      at Object.batchedUpdates (node_modules/react-test-renderer/lib/ReactDefaultBatchingStrategy.js:62:26)
      at Object.batchedUpdates (node_modules/react-test-renderer/lib/ReactUpdates.js:97:27)
      at Object.render (node_modules/react-test-renderer/lib/ReactTestMount.js:125:18)

Do you know if this a react-collapse issue, or a react-test-renderer issue?

nkbt commented

Ok, thanks for the quick response. We are using jsdom and jsdom-global in our tests--does that change anything?

nkbt commented

Ok, thanks. For those who got here looking for references to the same error, I ended up doing this:

const proxyquire = require('proxyquire').noCallThru()
const fakeCollapse = React.createClass({
  render() { return <div>{this.props.children}</div> }
})

const MyComponent = proxyquire('../../src/path/to/MyComponent',
                              { 'react-collapse': fakeCollapse }).default

Maybe there's something simpler but this works for us.

nkbt commented

Nice solution. I also got around it by using ReactTestUtils.renderIntoDocument which gave it a real DOM and therefore clientHeight worked ok. Not the most efficient. But gets the component tested.