Skip to content

Commit

Permalink
fix(auth, phone): call verifyPhoneNumber callbacks correctly
Browse files Browse the repository at this point in the history
success callback seems fine, error should be fine sometimes but is unstable
on simulator and emulator during testing
  • Loading branch information
mikehardy committed Dec 15, 2021
1 parent 88a3aa9 commit 7c082be
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -958,12 +958,12 @@ public void verifyPhoneNumber(
final String requestKey,
final int timeout,
final boolean forceResend) {
Log.d(TAG, "verifyPhoneNumber:" + phoneNumber);

FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
final Activity activity = getCurrentActivity();

Log.d(TAG, "verifyPhoneNumber:" + phoneNumber);

// reset force resending token if phone number changes
if (!phoneNumber.equals(mLastPhoneNumber)) {
mForceResendingToken = null;
Expand Down
71 changes: 55 additions & 16 deletions packages/auth/e2e/phone.e2e.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,33 +107,72 @@ describe('auth() => Phone', function () {

it('successfully runs verification complete handler', async function () {
const testPhone = getRandomPhoneNumber();
const thenCb = sinon.spy();
await firebase.auth().verifyPhoneNumber(testPhone).then(thenCb);
thenCb.should.be.calledOnce();
const successAuthSnapshot = thenCb.args[0][0];
if (device.getPlatform() === 'ios') {
successAuthSnapshot.state.should.equal('sent');
} else {
successAuthSnapshot.state.should.equal('timeout');
}
});

it('successfully runs and calls success callback', async function () {
const testPhone = getRandomPhoneNumber();
const successCb = sinon.spy();
const observerCb = sinon.spy();
const errorCb = sinon.spy();

await firebase
.auth()
.verifyPhoneNumber(testPhone)
.then($ => $);

return Promise.resolve();
.on('state_changed', observerCb, errorCb, successCb);

await Utils.spyToBeCalledOnceAsync(successCb);
errorCb.should.be.callCount(0);
successCb.should.be.calledOnce();
let observerAuthSnapshot = observerCb.args[0][0];
const successAuthSnapshot = successCb.args[0][0];
successAuthSnapshot.verificationId.should.be.a.String();
if (device.getPlatform() === 'ios') {
observerCb.should.be.calledOnce();
successAuthSnapshot.state.should.equal('sent');
} else {
// android waits for SMS auto retrieval which does not work on an emulator
// it gets a sent and a timeout message on observer, just the timeout on success
observerCb.should.be.calledTwice();
observerAuthSnapshot = observerCb.args[1][0];
successAuthSnapshot.state.should.equal('timeout');
}
JSON.stringify(successAuthSnapshot).should.equal(JSON.stringify(observerAuthSnapshot));
});

it('successfully runs and adds emitters', async function () {
const testPhone = await getRandomPhoneNumber();
const obervserCb = () => {};
const errorCb = () => {};
const successCb = () => {
return Promise.resolve();
};
// TODO determine why this is not stable on the emulator, is it also not stable on real device?
xit('successfully runs and calls error callback', async function () {
const successCb = sinon.spy();
const observerCb = sinon.spy();
const errorCb = sinon.spy();

await firebase
.auth()
.verifyPhoneNumber(testPhone)
.on('state_changed', obervserCb, errorCb, successCb, () => {});
.verifyPhoneNumber('notaphonenumber')
.on('state_changed', observerCb, errorCb, successCb);

await Utils.spyToBeCalledOnceAsync(errorCb);
errorCb.should.be.calledOnce();
observerCb.should.be.calledOnce();
// const observerEvent = observerCb.args[0][0];
successCb.should.be.callCount(0);
// const errorEvent = errorCb.args[0][0];
// errorEvent.error.should.containEql('auth/invalid-phone-number');
// JSON.stringify(errorEvent).should.equal(JSON.stringify(observerEvent));
});

it('catches an error and emits an error event', async function () {
return firebase
.auth()
.verifyPhoneNumber('test')
.catch(() => Promise.resolve());
const catchCb = sinon.spy();
await firebase.auth().verifyPhoneNumber('badphonenumber').catch(catchCb);
catchCb.should.be.calledOnce();
});
});
});
8 changes: 4 additions & 4 deletions packages/auth/lib/PhoneAuthListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,16 +206,16 @@ export default class PhoneAuthListener {
this._addUserObserver(observer);

if (isFunction(errorCb)) {
const subscription = this._auth.emitter.addListener(this._publicEvents.error, () => {
errorCb;
const subscription = this._auth.emitter.addListener(this._publicEvents.error, event => {
subscription.remove();
errorCb(event);
});
}

if (isFunction(successCb)) {
const subscription = this._auth.emitter.addListener(this._publicEvents.success, () => {
successCb;
const subscription = this._auth.emitter.addListener(this._publicEvents.success, event => {
subscription.remove();
successCb(event);
});
}

Expand Down

1 comment on commit 7c082be

@vercel
Copy link

@vercel vercel bot commented on 7c082be Dec 15, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.