Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { TestBed, inject, fakeAsync, tick } from '@angular/core/testing';
- import * as firebase from 'firebase';
- import { AuthService } from './auth.service';
- import { AngularFireAuth } from 'angularfire2/auth';
- import { SignUpUser } from '@spawntech/mystique/models';
- import { BehaviorSubject } from 'rxjs/BehaviorSubject';
- fdescribe('AuthService', () => {
- let service: AuthService;
- let afAuth: AngularFireAuth;
- const credentialsMock: SignUpUser = {
- name: 'abc',
- email: 'abc@123.com',
- password: 'password'
- };
- const userMocks = {
- uid: 'ABC123',
- email: credentialsMock.email
- };
- const fakeAuthState = new BehaviorSubject(null);
- const fakeSignInHandler = (email, password): Promise<any> => {
- fakeAuthState.next(credentialsMock);
- return Promise.resolve(credentialsMock);
- };
- const angularFireAuthStub = {
- auth: {
- createUserWithEmailAndPassword: jasmine
- .createSpy('createUserWithEmailAndPassword')
- .and.callFake(fakeSignInHandler),
- signInWithEmailAndPassword: jasmine
- .createSpy('signInWithEmailAndPassword')
- .and.callFake(fakeSignInHandler),
- sendEmailVerification: jasmine
- .createSpy('sendEmailVerification')
- .and.callFake(fakeSignInHandler)
- }
- };
- beforeEach(() => {
- TestBed.configureTestingModule({
- providers: [{ provide: AngularFireAuth, useValue: angularFireAuthStub }, AuthService]
- });
- service = TestBed.get(AuthService);
- afAuth = TestBed.get(AngularFireAuth);
- });
- afterEach(() => {
- angularFireAuthStub.auth.createUserWithEmailAndPassword.calls.reset();
- angularFireAuthStub.auth.signInWithEmailAndPassword.calls.reset();
- angularFireAuthStub.auth.sendEmailVerification.calls.reset();
- });
- describe('signUserUp', () => {
- it('uses the firebase createUserWithEmailAndPassword method', () => {
- service.signUserUp$(credentialsMock);
- expect(afAuth.auth.createUserWithEmailAndPassword).toHaveBeenCalledWith(
- credentialsMock.email,
- credentialsMock.password
- );
- });
- });
- constructor(private _firebase: AngularFireAuth) {}
- signUserUp$(signUpUser: SignUpUser): Observable<firebase.User> {
- const { email, password, name } = signUpUser;
- return Observable.from(
- this._firebase.auth
- .createUserWithEmailAndPassword(email, password)
- // .then((user: firebase.User) =>
- // user.updateProfile({ displayName: signUpUser.name, photoURL: null }).then(() => user)
- // )
- .catch((err: firebase.auth.Error) => {
- throw err.message;
- })
- );
- }
- describe('signInWithEmail', () => {
- it('throws if user does not exist', () => {
- service.signInWithEmail$(credentialsMock);
- expect(afAuth.auth.signInWithEmailAndPassword).toHaveBeenCalledWith(
- credentialsMock.email,
- credentialsMock.password
- );
- });
- });
- signInWithEmail$(signInUser: SignInUser): Observable<firebase.User> {
- console.log('In signInWithEmail')
- return Observable.from(this._firebase.auth.signInWithEmailAndPassword(signInUser.email, signInUser.password))
- .switchMap((user: firebase.User) => {
- console.log('before if()')
- if (user && user.emailVerified) {
- return Observable.of(user);
- } else {
- return this._firebase.auth.signOut().then(() => {
- throw new Error('User not yet verified');
- });
- }
- })
- .catch(e => {
- return Observable.throw(e.message);
- });
- }
Add Comment
Please, Sign In to add comment