Guest User

Untitled

a guest
May 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. import { async, ComponentFixture, TestBed } from '@angular/core/testing';
  2. import { ActivatedRoute, Router } from '@angular/router';
  3. import { By } from '@angular/platform-browser';
  4. import { Component, Input } from '@angular/core';
  5.  
  6. import { LoginMenuComponent } from './login-menu.component';
  7.  
  8. // rxjs
  9. import { Observable, of } from 'rxjs';
  10.  
  11. import { AuthService } from '@app/shared/services/auth.service';
  12.  
  13. import { Store } from '@ngrx/store';
  14. import { FirebaseApp } from 'angularfire2';
  15. class MockAuthService {
  16. public isLoggedIn = false;
  17. }
  18.  
  19. @Component({
  20. selector: 'mat-menu',
  21. template: '',
  22. exportAs: 'matMenu'
  23. })
  24. class MockMatMenuComponent {}
  25. @Component({
  26. selector: '[mat-icon-button]',
  27. template: '<ng-content></ng-content>'
  28. })
  29. class MockMatButtonComponent {
  30. @Input() matMenuTriggerFor;
  31. }
  32. describe('LoginMenuComponent', () => {
  33. let component: LoginMenuComponent;
  34. let fixture: ComponentFixture<LoginMenuComponent>;
  35. let element, de;
  36. let mockAuth;
  37. let mockStore;
  38. beforeEach(async(() => {
  39. TestBed.configureTestingModule({
  40. declarations: [
  41. MockMatMenuComponent,
  42. LoginMenuComponent,
  43. MockMatButtonComponent
  44. ],
  45. providers: [
  46. {
  47. provide: FirebaseApp,
  48. useValue: {
  49. storage: () => ({
  50. ref: () => ({
  51. child: () => ({ getDownloadURL: () => new Promise(() => {}) })
  52. })
  53. })
  54. }
  55. },
  56. {
  57. provide: Router,
  58. useValue: { events: { subscribe: jasmine.createSpy('subscribe') } }
  59. },
  60. { provide: AuthService, useClass: MockAuthService },
  61. {
  62. provide: Store,
  63. useValue: {
  64. dispatch: () => {},
  65. select: () => of({ displayName: 'koko' })
  66. }
  67. }
  68. ]
  69. }).compileComponents();
  70. }));
  71.  
  72. beforeEach(async(() => {
  73. fixture = TestBed.createComponent(LoginMenuComponent);
  74. component = fixture.componentInstance;
  75. element = fixture.nativeElement; // to access DOM element
  76. de = fixture.debugElement;
  77. }));
  78.  
  79. it('should create with "login" link', () => {
  80. mockAuth = de.injector.get(AuthService) as MockAuthService;
  81. mockAuth.isLoggedIn = false;
  82. fixture.detectChanges();
  83.  
  84. expect(de.query(By.css('a')).nativeElement.innerText).toBe('login');
  85. expect(de.query(By.css('span.user-login-name'))).toBeNull();
  86. });
  87. it('should display dropdown with user menus when loggedin', () => {
  88. mockAuth = de.injector.get(AuthService) as MockAuthService;
  89. mockAuth.isLoggedIn = true;
  90.  
  91. mockStore = de.injector.get(Store);
  92. mockStore.select = (p: any) => of({ displayName: 'shoko' });
  93.  
  94. fixture.detectChanges();
  95.  
  96. expect(
  97. de.query(By.css('button[mat-icon-button]')).nativeElement.outerHTML
  98. ).toContain('img');
  99. expect(
  100. de.query(By.css('span.user-login-name')).nativeElement.textContent
  101. ).toBe('shoko');
  102. expect(component.user.displayName).toBe('shoko');
  103. });
  104. });
Add Comment
Please, Sign In to add comment