【问题标题】:Ternary operators test case with jest三元运算符用 jest 测试用例
【发布时间】:2019-05-06 15:43:21
【问题描述】:

我想为以下案例编写一个开玩笑的测试用例,因为它显示分支覆盖率50% 并指出此代码。

render() {
        const {
          isExit
        } = data;
        const text = isExit ? 'Yes' : 'No';

<LabelValue label="New Line" value={isExit ? 'Yes' : 'No'} />

测试用例

it('Should display the data if API status is complete', () => {
    const wrapper = shallowWithTheme(<DataPage
      orderDetail={{ isExit: true}}
      theme={theme}
    />);

    // what to write here?   
  });

【问题讨论】:

  • 您可以尝试:wrapper.setProps({orderDetail: {isExit: false}}); wrapper.update(); 触发另一种情况。
  • @izb;是的,但是在 wrapper.update() 之后我应该测试什么
  • expect(wrapper).toMatchSnapshot()
  • 你为什么使用shallowWithTheme 而不仅仅是shallow
  • @RyanWalker:我正在为需要主题道具的组件使用 styled-component

标签: reactjs jestjs enzyme


【解决方案1】:
expect(wrapper.text()).to.equal('Yes');
expect(wrapper.text()).to.equal('No');

【讨论】:

    【解决方案2】:

    测试两个条件:

      it('Should display the data if API status is complete', () => {
        const wrapper = shallowWithTheme(<DataPage
          orderDetail={{ isExit: true}}
          theme={theme}
        />);
    
        expect(wrapper.html()).toMatchSnapshot(); // 1st snapshot
        wrapper
          .setProps({orderDetail: {isExit: false}, theme})
        // check your snapshot with new props :)
        expect(wrapper.html()).toMatchSnapshot(); // 2nd snapshot
      });
    

    祝你好运……

    【讨论】:

      猜你喜欢
      • 2018-07-07
      • 1970-01-01
      • 2021-01-25
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2016-03-16
      • 2020-03-14
      相关资源
      最近更新 更多