import { Location } from '@angular/common' ; import { Component , ModuleWithProviders , NgModule , NgModuleFactoryLoader } from "@angular/core" ; import { ComponentFixture , fakeAsync , inject , TestBed , tick } from "@angular/core/testing" ; import { Route , Router , RouterModule } from "@angular/router" ; import { RouterTestingModule , SpyNgModuleFactoryLoader } from "@angular/router/testing" ; import { TranslateModule , TranslateService } from '@rxweb/ngx-translate-extension' ; import { RxTranslateModule } from "@rxweb/translate" ; @ Component ( { selector : 'root-cmp' , template : `
      <router-outlet></router-outlet>` } ) class RootCmp { constructor ( public translate ) { translate . setTranslation ( 'en' , { "TEST" : "Root" , 'ROOT' : 'Root' } ) ; translate . use ( 'en' ) ; } } @ Component ( { selector : 'lazy' , template : 'lazy-loaded-parent [<router-outlet></router-outlet>]' } ) class ParentLazyLoadedComponent { } function getLazyLoadedModule ( importedModule ) { @ Component ( { selector : 'lazy' , template : 'lazy-loaded-child' } ) class ChildLazyLoadedComponent { constructor ( public translate ) { translate . setTranslation ( 'en' , { "TEST" : "Lazy" , 'CHILD' : 'Child' } ) ; translate . use ( 'en' ) ; expect ( translate . instant ( 'TEST' ) ) . toEqual ( 'Lazy' ) ; } } @ NgModule ( { declarations : [ ParentLazyLoadedComponent , ChildLazyLoadedComponent ] , imports : [ RouterModule . forChild ( [ < Route > { path : 'loaded' , component : ParentLazyLoadedComponent , children : [ { path : 'child' , component : ChildLazyLoadedComponent } ] } ] ) , importedModule , RxTranslateModule . forRoot ( { isTest : true , forNgxTranslate : true , cacheLanguageWiseObject : true , } ) ] } ) class LoadedModule { } return LoadedModule ; } function advance ( fixture ) { tick ( ) ; fixture . detectChanges ( ) ; } function createRoot ( router , type ) { const f = TestBed . createComponent ( type ) ; advance ( f ) ; router . initialNavigation ( ) ; advance ( f ) ; return f ; } describe ( "module" , ( ) => { beforeEach ( ( ) => { TestBed . configureTestingModule ( { imports : [ RouterTestingModule , TranslateModule . forRoot ( ) , RxTranslateModule . forRoot ( { isTest : true , forNgxTranslate : true , cacheLanguageWiseObject : true , } ) ] , declarations : [ RootCmp ] } ) ; } ) ; it ( "should work when lazy loaded using forChild" , fakeAsync ( inject ( [ Router , Location , NgModuleFactoryLoader ] , ( router , location , loader ) => { let LoadedModule = getLazyLoadedModule ( TranslateModule . forChild ( ) ) ; loader . stubbedModules = { expected : LoadedModule } ; const fixture = createRoot ( router , RootCmp ) , translate = TestBed . get ( TranslateService ) ; expect ( translate . instant ( 'TEST' ) ) . toEqual ( 'Root' ) ; router . resetConfig ( [ { path : 'lazy' , loadChildren : ( ) => import ( 'expected' ) . then ( m => m . default ) } ] ) ; router . navigateByUrl ( '/lazy/loaded/child' ) ; advance ( fixture ) ; expect ( location . path ( ) ) . toEqual ( '/lazy/loaded/child' ) ; expect ( translate . instant ( 'TEST' ) ) . toEqual ( 'Lazy' ) ; } ) ) ) ; it ( "should create 2 instances of the service when lazy loaded using forRoot" , fakeAsync ( inject ( [ Router , Location , NgModuleFactoryLoader ] , ( router , location , loader ) => { let LoadedModule = getLazyLoadedModule ( TranslateModule . forRoot ( ) ) ; loader . stubbedModules = { expected : LoadedModule } ; const fixture = createRoot ( router , RootCmp ) , translate = TestBed . get ( TranslateService ) ; expect ( translate . instant ( 'TEST' ) ) . toEqual ( 'Root' ) ; router . resetConfig ( [ { path : 'lazy' , loadChildren : ( ) => import ( 'expected' ) . then ( m => m . default ) } ] ) ; router . navigateByUrl ( '/lazy/loaded/child' ) ; advance ( fixture ) ; expect ( location . path ( ) ) . toEqual ( '/lazy/loaded/child' ) ; expect ( translate . instant ( 'TEST' ) ) . toEqual ( 'Root' ) ; } ) ) ) ; it ( "should relay events when lazy loading & using forChild with isolate false" , fakeAsync ( inject ( [ Router , Location , NgModuleFactoryLoader ] , ( router , location , loader ) => { let LoadedModule = getLazyLoadedModule ( TranslateModule . forChild ( ) ) ; loader . stubbedModules = { expected : LoadedModule } ; const fixture = createRoot ( router , RootCmp ) , translate = TestBed . get ( TranslateService ) ; let spy = jasmine . createSpy ( 'TranslationChange' ) ; let sub = translate . onTranslationChange . subscribe ( spy ) ; expect ( spy ) . toHaveBeenCalledTimes ( 0 ) ; router . resetConfig ( [ { path : 'lazy' , loadChildren : ( ) => import ( 'expected' ) . then ( m => m . default ) } ] ) ; router . navigateByUrl ( '/lazy/loaded/child' ) ; advance ( fixture ) ; expect ( spy ) . toHaveBeenCalledTimes ( 1 ) ; sub . unsubscribe ( ) ; } ) ) ) ; } ) ;