[英]How to test with Enzyme shallow and component based UIs?
We are using the Semantic React UI library . 我们正在使用语义React UI库 。 The code often looks like:
该代码通常如下所示:
<div className="EditTemplateMetaDataPage">
<Page title={...}>
<Container text>
<Segment>
<Grid columns={1}>
<Grid.Column>
<Button ...>
This might be interesting for many people, using similar React Component libraries like Material UI or Bootstrap React. 对于许多人来说,使用类似的React Component库(例如Material UI或Bootstrap React)可能会很有趣。
We currently use mount
with the enzyme
library instead of shallow
, because we would render only one level deep , to the first <Grid>
component in a test, which is just a visual helper, while we really need the deeper buried Button
instead. 我们目前使用的
mount
与enzyme
库而不是shallow
,因为我们将使只有一级,到第一<Grid>
在测试中,这仅仅是一个视觉助手组件,而我们真正需要更深埋藏Button
来代替。
Because of performance and to avoid overlapping tests it is recommended to use shallow
. 由于性能和避免重复测试 ,建议使用
shallow
。 (We follow London school TDD , and check only that sub-components exist and their interfaces are used properly) (我们遵循伦敦学校TDD的规定 ,仅检查子组件是否存在以及它们的接口是否正确使用)
We came up with using CSS-only for visual components, ie 我们想出了仅将CSS用于视觉组件,即
<div className='ui one column grid'>
instead of: 代替:
<Grid columns={1}>
But we are not sure, whether this is the optimal approach. 但是我们不确定这是否是最佳方法。 Do you have other ideas?
您还有其他想法吗? How can we use
shallow(...)
in this case? 在这种情况下,我们如何使用
shallow(...)
?
shallow actually allow you to use your component as selector, so you can do something like 浅实际上允许您使用您的组件作为选择器,因此您可以执行类似
import Button from '../Button'
const page = shallow(<Page />)
expect(page.find(Button).length).toBe(1)
http://airbnb.io/enzyme/docs/api/selector.html http://airbnb.io/enzyme/docs/api/selector.html
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.