@@ -2075,7 +2075,9 @@ describe('Integration', () => {
2075
2075
} ) ;
2076
2076
2077
2077
describe ( 'CanLoad' , ( ) => {
2078
+ let canLoadRunCount = 0 ;
2078
2079
beforeEach ( ( ) => {
2080
+ canLoadRunCount = 0 ;
2079
2081
TestBed . configureTestingModule ( {
2080
2082
providers : [
2081
2083
{ provide : 'alwaysFalse' , useValue : ( a : any ) => false } ,
@@ -2087,7 +2089,13 @@ describe('Integration', () => {
2087
2089
} ,
2088
2090
deps : [ Router ] ,
2089
2091
} ,
2090
- { provide : 'alwaysTrue' , useValue : ( a : any ) => true } ,
2092
+ {
2093
+ provide : 'alwaysTrue' ,
2094
+ useValue : ( ) => {
2095
+ canLoadRunCount ++ ;
2096
+ return true ;
2097
+ }
2098
+ } ,
2091
2099
]
2092
2100
} ) ;
2093
2101
} ) ;
@@ -2173,6 +2181,43 @@ describe('Integration', () => {
2173
2181
[ NavigationStart , '/blank' ] , [ RoutesRecognized , '/blank' ] , [ NavigationEnd , '/blank' ]
2174
2182
] ) ;
2175
2183
} ) ) ) ;
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
+ } ) ) ) ;
2176
2221
} ) ;
2177
2222
2178
2223
describe ( 'order' , ( ) => {
0 commit comments