Skip to content

Commit 711428b

Browse files
committed
Add tests for initializeAuth
1 parent 79b0493 commit 711428b

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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+
22+
import { expect } from 'chai';
23+
import { inMemoryPersistence } from '../../../internal';
24+
25+
import { Auth } from '../../model/auth';
26+
import {
27+
PopupRedirectResolver,
28+
AuthEventType,
29+
EventManager,
30+
AuthEventConsumer
31+
} from '../../model/popup_redirect';
32+
import { AuthPopup } from '../../platform_browser/util/popup';
33+
import {
34+
Persistence,
35+
PersistenceType,
36+
PersistenceValue,
37+
StorageEventListener
38+
} from '../persistence';
39+
import { ClientPlatform, _getClientVersion } from '../util/version';
40+
import { initializeAuth } from './initialize';
41+
import { registerAuth } from './register';
42+
43+
describe('src/core/auth/initialize', () => {
44+
let fakeApp: FirebaseApp;
45+
46+
class FakeSessionPersistence implements Persistence {
47+
static type: 'SESSION' = 'SESSION';
48+
readonly type = PersistenceType.SESSION;
49+
storage: Record<string, PersistenceValue> = {};
50+
51+
async _isAvailable(): Promise<boolean> {
52+
return true;
53+
}
54+
async _set(_key: string, _value: PersistenceValue): Promise<void> {
55+
return;
56+
}
57+
async _get<T extends PersistenceValue>(_key: string): Promise<T | null> {
58+
return null;
59+
}
60+
async _remove(_key: string): Promise<void> {
61+
return;
62+
}
63+
_addListener(_key: string, _listener: StorageEventListener): void {
64+
return;
65+
}
66+
_removeListener(_key: string, _listener: StorageEventListener): void {
67+
return;
68+
}
69+
}
70+
71+
const fakeSessionPersistence: externs.Persistence = FakeSessionPersistence;
72+
73+
class FakePopupRedirectResolver implements PopupRedirectResolver {
74+
readonly _redirectPersistence = fakeSessionPersistence;
75+
76+
async _initialize(_auth: Auth): Promise<EventManager> {
77+
return new (class implements EventManager {
78+
registerConsumer(_authEventConsumer: AuthEventConsumer): void {
79+
return;
80+
}
81+
unregisterConsumer(_authEventConsumer: AuthEventConsumer): void {
82+
return;
83+
}
84+
})();
85+
}
86+
async _openPopup(
87+
_auth: Auth,
88+
_provider: externs.AuthProvider,
89+
_authType: AuthEventType,
90+
_eventId?: string
91+
): Promise<AuthPopup> {
92+
return new AuthPopup(null);
93+
}
94+
_openRedirect(
95+
_auth: Auth,
96+
_provider: externs.AuthProvider,
97+
_authType: AuthEventType,
98+
_eventId?: string
99+
): Promise<never> {
100+
return new Promise((_, reject) => reject());
101+
}
102+
_isIframeWebStorageSupported(
103+
_auth: Auth,
104+
cb: (support: boolean) => unknown
105+
): void {
106+
cb(true);
107+
}
108+
}
109+
110+
const fakePopupRedirectResolver: externs.PopupRedirectResolver = FakePopupRedirectResolver;
111+
112+
before(() => {
113+
registerAuth(ClientPlatform.BROWSER);
114+
});
115+
116+
beforeEach(() => {
117+
fakeApp = initializeApp({
118+
apiKey: 'fake-key',
119+
appId: 'fake-app-id',
120+
authDomain: 'fake-auth-domain'
121+
});
122+
});
123+
124+
afterEach(async () => {
125+
await deleteApp(fakeApp);
126+
});
127+
128+
describe('initializeAuth', () => {
129+
it('should work with no deps', async () => {
130+
const auth = initializeAuth(fakeApp) as Auth;
131+
await auth._initializationPromise;
132+
133+
expect(auth.name).to.eq(fakeApp.name);
134+
expect(auth.config).to.eql({
135+
apiHost: 'identitytoolkit.googleapis.com',
136+
apiKey: 'fake-key',
137+
apiScheme: 'https',
138+
authDomain: 'fake-auth-domain',
139+
sdkClientVersion: _getClientVersion(ClientPlatform.BROWSER),
140+
tokenApiHost: 'securetoken.googleapis.com'
141+
});
142+
expect(auth._getPersistence()).to.eq('NONE');
143+
});
144+
145+
it('should set persistence', async () => {
146+
const auth = initializeAuth(fakeApp, {
147+
persistence: fakeSessionPersistence
148+
}) as Auth;
149+
await auth._initializationPromise;
150+
151+
expect(auth._getPersistence()).to.eq('SESSION');
152+
});
153+
154+
it('should set persistence with fallback', async () => {
155+
const auth = initializeAuth(fakeApp, {
156+
persistence: [fakeSessionPersistence, inMemoryPersistence]
157+
}) as Auth;
158+
await auth._initializationPromise;
159+
160+
expect(auth._getPersistence()).to.eq('SESSION');
161+
});
162+
163+
it('should set resolver', async () => {
164+
const auth = initializeAuth(fakeApp, {
165+
popupRedirectResolver: fakePopupRedirectResolver
166+
}) as Auth;
167+
await auth._initializationPromise;
168+
169+
expect(auth._popupRedirectResolver).to.be.instanceof(
170+
FakePopupRedirectResolver
171+
);
172+
});
173+
174+
it('should abort initialization if deleted synchronously', async () => {
175+
const auth = initializeAuth(fakeApp, {
176+
popupRedirectResolver: fakePopupRedirectResolver
177+
}) as Auth;
178+
await ((auth as unknown) as _FirebaseService)._delete();
179+
await auth._initializationPromise;
180+
181+
expect(auth._isInitialized).to.be.false;
182+
});
183+
});
184+
});

0 commit comments

Comments
 (0)