Guest User

Untitled

a guest
Feb 28th, 2018
327
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. password: 'password'
  16. };
  17.  
  18. const userMocks = {
  19. uid: 'ABC123',
  20. email: credentialsMock.email
  21. };
  22.  
  23. const fakeAuthState = new BehaviorSubject(null);
  24.  
  25. const fakeSignInHandler = (email, password): Promise<any> => {
  26. fakeAuthState.next(credentialsMock);
  27. return Promise.resolve(credentialsMock);
  28. };
  29.  
  30. const angularFireAuthStub = {
  31. auth: {
  32. createUserWithEmailAndPassword: jasmine
  33. .createSpy('createUserWithEmailAndPassword')
  34. .and.callFake(fakeSignInHandler),
  35. signInWithEmailAndPassword: jasmine
  36. .createSpy('signInWithEmailAndPassword')
  37. .and.callFake(fakeSignInHandler),
  38. sendEmailVerification: jasmine
  39. .createSpy('sendEmailVerification')
  40. .and.callFake(fakeSignInHandler)
  41. }
  42. };
  43.  
  44. beforeEach(() => {
  45. TestBed.configureTestingModule({
  46. providers: [{ provide: AngularFireAuth, useValue: angularFireAuthStub }, AuthService]
  47. });
  48.  
  49. service = TestBed.get(AuthService);
  50. afAuth = TestBed.get(AngularFireAuth);
  51. });
  52.  
  53. afterEach(() => {
  54. angularFireAuthStub.auth.createUserWithEmailAndPassword.calls.reset();
  55. angularFireAuthStub.auth.signInWithEmailAndPassword.calls.reset();
  56. angularFireAuthStub.auth.sendEmailVerification.calls.reset();
  57. });
  58.  
  59. describe('signUserUp', () => {
  60. it('uses the firebase createUserWithEmailAndPassword method', () => {
  61. service.signUserUp$(credentialsMock);
  62. expect(afAuth.auth.createUserWithEmailAndPassword).toHaveBeenCalledWith(
  63. credentialsMock.email,
  64. credentialsMock.password
  65. );
  66. });
  67. });
  68.  
  69. constructor(private _firebase: AngularFireAuth) {}
  70.  
  71. signUserUp$(signUpUser: SignUpUser): Observable<firebase.User> {
  72. const { email, password, name } = signUpUser;
  73. return Observable.from(
  74. this._firebase.auth
  75. .createUserWithEmailAndPassword(email, password)
  76. // .then((user: firebase.User) =>
  77. // user.updateProfile({ displayName: signUpUser.name, photoURL: null }).then(() => user)
  78. // )
  79. .catch((err: firebase.auth.Error) => {
  80. throw err.message;
  81. })
  82. );
  83. }
  84.  
  85. describe('signInWithEmail', () => {
  86. it('throws if user does not exist', () => {
  87. service.signInWithEmail$(credentialsMock);
  88. expect(afAuth.auth.signInWithEmailAndPassword).toHaveBeenCalledWith(
  89. credentialsMock.email,
  90. credentialsMock.password
  91. );
  92. });
  93. });
  94.  
  95. signInWithEmail$(signInUser: SignInUser): Observable<firebase.User> {
  96. console.log('In signInWithEmail')
  97. return Observable.from(this._firebase.auth.signInWithEmailAndPassword(signInUser.email, signInUser.password))
  98. .switchMap((user: firebase.User) => {
  99. console.log('before if()')
  100. if (user && user.emailVerified) {
  101. return Observable.of(user);
  102. } else {
  103. return this._firebase.auth.signOut().then(() => {
  104. throw new Error('User not yet verified');
  105. });
  106. }
  107. })
  108. .catch(e => {
  109. return Observable.throw(e.message);
  110. });
  111. }
Add Comment
Please, Sign In to add comment