代码之家  ›  专栏  ›  技术社区  ›  Black

作为子组件的浅层测试函数

  •  4
  • Black  · 技术社区  · 7 年前

    如何测试一个作为子级使用函数的组件?

    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
    
    1 回复  |  直到 7 年前
        1
  •  1
  •   Black    7 年前

    子对象必须作为一个属性来获取,而不是直接调用子对象,I18n也必须是浅层的。

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