Skip to content

Commit bcaf0c6

Browse files
committed
unit tests
1 parent d375e1d commit bcaf0c6

File tree

3 files changed

+104
-3
lines changed

3 files changed

+104
-3
lines changed

packages/auth/src/platform_browser/strategies/redirect.test.ts

+86
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,33 @@ describe('platform_browser/strategies/redirect', () => {
122122
'auth/argument-error'
123123
);
124124
});
125+
126+
it('awaits on the auth initialization promise before opening redirect', async () => {
127+
// Obtain an auth instance which does not await on the initialization promise.
128+
const authWithoutAwait: TestAuth = await testAuth(resolver, undefined, true);
129+
// completeRedirectFn calls getRedirectResult under the hood.
130+
const getRedirectResultSpy = sinon.spy(
131+
_getInstance<PopupRedirectResolverInternal>(resolver),
132+
'_completeRedirectFn'
133+
);
134+
const openRedirectSpy = sinon.spy(
135+
_getInstance<PopupRedirectResolverInternal>(resolver),
136+
'_openRedirect'
137+
);
138+
await signInWithRedirect(authWithoutAwait, provider);
139+
expect(getRedirectResultSpy).to.have.been.called;
140+
expect(getRedirectResultSpy).to.have.been.calledBefore(openRedirectSpy);
141+
expect(getRedirectResultSpy).to.have.been.calledWith(
142+
authWithoutAwait,
143+
resolver,
144+
true
145+
);
146+
expect(openRedirectSpy).to.have.been.calledWith(
147+
authWithoutAwait,
148+
provider,
149+
AuthEventType.SIGN_IN_VIA_REDIRECT
150+
);
151+
});
125152
});
126153

