简体   繁体   中英

Return null in Jest test in someComponent

Im new to testing react-native code using jest and I want to achieve this simple test. I have the following code and I want to test that if data === '0' then return value null .

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

    return //other item
  }
...
}

I tried the following test, but its still reading the return //other item since there is a data variable on it.

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

Okay, got some help from a very good friend. Here's the solution if someone has similar test.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM