Ilja
Ilja

Reputation: 46479

Mocking firebase's auth as object and function at the same time?

I am trying to write minimalistic mocks fro firebase's auth to satisfy use cases like

  facebookSignIn = async () => {
    try {
      const user = await FacebookLogIn.logIn();
      const credential = firebase.auth.FacebookAuthProvider.credential(user.accessToken);
      await firebase.auth().signInAndRetrieveDataWithCredential(credential);
    } catch (e) {
      Alert.alert("Error", e.message);
    }
  };

I've got this far

const firebase = {
  auth: {
    onAuthStateChange: callback => {
      callback({
        user: {
          uid: "123",
          email: "[email protected]"
        }
      });
    },
    FacebookAuthProvider: {
      credential: accessToken => ({ param: "1" });
    }
  }
};

my main concern now is difference when doing

const credential = firebase.auth.FacebookAuthProvider.credential(user.accessToken);

and

await firebase.auth().signInAndRetrieveDataWithCredential(credential);

you can see that auth here is an object and a function at the same time, I'm not entirely sure about how I can mock that :/

Upvotes: 2

Views: 623

Answers (1)

FINDarkside
FINDarkside

Reputation: 2435

In JavaScript functions are objects too. You can simply add the properties you need to it.

const firebase = {
  auth: function(){
    console.log('foo');
  }
};
firebase.auth.bar = 'bar';
firebase.auth();
console.log(firebase.auth.bar);

Upvotes: 3

Related Questions