[英]enzyme assert react component as children
I have this Input component that will have prop to show an Error component, I'm struggling to find a solution using enzyme. 我有这个输入组件,它会显示一个错误组件,我正在努力寻找一种使用酶的解决方案。
import Error from './Error'
const inputShallow = (props = {}) => shallow(<Input {...props} />)
describe.only('when there is an error', () => {
beforeEach(() => {
props.error = 'This is an error'
InputContainerInstance = inputShallow(props)
ErrorInstance = InputContainerInstance.children().find(
`.error`
)
})
it('Should contain the "Error" component', () => {
//what to do here hmmm??
})
})
Tried expect(ErrorInstance.contains(<Error />)).to.equal(true)
but won't work. 尝试了
expect(ErrorInstance.contains(<Error />)).to.equal(true)
但不起作用。
I have found the easiest way to verify the rendering of a component is to use snapshot testing. 我发现验证组件渲染的最简单方法是使用快照测试。
Since you are already using enzyme, just add enzyme-to-json as a dev dependency and add this 由于您已经在使用酶,因此只需将酶至json添加为dev依赖项并添加
"jest": {
"snapshotSerializers": [
"enzyme-to-json/serializer"
]
}
to your package.json (or just add the "snapshotSerializers" if you already have a "jest" key). 到您的package.json中(或者如果您已经有一个“笑话”键,只需添加“ snapshotSerializers”)。
Then in your test you can do the following: 然后,在测试中,您可以执行以下操作:
expect(InputContainerInstance).toMatchSnapshot();
enzyme-to-json creates a very nicely formatted snapshot that is easy to verify visually. Enzyme-to-json会创建一个格式很好的快照,易于直观地进行验证。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.