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' method callback not invoked #125

Closed abmantis closed 4 years ago

abmantis commented 4 years ago

Relevant code or config:

  function onPress() {
    cardView.current.measure((x, y, width, height, pageX, pageY) => {
      console.log('Measure called', pageX, pageY, width, height);
    });
  }

What you did:

Calling fireEvent.press on a button my component, that calls the onPress() callback I posted above. When running normally on the browser, pressing the button logs "Measure called ....".

What happened:

The measure callback is not invoked, even though the measure method is being called.

Problem description:

My project relies on measure to trigger some user-facing behavior.

Suggested solution:

Too new to React Native and JS in general :/

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

Sure, with some guidance.

EDIT:

I ended up mocking the method myself with the following code, but I was hoping this was not necessary with the native-testing-library preset.

jest.spyOn(View.prototype, 'measureInWindow').mockImplementation((cb) => cb(0, 0, 1, 1));

bgneu commented 4 years ago

@abmantis I faced the same problem on RN 0.61 and the main problem was that measure is implemented as a simple jest.fn() (see mock-native-method).

What I did was, similar to what you did, but mocking implementation on the preset...

import { mockNativeMethods } from "@testing-library/react-native/dist/preset/mock-native-methods"
...
mockNativeMethods.measure.mockImplementation( (cb) => { 
    ... 
    cb(0, 0, 100, 100, 10, 10) // Random values
})

So in terms of guidance, I hope this helps a bit. :)