Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6290

How to mock Firebase auth providers such as FacebookAuthProvider?

$
0
0

I am trying to mock react-native-firebase's firebase.auth object so that it will return a credential to the code under test so that I can test my method.

I have the following code under test:

public async FacebookSignIn(): Promise<void> {    try {      const result = await LoginManager.logInWithReadPermissions(['public_profile', 'email']);      if (result.isCancelled) {        throw new Error('User cancelled request');      }      const data = await AccessToken.getCurrentAccessToken();      if (!data) {        throw new Error('Something went wrong obtaining the users access token');      }      const credential = firebase.auth.FacebookAuthProvider.credential(data.accessToken);      await firebase.auth().signInWithCredential(credential);    }    catch (error) {      console.error(error);    }  }

I want to ensure that at the end of the function, my CUT calls signInWithCredential so I have a test case like so; func is a global :jest.fn()

describe('FacebookSignIn', () => {    it('should sign in user with facebook credentials', async () => {      await service.FacebookSignIn();      expect(func).toHaveBeenCalled();    })  })

However with the following mock I get the error TypeError: Cannot read property 'credential' of undefined:

jest.mock('react-native-firebase', () => {  return {    auth: () => {      return {        currentUser: null,        signOut: func,        signInAnonymously: func,        signInWithEmailAndPassword: func,        signInWithCredential: func,        FacebookAuthProvider: jest.fn(() => {          return {            credential: jest.fn(() => {              return {                provider: 'dummy',                token: '1234',                secret: '5678'              }            })          }        })      }    }  }});

I've looked at the type for firebase.auth and it can return this:

type AuthModule = {    (): RNFirebase.auth.Auth;    nativeModuleExists: boolean;} & RNFirebase.auth.AuthStatics

which to me looks like it is mocking the signIn methods correctly, but the AuthStatics methods aren't being mocked. AuthStatics is defined as follows:

interface AuthStatics {        EmailAuthProvider: EmailAuthProvider;        PhoneAuthProvider: AuthProvider;        GoogleAuthProvider: AuthProvider;        GithubAuthProvider: AuthProvider;        OAuthProvider: AuthProvider;        TwitterAuthProvider: AuthProvider;        FacebookAuthProvider: AuthProvider;        PhoneAuthState: {          CODE_SENT: string;          AUTO_VERIFY_TIMEOUT: string;          AUTO_VERIFIED: string;          ERROR: string;        };      }

How can I mock the FacebookAuthProvider property and avoid it being undefined for credential?


Viewing all articles
Browse latest Browse all 6290

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>