react-native-picker / picker

Picker is a cross-platform UI component for selecting an item from a list of options.
MIT License
1.47k stars 277 forks source link

Jest setup #467

Open Alaa-Ben opened 1 year ago

Alaa-Ben commented 1 year ago

Hi ! Are there any guides about how to setup this with jest ? A simple example with Picker causes the following error:

    Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()?

    Check the render method of `Picker`.

I figured this happens because in my setup file, I mock the platform (for other tests purposes):

jest.mock('react-native/Libraries/Utilities/Platform', () => {
  const platform = jest.requireActual('react-native/Libraries/Utilities/Platform')
  return {
    ...platform,
    OS: 'android',
    constants: {
      ...platform.constants,
      reactNativeVersion: {
        major: 0,
        minor: 70,
        patch: 6
      }
    }
  }
})

Any ideas ?

EDIT: I had to put Platform.OS = 'ios' before the render in my test file for this to work. Still means there's an issue with Platform=android & jest :/

alextorres94 commented 1 year ago

same here testing simulating android platform 😢

huynhnhutlam commented 1 year ago

Same issue on android platform