From 90b6df6c7604e1e05b7807b708e72a16f6a7144f Mon Sep 17 00:00:00 2001 From: Armando Magalhaes Date: Thu, 3 Oct 2019 14:13:02 +0200 Subject: [PATCH] feat: signinWithPhoneNumber. Related #8. --- src/index.tsx | 22 ++++++++++++++-------- src/test.tsx | 30 +++++++++++++++++++++++++++--- 2 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/index.tsx b/src/index.tsx index 23c67ae..3dde2b4 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -10,6 +10,7 @@ export type WrappedComponentProps = { signInWithFacebook: () => void; signInWithGithub: () => void; signInWithTwitter: () => void; + signInWithPhoneNumber: (phoneNumber: string, applicationVerifier: firebase.auth.ApplicationVerifier) => Promise; signInAnonymously: () => void; signOut: () => void; setError: (error: any) => void; @@ -68,16 +69,17 @@ const withFirebaseAuth = ({ setError = (error: any) => this.setState({ error }); - tryTo = async (operation: () => any) => { + async tryTo (operation: () => Promise): Promise { try { - return await operation(); + return operation(); } catch(error) { this.setError(error.message); + return error; } }; - tryToSignInWithProvider = (provider: PossibleProviders) => - this.tryTo(() => { + tryToSignInWithProvider = (provider: PossibleProviders): Promise => + this.tryTo(() => { const providerInstance = providers[provider]; if (!providerInstance) { @@ -88,10 +90,10 @@ const withFirebaseAuth = ({ }); signOut = () => - this.tryTo(() => firebaseAppAuth.signOut()); + this.tryTo(() => firebaseAppAuth.signOut()); signInAnonymously = () => - this.tryTo(() => firebaseAppAuth.signInAnonymously()); + this.tryTo(() => firebaseAppAuth.signInAnonymously()); signInWithGithub = () => this.tryToSignInWithProvider('githubProvider'); @@ -106,10 +108,13 @@ const withFirebaseAuth = ({ this.tryToSignInWithProvider('facebookProvider'); signInWithEmailAndPassword = (email: string, password: string) => - this.tryTo(() => firebaseAppAuth.signInWithEmailAndPassword(email, password)); + this.tryTo(() => firebaseAppAuth.signInWithEmailAndPassword(email, password)); + + signInWithPhoneNumber = (phoneNumber: string, applicationVerifier: firebase.auth.ApplicationVerifier) => + this.tryTo(() => firebaseAppAuth.signInWithPhoneNumber(phoneNumber, applicationVerifier)) createUserWithEmailAndPassword = (email: string, password: string) => - this.tryTo(() => firebaseAppAuth.createUserWithEmailAndPassword(email, password)); + this.tryTo(() => firebaseAppAuth.createUserWithEmailAndPassword(email, password)); sharedHandlers = { signInWithEmailAndPassword: this.signInWithEmailAndPassword, @@ -118,6 +123,7 @@ const withFirebaseAuth = ({ signInWithTwitter: this.signInWithTwitter, signInWithGoogle: this.signInWithGoogle, signInWithFacebook: this.signInWithFacebook, + signInWithPhoneNumber: this.signInWithPhoneNumber, setError: this.setError, signInAnonymously: this.signInAnonymously, signOut: this.signOut, diff --git a/src/test.tsx b/src/test.tsx index 96e8c97..bcae57d 100644 --- a/src/test.tsx +++ b/src/test.tsx @@ -19,7 +19,7 @@ const fakeUser = { }; describe('withFirebaseAuth', () => { - let currentAuthStateObserver = (_user: firebase.User) => {}; + let currentAuthStateObserver: any = (_user: firebase.User) => {}; let unsubcribeAuthStateChangeMock = jest.fn(); beforeEach(() => { @@ -30,6 +30,7 @@ describe('withFirebaseAuth', () => { testAppAuth.signInAnonymously = jest.fn(); testAppAuth.signOut = jest.fn(); testAppAuth.signInWithPopup = jest.fn(); + testAppAuth.signInWithPhoneNumber = jest.fn(); testAppAuth.onAuthStateChanged = jest.fn(observer => { currentAuthStateObserver = observer; return unsubcribeAuthStateChangeMock; @@ -204,8 +205,31 @@ describe('withFirebaseAuth', () => { }); it('should call createUserWithEmailAndPassword when prop is invoked', () => { + const email = 'test'; + const password = 'test'; + const WrappedComponent = ({ createUserWithEmailAndPassword }: WrappedComponentProps) => - ; + ; + + const EnhancedComponent = withFirebaseAuth({ + firebaseAppAuth: testAppAuth, + })(WrappedComponent); + + const wrapped = mount(); + + wrapped.find('button').simulate('click'); + + expect(testAppAuth.createUserWithEmailAndPassword).toHaveBeenCalledWith(email, password); + }); + + it('should call signInWithPhoneNumber when prop is invoked', () => { + const phoneNumber = "666999666"; + const applicationVerifier = { type: '', verify: () => Promise.resolve('') }; + + const WrappedComponent = ({ signInWithPhoneNumber }: WrappedComponentProps) => + ; const EnhancedComponent = withFirebaseAuth({ firebaseAppAuth: testAppAuth, @@ -215,7 +239,7 @@ describe('withFirebaseAuth', () => { wrapped.find('button').simulate('click'); - expect(testAppAuth.createUserWithEmailAndPassword).toHaveBeenCalled(); + expect(testAppAuth.signInWithPhoneNumber).toHaveBeenCalledWith(phoneNumber, applicationVerifier); }); it('should set an error when setError is invoked', () => {