admin管理员组文章数量:1289845
I have following React functional ponent which accepts name as a prop
const Name = ({ name }) => <h1>{name}</h1>;
Name.propTypes = {
name: PropTypes.string.isRequired
}
And my test is as follows
describe("<Name />", () => {
it("accepts name as a prop", () => {
const wrapper = shallow(<Name name="Monkey D Luffy"/>);
expect(wrapper.instance().props.name).toBe("Monkey D Luffy");
})
})
This returns following error
TypeError: Cannot read property 'props' of null
Update
I am calling the Name ponent as follows
class Person extends React.Component {
state = {name: "Name here"}
render () {
return <div><Name name={this.state.name} /></div>
}
}
I found out that calling instance()
on a stateless ponent not gonna work with react 16.* I can not use wrapper.prop()
in my case as it only returns the props of the root node
Why does it return null when calling instance function on the rendered ponent and how do I test props passed into a stateless ponent?
Original Question
Why does it return null when calling instance function on the rendered ponent
I have following React functional ponent which accepts name as a prop
const Name = ({ name }) => <h1>{name}</h1>;
Name.propTypes = {
name: PropTypes.string.isRequired
}
And my test is as follows
describe("<Name />", () => {
it("accepts name as a prop", () => {
const wrapper = shallow(<Name name="Monkey D Luffy"/>);
expect(wrapper.instance().props.name).toBe("Monkey D Luffy");
})
})
This returns following error
TypeError: Cannot read property 'props' of null
Update
I am calling the Name ponent as follows
class Person extends React.Component {
state = {name: "Name here"}
render () {
return <div><Name name={this.state.name} /></div>
}
}
I found out that calling instance()
on a stateless ponent not gonna work with react 16.* I can not use wrapper.prop()
in my case as it only returns the props of the root node
Why does it return null when calling instance function on the rendered ponent and how do I test props passed into a stateless ponent?
Original Question
Why does it return null when calling instance function on the rendered ponent
Share Improve this question edited Dec 17, 2018 at 15:16 skyboyer 23.8k7 gold badges62 silver badges71 bronze badges asked Dec 15, 2018 at 9:59 It worked yesterday.It worked yesterday. 4,62711 gold badges50 silver badges84 bronze badges 1- 1 how's your ponent structured as you mentioned you can't use wrapper.prop()? – Amit Chauhan Commented Dec 15, 2018 at 10:20
1 Answer
Reset to default 9instance() === null
for functional ponent because functional ponents don't have instances.
Enzyme's own API should be used to access ponent props:
expect(wrapper.prop('children')).toBe("Monkey D Luffy");
As the reference explains,
To return the props for the entire React ponent, use wrapper.instance().props. This is valid for stateful or stateless ponents in React 15.. But, wrapper.instance() will return null for stateless React ponent in React 16., so wrapper.instance().props will cause an error in this case.
本文标签: javascriptCalling instance() on shallow rendered functional component returns nullStack Overflow
版权声明:本文标题:javascript - Calling instance() on shallow rendered functional component returns null - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741428750a2378230.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论