Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Native Mocking Platform.Version

Tags:

react-native

I've been working with jest snapshots, and have some components which use the react-native.Platform. I need to have the Version and the OS set.

I am able to set Platform.OS manually just using Platform.OS='android' or Platform.OS='ios' but when I try and set Platform.Version=21 it does not work. I looked a little deeper and found its using a getter for this. Is there anyway I'd be able to set Platform.Version similarly to how I am setting Platform.OS.

like image 779
G. Herrmann Avatar asked Feb 13 '17 22:02

G. Herrmann


People also ask

Are jest mocks hoisted?

But often you need to instruct Jest to use a mock before modules use it. For this reason, Jest will automatically hoist jest.mock calls to the top of the module (before any imports).

How do you mock native modules?

Mocking native modules​Make sure that the path to the file in setupFiles is correct. Jest will run these files before running your tests, so it's the best place to put your global mocks. If you're not using Jest, then you'll need to mock these modules according to the test framework you are using.

Is Android react native?

android - Android Studio project preconfigured with React Native support. ios - Xcode project preconfigured with React Native support. node_modules - A folder containing React Native framework and other Javascript libraries.


1 Answers

The following trick works for me:

Object.defineProperty(Platform, 'Version', {
  get: () => 21
});
like image 52
Mila Avatar answered Oct 09 '22 02:10

Mila