Enzyme: Can't simulate press event in react-native

9

We have implemented NativeBase's CheckBox where you can find here.
It simply iterates over a list and return a set of ListItems which contains CheckBox.
We are trying to write unit test of this component and stuck at simulation of press event.
The press event is bind to ListItems. You can find the test code below. Having trouble testing pressevent.

import * as React from 'react'
import { mount } from 'enzyme'
import * as chai from 'chai'
import { ListItem } from 'native-base'

it('onPress()', () => {
        const wrapper = mount(getComponent(commonProps))
        wrapper.find(ListItem).first().simulate('press')
})

And the console output is

TypeError: ReactWrapper::simulate() event 'press' does not exist

We appreciate any feedback about event simulation.

cigdemgullu picture cigdemgullu  ·  19 Jun 2017

Most helpful comment

75

Just to be sure, because you're using press you're under React-Native?
Does wrapper.find(ListItem).first().props().onPress() work?

rndmerle picture rndmerle  ·  5 Jul 2017

All comments

75

Just to be sure, because you're using press you're under React-Native?
Does wrapper.find(ListItem).first().props().onPress() work?

rndmerle picture rndmerle  ·  5 Jul 2017
33

In general, I've found it's best to invoke the prop directly and avoid .simulate.

ljharb picture ljharb  ·  5 Jul 2017
0

Thanks a lot

cigdemgullu picture cigdemgullu  ·  6 Jul 2017