jest . mock ( 'app/core/auth/account.service' ) ; import { waitForAsync , ComponentFixture , TestBed } from '@angular/core/testing' ; import { Router , RouterEvent , NavigationEnd , NavigationStart } from '@angular/router' ; import { Title } from '@angular/platform-browser' ; import { Subject , of } from 'rxjs' ; import { TranslateModule , TranslateService , LangChangeEvent } from '@ngx-translate/core' ; import { AccountService } from 'app/core/auth/account.service' ; import { MainComponent } from './main.component' ; describe ( 'MainComponent' , ( ) => { let comp ; let fixture ; let titleService ; let translateService ; let mockAccountService ; const routerEventsSubject = new Subject < RouterEvent > ( ) ; const routerState = { snapshot : { root : { data : { } } } } ; class MockRouter { events = routerEventsSubject ; routerState = routerState ; } beforeEach ( waitForAsync ( ( ) => { TestBed . configureTestingModule ( { imports : [ TranslateModule . forRoot ( ) ] , declarations : [ MainComponent ] , providers : [ Title , AccountService , { provide : Router , useClass : MockRouter , } , ] , } ) . overrideTemplate ( MainComponent , '' ) . compileComponents ( ) ; } ) ) ; beforeEach ( ( ) => { fixture = TestBed . createComponent ( MainComponent ) ; comp = fixture . componentInstance ; titleService = TestBed . inject ( Title ) ; translateService = TestBed . inject ( TranslateService ) ; mockAccountService = TestBed . inject ( AccountService ) ; mockAccountService . identity = jest . fn ( ( ) => of ( null ) ) ; mockAccountService . getAuthenticationState = jest . fn ( ( ) => of ( null ) ) ; } ) ; describe ( 'page title' , ( ) => { const defaultPageTitle = 'global.title' ; const parentRoutePageTitle = 'parentTitle' ; const childRoutePageTitle = 'childTitle' ; const navigationEnd = new NavigationEnd ( 1 , '' , '' ) ; const navigationStart = new NavigationStart ( 1 , '' ) ; const langChangeEvent = { lang : 'en' , translations : null } ; beforeEach ( ( ) => { routerState . snapshot . root = { data : { } } ; jest . spyOn ( translateService , 'get' ) . mockImplementation ( ( key ) => of ( `${ key as string } translated` ) ) ; translateService . currentLang = 'en' ; jest . spyOn ( titleService , 'setTitle' ) ; comp . ngOnInit ( ) ; } ) ; describe ( 'navigation end' , ( ) => { it ( 'should set page title to default title if pageTitle is missing on routes' , ( ) => { routerEventsSubject . next ( navigationEnd ) ; expect ( translateService . get ) . toHaveBeenCalledWith ( defaultPageTitle ) ; expect ( titleService . setTitle ) . toHaveBeenCalledWith ( defaultPageTitle + ' translated' ) ; } ) ; it ( 'should set page title to root route pageTitle if there is no child routes' , ( ) => { routerState . snapshot . root . data = { pageTitle : parentRoutePageTitle } ; routerEventsSubject . next ( navigationEnd ) ; expect ( translateService . get ) . toHaveBeenCalledWith ( parentRoutePageTitle ) ; expect ( titleService . setTitle ) . toHaveBeenCalledWith ( parentRoutePageTitle + ' translated' ) ; } ) ; it ( 'should set page title to child route pageTitle if child routes exist and pageTitle is set for child route' , ( ) => { routerState . snapshot . root . data = { pageTitle : parentRoutePageTitle } ; routerState . snapshot . root . firstChild = { data : { pageTitle : childRoutePageTitle } } ; routerEventsSubject . next ( navigationEnd ) ; expect ( translateService . get ) . toHaveBeenCalledWith ( childRoutePageTitle ) ; expect ( titleService . setTitle ) . toHaveBeenCalledWith ( childRoutePageTitle + ' translated' ) ; } ) ; it ( 'should set page title to parent route pageTitle if child routes exists but pageTitle is not set for child route data' , ( ) => { routerState . snapshot . root . data = { pageTitle : parentRoutePageTitle } ; routerState . snapshot . root . firstChild = { data : { } } ; routerEventsSubject . next ( navigationEnd ) ; expect ( translateService . get ) . toHaveBeenCalledWith ( parentRoutePageTitle ) ; expect ( titleService . setTitle ) . toHaveBeenCalledWith ( parentRoutePageTitle + ' translated' ) ; } ) ; } ) ; describe ( 'navigation start' , ( ) => { it ( 'should not set page title on navigation start' , ( ) => { routerEventsSubject . next ( navigationStart ) ; expect ( titleService . setTitle ) . not . toHaveBeenCalled ( ) ; } ) ; } ) ; describe ( 'language change' , ( ) => { it ( 'should set page title to default title if pageTitle is missing on routes' , ( ) => { translateService . onLangChange . emit ( langChangeEvent ) ; expect ( translateService . get ) . toHaveBeenCalledWith ( defaultPageTitle ) ; expect ( titleService . setTitle ) . toHaveBeenCalledWith ( defaultPageTitle + ' translated' ) ; } ) ; it ( 'should set page title to root route pageTitle if there is no child routes' , ( ) => { routerState . snapshot . root . data = { pageTitle : parentRoutePageTitle } ; translateService . onLangChange . emit ( langChangeEvent ) ; expect ( translateService . get ) . toHaveBeenCalledWith ( parentRoutePageTitle ) ; expect ( titleService . setTitle ) . toHaveBeenCalledWith ( parentRoutePageTitle + ' translated' ) ; } ) ; it ( 'should set page title to child route pageTitle if child routes exist and pageTitle is set for child route' , ( ) => { routerState . snapshot . root . data = { pageTitle : parentRoutePageTitle } ; routerState . snapshot . root . firstChild = { data : { pageTitle : childRoutePageTitle } } ; translateService . onLangChange . emit ( langChangeEvent ) ; expect ( translateService . get ) . toHaveBeenCalledWith ( childRoutePageTitle ) ; expect ( titleService . setTitle ) . toHaveBeenCalledWith ( childRoutePageTitle + ' translated' ) ; } ) ; it ( 'should set page title to parent route pageTitle if child routes exists but pageTitle is not set for child route data' , ( ) => { routerState . snapshot . root . data = { pageTitle : parentRoutePageTitle } ; routerState . snapshot . root . firstChild = { data : { } } ; translateService . onLangChange . emit ( langChangeEvent ) ; expect ( translateService . get ) . toHaveBeenCalledWith ( parentRoutePageTitle ) ; expect ( titleService . setTitle ) . toHaveBeenCalledWith ( parentRoutePageTitle + ' translated' ) ; } ) ; } ) ; } ) ; describe ( 'page language attribute' , ( ) => { it ( 'should change page language attribute on language change' , ( ) => { comp . ngOnInit ( ) ; translateService . onLangChange . emit ( { lang : 'lang1' , translations : null } ) ; expect ( document . querySelector ( 'html' ) ?. getAttribute ( 'lang' ) ) . toEqual ( 'lang1' ) ; translateService . onLangChange . emit ( { lang : 'lang2' , translations : null } ) ; expect ( document . querySelector ( 'html' ) ?. getAttribute ( 'lang' ) ) . toEqual ( 'lang2' ) ; } ) ; } ) ; } ) ;