|
| 1 | +import {async, ComponentFixture, TestBed} from '@angular/core/testing'; |
| 2 | +import {DebugElement, EventEmitter} from '@angular/core'; |
| 3 | + |
| 4 | +import {AngularFireModule} from 'angularfire2'; |
| 5 | +import {AngularFireAuth, AngularFireAuthModule} from 'angularfire2/auth'; |
| 6 | +import {BehaviorSubject} from 'rxjs/internal/BehaviorSubject'; |
| 7 | +import {AngularFirestore} from 'angularfire2/firestore'; |
| 8 | +import {HttpClientTestingModule} from '@angular/common/http/testing'; |
| 9 | +import {AuthComponent, AuthProcessService, FirestoreSyncService} from '../../../..'; |
| 10 | +import {NgBootstrapAuthFirebaseUIConfigToken} from '../../ngb-auth-firebase-u-i.module'; |
| 11 | +import {defaultAuthFirebaseUIConfig} from '../../interfaces/config.interface'; |
| 12 | +import {FormsModule, ReactiveFormsModule} from '@angular/forms'; |
| 13 | +import {NgbModule} from '@ng-bootstrap/ng-bootstrap'; |
| 14 | + |
| 15 | +describe('AuthProvidersComponent', function () { |
| 16 | + let de: DebugElement; |
| 17 | + let comp: AuthComponent; |
| 18 | + let fixture: ComponentFixture<AuthComponent>; |
| 19 | + |
| 20 | + let authProcessServicePartial: Partial<AuthProcessService>; |
| 21 | + |
| 22 | + authProcessServicePartial = { |
| 23 | + onErrorEmitter: new EventEmitter<any>() |
| 24 | + }; |
| 25 | + |
| 26 | + const credentialsMock = { |
| 27 | + email: 'abc@123.com', |
| 28 | + password: 'password' |
| 29 | + }; |
| 30 | + |
| 31 | + const userMock = { |
| 32 | + uid: 'ABC123', |
| 33 | + email: credentialsMock.email, |
| 34 | + }; |
| 35 | + |
| 36 | + const fakeAuthState = new BehaviorSubject(null); // <= Pay attention to this guy |
| 37 | + |
| 38 | + const mockSignInHandler = (email: any, password: any): Promise<any> => { |
| 39 | + fakeAuthState.next(userMock); |
| 40 | + return Promise.resolve(userMock); |
| 41 | + }; |
| 42 | + |
| 43 | + const mockSignOutHandler = (): Promise<any> => { |
| 44 | + fakeAuthState.next(null); |
| 45 | + return Promise.resolve(); |
| 46 | + }; |
| 47 | + |
| 48 | + const angularFireAuthStub = { |
| 49 | + authState: fakeAuthState, |
| 50 | + auth: { |
| 51 | + createUserWithEmailAndPassword: jasmine |
| 52 | + .createSpy('createUserWithEmailAndPassword') |
| 53 | + .and |
| 54 | + .callFake(mockSignInHandler), |
| 55 | + signInWithEmailAndPassword: jasmine |
| 56 | + .createSpy('signInWithEmailAndPassword') |
| 57 | + .and |
| 58 | + .callFake(mockSignInHandler), |
| 59 | + signOut: jasmine |
| 60 | + .createSpy('signOut') |
| 61 | + .and |
| 62 | + .callFake(mockSignOutHandler), |
| 63 | + }, |
| 64 | + }; |
| 65 | + |
| 66 | + const FirestoreStub = { |
| 67 | + collection: (name: string) => ({ |
| 68 | + doc: (_id: string) => ({ |
| 69 | + valueChanges: () => new BehaviorSubject({foo: 'bar'}), |
| 70 | + set: (_d: any) => new Promise((resolve, _reject) => resolve()), |
| 71 | + }), |
| 72 | + }), |
| 73 | + }; |
| 74 | + |
| 75 | + beforeEach(async(() => { |
| 76 | + |
| 77 | + Object.defineProperty(window, 'matchMedia', { |
| 78 | + value: jest.fn(() => { |
| 79 | + return {matches: true} |
| 80 | + }) |
| 81 | + }); |
| 82 | + |
| 83 | + TestBed.configureTestingModule({ |
| 84 | + imports: |
| 85 | + [ |
| 86 | + HttpClientTestingModule, |
| 87 | + NgbModule.forRoot(), |
| 88 | + FormsModule, |
| 89 | + ReactiveFormsModule |
| 90 | + ], |
| 91 | + declarations: [AuthComponent], |
| 92 | + providers: [ |
| 93 | + HttpClientTestingModule, |
| 94 | + FirestoreSyncService, |
| 95 | + AngularFireModule, |
| 96 | + {provide: AngularFirestore, useValue: FirestoreStub}, |
| 97 | + {provide: AngularFireAuth, useValue: angularFireAuthStub}, |
| 98 | + {provide: AuthProcessService, useValue: authProcessServicePartial}] |
| 99 | + }); |
| 100 | + |
| 101 | + })); |
| 102 | + |
| 103 | + beforeEach(() => { |
| 104 | + fixture = TestBed.createComponent(AuthComponent); |
| 105 | + comp = fixture.componentInstance; |
| 106 | + fixture.detectChanges(); |
| 107 | + }); |
| 108 | + |
| 109 | + it('should create components', () => expect(comp).toBeDefined()); |
| 110 | + |
| 111 | +}); |
0 commit comments