2017-03-07 17:27:20 -05:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google Inc. All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2019-08-22 22:16:25 -04:00
|
|
|
import {APP_BASE_HREF, DOCUMENT, Location, ɵgetDOM as getDOM} from '@angular/common';
|
2017-03-07 17:27:20 -05:00
|
|
|
import {ApplicationRef, CUSTOM_ELEMENTS_SCHEMA, Component, NgModule, destroyPlatform} from '@angular/core';
|
|
|
|
import {inject} from '@angular/core/testing';
|
2019-08-22 22:16:25 -04:00
|
|
|
import {BrowserModule} from '@angular/platform-browser';
|
2017-03-07 17:27:20 -05:00
|
|
|
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
|
2019-03-11 20:20:40 -04:00
|
|
|
import {NavigationEnd, Resolve, Router, RouterModule} from '@angular/router';
|
2018-05-17 07:33:50 -04:00
|
|
|
import {filter, first} from 'rxjs/operators';
|
2017-03-07 17:27:20 -05:00
|
|
|
|
|
|
|
describe('bootstrap', () => {
|
2017-12-17 23:59:37 -05:00
|
|
|
if (isNode) return;
|
2017-03-07 17:27:20 -05:00
|
|
|
let log: any[] = [];
|
2017-04-17 14:13:13 -04:00
|
|
|
let testProviders: any[] = null !;
|
2017-03-07 17:27:20 -05:00
|
|
|
|
|
|
|
@Component({selector: 'test-app', template: 'root <router-outlet></router-outlet>'})
|
|
|
|
class RootCmp {
|
|
|
|
constructor() { log.push('RootCmp'); }
|
|
|
|
}
|
|
|
|
|
|
|
|
@Component({selector: 'test-app2', template: 'root <router-outlet></router-outlet>'})
|
|
|
|
class SecondRootCmp {
|
|
|
|
}
|
|
|
|
|
|
|
|
class TestResolver implements Resolve<any> {
|
|
|
|
resolve() {
|
|
|
|
let resolve: any = null;
|
|
|
|
const res = new Promise(r => resolve = r);
|
|
|
|
setTimeout(() => resolve('test-data'), 0);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
beforeEach(inject([DOCUMENT], (doc: any) => {
|
|
|
|
destroyPlatform();
|
|
|
|
|
|
|
|
const el1 = getDOM().createElement('test-app', doc);
|
|
|
|
const el2 = getDOM().createElement('test-app2', doc);
|
|
|
|
getDOM().appendChild(doc.body, el1);
|
|
|
|
getDOM().appendChild(doc.body, el2);
|
|
|
|
|
|
|
|
log = [];
|
|
|
|
testProviders = [{provide: APP_BASE_HREF, useValue: ''}];
|
|
|
|
}));
|
|
|
|
|
|
|
|
afterEach(inject([DOCUMENT], (doc: any) => {
|
|
|
|
const oldRoots = getDOM().querySelectorAll(doc, 'test-app,test-app2');
|
|
|
|
for (let i = 0; i < oldRoots.length; i++) {
|
|
|
|
getDOM().remove(oldRoots[i]);
|
|
|
|
}
|
|
|
|
}));
|
|
|
|
|
|
|
|
it('should wait for resolvers to complete when initialNavigation = enabled', (done) => {
|
|
|
|
@Component({selector: 'test', template: 'test'})
|
|
|
|
class TestCmpEnabled {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
imports: [
|
|
|
|
BrowserModule, RouterModule.forRoot(
|
|
|
|
[{path: '**', component: TestCmpEnabled, resolve: {test: TestResolver}}],
|
|
|
|
{useHash: true, initialNavigation: 'enabled'})
|
|
|
|
],
|
|
|
|
declarations: [RootCmp, TestCmpEnabled],
|
|
|
|
bootstrap: [RootCmp],
|
|
|
|
providers: [...testProviders, TestResolver],
|
|
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
|
|
|
})
|
|
|
|
class TestModule {
|
|
|
|
constructor(router: Router) {
|
|
|
|
log.push('TestModule');
|
|
|
|
router.events.subscribe(e => log.push(e.constructor.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
platformBrowserDynamic([]).bootstrapModule(TestModule).then(res => {
|
|
|
|
const router = res.injector.get(Router);
|
2017-04-17 14:13:13 -04:00
|
|
|
const data = router.routerState.snapshot.root.firstChild !.data;
|
2017-03-07 17:27:20 -05:00
|
|
|
expect(data['test']).toEqual('test-data');
|
2017-07-01 13:30:17 -04:00
|
|
|
expect(log).toEqual([
|
2017-09-04 16:00:59 -04:00
|
|
|
'TestModule', 'NavigationStart', 'RoutesRecognized', 'GuardsCheckStart',
|
2017-09-06 14:00:32 -04:00
|
|
|
'ChildActivationStart', 'ActivationStart', 'GuardsCheckEnd', 'ResolveStart', 'ResolveEnd',
|
2018-05-17 07:33:50 -04:00
|
|
|
'RootCmp', 'ActivationEnd', 'ChildActivationEnd', 'NavigationEnd', 'Scroll'
|
2017-07-01 13:30:17 -04:00
|
|
|
]);
|
2017-03-07 17:27:20 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-08-22 19:39:06 -04:00
|
|
|
it('should NOT wait for resolvers to complete when initialNavigation = legacy_enabled',
|
|
|
|
(done) => {
|
|
|
|
@Component({selector: 'test', template: 'test'})
|
|
|
|
class TestCmpLegacyEnabled {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
imports: [
|
|
|
|
BrowserModule,
|
|
|
|
RouterModule.forRoot(
|
|
|
|
[{path: '**', component: TestCmpLegacyEnabled, resolve: {test: TestResolver}}],
|
|
|
|
{useHash: true, initialNavigation: 'legacy_enabled'})
|
|
|
|
],
|
|
|
|
declarations: [RootCmp, TestCmpLegacyEnabled],
|
|
|
|
bootstrap: [RootCmp],
|
|
|
|
providers: [...testProviders, TestResolver],
|
|
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
|
|
|
})
|
|
|
|
class TestModule {
|
|
|
|
constructor(router: Router) {
|
|
|
|
log.push('TestModule');
|
|
|
|
router.events.subscribe(e => log.push(e.constructor.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
platformBrowserDynamic([]).bootstrapModule(TestModule).then(res => {
|
2019-06-14 06:19:09 -04:00
|
|
|
const router: Router = res.injector.get(Router);
|
2017-08-22 19:39:06 -04:00
|
|
|
expect(router.routerState.snapshot.root.firstChild).toBeNull();
|
|
|
|
// ResolveEnd has not been emitted yet because bootstrap returned too early
|
|
|
|
expect(log).toEqual([
|
|
|
|
'TestModule', 'RootCmp', 'NavigationStart', 'RoutesRecognized', 'GuardsCheckStart',
|
2017-09-06 14:00:32 -04:00
|
|
|
'ChildActivationStart', 'ActivationStart', 'GuardsCheckEnd', 'ResolveStart'
|
2017-08-22 19:39:06 -04:00
|
|
|
]);
|
|
|
|
|
|
|
|
router.events.subscribe((e) => {
|
|
|
|
if (e instanceof NavigationEnd) {
|
|
|
|
done();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-03-07 17:27:20 -05:00
|
|
|
it('should not run navigation when initialNavigation = disabled', (done) => {
|
|
|
|
@Component({selector: 'test', template: 'test'})
|
|
|
|
class TestCmpDiabled {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
imports: [
|
|
|
|
BrowserModule, RouterModule.forRoot(
|
|
|
|
[{path: '**', component: TestCmpDiabled, resolve: {test: TestResolver}}],
|
|
|
|
{useHash: true, initialNavigation: 'disabled'})
|
|
|
|
],
|
|
|
|
declarations: [RootCmp, TestCmpDiabled],
|
|
|
|
bootstrap: [RootCmp],
|
|
|
|
providers: [...testProviders, TestResolver],
|
|
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
|
|
|
})
|
|
|
|
class TestModule {
|
|
|
|
constructor(router: Router) {
|
|
|
|
log.push('TestModule');
|
|
|
|
router.events.subscribe(e => log.push(e.constructor.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
platformBrowserDynamic([]).bootstrapModule(TestModule).then(res => {
|
2017-08-22 19:39:06 -04:00
|
|
|
const router = res.injector.get(Router);
|
|
|
|
expect(log).toEqual(['TestModule', 'RootCmp']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not run navigation when initialNavigation = legacy_disabled', (done) => {
|
|
|
|
@Component({selector: 'test', template: 'test'})
|
|
|
|
class TestCmpLegacyDisabled {
|
|
|
|
}
|
|
|
|
|
|
|
|
@NgModule({
|
|
|
|
imports: [
|
|
|
|
BrowserModule,
|
|
|
|
RouterModule.forRoot(
|
|
|
|
[{path: '**', component: TestCmpLegacyDisabled, resolve: {test: TestResolver}}],
|
|
|
|
{useHash: true, initialNavigation: 'legacy_disabled'})
|
|
|
|
],
|
|
|
|
declarations: [RootCmp, TestCmpLegacyDisabled],
|
|
|
|
bootstrap: [RootCmp],
|
|
|
|
providers: [...testProviders, TestResolver],
|
|
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
|
|
|
})
|
|
|
|
class TestModule {
|
|
|
|
constructor(router: Router) {
|
|
|
|
log.push('TestModule');
|
|
|
|
router.events.subscribe(e => log.push(e.constructor.name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
platformBrowserDynamic([]).bootstrapModule(TestModule).then(res => {
|
2017-03-07 17:27:20 -05:00
|
|
|
const router = res.injector.get(Router);
|
|
|
|
expect(log).toEqual(['TestModule', 'RootCmp']);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should not init router navigation listeners if a non root component is bootstrapped',
|
|
|
|
(done) => {
|
|
|
|
@NgModule({
|
|
|
|
imports: [BrowserModule, RouterModule.forRoot([], {useHash: true})],
|
|
|
|
declarations: [SecondRootCmp, RootCmp],
|
|
|
|
entryComponents: [SecondRootCmp],
|
|
|
|
bootstrap: [RootCmp],
|
|
|
|
providers: testProviders,
|
|
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
|
|
|
})
|
|
|
|
class TestModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
platformBrowserDynamic([]).bootstrapModule(TestModule).then(res => {
|
|
|
|
const router = res.injector.get(Router);
|
2017-10-24 07:54:08 -04:00
|
|
|
spyOn(router as any, 'resetRootComponentType').and.callThrough();
|
2017-03-07 17:27:20 -05:00
|
|
|
|
|
|
|
const appRef: ApplicationRef = res.injector.get(ApplicationRef);
|
|
|
|
appRef.bootstrap(SecondRootCmp);
|
|
|
|
|
2017-12-17 18:10:54 -05:00
|
|
|
expect((router as any).resetRootComponentType).not.toHaveBeenCalled();
|
2017-03-07 17:27:20 -05:00
|
|
|
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should reinit router navigation listeners if a previously bootstrapped root component is destroyed',
|
|
|
|
(done) => {
|
|
|
|
@NgModule({
|
|
|
|
imports: [BrowserModule, RouterModule.forRoot([], {useHash: true})],
|
|
|
|
declarations: [SecondRootCmp, RootCmp],
|
|
|
|
entryComponents: [SecondRootCmp],
|
|
|
|
bootstrap: [RootCmp],
|
|
|
|
providers: testProviders,
|
|
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
|
|
|
})
|
|
|
|
class TestModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
platformBrowserDynamic([]).bootstrapModule(TestModule).then(res => {
|
|
|
|
const router = res.injector.get(Router);
|
2017-10-24 07:54:08 -04:00
|
|
|
spyOn(router as any, 'resetRootComponentType').and.callThrough();
|
2017-03-07 17:27:20 -05:00
|
|
|
|
|
|
|
const appRef: ApplicationRef = res.injector.get(ApplicationRef);
|
|
|
|
appRef.components[0].onDestroy(() => {
|
|
|
|
appRef.bootstrap(SecondRootCmp);
|
2017-12-17 18:10:54 -05:00
|
|
|
expect((router as any).resetRootComponentType).toHaveBeenCalled();
|
2017-03-07 17:27:20 -05:00
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
appRef.components[0].destroy();
|
|
|
|
});
|
|
|
|
});
|
2018-05-17 07:33:50 -04:00
|
|
|
|
|
|
|
|
|
|
|
it('should restore the scrolling position', async(done) => {
|
|
|
|
@Component({
|
|
|
|
selector: 'component-a',
|
|
|
|
template: `
|
|
|
|
<div style="height: 3000px;"></div>
|
|
|
|
<div id="marker1"></div>
|
|
|
|
<div style="height: 3000px;"></div>
|
|
|
|
<div id="marker2"></div>
|
|
|
|
<div style="height: 3000px;"></div>
|
|
|
|
<a name="marker3"></a>
|
|
|
|
<div style="height: 3000px;"></div>
|
|
|
|
`
|
|
|
|
})
|
|
|
|
class TallComponent {
|
|
|
|
}
|
|
|
|
@NgModule({
|
|
|
|
imports: [
|
|
|
|
BrowserModule,
|
|
|
|
RouterModule.forRoot(
|
|
|
|
[
|
|
|
|
{path: '', pathMatch: 'full', redirectTo: '/aa'},
|
|
|
|
{path: 'aa', component: TallComponent}, {path: 'bb', component: TallComponent},
|
|
|
|
{path: 'cc', component: TallComponent},
|
|
|
|
{path: 'fail', component: TallComponent, canActivate: ['returnFalse']}
|
|
|
|
],
|
|
|
|
{
|
|
|
|
useHash: true,
|
|
|
|
scrollPositionRestoration: 'enabled',
|
|
|
|
anchorScrolling: 'enabled',
|
|
|
|
scrollOffset: [0, 100],
|
|
|
|
onSameUrlNavigation: 'reload'
|
|
|
|
})
|
|
|
|
],
|
|
|
|
declarations: [TallComponent, RootCmp],
|
|
|
|
bootstrap: [RootCmp],
|
|
|
|
providers: [...testProviders, {provide: 'returnFalse', useValue: () => false}],
|
|
|
|
schemas: [CUSTOM_ELEMENTS_SCHEMA]
|
|
|
|
})
|
|
|
|
class TestModule {
|
|
|
|
}
|
|
|
|
|
|
|
|
const res = await platformBrowserDynamic([]).bootstrapModule(TestModule);
|
|
|
|
const router = res.injector.get(Router);
|
|
|
|
const location: Location = res.injector.get(Location);
|
|
|
|
|
|
|
|
await router.navigateByUrl('/aa');
|
|
|
|
window.scrollTo(0, 5000);
|
|
|
|
|
|
|
|
await router.navigateByUrl('/fail');
|
|
|
|
expect(window.scrollY).toEqual(5000);
|
|
|
|
|
|
|
|
await router.navigateByUrl('/bb');
|
|
|
|
window.scrollTo(0, 3000);
|
|
|
|
|
|
|
|
expect(window.scrollY).toEqual(3000);
|
|
|
|
|
|
|
|
await router.navigateByUrl('/cc');
|
|
|
|
expect(window.scrollY).toEqual(0);
|
|
|
|
|
|
|
|
await router.navigateByUrl('/aa#marker2');
|
|
|
|
expect(window.scrollY >= 5900).toBe(true);
|
|
|
|
expect(window.scrollY < 6000).toBe(true); // offset
|
|
|
|
|
|
|
|
await router.navigateByUrl('/aa#marker3');
|
|
|
|
expect(window.scrollY >= 8900).toBe(true);
|
|
|
|
expect(window.scrollY < 9000).toBe(true);
|
|
|
|
done();
|
|
|
|
});
|
|
|
|
|
|
|
|
function waitForNavigationToComplete(router: Router): Promise<any> {
|
|
|
|
return router.events.pipe(filter((e: any) => e instanceof NavigationEnd), first()).toPromise();
|
|
|
|
}
|
2017-03-07 17:27:20 -05:00
|
|
|
});
|