Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RN v0.62.0-rc.0 - Touchable[Highlight|WithoutFeedback] are rendering into a component without name under jest #27721

Closed
devproivansurzhenko opened this issue Jan 9, 2020 · 22 comments
Labels
Bug Resolution: Locked This issue was locked by the bot.

Comments

@devproivansurzhenko
Copy link

I am trying to migrate from 0.59.3 to 0.62 rc0

However, I got a strange state of the app. Touchable[Highlight|WithoutFeedback] became to render without display name under jest (see snippets below).

I have the following control:

function backspaceIcon(backspacer: Backspacer, iconProps: IconProps) {
  return (
    <TouchableHighlight
      activeOpacity={1}
      underlayColor="#FFF"
      onShowUnderlay={() => (backspacer.backspaceUnderlay = true)}
      onHideUnderlay={() => (backspacer.backspaceUnderlay = false)}
      onPressIn={() => backspacer.backspace()}
      delayLongPress={500}
      onLongPress={() => backspacer.startBackspace()}
      onPressOut={() => backspacer.endBackspace()}
      style={styles.backspace}
      accessibilityRole="button"
      accessibilityLabel="delete"
    >
      <Icon
        {...iconProps}
        color={backspacer.backspaceUnderlay ? '#999' : '#333'}
      />
    </TouchableHighlight>
  )
}

Snapshot before migration:

<TouchableHighlight
  accessibilityLabel="delete"
  accessibilityRole="button"
  activeOpacity={1}
  delayLongPress={500}
  onHideUnderlay={[Function]}
  onLongPress={[Function]}
  onPressIn={[Function]}
  onPressOut={[Function]}
  onShowUnderlay={[Function]}
  style={
    Object {
      "alignItems": "center",
      "borderRadius": 15,
      "display": "flex",
      "height": 60,
      "justifyContent": "center",
      "width": 60,
    }
  }
  underlayColor="#FFF"
>
  <View
    color="#333"
    name="ios-backspace"
    size={30}
  />
</TouchableHighlight>

Snapshot after migration (no TouchableHighlight name):

<
  accessibilityLabel="delete"
  accessibilityRole="button"
  activeOpacity={1}
  delayLongPress={500}
  onHideUnderlay={[Function]}
  onLongPress={[Function]}
  onPressIn={[Function]}
  onPressOut={[Function]}
  onShowUnderlay={[Function]}
  style={
    Object {
      "alignItems": "center",
      "borderRadius": 15,
      "display": "flex",
      "height": 60,
      "justifyContent": "center",
      "width": 60,
    }
  }
  underlayColor="#FFF"
>
  <View
    color="#333"
    name="ios-backspace"
    size={30}
  />
</>

The same thing happens to TouchableWithoutFeedback. However, other controls look fine.

React Native version:
0.62 rc0

Steps To Reproduce

Not sure.

Describe what you expected to happen:
TouchableHighlight rendered to TouchableHighlight
TouchableWithoutFeedback rendered to TouchableWithoutFeedback

@devproivansurzhenko
Copy link
Author

Interesting... I have replaced the following line

module.exports = (React.forwardRef((props, hostRef) => (

with

module.exports = TouchableHighlight

and now it works fine.

Those lines are introduced there: 7c01172

@yungsters

@yungsters
Copy link
Contributor

@scotthovestadt is working on a fix to how Jest renders React.forwardRef in snapshots.

@devproivansurzhenko
Copy link
Author

@scotthovestadt Am I right that PR jestjs/jest#9422 is what I am looking for?

@devproivansurzhenko
Copy link
Author

Ah.. no.. it is not. Sorry

@MarianBe
Copy link

I have the same problems here, since 0.62.0 all of my tests with TouchableHighlight or TouchableOpacity-Components now fail.

@devproivansurzhenko
Copy link
Author

@yungsters @scotthovestadt Is there any link that we can use to track it? or even help with the fix.

@halilb
Copy link

halilb commented Apr 2, 2020

This problem has broken our native-testing-librarytests after upgrading to react-native 0.62: testing-library/native-testing-library#113

I applied a temp fix by mocking TouchableOpacity:

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

@antpuleo2586
Copy link

@halilb Thanks for your snippet, but I had to modify slightly because I have the same issue with TouchableHighlight. All tests now passing though:

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

    MockTouchable.displayName = 'TouchableOpacity';

    return MockTouchable;
});

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

    MockTouchable.displayName = 'TouchableHighlight';

    return MockTouchable;
});

