简体   繁体   English

如何使用Enzyme浅表和基于组件的UI进行测试?

[英]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. 我们目前使用的mountenzyme库而不是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.

相关问题 酶:如何测试浅层组件上的相互作用 - Enzyme: How to test interactions on shallow component Jest Enzyme如何对包裹成分的存在进行浅层测试 - Jest Enzyme how to shallow test for existence of wrapped component 酶在浅层试验中找不到儿童成分 - Enzyme cannot find child component in shallow test 如何使用 Shallow 组件 Jest/Enzyme 对不同的测试场景进行不同的模拟实现? - How to have different mock implementations for different test scenarios using the Shallow component Jest/Enzyme? 如何使用酶浅包装器对作为道具传递给子组件的React函数进行单元测试 - How to unit test React functions passed in as prop to child component with Enzyme shallow wrapper 如何使用酶作为实例()测试功能组件内部的方法,为浅包装器返回 null? - How to test methods inside functional component using enzyme as instance() returns null for shallow wrapper? 酶,测试一个使用useContext hook的react组件? - Enzyme, test a react component that uses useContext hook with shallow? 如何用酶测试包裹的成分? - How to test wrapped component with enzyme? 如何使用浅渲染测试装饰的React组件 - How to test decorated React component with shallow rendering 如何使用Jest模拟+酶浅测试和/或模拟ref回调节点并测试.querySelector? - How to test and/or mock ref callback nodes and test .querySelector with Jest mocking + Enzyme shallow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM