0.14-rc1: findDOMNode(statelessComponent) doesn’t work with TestUtils.renderIntoDocument
See original GitHub issueUnsure if this also impacts in a browser. I wrote an example of this here: https://github.com/iamdustan/react-testing-stateless-components
I wrote two components. One with class Component extends React.Component
and the other var Component = (props) => ()
.
The test case is identical for each. The class component test passes. The function component test fails.
_https://github.com/iamdustan/react-testing-stateless-components/blob/master/class.js_
import React from 'react';
class Component extends React.Component {
render() {
var {onClick, text} = this.props;
return (
<button onClick={onClick}>{text}</button>
);
}
};
Component.propTypes = {
onClick: React.PropTypes.func.isRequired,
text: React.PropTypes.string.isRequired,
};
export default Component;
_https://github.com/iamdustan/react-testing-stateless-components/blob/master/stateless.js_
import React from 'react';
var Component = ({onClick, text}) => (
<button onClick={onClick}>{text}</button>
);
Component.propTypes = {
onClick: React.PropTypes.func.isRequired,
text: React.PropTypes.string.isRequired,
};
export default Component;
Output
Using Jest CLI v5.0.3
FAIL __tests__/stateless-test.js (0.998s)
● thing › it render
- TypeError: Cannot read property 'textContent' of null
at Spec.<anonymous> (/Users/dkasten/projects/react-test-case/__tests__/stateless-test.js:15:34)
at jasmine.Block.execute (/Users/dkasten/projects/react-test-case/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:1065:17)
at jasmine.Queue.next_ (/Users/dkasten/projects/react-test-case/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2098:31)
at null._onTimeout (/Users/dkasten/projects/react-test-case/node_modules/jest-cli/vendor/jasmine/jasmine-1.3.0.js:2088:18)
at Timer.listOnTimeout (timers.js:89:15)
PASS __tests__/class-test.js (1.109s)
1 test failed, 1 test passed (2 total)
This failure is not due to interactions with Jest as it also exists on another project that is not using Jest (although still using jsdom).
Issue Analytics
- State:
- Created 8 years ago
- Reactions:4
- Comments:13 (6 by maintainers)
Top Results From Across the Web
附录三、React 测试入门教学
以下是 react-addons-test-utils-example/src/test/shallowRender.test.js : ... 0.14-rc1: findDOMNode(statelessComponent) doesn't work with TestUtils.
Read more >附录三、React 测试入门教学- 《从零开始学ReactJS(ReactJS 101 ...
以下是 react-addons-test-utils-example/src/test/shallowRender.test.js : 复制代码 ... 0.14-rc1: findDOMNode(statelessComponent) doesn't work with TestUtils.
Read more >React讀書筆記Script - 程式人生
0.14-rc1 : findDOMNode(statelessComponent) doesn't work with TestUtils.renderIntoDocument #4839. Writing Redux Tests 【譯】展望2016,React.js ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
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
One way to avoid ‘null’ or the TestUtils altogether when testing event handlers is to instantiate the component as a constructor (i.e. var comp = new MyComp(props) ).
Interesting find from this article.
I moved the stateless currently failing approach to wrap it in a
<div />
with the test utils. From this I can get to testing what I want, but it’s a bit awkward.https://github.com/iamdustan/react-testing-stateless-components/blob/master/__tests__/stateless-failure-test.js https://github.com/iamdustan/react-testing-stateless-components/blob/master/__tests__/stateless-success-test.js
Is there a better way of doing this that I’m unaware of? Or is this just the way it is going to be for a while?