jest . mock ( 'app/core/auth/account.service' ) ; import { ComponentFixture , TestBed , waitForAsync } from '@angular/core/testing' ; import { Router } from '@angular/router' ; import { RouterTestingModule } from '@angular/router/testing' ; import { of , Subject } from 'rxjs' ; import { AccountService } from 'app/core/auth/account.service' ; import { Account } from 'app/core/auth/account.model' ; import { HomeComponent } from './home.component' ; describe ( 'Home Component' , ( ) => { let comp ; let fixture ; let mockAccountService ; let mockRouter ; const account = { activated : true , authorities : [ ] , email : '' , firstName : null , langKey : '' , lastName : null , login : 'login' , imageUrl : null , } ; beforeEach ( waitForAsync ( ( ) => { TestBed . configureTestingModule ( { imports : [ RouterTestingModule . withRoutes ( [ ] ) ] , declarations : [ HomeComponent ] , providers : [ AccountService ] , } ) . overrideTemplate ( HomeComponent , '' ) . compileComponents ( ) ; } ) ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( HomeComponent ) ; comp = fixture . componentInstance ; mockAccountService = TestBed . inject ( AccountService ) ; mockAccountService . identity = jest . fn ( ( ) => of ( null ) ) ; mockAccountService . getAuthenticationState = jest . fn ( ( ) => of ( null ) ) ; mockRouter = TestBed . inject ( Router ) ; jest . spyOn ( mockRouter , 'navigate' ) . mockImplementation ( ( ) => Promise . resolve ( true ) ) ; } ) ; describe ( 'ngOnInit' , ( ) => { it ( 'Should synchronize account variable with current account' , ( ) => { const authenticationState = new Subject < Account | null > ( ) ; mockAccountService . getAuthenticationState = jest . fn ( ( ) => authenticationState . asObservable ( ) ) ; comp . ngOnInit ( ) ; expect ( comp . account ) . toBeNull ( ) ; authenticationState . next ( account ) ; expect ( comp . account ) . toEqual ( account ) ; authenticationState . next ( null ) ; expect ( comp . account ) . toBeNull ( ) ; } ) ; } ) ; describe ( 'login' , ( ) => { it ( 'Should navigate to /login on login' , ( ) => { comp . login ( ) ; expect ( mockRouter . navigate ) . toHaveBeenCalledWith ( [ '/login' ] ) ; } ) ; } ) ; describe ( 'ngOnDestroy' , ( ) => { it ( 'Should destroy authentication state subscription on component destroy' , ( ) => { const authenticationState = new Subject < Account | null > ( ) ; mockAccountService . getAuthenticationState = jest . fn ( ( ) => authenticationState . asObservable ( ) ) ; comp . ngOnInit ( ) ; expect ( comp . account ) . toBeNull ( ) ; authenticationState . next ( account ) ; expect ( comp . account ) . toEqual ( account ) ; comp . ngOnDestroy ( ) ; authenticationState . next ( null ) ; expect ( comp . account ) . toEqual ( account ) ; } ) ; } ) ; } ) ;