-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Expand file tree
/
Copy pathauth.spec.ts
More file actions
163 lines (139 loc) · 4.5 KB
/
auth.spec.ts
File metadata and controls
163 lines (139 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import firebase from 'firebase/app';
import { Observable, Subject } from 'rxjs';
import { TestBed } from '@angular/core/testing';
import { AngularFireModule, FIREBASE_APP_NAME, FIREBASE_OPTIONS, FirebaseApp } from '@angular/fire';
import { AngularFireAuth, AngularFireAuthModule } from './public_api';
import { COMMON_CONFIG } from '../test-config';
import 'firebase/auth';
import { rando } from '../firestore/utils.spec';
const firebaseUser = {
uid: '12345',
providerData: [{ displayName: 'jeffbcrossyface' }]
} as firebase.User;
describe('AngularFireAuth', () => {
let app: FirebaseApp;
let afAuth: AngularFireAuth;
let mockAuthState: Subject<firebase.User>;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG, rando()),
AngularFireAuthModule
]
});
app = TestBed.inject(FirebaseApp);
afAuth = TestBed.inject(AngularFireAuth);
mockAuthState = new Subject<firebase.User>();
// @ts-ignore
spyOn(afAuth, 'authState').and.returnValue(mockAuthState);
// @ts-ignore
spyOn(afAuth, 'idToken').and.returnValue(mockAuthState);
(afAuth as any).authState = mockAuthState as Observable<firebase.User>;
(afAuth as any).idToken = mockAuthState as Observable<firebase.User>;
});
afterEach(() => {
app.delete();
});
describe('Zones', () => {
it('should call operators and subscriber in the same zone as when service was initialized', (done) => {
// Initialize the app outside of the zone, to mimick real life behavior.
const ngZone = Zone.current.fork({
name: 'ngZone'
});
ngZone.run(() => {
const subs = [
afAuth.authState.subscribe(() => {
expect(Zone.current.name).toBe('ngZone');
done();
}, done.fail),
afAuth.authState.subscribe(() => {
expect(Zone.current.name).toBe('ngZone');
done();
}, done.fail)
];
mockAuthState.next(firebaseUser);
subs.forEach(s => s.unsubscribe());
});
});
});
it('should exist', () => {
expect(afAuth instanceof AngularFireAuth).toBe(true);
});
it('should have an initialized Firebase app', () => {
expect(afAuth.app).toBeDefined();
});
it('should emit auth updates through authState', (done: any) => {
let count = 0;
// Check that the first value is null and second is the auth user
const subs = afAuth.authState.subscribe({
next: (user => {
if (count === 0) {
expect(user).toBe(null);
count = count + 1;
mockAuthState.next(firebaseUser);
} else {
expect(user).toEqual(firebaseUser);
subs.unsubscribe();
done();
}
}),
error: done,
complete: done.fail
});
mockAuthState.next(null);
});
it('should emit auth updates through idToken', (done: any) => {
let count = 0;
// Check that the first value is null and second is the auth user
const subs = afAuth.idToken.subscribe({
next: user => {
if (count === 0) {
expect(user).toBe(null);
count = count + 1;
mockAuthState.next(firebaseUser);
} else {
expect(user as any).toEqual(firebaseUser);
subs.unsubscribe();
done();
}
},
error: done,
complete: done.fail
});
mockAuthState.next(null);
});
});
const FIREBASE_APP_NAME_TOO = (Math.random() + 1).toString(36).substring(7);
describe('AngularFireAuth with different app', () => {
let app: FirebaseApp;
let afAuth: AngularFireAuth;
beforeEach(() => {
TestBed.configureTestingModule({
imports: [
AngularFireModule.initializeApp(COMMON_CONFIG, rando()),
AngularFireAuthModule
],
providers: [
{ provide: FIREBASE_APP_NAME, useValue: FIREBASE_APP_NAME_TOO },
{ provide: FIREBASE_OPTIONS, useValue: COMMON_CONFIG }
]
});
app = TestBed.inject(FirebaseApp);
afAuth = TestBed.inject(AngularFireAuth);
});
afterEach(() => {
app.delete();
});
describe('<constructor>', () => {
it('should be an AngularFireAuth type', () => {
expect(afAuth instanceof AngularFireAuth).toEqual(true);
});
it('should have an initialized Firebase app', () => {
expect(afAuth.app).toBeDefined();
});
it('should have an initialized Firebase app instance member', async () => {
const app = await afAuth.app;
expect(app.name).toEqual(FIREBASE_APP_NAME_TOO);
});
});
});