Some of the code I am trying to test detects the platform, using, e.g.:
import { Platform } from 'react-native';
...
if (Platform.OS === 'android') {
...
} else {
...
}
Is there a sensible way to mock this with Jest and/or something else, so I can test both branches in one test run?
Or is the smart way to decouple it and put the platform into, e.g., a context variable? Although it always feels restructuring code to make it easier to test is something of a cheat.
I'm using the solution from this github issue https://github.com/facebook/jest/issues/1370#issuecomment-352597475
I moved the jest config from
package.json
to separate files. So far everything seems to work great, including: a) the right file is imported according to the platform. For example on ios: .ios.tsx, then .native.tsx then .tsx b) PLATFORM.IOS returns true when running test-ios, no need to mock anything