【问题标题】:Return null in Jest test in someComponent在 someComponent 的 Jest 测试中返回 null
【发布时间】:2018-08-14 05:52:38
【问题描述】:

我是使用 jest 测试 react-native 代码的新手,我想实现这个简单的测试。我有以下代码,我想测试如果data === '0' 则返回值null

export class Test extends Component {
...
  _renderRow (data) {
    if (data === '0') {
      return null
    }

    return //other item
  }
...
}

我尝试了以下测试,但它仍在读取return //other item,因为上面有一个数据变量。

const _renderRowSpy = jest.spyOn(Feed.prototype, '_renderRow')
  it('should be null', () => {
    function callback(data) {
      expect(data.type()).to.equal(null);
    }
    _renderRowSpy(callback);
  });

【问题讨论】:

  • 有什么建议我可以在测试中编写条件,或者只是读取 if 语句中的值?

标签: javascript react-native testing jestjs enzyme


【解决方案1】:

好的,从一位非常好的朋友那里得到了一些帮助。如果有人有类似的测试,这是解决方案。

it('should be null', () => {
    const instance = wrapper.instance()
    const result = instance._renderRow('0')
    expect(result).toBe(null)
  })

【讨论】:

    猜你喜欢
    • 2018-09-24
    • 2018-04-25
    • 2022-09-29
    • 2018-09-15
    • 2020-12-29
    • 1970-01-01
    • 1970-01-01
    • 2018-03-24
    • 1970-01-01
    相关资源
    最近更新 更多