Need a test for setState in shallow render
See original GitHub issueRelated 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:
- Created 8 years ago
- Comments:15 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
We have a bunch of tests using
setState
with shallow renderer now. Also the new shallow renderer doesn’t usegetActiveElement()
anyway.@stefman1 That was a bug in a fbjs release. You can update to the new version. https://twitter.com/intelligibabble/status/844238291335458816