testing-library / native-testing-library

🐳 Simple and complete React Native testing utilities that encourage good testing practices.
https://native-testing-library.com
MIT License
516 stars 44 forks source link

fireEvent.press does not work on TouchableOpacity #113

Closed hsjoberg closed 4 years ago

hsjoberg commented 4 years ago

Relevant code or config:

import React from "react";
import { TouchableOpacity } from "react-native";
import { render, fireEvent } from "@testing-library/react-native";

it("test", async () => {
  const { findByTestId } = render(
    <TouchableOpacity testID="button" onPress={() => console.log("TEST")} />
  );
  const button = await findByTestId("button");
  fireEvent.press(button);
});

What you did:

In 0.62.0-rc.3, fireEvent.press does not seem to work at all on TouchableOpacity. Trying on for example a Button seems to work fine, just not TouchableOpacity (and presumably similar components).

Reproduction:

See example code above. Running it you should see a console logged "TEST" message, in 0.62.0-rc.3, it does not appear.

Can you help us fix this issue by submitting a pull request?

Yes, though I don't know what's wrong yet.

akiver commented 4 years ago

I had the same issue after upgrading to RN 0.62. I think it's related to this issue https://github.com/facebook/react-native/issues/27721.

displayName is missing and native-testing-library rely on it to fire events.

[Function: Component] {
      displayName: '', <- Supposed to be TouchableOpacity or TouchableHighlight
      '$$typeof': Symbol(react.forward_ref),
      render: [Function]
 }
halilb commented 4 years ago

Our tests have also broken after upgrading to RN 0.62. I've applied a temporary fix to make the tests pass again by mocking TouchableOpacity with TouchableHighlight in jest.setup.js file:

jest.mock(
  'react-native/Libraries/Components/Touchable/TouchableOpacity.js',
  () => {
    const { TouchableHighlight } = require('react-native')
    const MockTouchable = props => {
      return <TouchableHighlight {...props} />
    }
    MockTouchable.displayName = 'TouchableOpacity'

    return MockTouchable
  }
)
hyochan commented 4 years ago

Thanks for the issue. Having same problem. When I've updated the snapshot I see all the TouchableOpacity are gone. image

mym0404 commented 4 years ago

I am experiencing exactly the same thing.

andrico1234 commented 4 years ago

@halilb Your fix worked for me!

kevinpiac commented 4 years ago

jest.mock( 'react-native/Libraries/Components/Touchable/TouchableOpacity.js', () => { const { TouchableHighlight } = require('react-native') const MockTouchable = props => { return <TouchableHighlight {...props} /> } MockTouchable.displayName = 'TouchableOpacity'

return MockTouchable

} )

@halilb Thanks for this solution, however, this fix is not totally working for me since it breaks a lot of other unit tests. πŸ€•

However, this simple snippet as mentioned here works fine for me :)

I hope it would help some mates πŸ˜ƒ

⏬Working solution for me ⏬

jest.mock('react-native/Libraries/Components/Touchable/TouchableOpacity', () => 'TouchableOpacity');

Zloka commented 4 years ago

I'm running into the same issue when trying to the Button component of react-native-paper. It seems to use TouchableWithoutFeedback under the hood.

Any pointers on how I'd go about mocking that out? Using the above (but for TouchableWithoutFeedback) doesn't seem to do the trick πŸ€”

EDIT: Naturally got it working a few minutes after posting... The following snippet did the trick in my case:

jest.mock(
  'react-native/Libraries/Components/Touchable/TouchableHighlight',
  () => {
    const TouchableHighlight = jest.requireActual(
      'react-native/Libraries/Components/Touchable/TouchableHighlight',
    );

    TouchableHighlight.displayName = 'TouchableHighlight';

    return TouchableHighlight;
  },
);
michelalbers commented 4 years ago

Can confirm the issue.

daveols commented 4 years ago

I've fixed this in #136 πŸŽ‰

daveols commented 4 years ago

You can now upgrade to version 6.0.0 for this fix! πŸš€