fix(router): add null support for RouterLink directive (#32616)

Value of "undefined" passed as segment in routerLink is stringified to string "undefined".
This change introduces the same behavior for value of "null".

PR Close #32616
This commit is contained in:
Krzysztof Grzybek
2019-09-11 20:38:56 +02:00
committed by Andrew Kushnir
parent 3190ccf3b2
commit 69948ce919
3 changed files with 52 additions and 5 deletions

View File

@ -68,7 +68,21 @@ describe('createUrlTree', () => {
expect(params[1].path).toEqual('11');
});
it('should support first segments contaings slashes', () => {
it('should work if command = null', () => {
const p = serializer.parse('/a/b');
const t = createRoot(p, [null]);
const params = t.root.children[PRIMARY_OUTLET].segments;
expect(params[0].path).toEqual('null');
});
it('should work if command is undefined', () => {
const p = serializer.parse('/a/b');
const t = createRoot(p, [undefined]);
const params = t.root.children[PRIMARY_OUTLET].segments;
expect(params[0].path).toEqual('undefined');
});
it('should support first segments containing slashes', () => {
const p = serializer.parse('/');
const t = createRoot(p, [{segmentPath: '/one'}, 'two/three']);
expect(serializer.serialize(t)).toEqual('/%2Fone/two%2Fthree');

View File

@ -1994,6 +1994,36 @@ describe('Integration', () => {
expect(() => buttons[1].click()).not.toThrow();
}));
it('should not throw when some command is null', fakeAsync(() => {
@Component({
selector: 'someCmp',
template:
`<router-outlet></router-outlet><a [routerLink]="[null]">Link</a><button [routerLink]="[null]">Button</button>`
})
class CmpWithLink {
}
TestBed.configureTestingModule({declarations: [CmpWithLink]});
const router: Router = TestBed.inject(Router);
expect(() => createRoot(router, CmpWithLink)).not.toThrow();
}));
it('should not throw when some command is undefined', fakeAsync(() => {
@Component({
selector: 'someCmp',
template:
`<router-outlet></router-outlet><a [routerLink]="[undefined]">Link</a><button [routerLink]="[undefined]">Button</button>`
})
class CmpWithLink {
}
TestBed.configureTestingModule({declarations: [CmpWithLink]});
const router: Router = TestBed.inject(Router);
expect(() => createRoot(router, CmpWithLink)).not.toThrow();
}));
it('should update hrefs when query params or fragment change', fakeAsync(() => {
@Component({
selector: 'someRoot',