首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >测试是否渲染具有所需PropTypes的Material-UI withStyles组件?

测试是否渲染具有所需PropTypes的Material-UI withStyles组件?
EN

Stack Overflow用户
提问于 2020-07-10 10:18:24
回答 1查看 37关注 0票数 0

我是个单元测试新手,在测试带有所需propTypes的Material-UI withStyles组件是否呈现子Material-UI元素时遇到了麻烦。

profile.js

代码语言:javascript
运行
复制
const StaticProfile = (props) => {
  const { classes, profile } = props
}

return (
  <Paper>
    <div>
      <MuiLink></MuiLink>
      <Typography>
      <LocationOn>
      <LinkIcon>
      <Calendar>
    </div>
  </Paper>
)

profile.test.js

代码语言:javascript
运行
复制
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);
  })
})

但是,组件看起来根本不像是在接收到此错误时呈现的。

代码语言:javascript
运行
复制
expect(received).toBe(expected) // Object.is equality

Expected: 1
Received: 0

有人能给我指个方向吗?

EN

Stack Overflow用户

回答已采纳

发布于 2020-07-10 16:37:41

您应该使用wrapper.dive().find(Paper),因为StaticProfile是由withStyles高阶组件包装的。

试试这个:

代码语言:javascript
运行
复制
it('should render a Paper element', () => {
  expect(wrapper.dive().find(Paper)).toHaveLength(1);
})

票数 0
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62826380

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档