我是个单元测试新手,在测试带有所需propTypes的Material-UI withStyles组件是否呈现子Material-UI元素时遇到了麻烦。
profile.js
const StaticProfile = (props) => {
const { classes, profile } = props
}
return (
<Paper>
<div>
<MuiLink></MuiLink>
<Typography>
<LocationOn>
<LinkIcon>
<Calendar>
</div>
</Paper>
)
profile.test.js
describe('<StaticProfile />', () => {
let shallow;
let wrapper;
const myProps = {
profile: {},
classes: {}
}
beforeEach(() => {
shallow = createShallow();
wrapper = shallow(<StaticProfile {...myProps} />);
})
it('should render a Paper element', () => {
expect(wrapper.find(Paper).length).toBe(1);
})
})
但是,组件看起来根本不像是在接收到此错误时呈现的。
expect(received).toBe(expected) // Object.is equality
Expected: 1
Received: 0
有人能给我指个方向吗?
发布于 2020-07-10 16:37:41
您应该使用wrapper.dive().find(Paper)
,因为StaticProfile是由withStyles
高阶组件包装的。
试试这个:
it('should render a Paper element', () => {
expect(wrapper.dive().find(Paper)).toHaveLength(1);
})
https://stackoverflow.com/questions/62826380
复制相似问题