Skip to content

Commit 959e21a

Browse files
authored
Add tests for initializeAuth (#3957)
1 parent 79b0493 commit 959e21a

File tree

1 file changed

+189
-0
lines changed

1 file changed

+189
-0
lines changed
Lines changed: 189 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { deleteApp, initializeApp } from '@firebase/app-exp';
19+
import { FirebaseApp, _FirebaseService } from '@firebase/app-types-exp';
20+
import * as externs from '@firebase/auth-types-exp';
21+
import { isNode } from '@firebase/util';
22+
23+
import { expect } from 'chai';
24+
import { inMemoryPersistence } from '../../../internal';
25+
26+
import { Auth } from '../../model/auth';
27+
import {
28+
PopupRedirectResolver,
29+
AuthEventType,
30+
EventManager,
31+
AuthEventConsumer
32+
} from '../../model/popup_redirect';
33+
import { AuthPopup } from '../../platform_browser/util/popup';
34+
import {
35+
Persistence,
36+
PersistenceType,
37+
PersistenceValue,
38+
StorageEventListener
39+
} from '../persistence';
40+
import { ClientPlatform, _getClientVersion } from '../util/version';
41+
import { initializeAuth } from './initialize';
42+
import { registerAuth } from './register';
43+
44+
describe('src/core/auth/initialize', () => {
45+
let fakeApp: FirebaseApp;
46+
47+
class FakeSessionPersistence implements Persistence {
48+
static type: 'SESSION' = 'SESSION';
49+
readonly type = PersistenceType.SESSION;
50+
storage: Record<string, PersistenceValue> = {};
51+
52+
async _isAvailable(): Promise<boolean> {
53+
return true;
54+
}
55+
async _set(_key: string, _value: PersistenceValue): Promise<void> {
56+
return;
57+
}
58+
async _get<T extends PersistenceValue>(_key: string): Promise<T | null> {
59+
return null;
60+
}
61+
async _remove(_key: string): Promise<void> {
62+
return;
63+
}
64+
_addListener(_key: string, _listener: StorageEventListener): void {
65+
return;
66+
}
67+
_removeListener(_key: string, _listener: StorageEventListener): void {
68+
return;
69+
}
70+
}
71+
72+
const fakeSessionPersistence: externs.Persistence = FakeSessionPersistence;
73+
74+
class FakePopupRedirectResolver implements PopupRedirectResolver {
75+
readonly _redirectPersistence = fakeSessionPersistence;
76+
77+
async _initialize(_auth: Auth): Promise<EventManager> {
78+
return new (class implements EventManager {
79+
registerConsumer(_authEventConsumer: AuthEventConsumer): void {
80+
return;
81+
}
82+
unregisterConsumer(_authEventConsumer: AuthEventConsumer): void {
83+
return;
84+
}
85+
})();
86+
}
87+
async _openPopup(
88+
_auth: Auth,
89+
_provider: externs.AuthProvider,
90+
_authType: AuthEventType,
91+
_eventId?: string
92+
): Promise<AuthPopup> {
93+
return new AuthPopup(null);
94+
}
95+
_openRedirect(
96+
_auth: Auth,
97+
_provider: externs.AuthProvider,
98+
_authType: AuthEventType,
99+
_eventId?: string
100+
): Promise<never> {
101+
return new Promise((_, reject) => reject());
102+
}
103+
_isIframeWebStorageSupported(
104+
_auth: Auth,
105+
cb: (support: boolean) => unknown
106+
): void {
107+
cb(true);
108+
}
109+
}
110+
111+
const fakePopupRedirectResolver: externs.PopupRedirectResolver = FakePopupRedirectResolver;
112+
113+
before(() => {
114+
registerAuth(ClientPlatform.BROWSER);
115+
});
116+
117+
beforeEach(() => {
118+
fakeApp = initializeApp({
119+
apiKey: 'fake-key',
120+
appId: 'fake-app-id',
121+
authDomain: 'fake-auth-domain'
122+
});
123+
});
124+
125+
afterEach(async () => {
126+
await deleteApp(fakeApp);
127+
});
128+
129+
describe('initializeAuth', () => {
130+
it('should work with no deps', async () => {
131+
const auth = initializeAuth(fakeApp) as Auth;
132+
await auth._initializationPromise;
133+
134+
expect(auth.name).to.eq(fakeApp.name);
135+
const expectedSdkClientVersion = _getClientVersion(
136+
isNode() ? ClientPlatform.NODE : ClientPlatform.BROWSER
137+
);
138+
139+
expect(auth.config).to.eql({
140+
apiHost: 'identitytoolkit.googleapis.com',
141+
apiKey: 'fake-key',
142+
apiScheme: 'https',
143+
authDomain: 'fake-auth-domain',
144+
sdkClientVersion: expectedSdkClientVersion,
145+
tokenApiHost: 'securetoken.googleapis.com'
146+
});
147+
expect(auth._getPersistence()).to.eq('NONE');
148+
});
149+
150+
it('should set persistence', async () => {
151+
const auth = initializeAuth(fakeApp, {
152+
persistence: fakeSessionPersistence
153+
}) as Auth;
154+
await auth._initializationPromise;
155+
156+
expect(auth._getPersistence()).to.eq('SESSION');
157+
});
158+
159+
it('should set persistence with fallback', async () => {
160+
const auth = initializeAuth(fakeApp, {
161+
persistence: [fakeSessionPersistence, inMemoryPersistence]
162+
}) as Auth;
163+
await auth._initializationPromise;
164+
165+
expect(auth._getPersistence()).to.eq('SESSION');
166+
});
167+
168+
it('should set resolver', async () => {
169+
const auth = initializeAuth(fakeApp, {
170+
popupRedirectResolver: fakePopupRedirectResolver
171+
}) as Auth;
172+
await auth._initializationPromise;
173+
174+
expect(auth._popupRedirectResolver).to.be.instanceof(
175+
FakePopupRedirectResolver
176+
);
177+
});
178+
179+
it('should abort initialization if deleted synchronously', async () => {
180+
const auth = initializeAuth(fakeApp, {
181+
popupRedirectResolver: fakePopupRedirectResolver
182+
}) as Auth;
183+
await ((auth as unknown) as _FirebaseService)._delete();
184+
await auth._initializationPromise;
185+
186+
expect(auth._isInitialized).to.be.false;
187+
});
188+
});
189+
});

0 commit comments

Comments
 (0)