-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Getting props trough shallow rendering is not working with defaultProps #384
Comments
I was able to reproduce the error here: Aweary/enzyme-test-repo/blob/issue-#384/test.js. |
+1 it looks like |
This is expected behavior, |
mount is not returning default props either |
You shouldn't need to assert on defaultProps; you should only assert on the props you pass in, and on the behavior of the component. defaultProps are an implementation detail. |
Even tho they are just an implementation detail, lack of them may break the component. I'm currently having a problem where the props that should have a default value are |
@zgredzik yes, but you should thus be testing the behavior that would break - not the presence or absence of default props. |
@ljharb I might have been not explicit enough: I am testing other behaviours but the component crashes while being mounted, thus making the test fail. The component itself requires some props to exist in a specified format: either by using When I want to test the behaviour of the component that does not depend on the optional properties I don't see a reason to be forced to pass them. Unless I got the whole concept of how |
The component gets the defaulted prop values; it's just .props() that isn't. I'm not seeing the issue. |
I guess I misunderstood what @mgenev was reporting and prematurely assumed that was exactly the same problem I encountered. Furthermore I've dug deeper into my own issue and it seems my lack of the default value is because of a problem in a 3rd party library I'm using and not a problem with |
try |
Yes, For a shallow wrapper, you can call a wrapper.instance().props.propName to get the value of respective prop. |
I have the problem using context when a child accesses its parent's props: class Parent {
static defaultProps {
hello: 'world'
};
static childContextTypes = {
parent: PropTypes.object.isRequired
};
getChildContext() {
return {
parent: this
};
}
}
class Child {
static contextTypes = {
parent: PropTypes.object.isRequired
};
render() {
console.log(this.context.parent.props.hello); // hello is undefined
}
}
const parent = new Parent({});
shallow(
<Child />,
{context: {parent: parent}}
); Hackish solution: class ParentEnzymeFix extends Parent {
constructor(props) {
const defaultProps = {
hello: 'world'
};
super({...defaultProps, ...props});
}
}
const parent = new ParentEnzymeFix({}); Edit: This generates a React warning: function new_Parent(props) {
const defaultProps = {
hello: 'world'
};
return new Parent({...defaultProps, ...props});
}
const parent = new_Parent({}); |
Issue:
When using
shallow
rendering and setting default props inside React component withgetDefaultProps()
(React.createClass) or withstatic defaultProps
(ES7), callingwrapper.props()
orwrapper.prop('myProp')
does not return any values (undefined) (it should return default ones)Although default props ARE set inside component, i cannot test them from outside with enzyme.
If i'll use
mount
, its working correctly.Gist:
https://gist.github.com/nehaleem/7fc6d6b2940bc523a2009a566ebfed97
The text was updated successfully, but these errors were encountered: