【问题标题】:Shallow test function as child components浅层测试功能作为子组件
【发布时间】:2018-09-17 17:57:44
【问题描述】:

我添加了一个组件,它使用函数作为子组件。我对这个停止工作的组件进行了浅酶测试。

如何对一个使用函数的组件进行浅层测试?

组件

return (
  <I18n>
    {({ i18n }) => (
      <div />
    )}
  </I18n>
)

我尝试过的测试

import { shallow } from 'enzyme';

wrapper = shallow(<Component />)
  .find('I18n')
  .children();
console.log(wrapper.debug()); //outputs: [function]


wrapper = shallow(<Component />)
  .find('I18n')
  .children()();
// TypeError: (0 , _enzyme.shallow)(...).find(...).children(...) is not a function

【问题讨论】:

    标签: reactjs jestjs enzyme


    【解决方案1】:

    子元素必须作为属性获取,而不是直接调用子元素,并且 I18n 也必须是浅渲染的。

    const outerWrapper = shallow(< Component />);
    wrapper = shallow(outerWrapper.find('I18n').prop('children')());
    

    【讨论】:

      猜你喜欢
      • 2020-05-15
      • 2019-01-28
      • 2020-12-18
      • 2018-02-09
      • 2022-12-03
      • 2019-09-25
      • 2020-03-03
      • 1970-01-01
      • 2019-07-22
      相关资源
      最近更新 更多