【问题标题】:How to access Nested component in Enzyme React testing如何在 Enzyme React 测试中访问嵌套组件
【发布时间】:2018-10-11 10:22:53
【问题描述】:
import React from "react";
import Adapter from "enzyme-adapter-react-16";
import { configure, shallow, mount } from "enzyme";
import Banking, { BankingForm } from './Banking';

configure({ adapter: new Adapter() });
describe('FormikHandlers', () => {
    describe('handleChange', () => {
      it('change value for firstBankName', async () => {
      const component = shallow(<Banking />);

      expect(component.find(BankingForm)).toHaveLength(1);

    });
  });
});

在这里,我将 Banking 作为父组件,将 bankingForm 作为具有所有字段的子组件。我在 Baking 父组件中使用 formik 渲染道具。 此测试失败,因为当它被浅渲染时,我无法在组件内部获取“BankingForm”组件。 我在酶的观点上做错了吗?

【问题讨论】:

  • 你能展示你的组件吗?你在那里使用任何 HOC 吗?除了.dive()/mount() 可能会有所帮助我想知道处理彻底更好的测试方法是否更好

标签: reactjs unit-testing jestjs enzyme formik


【解决方案1】:

您可以使用mount 来渲染整个组件,或者使用dive 让树更深一层

【讨论】:

  • component.dive().find(BankingForm) 那样?
  • 可以,但仍取决于您的组件结构。查看dive 文档的示例。
  • 其实是把
    包裹在了组件里面。所以不能直接使用。
猜你喜欢
  • 2019-05-26
  • 2016-10-14
  • 2019-06-10
  • 2017-01-01
  • 2020-10-06
  • 2017-02-12
  • 2018-04-25
  • 1970-01-01
  • 2015-07-19
相关资源
最近更新 更多