Skip to content
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

ReactTestUtils.isCompositeComponent (called by scry*) throws on stateless components #4882

Closed
taion opened this issue Sep 15, 2015 · 4 comments
Milestone

Comments

@taion
Copy link

taion commented Sep 15, 2015

Example:

const Component1 = () => <div>Foo</div>;
class Component2 extends React.Component {
  render() {
    return (
        <div>Foo</div>
    );
  }
}
const instance = ReactTestUtils.renderIntoDocument(<Component1 />);

const div = ReactTestUtils.findRenderedDOMComponentWithTag(
  instance, 'DIV'
);
expect(div).to.exist;

This works with Component2 but not with Component1.

@jimfb
Copy link
Contributor

jimfb commented Sep 18, 2015

Stateless components have no public instance (return value of renderIntoDocument). If you were to wrap it in another (stateful) composite then you could use that instance. There is some relevant discussion in #4839. At this point, I think we're going to keep this behavior. I created an issue to document it: #4913

@jimfb jimfb closed this as completed Sep 18, 2015
@happypoulp
Copy link

@jimfb I am having the same issue with scryRenderedDOMComponentsWithTag and I don't understand the "wrapping" approach you're suggesting.
I tried:

const StatelessComponent = () => <div>foo</div>

const Wrap = class Wrap extends React.Component {
  render () {
    return <StatelessComponent />
  }
}

but then calling

  const component = renderIntoDocument(<Wrap />)
  const div = scryRenderedDOMComponentsWithTag(component, 'div')

produce this error:

.../node_modules/react/lib/ReactTestUtils.js:102
    return typeof inst.render === 'function' && typeof inst.setState === 'function';
                      ^

TypeError: Cannot read property 'render' of null
    at Object.ReactTestUtils.isCompositeComponent (.../node_modules/react/lib/ReactTestUtils.js:102:23)
...

Did I misunderstood what "wrap it in another (stateful) composite" means?

Thanks!

@sophiebits
Copy link
Collaborator

@happypoulp That might just be a different issue. Can you open it separately?

@sophiebits
Copy link
Collaborator

Actually never mind we can use this one.

@sophiebits sophiebits reopened this Sep 24, 2015
@sophiebits sophiebits changed the title ReactTestUtils scrying doesn't work with stateless components ReactTestUtils.isCompositeComponent (called by scry*) throws on stateless components Sep 24, 2015
@sophiebits sophiebits added this to the 0.14 milestone Sep 24, 2015
sophiebits added a commit to sophiebits/react that referenced this issue Sep 30, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants