|
| 1 | +import { User } from 'firebase/app'; |
| 2 | +import { Observable, Subject } from 'rxjs' |
| 3 | +import { TestBed, inject } from '@angular/core/testing'; |
| 4 | +import { FirebaseApp, FirebaseOptionsToken, AngularFireModule, FirebaseNameOrConfigToken } from '@angular/fire'; |
| 5 | +import { AngularFireAuth, AngularFireAuthModule } from '@angular/fire/auth'; |
| 6 | +import { COMMON_CONFIG } from './test-config'; |
| 7 | +import { take, skip } from 'rxjs/operators'; |
| 8 | + |
| 9 | +function authTake(auth: Observable<any>, count: number): Observable<any> { |
| 10 | + return take.call(auth, 1); |
| 11 | +} |
| 12 | + |
| 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', () => { |
| 23 | + let app: FirebaseApp; |
| 24 | + let afAuth: AngularFireAuth; |
| 25 | + let authSpy: jasmine.Spy; |
| 26 | + let mockAuthState: Subject<User>; |
| 27 | + |
| 28 | + beforeEach(() => { |
| 29 | + TestBed.configureTestingModule({ |
| 30 | + imports: [ |
| 31 | + AngularFireModule.initializeApp(COMMON_CONFIG), |
| 32 | + AngularFireAuthModule |
| 33 | + ] |
| 34 | + }); |
| 35 | + inject([FirebaseApp, AngularFireAuth], (app_: FirebaseApp, _auth: AngularFireAuth) => { |
| 36 | + app = app_; |
| 37 | + afAuth = _auth; |
| 38 | + })(); |
| 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 | + }); |
| 46 | + |
| 47 | + 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 | + }); |
| 72 | + }); |
| 73 | + |
| 74 | + it('should be exist', () => { |
| 75 | + expect(afAuth instanceof AngularFireAuth).toBe(true); |
| 76 | + }); |
| 77 | + |
| 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!); |
| 121 | + }); |
| 122 | + |
| 123 | +}); |
| 124 | + |
| 125 | +const FIREBASE_APP_NAME_TOO = (Math.random() + 1).toString(36).substring(7); |
| 126 | + |
| 127 | +describe('AngularFireAuth with different app', () => { |
| 128 | + let app: FirebaseApp; |
| 129 | + let afAuth: AngularFireAuth; |
| 130 | + |
| 131 | + beforeEach(() => { |
| 132 | + TestBed.configureTestingModule({ |
| 133 | + imports: [ |
| 134 | + AngularFireModule.initializeApp(COMMON_CONFIG), |
| 135 | + AngularFireAuthModule |
| 136 | + ], |
| 137 | + providers: [ |
| 138 | + { provide: FirebaseNameOrConfigToken, useValue: FIREBASE_APP_NAME_TOO }, |
| 139 | + { provide: FirebaseOptionsToken, useValue: COMMON_CONFIG } |
| 140 | + ] |
| 141 | + }); |
| 142 | + inject([FirebaseApp, AngularFireAuth], (app_: FirebaseApp, _afAuth: AngularFireAuth) => { |
| 143 | + app = app_; |
| 144 | + afAuth = _afAuth; |
| 145 | + })(); |
| 146 | + }); |
| 147 | + |
| 148 | + afterEach(done => { |
| 149 | + app.delete().then(done, done.fail); |
| 150 | + }); |
| 151 | + |
| 152 | + describe('<constructor>', () => { |
| 153 | + |
| 154 | + it('should be an AngularFireAuth type', () => { |
| 155 | + expect(afAuth instanceof AngularFireAuth).toEqual(true); |
| 156 | + }); |
| 157 | + |
| 158 | + it('should have an initialized Firebase app', () => { |
| 159 | + expect(afAuth.auth.app).toBeDefined(); |
| 160 | + expect(afAuth.auth.app).toEqual(app); |
| 161 | + }); |
| 162 | + |
| 163 | + it('should have an initialized Firebase app instance member', () => { |
| 164 | + expect(afAuth.auth.app.name).toEqual(FIREBASE_APP_NAME_TOO); |
| 165 | + }); |
| 166 | + }); |
| 167 | + |
| 168 | +}); |
0 commit comments