admin管理员组文章数量:1022712
Say I have
const BaseComponent = (props) => {
const classNames = ['base-ponent', props.className];
return (
<div className={classNames.join(' ')}>
{props.children}
</div>
)
};
const SomeComponent = () => {
return (
<BaseComponent
className="foo-bar"
>
Hello
</BaseComponent>
);
}
The rendered dom here would be <div class="base-ponent foo-bar">Hello</div>
Now, if I shallow mount SomeComponent
and test the classes, only foo-bar
is available:
const dom = shallow(<SomeComponent/>);
console.log(dom.hassClass('base-ponent')); // es out as false
I understand that only foo-bar
was passed as the class to SomeComponent
but how do I validate all other classes here too?
Say I have
const BaseComponent = (props) => {
const classNames = ['base-ponent', props.className];
return (
<div className={classNames.join(' ')}>
{props.children}
</div>
)
};
const SomeComponent = () => {
return (
<BaseComponent
className="foo-bar"
>
Hello
</BaseComponent>
);
}
The rendered dom here would be <div class="base-ponent foo-bar">Hello</div>
Now, if I shallow mount SomeComponent
and test the classes, only foo-bar
is available:
const dom = shallow(<SomeComponent/>);
console.log(dom.hassClass('base-ponent')); // es out as false
I understand that only foo-bar
was passed as the class to SomeComponent
but how do I validate all other classes here too?
2 Answers
Reset to default 4What if you use the .prop
API.
expect(dom.find('div').prop('className'))
.to.be.equal('base-ponent foo-bar');
shallow()
does only render the top level ponent which does not include your other class. You can use dive() to let it render the one ponent one level deeper. dive()
renders the only one non-DOM child of the wrapper it was called on.
const dom = shallow(<SomeComponent/>);
console.log(dom.dive().hasClass('base-ponent')); // now es out as true
If you want inspect the whole DOM you will have to use render()
instead of shallow()
.
Also not that a call to html()
on a ShallowWrapper
will always return the markup of a full render no matter if it was a shallow render or not.
Say I have
const BaseComponent = (props) => {
const classNames = ['base-ponent', props.className];
return (
<div className={classNames.join(' ')}>
{props.children}
</div>
)
};
const SomeComponent = () => {
return (
<BaseComponent
className="foo-bar"
>
Hello
</BaseComponent>
);
}
The rendered dom here would be <div class="base-ponent foo-bar">Hello</div>
Now, if I shallow mount SomeComponent
and test the classes, only foo-bar
is available:
const dom = shallow(<SomeComponent/>);
console.log(dom.hassClass('base-ponent')); // es out as false
I understand that only foo-bar
was passed as the class to SomeComponent
but how do I validate all other classes here too?
Say I have
const BaseComponent = (props) => {
const classNames = ['base-ponent', props.className];
return (
<div className={classNames.join(' ')}>
{props.children}
</div>
)
};
const SomeComponent = () => {
return (
<BaseComponent
className="foo-bar"
>
Hello
</BaseComponent>
);
}
The rendered dom here would be <div class="base-ponent foo-bar">Hello</div>
Now, if I shallow mount SomeComponent
and test the classes, only foo-bar
is available:
const dom = shallow(<SomeComponent/>);
console.log(dom.hassClass('base-ponent')); // es out as false
I understand that only foo-bar
was passed as the class to SomeComponent
but how do I validate all other classes here too?
2 Answers
Reset to default 4What if you use the .prop
API.
expect(dom.find('div').prop('className'))
.to.be.equal('base-ponent foo-bar');
shallow()
does only render the top level ponent which does not include your other class. You can use dive() to let it render the one ponent one level deeper. dive()
renders the only one non-DOM child of the wrapper it was called on.
const dom = shallow(<SomeComponent/>);
console.log(dom.dive().hasClass('base-ponent')); // now es out as true
If you want inspect the whole DOM you will have to use render()
instead of shallow()
.
Also not that a call to html()
on a ShallowWrapper
will always return the markup of a full render no matter if it was a shallow render or not.
本文标签: javascriptReact Enzyme get all classes even if passed by other componentsStack Overflow
版权声明:本文标题:javascript - React Enzyme get all classes even if passed by other components - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745531301a2154762.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论