Guest User

Untitled

a guest
Feb 28th, 2018
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. import { TestBed, inject, fakeAsync, tick } from '@angular/core/testing';
  2.  
  3. import * as firebase from 'firebase';
  4. import { AuthService } from './auth.service';
  5. import { AngularFireAuth } from 'angularfire2/auth';
  6. import { SignUpUser } from '@spawntech/mystique/models';
  7. import { BehaviorSubject } from 'rxjs/BehaviorSubject';
  8.  
  9. fdescribe('AuthService', () => {
  10. let service: AuthService;
  11. let afAuth: AngularFireAuth;
  12.  
  13. const credentialsMock: SignUpUser = {
  14. name: 'abc',
  15. email: 'abc@123.com',
  16. password: 'password'
  17. };
  18.  
  19. const userMocks = {
  20. uid: 'ABC123',
  21. email: credentialsMock.email
  22. };
  23.  
  24. const fakeAuthState = new BehaviorSubject(null);
  25.  
  26. const fakeSignInHandler = (email, password): Promise<any> => {
  27. fakeAuthState.next(credentialsMock);
  28. return Promise.resolve(credentialsMock);
  29. };
  30.  
  31. const angularFireAuthStub = {
  32. auth: {
  33. createUserWithEmailAndPassword: jasmine
  34. .createSpy('createUserWithEmailAndPassword')
  35. .and.callFake(fakeSignInHandler),
  36. signInWithEmailAndPassword: jasmine
  37. .createSpy('signInWithEmailAndPassword')
  38. .and.callFake(fakeSignInHandler),
  39. sendEmailVerification: jasmine
  40. .createSpy('sendEmailVerification')
  41. .and.callFake(fakeSignInHandler)
  42. }
  43. };
  44.  
  45. beforeEach(() => {
  46. TestBed.configureTestingModule({
  47. providers: [{ provide: AngularFireAuth, useValue: angularFireAuthStub }, AuthService]
  48. });
  49.  
  50. service = TestBed.get(AuthService);
  51. afAuth = TestBed.get(AngularFireAuth);
  52. });
  53.  
  54. afterEach(() => {
  55. angularFireAuthStub.auth.createUserWithEmailAndPassword.calls.reset();
  56. angularFireAuthStub.auth.signInWithEmailAndPassword.calls.reset();
  57. angularFireAuthStub.auth.sendEmailVerification.calls.reset();
  58. });
  59.  
  60. describe('signUserUp', () => {
  61. it('uses the firebase createUserWithEmailAndPassword method', () => {
  62. service.signUserUp$(credentialsMock);
  63. expect(afAuth.auth.createUserWithEmailAndPassword).toHaveBeenCalledWith(
  64. credentialsMock.email,
  65. credentialsMock.password
  66. );
  67. });
  68. });
  69.  
  70. constructor(private _firebase: AngularFireAuth) {}
  71.  
  72. signUserUp$(signUpUser: SignUpUser): Observable<firebase.User> {
  73. const { email, password, name } = signUpUser;
  74. return Observable.from(
  75. this._firebase.auth
  76. .createUserWithEmailAndPassword(email, password)
  77. // .then((user: firebase.User) =>
  78. // user.updateProfile({ displayName: signUpUser.name, photoURL: null }).then(() => user)
  79. // )
  80. .catch((err: firebase.auth.Error) => {
  81. throw err.message;
  82. })
  83. );
  84. }
  85.  
  86. describe('signInWithEmail', () => {
  87. it('throws if user does not exist', () => {
  88. service.signInWithEmail$(credentialsMock);
  89. expect(afAuth.auth.signInWithEmailAndPassword).toHaveBeenCalledWith(
  90. credentialsMock.email,
  91. credentialsMock.password
  92. );
  93. });
  94. });
  95.  
  96. signInWithEmail$(signInUser: SignInUser): Observable<firebase.User> {
  97. console.log('In signInWithEmail')
  98. return Observable.from(this._firebase.auth.signInWithEmailAndPassword(signInUser.email, signInUser.password))
  99. .switchMap((user: firebase.User) => {
  100. console.log('before if()')
  101. if (user && user.emailVerified) {
  102. return Observable.of(user);
  103. } else {
  104. return this._firebase.auth.signOut().then(() => {
  105. throw new Error('User not yet verified');
  106. });
  107. }
  108. })
  109. .catch(e => {
  110. return Observable.throw(e.message);
  111. });
  112. }
Add Comment
Please, Sign In to add comment