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.mockComponent does not pass props #2660

Closed
awei01 opened this issue Dec 4, 2014 · 3 comments
Closed

ReactTestUtils.mockComponent does not pass props #2660

awei01 opened this issue Dec 4, 2014 · 3 comments

Comments

@awei01
Copy link

awei01 commented Dec 4, 2014

In using mocked components in test, all props don't get passed to the ConvenienceConstructor mocked component. Only the this.props.children get passed. Is there a particular reason that this.props don't get passed as well? I think it would make asserting that certain props get passed down to the sub-module easier.

Looking at ReactTestUtils.js (https://github.com/facebook/react/blob/master/src/test/ReactTestUtils.js#L254):

mockComponent: function(module, mockTagName) {
    mockTagName = mockTagName || module.mockTagName || "div";

    var ConvenienceConstructor = React.createClass({displayName: 'ConvenienceConstructor',
      render: function() {
        return React.createElement(
          mockTagName,
          null, // why isn't this this.props to allow for assertions on passed props?
          this.props.children
        );
      }
    });

...

After doing a little more digging, I found some more info. Given the module:

module.exports = React.createClass({
    _renderItems: function() {
        return this.props.items.map(function(item, index) {
            return <Child key={ index } somePassedProp={ item }/>
        });
    },
    render: function() {
        var items = this._renderItems();
        return <Parent ref="parent" someProp="foo">{items}</Parent>;
    }
});

The props do get passed properly and can be asserted when accessing the mocked component using a named ref. Thus, you can assert that component.refs.props.someProp = "foo". However, unless I'm missing something, you cannot access all of the props of a sub-module that is iterated over: var children = TestUtils.scryRenderedDOMComponentsWithTag(component, 'CHILD'); children[0].props.somePassedProp will always be undefined.

@liambrown89
Copy link

I agree with awei01 - it would be nice to be able to easily assert the props that are being passed to a sub component.

@liambrown89
Copy link

Any word/news/comment on this?

@gaearon
Copy link
Collaborator

gaearon commented Oct 27, 2016

mockComponent is neither maintained and not recommended. Please either use shallow rendering instead, or mock components with Jest which gives you an ability to pass anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants