0

In App.js I have some thing like

Class App extends Component {
 constructor(props){
  super(props)
  global.test = NativeModules.TestClass
 }
}

And in Test class I am using it like

Class Test extends Component {
 constructor(props){
  super(props)
  global.test.testFunction("Testing")
 }
}

So how to mock global.test.testFunction for the above class

1 Answers1

-1

I suggest another approach for global function, variables. You can create a Utils class for each global functions, variables (constants or enums) and import the Utils wherever you need to use them.

Here is a very basic example for Utils Class:

Utils Class

export default class Utils {

    static navigationRef = null;

    static showAlert(title, desc) {
        Alert.alert(title, desc);
    }
}

USAGE:

import Utils from "./shared/utils"

// Example of static function
Utils.showAlert("Hello" "Alert Description")

// Example of static variable
Utils.navigationRef = this.props.navigation;
FreakyCoder
  • 840
  • 11
  • 24