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

View's `measure` + other native methods are null #129

Closed bgneu closed 4 years ago

bgneu commented 4 years ago

Relevant code or config:

_updatePosition(callback) {
    if (this._button && this._button.measure) {
      this._button.measure((fx, fy, width, height, px, py) => {
        this._buttonFrame = {
          x: px,
          y: py,
          w: width,
          h: height,
        };
        callback && callback();
      });
    }
  }

this._button is a ref to TouchableOpacity component.

What you did:

Upgraded existing project from RN 0.61.5, where I had this reported issue, to 0.62.2.

With the upgrade, however, it seems like the mock-native-methods are no longer accessible because on close inspectionthis._button.measure === null returns true on tests. This is also true for the other native methods.

What happened:

The measure function is not available, therefore not being called.

Reproduction:

I'll try to update the issue with a repo that reproduces the issue.

Problem description:

The measure method, along with all other native methods mentioned in RN docs should be available on most of the default components provided by React Native.

Suggested solution:

Attempted to look into the @testing-library/react-native preset code, but could not spend too much time as to come up with a solution or improvement.

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

Would love to, but I have some other work priorities which may not allow me to devote the needed time to fix this. If it changes before this is resolved I'll definitely look into it.

bgneu commented 4 years ago

Closing this issue, because as I debugged a bit more the issue is with the Touchable components themselves. In order to get the press fireEvent to work we have to mock TouchableOpacity (see https://github.com/testing-library/native-testing-library/issues/113#issuecomment-607796505), however once mocked, attempting to get a reference to the component does not work, which then snowballs into the issue that caused me to create this ticket.

A work around that's working for me now is

jest.mock('react-native/Libraries/Components/Touchable/TouchableOpacity', () => {
  const TouchableOpacity = jest.requireActual(
      'react-native/Libraries/Components/Touchable/TouchableOpacity',
  )
  TouchableOpacity.displayName = 'TouchableOpacity';
  return TouchableOpacity;
});
RicardoBrito1938 commented 4 years ago

@bgneu thanks, you saved me