fix(router): rerun resolvers when url changes

Closes #12603
This commit is contained in:
vsavkin
2016-10-28 15:17:00 -07:00
parent 091c390032
commit fe47e6b783
6 changed files with 67 additions and 9 deletions

View File

@ -41,7 +41,7 @@ describe('Integration', () => {
expect(location.path()).toEqual('/simple');
})));
describe('should execute navigations serialy', () => {
describe('should execute navigations serially', () => {
let log: any[] = [];
beforeEach(() => {
@ -693,6 +693,7 @@ describe('Integration', () => {
{provide: 'resolveFour', useValue: (a: any, b: any) => 4},
{provide: 'resolveSix', useClass: ResolveSix},
{provide: 'resolveError', useValue: (a: any, b: any) => Promise.reject('error')},
{provide: 'numberOfUrlSegments', useValue: (a: any, b: any) => a.url.length}
]
});
});
@ -787,6 +788,29 @@ describe('Integration', () => {
const cmp = fixture.debugElement.children[1].componentInstance;
expect(cmp.route.snapshot.data).toEqual({two: 2});
})));
it('should rerun resolvers when the urls segments of a wildcard route change',
fakeAsync(inject([Router, Location], (router: Router, location: Location) => {
const fixture = createRoot(router, RootCmp);
router.resetConfig([{
path: '**',
component: CollectParamsCmp,
resolve: {numberOfUrlSegments: 'numberOfUrlSegments'}
}]);
let e: any = null;
router.navigateByUrl('/one/two');
advance(fixture);
const cmp = fixture.debugElement.children[1].componentInstance;
expect(cmp.route.snapshot.data).toEqual({numberOfUrlSegments: 2});
router.navigateByUrl('/one/two/three');
advance(fixture);
expect(cmp.route.snapshot.data).toEqual({numberOfUrlSegments: 3});
})));
});
describe('router links', () => {

View File

@ -95,6 +95,6 @@ function checkResolveData(
function createActivatedRouteSnapshot(cmp: string, extra: any = {}): ActivatedRouteSnapshot {
return new ActivatedRouteSnapshot(
<any>null, {}, <any>null, <any>null, <any>null, <any>null, <any>cmp, <any>null, <any>null, -1,
<any>[], {}, <any>null, <any>null, <any>null, <any>null, <any>cmp, <any>null, <any>null, -1,
extra.resolve);
}

View File

@ -6,7 +6,9 @@
* found in the LICENSE file at https://angular.io/license
*/
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot} from '../src/router_state';
import {ActivatedRoute, ActivatedRouteSnapshot, RouterState, RouterStateSnapshot, equalParamsAndUrlSegments} from '../src/router_state';
import {Params} from '../src/shared';
import {UrlSegment} from '../src/url_tree';
import {TreeNode} from '../src/utils/tree';
describe('RouterState & Snapshot', () => {
@ -93,6 +95,33 @@ describe('RouterState & Snapshot', () => {
expect(p[1]).toBe(b);
});
});
describe('equalParamsAndUrlSegments', () => {
function createSnapshot(params: Params, url: UrlSegment[]): ActivatedRouteSnapshot {
return new ActivatedRouteSnapshot(
url, params, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null, <any>null,
-1, null);
}
it('should return false when params are different', () => {
expect(equalParamsAndUrlSegments(createSnapshot({a: 1}, []), createSnapshot({a: 2}, [])))
.toEqual(false);
});
it('should return false when urls are different', () => {
expect(equalParamsAndUrlSegments(
createSnapshot({a: 1}, [new UrlSegment('a', {})]),
createSnapshot({a: 1}, [new UrlSegment('b', {})])))
.toEqual(false);
});
it('should return true othewise', () => {
expect(equalParamsAndUrlSegments(
createSnapshot({a: 1}, [new UrlSegment('a', {})]),
createSnapshot({a: 1}, [new UrlSegment('a', {})])))
.toEqual(true);
});
});
});
function createActivatedRouteSnapshot(cmp: string) {