127154
context('linkWithRedirect', () => {
@@ -159,6 +186,35 @@ describe('platform_browser/strategies/redirect', () => {
159186
);
160187
});
161188

189+
it('awaits on the auth initialization promise before opening redirect', async () => {
190+
// Obtain an auth instance which does not await on the initialization promise.
191+
const authWithoutAwait: TestAuth = await testAuth(resolver, undefined, true);
192+
user = testUser(authWithoutAwait, 'uid', 'email', true);
193+
// completeRedirectFn calls getRedirectResult under the hood.
194+
const getRedirectResultSpy = sinon.spy(
195+
_getInstance<PopupRedirectResolverInternal>(resolver),
196+
'_completeRedirectFn'
197+
);
198+
const openRedirectSpy = sinon.spy(
199+
_getInstance<PopupRedirectResolverInternal>(resolver),
200+
'_openRedirect'
201+
);
202+
await authWithoutAwait._updateCurrentUser(user);
203+
await linkWithRedirect(user, provider, resolver);
204+
expect(getRedirectResultSpy).to.have.been.called;
205+
expect(getRedirectResultSpy).to.have.been.calledBefore(openRedirectSpy);
206+
expect(getRedirectResultSpy).to.have.been.calledWith(
207+
authWithoutAwait,
208+
resolver,
209+
true
210+
);
211+
expect(openRedirectSpy).to.have.been.calledWith(
212+
authWithoutAwait,
213+
provider,
214+
AuthEventType.LINK_VIA_REDIRECT
215+
);
216+
});
217+
162218
it('errors if no resolver available', async () => {
163219
auth._popupRedirectResolver = null;
164220
await expect(linkWithRedirect(user, provider)).to.be.rejectedWith(
@@ -236,6 +292,36 @@ describe('platform_browser/strategies/redirect', () => {
236292
);
237293
});
238294

295+
it('awaits on the auth initialization promise before opening redirect', async () => {
296+
// Obtain an auth instance which does not await on the initialization promise.
297+
const authWithoutAwait: TestAuth = await testAuth(resolver, undefined, true);
298+
user = testUser(authWithoutAwait, 'uid', 'email', true);
299+
// completeRedirectFn calls getRedirectResult under the hood.
300+
const getRedirectResultSpy = sinon.spy(
301+
_getInstance<PopupRedirectResolverInternal>(resolver),
302+
'_completeRedirectFn'
303+
);
304+
const openRedirectSpy = sinon.spy(
305+
_getInstance<PopupRedirectResolverInternal>(resolver),
306+
'_openRedirect'
307+
);
308+
await authWithoutAwait._updateCurrentUser(user);
309+
await signInWithRedirect(authWithoutAwait, provider);
310+
await reauthenticateWithRedirect(user, provider);
311+
expect(getRedirectResultSpy).to.have.been.called;
312+
expect(getRedirectResultSpy).to.have.been.calledBefore(openRedirectSpy);
313+
expect(getRedirectResultSpy).to.have.been.calledWith(
314+
authWithoutAwait,
315+
resolver,
316+
true
317+
);
318+
expect(openRedirectSpy).to.have.been.calledWith(
319+
authWithoutAwait,
320+
provider,
321+
AuthEventType.REAUTH_VIA_REDIRECT
322+
);
323+
});
324+
239325
it('errors if no resolver available', async () => {
240326
auth._popupRedirectResolver = null;
241327
await expect(

packages/auth/src/platform_browser/strategies/redirect.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ export async function _signInWithRedirect(
9191
): Promise<void | never> {
9292
const authInternal = _castAuth(auth);
9393
_assertInstanceOf(auth, provider, FederatedAuthProvider);
94-
// Wait for auth initialization to complete, this will process pending redirects and clear session storage.
94+
// Wait for auth initialization to complete, this will process pending redirects and clear the
95+
// PENDING_REDIRECT_KEY in persistence. This should be completed before starting a new
96+
// redirect and creating a PENDING_REDIRECT_KEY entry.
9597
await authInternal._initializationPromise;
9698
const resolverInternal = _withDefaultResolver(authInternal, resolver);
9799
await _setPendingRedirectStatus(resolverInternal, authInternal);
@@ -149,6 +151,10 @@ export async function _reauthenticateWithRedirect(
149151
): Promise<void | never> {
150152
const userInternal = getModularInstance(user) as UserInternal;
151153
_assertInstanceOf(userInternal.auth, provider, FederatedAuthProvider);
154+
// Wait for auth initialization to complete, this will process pending redirects and clear the
155+
// PENDING_REDIRECT_KEY in persistence. This should be completed before starting a new
156+
// redirect and creating a PENDING_REDIRECT_KEY entry.
157+
await userInternal.auth._initializationPromise;
152158
// Allow the resolver to error before persisting the redirect user
153159
const resolverInternal = _withDefaultResolver(userInternal.auth, resolver);
154160
await _setPendingRedirectStatus(resolverInternal, userInternal.auth);
@@ -201,6 +207,10 @@ export async function _linkWithRedirect(
201207
): Promise<void | never> {
202208
const userInternal = getModularInstance(user) as UserInternal;
203209
_assertInstanceOf(userInternal.auth, provider, FederatedAuthProvider);
210+
// Wait for auth initialization to complete, this will process pending redirects and clear the
211+
// PENDING_REDIRECT_KEY in persistence. This should be completed before starting a new
212+
// redirect and creating a PENDING_REDIRECT_KEY entry.
213+
await userInternal.auth._initializationPromise;
204214
// Allow the resolver to error before persisting the redirect user
205215
const resolverInternal = _withDefaultResolver(userInternal.auth, resolver);
206216
await _assertLinkedStatus(false, userInternal, provider.providerId);

packages/auth/test/helpers/mock_auth.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ export class MockPersistenceLayer extends InMemoryPersistence {
7171

7272
export async function testAuth(
7373
popupRedirectResolver?: PopupRedirectResolver,
74-
persistence = new MockPersistenceLayer()
74+
persistence = new MockPersistenceLayer(),
75+
skipAwaitOnInit?: boolean,
7576
): Promise<TestAuth> {
7677
const auth: TestAuth = new AuthImpl(
7778
FAKE_APP,
@@ -88,7 +89,11 @@ export async function testAuth(
8889
) as TestAuth;
8990
auth._updateErrorMap(debugErrorMap);
9091

91-
await auth._initializeWithPersistence([persistence], popupRedirectResolver);
92+
if (skipAwaitOnInit) {
93+
auth._initializeWithPersistence([persistence], popupRedirectResolver);
94+
} else {
95+
await auth._initializeWithPersistence([persistence], popupRedirectResolver);
96+
}
9297
auth.persistenceLayer = persistence;
9398
auth.settings.appVerificationDisabledForTesting = true;
9499
return auth;

0 commit comments

Comments
 (0)