Skip to content
This repository has been archived by the owner on Jul 30, 2020. It is now read-only.

fireEvent.press does not work on TouchableOpacity #113

Closed
hsjoberg opened this issue Feb 28, 2020 · 10 comments
Closed

fireEvent.press does not work on TouchableOpacity #113

hsjoberg opened this issue Feb 28, 2020 · 10 comments

Comments

@hsjoberg
Copy link

hsjoberg commented Feb 28, 2020

  • react-native or expo: react-native
  • native-testing-library version: 5.0.3
  • jest-preset: @testing-library/react-native
  • react-native: 0.62.0-rc.3
  • node version: v12.12.0

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.

@hsjoberg hsjoberg changed the title fireEvent.press does not work TouchableOpacity fireEvent.press does not work on TouchableOpacity Feb 28, 2020
@akiver
Copy link

akiver commented Mar 28, 2020

I had the same issue after upgrading to RN 0.62.
I think it's related to this issue facebook/react-native#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
Copy link

halilb commented Apr 2, 2020

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
Copy link

hyochan commented Apr 2, 2020

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

@mym0404
Copy link

mym0404 commented Apr 10, 2020

I am experiencing exactly the same thing.

@andrico1234
Copy link

@halilb Your fix worked for me!

@kevinpiac
Copy link

kevinpiac commented Apr 27, 2020

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
Copy link

Zloka commented Jun 3, 2020

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
Copy link

Can confirm the issue.

@daveols
Copy link
Contributor

daveols commented Jul 14, 2020

I've fixed this in #136 🎉

@daveols
Copy link
Contributor

daveols commented Jul 24, 2020

You can now upgrade to version 6.0.0 for this fix! 🚀

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests