Skip to content

Commit ea7b605

Browse files
committed
Add unit test
1 parent 817f8be commit ea7b605

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

packages/auth/src/core/auth/firebase_internal.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { expect, use } from 'chai';
2020
import * as sinon from 'sinon';
2121
import chaiAsPromised from 'chai-as-promised';
2222

23-
import { testAuth, testUser } from '../../../test/helpers/mock_auth';
23+
import { regionalTestAuth, testAuth, testUser } from '../../../test/helpers/mock_auth';
2424
import { AuthInternal } from '../../model/auth';
2525
import { UserInternal } from '../../model/user';
2626
import { AuthInterop } from './firebase_internal';
@@ -37,6 +37,9 @@ describe('core/auth/firebase_internal', () => {
3737

3838
afterEach(() => {
3939
sinon.restore();
40+
delete (auth as unknown as Record<string, unknown>)[
41+
'_initializationPromise'
42+
];
4043
});
4144

4245
context('getUid', () => {
@@ -215,3 +218,22 @@ describe('core/auth/firebase_internal', () => {
215218
});
216219
});
217220
});
221+
222+
describe('core/auth/firebase_internal - Regional Firebase Auth', () => {
223+
let regionalAuth: AuthInternal;
224+
let regionalAuthInternal: AuthInterop;
225+
beforeEach(async () => {
226+
regionalAuth = await regionalTestAuth();
227+
regionalAuthInternal = new AuthInterop(regionalAuth);
228+
});
229+
230+
afterEach(() => {
231+
sinon.restore();
232+
});
233+
234+
context('getFirebaseToken', () => {
235+
it('returns null if firebase token is undefined', async () => {
236+
expect(await regionalAuthInternal.getFirebaseToken()).to.be.null;
237+
});
238+
});
239+
});

packages/auth/src/core/auth/firebase_internal.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ export class AuthInterop implements FirebaseAuthInternal {
5454

5555
async getFirebaseToken(): Promise<{ accessToken: string } | null> {
5656
this.assertAuthConfigured();
57+
await this.auth._initializationPromise;
5758
this.assertRegionalAuthConfigured();
5859
if (!this.auth.firebaseToken) {
5960
return null;

packages/auth/test/helpers/mock_auth.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,10 @@ export async function testAuth(
118118
return auth;
119119
}
120120

121-
export async function regionalTestAuth(): Promise<TestAuth> {
121+
export async function regionalTestAuth(
122+
popupRedirectResolver?: PopupRedirectResolver,
123+
persistence = new MockPersistenceLayer(),
124+
skipAwaitOnInit?: boolean): Promise<TestAuth> {
122125
const tenantConfig = { 'location': 'us', 'tenantId': 'tenant-1' };
123126
const auth: TestAuth = new AuthImpl(
124127
FAKE_APP,
@@ -135,6 +138,15 @@ export async function regionalTestAuth(): Promise<TestAuth> {
135138
},
136139
tenantConfig
137140
) as TestAuth;
141+
if (skipAwaitOnInit) {
142+
// This is used to verify scenarios where auth flows (like signInWithRedirect) are invoked before auth is fully initialized.
143+
// eslint-disable-next-line @typescript-eslint/no-floating-promises
144+
auth._initializeWithPersistence([persistence], popupRedirectResolver);
145+
} else {
146+
await auth._initializeWithPersistence([persistence], popupRedirectResolver);
147+
}
148+
auth.persistenceLayer = persistence;
149+
auth.settings.appVerificationDisabledForTesting = true;
138150
return auth;
139151
}
140152

0 commit comments

Comments
 (0)