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

|ReactNative |-条件呈现?:使用Jest&&Enzyme

  •  0
  • Mayoul  · 技术社区  · 7 年前

    下午好 我有一个全局结构如下的组件文件:

      class Component ...
    
          render(){
               const {array} = this.props
    
               {!array.includes(value) ?
                    (<View ...props
                        id="myComponent"/>
                          ....
                     </View>)  :
    
                    (<View ...props
                       id="myOtherComponent"/>
                          ....
                    </View>)
                }
            }
    

    describe('Testing Component', () => {
            test('conditional rendering', () => {
            const wrapper = shallow(<Component array={[value]}/>);
            expect(wrapper.find(n => n.prop('id') === "myOtherComponent").exists(true))
        });
    });
    

    但是,即使我修改了为阵列发送的道具,它也总是返回给我 true ... 检查嵌套组件是否已实际验证和呈现的关键字是什么。。。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Bruno Eduardo    7 年前

    我认为你的错误在于 expect 论点

    1. findWhere 功能代替 find ;
    2. exists 方法不应在此字段中接收参数 案例,因为它只接收酶的选择器,而不接收布尔值(您可以阅读更多关于它的信息) here );
    3. toBeTruthy 致电 预料 线

    这里有一个与您类似的情况,我们对其进行了测试,效果很好:

    it('tests name', () => {
      const mockComponent = shallow(<Component {...props} />);
      const textNode = mockComponent.findWhere(n => n.text() === props.name);
      expect(textNode.exists()).toBeTruthy();
    });
    

    describe('Testing Component', () => {
            test('conditional rendering', () => {
            const wrapper = shallow(<Component array={[value]}/>);
            const node = wrapper.findWhere(n => n.prop('id') === 'myOtherComponent');
            expect(node.exists()).toBeTruthy();
        });
    });