From ef9f8df9edc3ba8e1f48cd4ecbc7b8fe61c6d879 Mon Sep 17 00:00:00 2001 From: Andrew Scott Date: Fri, 8 May 2020 10:55:40 -0700 Subject: [PATCH] fix(router): update type for routerLink to include null and undefined (#37018) PR #13380 added support for `null` and `undefined` but the type on the parameter was not updated. This would result in a compilation error if `fullTemplateTypeCheck` is enabled. Fixes #36544 PR Close #37018 --- goldens/public-api/router/router.d.ts | 4 ++-- packages/router/src/directives/router_link.ts | 4 ++-- packages/router/test/integration.spec.ts | 20 ++++++++++++------- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/goldens/public-api/router/router.d.ts b/goldens/public-api/router/router.d.ts index 9f64ce7520..1135b55078 100644 --- a/goldens/public-api/router/router.d.ts +++ b/goldens/public-api/router/router.d.ts @@ -380,7 +380,7 @@ export declare class RouterLink { }; queryParamsHandling: QueryParamsHandling; replaceUrl: boolean; - set routerLink(commands: any[] | string); + set routerLink(commands: any[] | string | null | undefined); skipLocationChange: boolean; state?: { [k: string]: any; @@ -414,7 +414,7 @@ export declare class RouterLinkWithHref implements OnChanges, OnDestroy { }; queryParamsHandling: QueryParamsHandling; replaceUrl: boolean; - set routerLink(commands: any[] | string); + set routerLink(commands: any[] | string | null | undefined); skipLocationChange: boolean; state?: { [k: string]: any; diff --git a/packages/router/src/directives/router_link.ts b/packages/router/src/directives/router_link.ts index a6462ce08e..e3c5a072a7 100644 --- a/packages/router/src/directives/router_link.ts +++ b/packages/router/src/directives/router_link.ts @@ -139,7 +139,7 @@ export class RouterLink { } @Input() - set routerLink(commands: any[]|string) { + set routerLink(commands: any[]|string|null|undefined) { if (commands != null) { this.commands = Array.isArray(commands) ? commands : [commands]; } else { @@ -229,7 +229,7 @@ export class RouterLinkWithHref implements OnChanges, OnDestroy { } @Input() - set routerLink(commands: any[]|string) { + set routerLink(commands: any[]|string|null|undefined) { if (commands != null) { this.commands = Array.isArray(commands) ? commands : [commands]; } else { diff --git a/packages/router/test/integration.spec.ts b/packages/router/test/integration.spec.ts index 1098c0c4d9..934aef71b5 100644 --- a/packages/router/test/integration.spec.ts +++ b/packages/router/test/integration.spec.ts @@ -1968,11 +1968,15 @@ describe('Integration', () => { expect(native.getAttribute('href')).toEqual('/home'); })); - it('should not throw when commands is null', fakeAsync(() => { + it('should not throw when commands is null or undefined', fakeAsync(() => { @Component({ selector: 'someCmp', - template: - `Link` + template: ` + Link + + Link + + ` }) class CmpWithLink { } @@ -1982,10 +1986,12 @@ describe('Integration', () => { let fixture: ComponentFixture = createRoot(router, CmpWithLink); router.resetConfig([{path: 'home', component: SimpleCmp}]); - const anchor = fixture.nativeElement.querySelector('a'); - const button = fixture.nativeElement.querySelector('button'); - expect(() => anchor.click()).not.toThrow(); - expect(() => button.click()).not.toThrow(); + const anchors = fixture.nativeElement.querySelectorAll('a'); + const buttons = fixture.nativeElement.querySelectorAll('button'); + expect(() => anchors[0].click()).not.toThrow(); + expect(() => anchors[1].click()).not.toThrow(); + expect(() => buttons[0].click()).not.toThrow(); + expect(() => buttons[1].click()).not.toThrow(); })); it('should update hrefs when query params or fragment change', fakeAsync(() => {