hyochan added a commit to hyochan/dooboo-native-ts that referenced this issue Apr 2, 2020
@halilb
Copy link

halilb commented Apr 2, 2020

@halilb Thanks for your snippet, but I had to modify slightly because I have the same issue with TouchableHighlight. All tests now passing though:

Oh I've forgotten adding MockTouchable.displayName = 'TouchableOpacity' line there. Thanks for the feedback. Mocking that with View also worked for me.

@sebk
Copy link

sebk commented Apr 4, 2020

Isn't a simpler version enough:

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

Currently for my snapshot tests this line seems to be enough. Do you see any problem with that?

@mtt87
Copy link

mtt87 commented Apr 8, 2020

I discovered that if you export them from react-native-gesture-handler it still works with RN0.62 however by using the react native one it breaks testing libraries, for example .toBeDisabled()

// works
import { TouchableOpacity } from 'react-native-gesture-handler'
// doesn't work
import { TouchableOpacity } from 'react-native'

More info here testing-library/jest-native#23 (comment) but tl;dr I suspect the issue might be related to bundling/transpiling? Seems like the displayName or name is lost.

Actually I think the reason behind this is the fact that I heard they want to move towards a single Touchable element (which seems like they have already built behind the scenes perhaps) so they used that forwardRef to provide backwards compatibility but seems like it didn't work as expected as the displayName is lost.

Looking at #21937 (comment) seems like a quick fix could be

TouchableOpacity = (React.forwardRef((props, hostRef) => (...
TouchableOpacity.displayName = 'TouchableOpacity';
module.exports = TouchableOpacity

@devproivansurzhenko
Copy link
Author

I still have issues with firing events. My fireEvent.press(...) does not cause handler calls.

Sounds like it is related to this ticket also testing-library/native-testing-library#106

@gerryfletch
Copy link

Oddly I have the same issue with TextInput being rendered as Component after upgrading to 0.62.2. I mocked it in the same fashion and my tests passed.

@caschomburg123
Copy link

caschomburg123 commented Apr 19, 2020 via email

@kevinpiac
Copy link

kevinpiac commented Apr 27, 2020

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

@sebk Working for me! Thanks :)

@askel4dd
Copy link

Came up with this, snapshots will be changed a little, but events tests will pass:

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

    TouchableOpacity.displayName = 'TouchableOpacity';

    return TouchableOpacity
});

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

    TouchableHighlight.displayName = 'TouchableHighlight';

    return TouchableHighlight
});

@diegotsi
Copy link

@halilb Thanks for your snippet, but I had to modify slightly because I have the same issue with TouchableHighlight. All tests now passing though:

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

    MockTouchable.displayName = 'TouchableOpacity';

    return MockTouchable;
});

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

    MockTouchable.displayName = 'TouchableHighlight';

    return MockTouchable;
});

same issue here, this solution worked for me.

@stale
Copy link

stale bot commented Aug 16, 2020

Hey there, it looks like there has been no activity on this issue recently. Has the issue been fixed, or does it still require the community's attention? This issue may be closed if no further activity occurs. You may also label this issue as a "Discussion" or add it to the "Backlog" and I will leave it open. Thank you for your contributions.

@stale stale bot added the Stale There has been a lack of activity on this issue and it may be closed soon. label Aug 16, 2020
@devproivansurzhenko
Copy link
Author

I am on 0.62.1. Issue is still appearing. We just skipped those specs for now. I am planning to migrate my project to 0.63 during the current week.

@stale stale bot removed the Stale There has been a lack of activity on this issue and it may be closed soon. label Aug 17, 2020
js-jslog pushed a commit to js-jslog/harpguru that referenced this issue Aug 28, 2020
js-jslog pushed a commit to js-jslog/harpguru that referenced this issue Aug 28, 2020
@MattyK14
Copy link

MattyK14 commented Sep 2, 2020

@devproivansurzhenko I just migrated to 0.63.2, are you not still seeing this problem?

@chgsilva
Copy link

i am facing this issue on 0.63.4

@brunohkbx
Copy link
Contributor

This issue should be fixed here: #29531. Could someone please review it?

hyochan added a commit to hyochan/dooboolab.com that referenced this issue Jan 16, 2021
@facebook facebook locked as resolved and limited conversation to collaborators Apr 1, 2022
@react-native-bot react-native-bot added the Resolution: Locked This issue was locked by the bot. label Apr 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug Resolution: Locked This issue was locked by the bot.
Projects
None yet
Development

Successfully merging a pull request may close this issue.