|
1 |
| -import { User } from 'firebase/app'; |
2 |
| -import { Observable, Subject } from 'rxjs' |
| 1 | +import { ReflectiveInjector, Provider } from '@angular/core'; |
3 | 2 | import { TestBed, inject } from '@angular/core/testing';
|
4 | 3 | import { FirebaseApp, FirebaseOptionsToken, AngularFireModule, FirebaseNameOrConfigToken } from '@angular/fire';
|
5 |
| -import { AngularFireAuth, AngularFireAuthModule } from '@angular/fire/auth'; |
| 4 | +import { AngularFireAnalytics, AngularFireAnalyticsModule, AUTOMATICALLY_SET_CURRENT_SCREEN, AUTOMATICALLY_LOG_SCREEN_VIEWS, ANALYTICS_COLLECTION_ENABLED, AUTOMATICALLY_TRACK_USER_IDENTIFIER, APP_VERSION, APP_NAME } from '@angular/fire/analytics'; |
6 | 5 | import { COMMON_CONFIG } from './test-config';
|
7 |
| -import { take, skip } from 'rxjs/operators'; |
8 | 6 |
|
9 |
| -function authTake(auth: Observable<any>, count: number): Observable<any> { |
10 |
| - return take.call(auth, 1); |
11 |
| -} |
12 | 7 |
|
13 |
| -function authSkip(auth: Observable<any>, count: number): Observable<any> { |
14 |
| - return skip.call(auth, 1); |
15 |
| -} |
16 |
| - |
17 |
| -const firebaseUser = <User> { |
18 |
| - uid: '12345', |
19 |
| - providerData: [{ displayName: 'jeffbcrossyface' }] |
20 |
| -}; |
21 |
| - |
22 |
| -describe('AngularFireAuth', () => { |
| 8 | +describe('AngularFireAnalytics', () => { |
23 | 9 | let app: FirebaseApp;
|
24 |
| - let afAuth: AngularFireAuth; |
25 |
| - let authSpy: jasmine.Spy; |
26 |
| - let mockAuthState: Subject<User>; |
| 10 | + let analytics: AngularFireAnalytics; |
27 | 11 |
|
28 | 12 | beforeEach(() => {
|
29 | 13 | TestBed.configureTestingModule({
|
30 | 14 | imports: [
|
31 | 15 | AngularFireModule.initializeApp(COMMON_CONFIG),
|
32 |
| - AngularFireAuthModule |
| 16 | + AngularFireAnalyticsModule |
33 | 17 | ]
|
34 | 18 | });
|
35 |
| - inject([FirebaseApp, AngularFireAuth], (app_: FirebaseApp, _auth: AngularFireAuth) => { |
| 19 | + inject([FirebaseApp, AngularFireAnalytics], (app_: FirebaseApp, _analytics: AngularFireAnalytics) => { |
36 | 20 | app = app_;
|
37 |
| - afAuth = _auth; |
| 21 | + analytics = _analytics; |
38 | 22 | })();
|
39 |
| - |
40 |
| - mockAuthState = new Subject<User>(); |
41 |
| - spyOn(afAuth, 'authState').and.returnValue(mockAuthState); |
42 |
| - spyOn(afAuth, 'idToken').and.returnValue(mockAuthState); |
43 |
| - afAuth.authState = mockAuthState as Observable<User>; |
44 |
| - afAuth.idToken = mockAuthState as Observable<User>; |
45 | 23 | });
|
46 | 24 |
|
47 | 25 | afterEach(done => {
|
48 |
| - afAuth.auth.app.delete().then(done, done.fail); |
49 |
| - }); |
50 |
| - |
51 |
| - describe('Zones', () => { |
52 |
| - it('should call operators and subscriber in the same zone as when service was initialized', (done) => { |
53 |
| - // Initialize the app outside of the zone, to mimick real life behavior. |
54 |
| - let ngZone = Zone.current.fork({ |
55 |
| - name: 'ngZone' |
56 |
| - }); |
57 |
| - ngZone.run(() => { |
58 |
| - const subs = [ |
59 |
| - afAuth.authState.subscribe(user => { |
60 |
| - expect(Zone.current.name).toBe('ngZone'); |
61 |
| - done(); |
62 |
| - }, done.fail), |
63 |
| - afAuth.authState.subscribe(user => { |
64 |
| - expect(Zone.current.name).toBe('ngZone'); |
65 |
| - done(); |
66 |
| - }, done.fail) |
67 |
| - ]; |
68 |
| - mockAuthState.next(firebaseUser); |
69 |
| - subs.forEach(s => s.unsubscribe()); |
70 |
| - }); |
71 |
| - }); |
| 26 | + app.delete(); |
| 27 | + done(); |
72 | 28 | });
|
73 | 29 |
|
74 | 30 | it('should be exist', () => {
|
75 |
| - expect(afAuth instanceof AngularFireAuth).toBe(true); |
| 31 | + expect(analytics instanceof AngularFireAnalytics).toBe(true); |
76 | 32 | });
|
77 | 33 |
|
78 |
| - it('should have the Firebase Auth instance', () => { |
79 |
| - expect(afAuth.auth).toBeDefined(); |
80 |
| - }); |
81 |
| - |
82 |
| - it('should have an initialized Firebase app', () => { |
83 |
| - expect(afAuth.auth.app).toBeDefined(); |
84 |
| - expect(afAuth.auth.app).toEqual(app); |
85 |
| - }); |
86 |
| - |
87 |
| - it('should emit auth updates through authState', (done: any) => { |
88 |
| - let count = 0; |
89 |
| - |
90 |
| - // Check that the first value is null and second is the auth user |
91 |
| - const subs = afAuth.authState.subscribe(user => { |
92 |
| - if (count === 0) { |
93 |
| - expect(user).toBe(null!); |
94 |
| - count = count + 1; |
95 |
| - mockAuthState.next(firebaseUser); |
96 |
| - } else { |
97 |
| - expect(user).toEqual(firebaseUser); |
98 |
| - subs.unsubscribe(); |
99 |
| - done(); |
100 |
| - } |
101 |
| - }, done, done.fail); |
102 |
| - mockAuthState.next(null!); |
103 |
| - }); |
104 |
| - |
105 |
| - it('should emit auth updates through idToken', (done: any) => { |
106 |
| - let count = 0; |
107 |
| - |
108 |
| - // Check that the first value is null and second is the auth user |
109 |
| - const subs = afAuth.idToken.subscribe(user => { |
110 |
| - if (count === 0) { |
111 |
| - expect(user).toBe(null!); |
112 |
| - count = count + 1; |
113 |
| - mockAuthState.next(firebaseUser); |
114 |
| - } else { |
115 |
| - expect(user).toEqual(firebaseUser); |
116 |
| - subs.unsubscribe(); |
117 |
| - done(); |
118 |
| - } |
119 |
| - }, done, done.fail); |
120 |
| - mockAuthState.next(null!); |
| 34 | + it('should have the Firebase Functions instance', () => { |
| 35 | + expect(analytics.analytics).toBeDefined(); |
121 | 36 | });
|
122 | 37 |
|
123 | 38 | });
|
124 | 39 |
|
125 | 40 | const FIREBASE_APP_NAME_TOO = (Math.random() + 1).toString(36).substring(7);
|
126 | 41 |
|
127 |
| -describe('AngularFireAuth with different app', () => { |
| 42 | +describe('AngularFireAnalytics with different app', () => { |
128 | 43 | let app: FirebaseApp;
|
129 |
| - let afAuth: AngularFireAuth; |
| 44 | + let analytics: AngularFireAnalytics; |
130 | 45 |
|
131 | 46 | beforeEach(() => {
|
132 | 47 | TestBed.configureTestingModule({
|
133 | 48 | imports: [
|
134 | 49 | AngularFireModule.initializeApp(COMMON_CONFIG),
|
135 |
| - AngularFireAuthModule |
| 50 | + AngularFireAnalyticsModule |
136 | 51 | ],
|
137 | 52 | providers: [
|
138 | 53 | { provide: FirebaseNameOrConfigToken, useValue: FIREBASE_APP_NAME_TOO },
|
139 |
| - { provide: FirebaseOptionsToken, useValue: COMMON_CONFIG } |
| 54 | + { provide: FirebaseOptionsToken, useValue: COMMON_CONFIG }, |
| 55 | + { provide: AUTOMATICALLY_SET_CURRENT_SCREEN, useValue: true } |
| 56 | + { provide: AUTOMATICALLY_LOG_SCREEN_VIEWS, useValue: true }, |
| 57 | + { provide: ANALYTICS_COLLECTION_ENABLED, useValue: true }, |
| 58 | + { provide: AUTOMATICALLY_TRACK_USER_IDENTIFIER, useValue: true }, |
| 59 | + { provide: APP_VERSION, useValue: '0.0' }, |
| 60 | + { provide: APP_NAME, useValue: 'Test App!' } |
140 | 61 | ]
|
141 | 62 | });
|
142 |
| - inject([FirebaseApp, AngularFireAuth], (app_: FirebaseApp, _afAuth: AngularFireAuth) => { |
| 63 | + inject([FirebaseApp, AngularFireAnalytics], (app_: FirebaseApp, _analytics: AngularFireAnalytics) => { |
143 | 64 | app = app_;
|
144 |
| - afAuth = _afAuth; |
| 65 | + analytics = _analytics; |
145 | 66 | })();
|
146 | 67 | });
|
147 | 68 |
|
148 | 69 | afterEach(done => {
|
149 |
| - app.delete().then(done, done.fail); |
| 70 | + app.delete(); |
| 71 | + done(); |
150 | 72 | });
|
151 | 73 |
|
152 | 74 | describe('<constructor>', () => {
|
153 | 75 |
|
154 | 76 | it('should be an AngularFireAuth type', () => {
|
155 |
| - expect(afAuth instanceof AngularFireAuth).toEqual(true); |
| 77 | + expect(analytics instanceof AngularFireAnalytics).toEqual(true); |
156 | 78 | });
|
157 | 79 |
|
158 |
| - it('should have an initialized Firebase app', () => { |
159 |
| - expect(afAuth.auth.app).toBeDefined(); |
160 |
| - expect(afAuth.auth.app).toEqual(app); |
| 80 | + it('should have the Firebase Functions instance', () => { |
| 81 | + expect(analytics.analytics).toBeDefined(); |
161 | 82 | });
|
162 | 83 |
|
163 |
| - it('should have an initialized Firebase app instance member', () => { |
164 |
| - expect(afAuth.auth.app.name).toEqual(FIREBASE_APP_NAME_TOO); |
165 |
| - }); |
166 | 84 | });
|
167 | 85 |
|
168 | 86 | });
|
0 commit comments