forked from angular/angular
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(router): do not finish bootstrap until all the routes are resolved
Fixes angular#12162
- Loading branch information
Showing
5 changed files
with
258 additions
and
143 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/** | ||
* @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 should wait for resolvers to complete', () => { | ||
|
||
@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 work', (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(); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.