dimanche 10 juillet 2016

How to mock third party React Native NativeModules?

A component is importing a library that includes a native module. Here is a contrived example:

import React from 'react';
import { View } from 'react-native';
import { Answers } from 'react-native-fabric';

export default function MyTouchComponent({ params }) {
  return <View onPress={() => { Answers.logContentView() }} />
}

And here is the relevant part of Answers from react-native-fabric:

var { NativeModules, Platform } = require('react-native');
var SMXAnswers = NativeModules.SMXAnswers;

When importing this component in a mocha test, this fails on account that SMXAnswers is undefined:

How do you mock SMXAnswers or react-native-fabric so that it doesn't break and allows you to test your components?

p.s.: you can see the full setup and the component I'm trying to test on GitHub.

Aucun commentaire:

Enregistrer un commentaire