johntanquinco
johntanquinco

Reputation: 1393

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);
  });

Upvotes: 4

Views: 6061

Answers (1)

johntanquinco
johntanquinco

Reputation: 1393

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)
  })

Upvotes: 3

Related Questions