同时嘲笑firebase&autah作为对象和功能?

时间:2018-05-07 13:17:23

标签: javascript firebase testing mocking firebase-authentication

我正在尝试为firebase的auth编写简约模拟以满足

等用例
  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);
    }
  };

我到目前为止

const firebase = {
  auth: {
    onAuthStateChange: callback => {
      callback({
        user: {
          uid: "123",
          email: "test@email.com"
        }
      });
    },
    FacebookAuthProvider: {
      credential: accessToken => ({ param: "1" });
    }
  }
};

我现在主要担心的是做什么时的差异

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

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

你可以看到auth在这里同时是一个对象和一个函数,我不完全确定如何模仿它:/

1 个答案:

答案 0 :(得分:3)

在JavaScript中,函数也是对象。您只需添加所需的属性即可。

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