@@ -2075,7 +2075,9 @@ describe('Integration', () => {
20752075 } ) ;
20762076
20772077 describe ( 'CanLoad' , ( ) => {
2078+ let canLoadRunCount = 0 ;
20782079 beforeEach ( ( ) => {
2080+ canLoadRunCount = 0 ;
20792081 TestBed . configureTestingModule ( {
20802082 providers : [
20812083 { provide : 'alwaysFalse' , useValue : ( a : any ) => false } ,
@@ -2087,7 +2089,13 @@ describe('Integration', () => {
20872089 } ,
20882090 deps : [ Router ] ,
20892091 } ,
2090- { provide : 'alwaysTrue' , useValue : ( a : any ) => true } ,
2092+ {
2093+ provide : 'alwaysTrue' ,
2094+ useValue : ( ) => {
2095+ canLoadRunCount ++ ;
2096+ return true ;
2097+ }
2098+ } ,
20912099 ]
20922100 } ) ;
20932101 } ) ;
@@ -2173,6 +2181,43 @@ describe('Integration', () => {
21732181 [ NavigationStart , '/blank' ] , [ RoutesRecognized , '/blank' ] , [ NavigationEnd , '/blank' ]
21742182 ] ) ;
21752183 } ) ) ) ;
2184+
2185+ it ( 'should execute CanLoad only once' ,
2186+ fakeAsync ( inject (
2187+ [ Router , Location , NgModuleFactoryLoader ] ,
2188+ ( router : Router , location : Location , loader : SpyNgModuleFactoryLoader ) => {
2189+
2190+ @Component ( { selector : 'lazy' , template : 'lazy-loaded' } )
2191+ class LazyLoadedComponent {
2192+ }
2193+
2194+ @NgModule ( {
2195+ declarations : [ LazyLoadedComponent ] ,
2196+ imports :
2197+ [ RouterModule . forChild ( [ { path : 'loaded' , component : LazyLoadedComponent } ] ) ]
2198+ } )
2199+ class LazyLoadedModule {
2200+ }
2201+
2202+ loader . stubbedModules = { lazy : LazyLoadedModule } ;
2203+ const fixture = createRoot ( router , RootCmp ) ;
2204+
2205+ router . resetConfig ( [ { path : 'lazy' , canLoad : [ 'alwaysTrue' ] , loadChildren : 'lazy' } ] ) ;
2206+
2207+ router . navigateByUrl ( '/lazy/loaded' ) ;
2208+ advance ( fixture ) ;
2209+ expect ( location . path ( ) ) . toEqual ( '/lazy/loaded' ) ;
2210+ expect ( canLoadRunCount ) . toEqual ( 1 ) ;
2211+
2212+ router . navigateByUrl ( '/' ) ;
2213+ advance ( fixture ) ;
2214+ expect ( location . path ( ) ) . toEqual ( '/' ) ;
2215+
2216+ router . navigateByUrl ( '/lazy/loaded' ) ;
2217+ advance ( fixture ) ;
2218+ expect ( location . path ( ) ) . toEqual ( '/lazy/loaded' ) ;
2219+ expect ( canLoadRunCount ) . toEqual ( 1 ) ;
2220+ } ) ) ) ;
21762221 } ) ;
21772222
21782223 describe ( 'order' , ( ) => {
0 commit comments