fix(router): don't mutate route configs (#22358)
Fixes #22203 PR Close #22358
This commit is contained in:

committed by
Victor Berchet

parent
b3ffeaa22b
commit
45eff4cc65
@ -22,6 +22,38 @@ import {RouterTestingModule} from '../testing/src/router_testing_module';
|
||||
import {Logger, createActivatedRouteSnapshot, provideTokenLogger} from './helpers';
|
||||
|
||||
describe('Router', () => {
|
||||
|
||||
describe('resetConfig', () => {
|
||||
class TestComponent {}
|
||||
|
||||
beforeEach(() => { TestBed.configureTestingModule({imports: [RouterTestingModule]}); });
|
||||
|
||||
it('should copy config to avoid mutations of user-provided objects', () => {
|
||||
const r: Router = TestBed.get(Router);
|
||||
const configs = [{
|
||||
path: 'a',
|
||||
component: TestComponent,
|
||||
children: [{path: 'b', component: TestComponent}, {path: 'c', component: TestComponent}]
|
||||
}];
|
||||
r.resetConfig(configs);
|
||||
|
||||
let rConfigs = r.config;
|
||||
|
||||
// routes array and shallow copy
|
||||
expect(configs).not.toBe(rConfigs);
|
||||
expect(configs[0]).not.toBe(rConfigs[0]);
|
||||
expect(configs[0].path).toBe(rConfigs[0].path);
|
||||
expect(configs[0].component).toBe(rConfigs[0].component);
|
||||
|
||||
// children should be new array and routes shallow copied
|
||||
expect(configs[0].children).not.toBe(rConfigs[0].children);
|
||||
expect(configs[0].children[0]).not.toBe(rConfigs[0].children ![0]);
|
||||
expect(configs[0].children[0].path).toBe(rConfigs[0].children ![0].path);
|
||||
expect(configs[0].children[1]).not.toBe(rConfigs[0].children ![1]);
|
||||
expect(configs[0].children[1].path).toBe(rConfigs[0].children ![1].path);
|
||||
});
|
||||
});
|
||||
|
||||
describe('resetRootComponentType', () => {
|
||||
class NewRootComponent {}
|
||||
|
||||
|
@ -210,4 +210,35 @@ describe('RouterPreloader', () => {
|
||||
expect((c[1] as any)._loadedConfig).toBeDefined();
|
||||
})));
|
||||
});
|
||||
|
||||
describe('should copy loaded configs', () => {
|
||||
const configs = [{path: 'LoadedModule1', component: LazyLoadedCmp}];
|
||||
@NgModule({declarations: [LazyLoadedCmp], imports: [RouterModule.forChild(configs)]})
|
||||
class LoadedModule {
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
TestBed.configureTestingModule({
|
||||
imports: [RouterTestingModule.withRoutes([{path: 'lazy1', loadChildren: 'expected'}])],
|
||||
providers: [{provide: PreloadingStrategy, useExisting: PreloadAllModules}]
|
||||
});
|
||||
});
|
||||
|
||||
it('should work',
|
||||
fakeAsync(inject(
|
||||
[NgModuleFactoryLoader, RouterPreloader, Router],
|
||||
(loader: SpyNgModuleFactoryLoader, preloader: RouterPreloader, router: Router) => {
|
||||
loader.stubbedModules = {expected: LoadedModule};
|
||||
|
||||
preloader.preload().subscribe(() => {});
|
||||
|
||||
tick();
|
||||
|
||||
const c = router.config as{_loadedConfig: LoadedRouterConfig}[];
|
||||
expect(c[0]._loadedConfig).toBeDefined();
|
||||
expect(c[0]._loadedConfig !.routes).not.toBe(configs);
|
||||
expect(c[0]._loadedConfig !.routes[0]).not.toBe(configs[0]);
|
||||
expect(c[0]._loadedConfig !.routes[0].component).toBe(configs[0].component);
|
||||
})));
|
||||
});
|
||||
});
|
||||
|
Reference in New Issue
Block a user