question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Need a test for setState in shallow render

See original GitHub issue

Related to #2393

Rendering a component like this:

<div onClick={() => this.setState({something: 1}) />

rendering via shallow render, then calling handler

var result = shallowRenderer.getRenderOutput();
result.props.onClick()

Produces the following:

    ReferenceError: document is not defined
      at getActiveElement (myapp/node_modules/react/lib/getActiveElement.js:23:12)
      at ReactReconcileTransaction.ReactInputSelection.getSelectionInformation (myapp/node_modules/react/lib/ReactInputSelection.js:40:23)
      at ReactReconcileTransaction.Mixin.initializeAll (myapp/node_modules/react/lib/Transaction.js:168:30)
      at ReactReconcileTransaction.Mixin.perform (myapp/node_modules/react/lib/Transaction.js:133:12)
      at ReactUpdatesFlushTransaction.Mixin.perform (myapp/node_modules/react/lib/Transaction.js:134:20)
      at ReactUpdatesFlushTransaction.assign.perform (myapp/node_modules/react/lib/ReactUpdates.js:95:38)
      at Object.flushBatchedUpdates (myapp/node_modules/react/lib/ReactUpdates.js:175:19)
      at Object.wrapper [as flushBatchedUpdates] (myapp/node_modules/react/lib/ReactPerf.js:70:21)
      at ReactDefaultBatchingStrategyTransaction.Mixin.closeAll (myapp/node_modules/react/lib/Transaction.js:207:25)
      at ReactDefaultBatchingStrategyTransaction.Mixin.perform (myapp/node_modules/react/lib/Transaction.js:148:16)
      at Object.ReactDefaultBatchingStrategy.batchedUpdates (myapp/node_modules/react/lib/ReactDefaultBatchingStrategy.js:66:19)
      at Object.enqueueUpdate (myapp/node_modules/react/lib/ReactUpdates.js:215:22)
      at enqueueUpdate (myapp/node_modules/react/lib/ReactUpdateQueue.js:30:18)
      at Object.ReactUpdateQueue.enqueueSetState (myapp/node_modules/react/lib/ReactUpdateQueue.js:208:5)
      at [object Object].ReactComponent.setState (myapp/node_modules/react/lib/ReactComponent.js:69:20)
      at Object.React.createElement.React.createElement.React.createElement.onChange (myapp/client/components/LoginForm.js:30:35)

Ideally, this module should realise it’s running on the server, and skip this behaviour check

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:15 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
gaearoncommented, Jan 2, 2018

We have a bunch of tests using setState with shallow renderer now. Also the new shallow renderer doesn’t use getActiveElement() anyway.

1reaction
sebmarkbagecommented, Mar 21, 2017

@stefman1 That was a bug in a fbjs release. You can update to the new version. https://twitter.com/intelligibabble/status/844238291335458816

Read more comments on GitHub >

github_iconTop Results From Across the Web

setState(nextState[, callback]) · Enzyme - GitHub Pages
A method to invoke setState() on the root component instance, similar to how ... state you want to test, in order to be...
Read more >
How does Enzyme setState() works on shallow components?
Here, I want to setState in my test file and check if the classNames are correct according to the currentState. const wrapper =...
Read more >
Shallow Renderer - React
When writing unit tests for React, shallow rendering can be helpful. Shallow rendering lets you render a component “one level deep” and assert...
Read more >
How to use setState function in ShallowWrapper - Tabnine
This method is useful for testing your component in hard to achieve states, however should be used sparingly.
Read more >
Testing state changes in React functional components
You may encounter components that hold information that affects their rendering, but you don't want that data to be available for the entire ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found