Revert "fix(router): do not finish bootstrap until all the routes are resolved (#14608)"

This reverts commit 2a191cae2d.
This commit is contained in:
Victor Berchet
2017-02-20 19:48:23 -08:00
parent 2a191cae2d
commit 670f2eca00
12 changed files with 154 additions and 304 deletions

View File

@ -1,128 +0,0 @@
/**
* @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
*/
import {APP_BASE_HREF} from '@angular/common';
import {ApplicationRef, CUSTOM_ELEMENTS_SCHEMA, Component, NgModule, destroyPlatform} from '@angular/core';
import {BrowserModule, DOCUMENT} from '@angular/platform-browser';
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {getDOM} from '@angular/platform-browser/src/dom/dom_adapter';
import {Resolve, Router, RouterModule} from '@angular/router';
describe('bootstrap', () => {
@Component({selector: 'test-app', template: 'root <router-outlet></router-outlet>'})
class RootCmp {
}
@Component({selector: 'test-app2', template: 'root <router-outlet></router-outlet>'})
class SecondRootCmp {
}
@Component({selector: 'test', template: 'test'})
class TestCmp {
}
class TestResolver implements Resolve<any> {
resolve() {
let resolve: any = null;
const res = new Promise(r => resolve = r);
setTimeout(() => resolve('test-data'), 0);
return res;
}
}
let testProviders: any[] = null;
beforeEach(() => {
destroyPlatform();
const fakeDoc = getDOM().createHtmlDocument();
const el1 = getDOM().createElement('test-app', fakeDoc);
const el2 = getDOM().createElement('test-app2', fakeDoc);
getDOM().appendChild(fakeDoc.body, el1);
getDOM().appendChild(fakeDoc.body, el2);
testProviders =
[{provide: DOCUMENT, useValue: fakeDoc}, {provide: APP_BASE_HREF, useValue: ''}];
});
it('should wait for resolvers to complete', (done) => {
@NgModule({
imports: [
BrowserModule,
RouterModule.forRoot(
[{path: '**', component: TestCmp, resolve: {test: TestResolver}}], {useHash: true})
],
declarations: [SecondRootCmp, RootCmp, TestCmp],
bootstrap: [RootCmp],
providers: [...testProviders, TestResolver],
schemas: [CUSTOM_ELEMENTS_SCHEMA]
})
class TestModule {
}
platformBrowserDynamic([]).bootstrapModule(TestModule).then(res => {
const router = res.injector.get(Router);
const data = router.routerState.snapshot.root.firstChild.data;
expect(data['test']).toEqual('test-data');
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);
spyOn(router, 'resetRootComponentType').and.callThrough();
const appRef: ApplicationRef = res.injector.get(ApplicationRef);
appRef.bootstrap(SecondRootCmp);
expect(router.resetRootComponentType).not.toHaveBeenCalled();
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);
spyOn(router, 'resetRootComponentType').and.callThrough();
const appRef: ApplicationRef = res.injector.get(ApplicationRef);
appRef.components[0].onDestroy(() => {
appRef.bootstrap(SecondRootCmp);
expect(router.resetRootComponentType).toHaveBeenCalled();
done();
});
appRef.components[0].destroy();
});
});
});

View File

@ -0,0 +1,91 @@
/**
* @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
*/
import {APP_BASE_HREF} from '@angular/common';
import {ApplicationRef, Component, NgModule} from '@angular/core';
import {TestBed, inject} from '@angular/core/testing';
import {DOCUMENT} from '@angular/platform-browser';
import {Router, RouterModule, Routes} from '@angular/router';
@Component({selector: 'app-root', template: ''})
export class AppRootComponent {
}
@Component({selector: 'bootstrappable-component', template: ''})
export class BootstrappableComponent {
}
export const appRoutes: Routes = [{path: '**', redirectTo: ''}];
@NgModule({
imports: [RouterModule.forRoot(appRoutes)],
declarations: [AppRootComponent, BootstrappableComponent],
entryComponents: [AppRootComponent, BootstrappableComponent],
providers: [{provide: APP_BASE_HREF, useValue: '/'}]
})
export class RouterInitTestModule {
}
describe('RouterModule', () => {
describe('RouterInitializer', () => {
beforeEach(() => { TestBed.configureTestingModule({imports: [RouterInitTestModule]}); });
beforeEach(inject([DOCUMENT], function(doc: HTMLDocument) {
const elRootApp = doc.createElement('app-root');
doc.body.appendChild(elRootApp);
const elBootComp = doc.createElement('bootstrappable-component');
doc.body.appendChild(elBootComp);
}));
it('should not init router navigation listeners if a non root component is bootstrapped',
() => {
const appRef: ApplicationRef = TestBed.get(ApplicationRef);
const r: Router = TestBed.get(Router);
const spy = spyOn(r, 'resetRootComponentType').and.callThrough();
appRef.bootstrap(AppRootComponent);
expect(r.resetRootComponentType).toHaveBeenCalled();
spy.calls.reset();
appRef.bootstrap(BootstrappableComponent);
expect(r.resetRootComponentType).not.toHaveBeenCalled();
});
it('should reinit router navigation listeners if a previously bootstrapped root component is destroyed',
(done) => {
const appRef: ApplicationRef = TestBed.get(ApplicationRef);
const r: Router = TestBed.get(Router);
const spy = spyOn(r, 'resetRootComponentType').and.callThrough();
const compRef = appRef.bootstrap(AppRootComponent);
expect(r.resetRootComponentType).toHaveBeenCalled();
spy.calls.reset();
compRef.onDestroy(() => {
appRef.bootstrap(BootstrappableComponent);
expect(r.resetRootComponentType).toHaveBeenCalled();
done();
});
compRef.destroy();
});
});
});