import { Injectable , NO_ERRORS_SCHEMA } from '@angular/core' ; import { async , ComponentFixture , inject , TestBed , } from '@angular/core/testing' ; import { Store } from '@ngrx/store' ; import { TranslateService } from '@ngx-translate/core' ; import { BehaviorSubject } from 'rxjs/BehaviorSubject' ; import { LANGUAGES } from 'app/core/injection-tokens' ; import * as UiActions from 'app/shared/states/ui/ui.actions' ; import { AppComponent } from './app.component' ; describe ( 'AppComponent' , ( ) => { let fixture ; beforeEach ( async ( ( ) => { TestBed . configureTestingModule ( { declarations : [ AppComponent ] , providers : [ { provide : TranslateService , useClass : TranslateMockService } , { provide : LANGUAGES , useValue : [ 'en' , 'fr' ] } , { provide : Store , useClass : StoreMockService } , ] , schemas : [ NO_ERRORS_SCHEMA ] , } ) . compileComponents ( ) ; } ) ) ; beforeEach ( async ( ( ) => { fixture = TestBed . createComponent ( AppComponent ) ; } ) ) ; it ( 'should set the lang by using the one from the browser if the environment variable for that is true' , inject ( [ TranslateService , Store ] , ( translateService , store ) => { spyOn ( translateService , 'getBrowserLang' ) . and . callThrough ( ) ; spyOn ( translateService , 'setDefaultLang' ) ; spyOn ( translateService , 'use' ) ; spyOn ( store , 'dispatch' ) ; translateService . browserLang = 'en' ; store . updateStore ( { ui : { language : 'en' } , } ) ; fixture . detectChanges ( ) ; expect ( translateService . setDefaultLang ) . toHaveBeenCalledWith ( 'en' ) ; expect ( store . dispatch ) . toHaveBeenCalledWith ( new UiActions . SetLanguage ( { language : 'en' } ) ) ; expect ( translateService . use ) . toHaveBeenCalledWith ( 'en' ) ; } ) ) ; } ) ; @ Injectable ( ) export class TranslateMockService { browserLang : string ; getBrowserLang ( ) { return this . browserLang ; } setDefaultLang ( defaultLang ) { } use ( lang ) { } } @ Injectable ( ) export class StoreMockService extends BehaviorSubject < any > { value : any ; constructor ( ) { super ( null ) ; } dispatch ( action ) { } updateStore ( state ) { this . next ( state ) ; } }