-
Notifications
You must be signed in to change notification settings - Fork 47.3k
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
0.14-rc1: findDOMNode(statelessComponent) doesn’t work with TestUtils.renderIntoDocument #4839
Comments
The test (to save a couple clicks): it('render', function() {
var React = require('react');
var findDOMNode = require('react-dom').findDOMNode;
var StatelessComponent = require('../stateless');
var TestUtils = require('react/lib/ReactTestUtils');
var spy = jasmine.createSpy();
var component = TestUtils.renderIntoDocument(
<StatelessComponent text="Hello" onClick={spy} />
);
expect(findDOMNode(component).textContent).toBe('Hello');
}); I would also expect this to work… cc @spicyj |
I almost copied everything into here. Thanks @zpao. |
I haven’t hit any issues with stateless components and the shallow renderer, yet. |
Stateless components have no public instance. If you were to wrap it in another (stateful) composite then you could call findDOMNode on that component. cc @sebmarkbage for verification |
I moved the stateless currently failing approach to wrap it in a
https://github.com/iamdustan/react-testing-stateless-components/blob/master/__tests__/stateless-failure-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? |
You can use Shallow testing to avoid needing to reach into the DOM: https://facebook.github.io/react/docs/test-utils.html#shallow-rendering |
How do you test event handling with the shallow renderer? E.g. Are |
If you came here wondering about |
@iamdustan Right now it seems you cannot test event handling with the shallow renderer. At least that is what I gather from #1445. |
I'm having this same issue while trying to test stateless components ( Has there been any progress on this issue? |
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 can confirm that using the |
Closing as works by design. If you end up here read #4936 for more information. |
Unsure 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 othervar 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
https://github.com/iamdustan/react-testing-stateless-components/blob/master/stateless.js
Output
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).
The text was updated successfully, but these errors were encountered: