Skip to content

Can't trigger on TouchableOpacity.onPress or Text.onPress event #12319

@rodrigopivi

Description

@rodrigopivi

Testing react-native TouchableOpacity.onPress does not work, can't manually trigger an onPress() event from tests.

Given a component that contains a TouchableOpacity:

export default function Button(props) {
    return (
        <TouchableOpacity
            accessible={false}
            onPress ={ props.onPress }
            style ={ someStyle }
        >
            <Text style={ someTextStyle }>
                Press ME!
            </Text>
        </TouchableOpacity>
    );
};

And given a test, that renders it:

it("renders and press button", () => {
    const btn = create(<Button onPress={() => ...} />);
    const btnTree = btn.toJSON();
    expect(btnTree).toMatchSnapshot(); // works
    debugger;
    btnTree.children[0].props.onPress() // too bad
});

If i break point just before trying to trigger the onPress, i can get to explore what is inside the tree for the TouchableOpacity component (btnTree.children[0]):

{ type: 'View',
  props: 
   { accessible: false,
     accessibilityLabel: undefined,
     accessibilityComponentType: undefined,
     accessibilityTraits: undefined,
     style: 
      { backgroundColor: '#F7C92F',
        alignSelf: 'stretch',
        borderRadius: 1,
        marginBottom: 5,
        opacity: 1 },
     testID: undefined,
     onLayout: undefined,
     isTVSelectable: true,
     tvParallaxProperties: undefined,
     hitSlop: undefined,
     onStartShouldSetResponder: [Function: val],
     onResponderTerminationRequest: [Function: val],
     onResponderGrant: [Function: val],
     onResponderMove: [Function: val],
     onResponderRelease: [Function: val],
     onResponderTerminate: [Function: val] },
  children: 
   [ { type: 'Text',
       props: [Object],
       children: [Object],
       '$$typeof': [Object] } ],
  '$$typeof': 
   { handle: 15,
     type: 'symbol',
     description: 'react.test.json',
     text: 'Symbol(react.test.json)' } }

As you can see, there is no onPress at the props, now if instead of adding the onPress at the TouchableOpacity, i add it to the Text component. I would be able to see an onPress prop there, but i'm still unable to call the method.

I would expect to be able to trigger an onPress for testing.

Using "react-native": "0.41.0", "react-test-renderer": "~15.4.2", "jest": "^18.1.0", with "preset": "react-native",

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions