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 Answer 1

-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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.