[英]Enzyme Styledcomponents shallow rendering returning `WithTheme(Component) `
This is a simple component I am trying to snapshot test这是我试图快照测试的一个简单组件
import React from 'react'
// snapshot test for this
export default function DogItem(props) {
const { imageUrl, name, subBreeds } = props
return (
<Card title={name} width={350} m={2}>
<Img src={imageUrl}></Img>
<Flex mt={2}>
{subBreeds.map(breed => (
<Badge key={breed.name} primary mr={2}>
{breed}
</Badge>
))}
</Flex>
</Card>
)
}
and here is my Jest test case这是我的 Jest 测试用例
import DogItem from '../DogItem'
import React from 'react'
import { shallow } from 'enzyme'
it('Renders table with correct attributes', () => {
let rendered = shallow(
<DogItem imageUrl="testUrl" name="dog name" subBreeds={[1, 2, 3]} />
)
console.log(rendered.debug());
})
In the output I get this在 output 我明白了
<Img src="testUrl" />
<Flex mt={2}>
<WithTheme(Component) primary={true} mr={2}>
1
</WithTheme(Component)>
<WithTheme(Component) primary={true} mr={2}>
2
</WithTheme(Component)>
<WithTheme(Component) primary={true} mr={2}>
3
</WithTheme(Component)>
</Flex>
</WithTheme(Component)>
I am not quite sure why I am getting WithTheme(Component)
instead of Badge
or Img
.我不太清楚为什么我得到
WithTheme(Component)
而不是Badge
或Img
。 I do realize these components are styled components however shallow is just suppose to render the one level deep right?我确实意识到这些组件是样式化的组件,但是浅层只是假设渲染一层深,对吗?
First, your tree is rendered one level deep, that's still correct.首先,你的树被渲染了一层,这仍然是正确的。
Second, it's about way enzyme
realizes what component to output for human-readable form like .debug()
(and also for enzyme-to-json
as well).其次,它是关于
enzyme
实现 output 的哪些组件以用于人类可读的形式,如.debug()
(以及enzyme-to-json
也是如此)。 It just uses displayName
property, and for HOCs like withTheme
you have to specify it manually:它只使用
displayName
属性,对于像withTheme
这样的 HOC,您必须手动指定它:
const Badge = withTheme(...)
Badge.displayName = 'Badge';
In their repo team's member mentionstheir Babel plugin that(probably) will do that for you automatically.在他们的 repo 团队的成员中提到他们的 Babel 插件(可能)会自动为您执行此操作。 But telling the truth I did not get what it can use for
displayName
alongside just file name.但说实话,除了文件名,我没有得到它可以用于
displayName
的东西。 Give it a try anyway.还是试试吧。
Without that there is no way for withTheme
to know what variable's name you are going to use.没有它,
withTheme
就无法知道您将使用什么变量的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.