如何找到子组件而不必指定将它们包装的HOC?

问题描述 投票:0回答:1

[通常,当我使用Material-ui时,它的可扩展性妨碍了测试。

这是因为即使我正在使用,说:

const MyEventButton = () => (<IconButton />
  <Event />
</IconButton>)

我在浅渲染的测试输出中得到的是:

<WithStyles(ForwardRef(IconButton)) >
  <Event />
</WithStyles(ForwardRef(IconButton)) >

此HOC包装不是我在测试中过度关心的事情(即我正在[[not测试组件是WithStyles或使用ForwardRef]),因为这是一个实现细节,如果没有我,它可能会更改想要/需要更新测试。

最终,这意味着我不能将测试编写为:

it('Renders IconButton', () => { const wrapper = shallow(<MyEventButton />); expect(wrapper.exists('IconButton')).toEqual(true); }

我必须做:

it('Renders IconButton', () => { const wrapper = shallow(<MyEventButton />); expect(wrapper.exists('WithStyles(ForwardRef(IconButton))')).toEqual(true); }

感觉就像我将测试与材料当前的内部工作紧密结合在一起。

是否有一种方法可以在HOC内部进行对等并获取基本的Child组件?优选地,不必dive或猜测children().at(0)在组件中的位置。出于类似原因,使用`wrapper.exists()也会失败。

我看过Stackoverflow和互联网,却找不到可行的解决方案。

javascript reactjs unit-testing material-ui enzyme
1个回答
0
投票
您不必创建组件来测试它的存在
© www.soinside.com 2019 - 2024. All rights reserved.