Angular Testing: routerLink
Today, I'll show you how you can test such a code:
The routerLink directive on the anchor tag give the router control over this element. Find more info here.
A. Provide the routerTestingModule with dummy routes as:
B. Declare Router and Location for use in your test:
C. Routing is an asynchronous activity so we use one of the asynchronous testing methods at our disposal, in this case the fakeAsync method.
Happy coding!!!
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
<a class="test-btn" [routerLink]="['/vedrax-index/category',category, 'demands']" | |
[queryParams]="{period:activity.period,size:activity.demandCount}"> | |
Test | |
</a> |
The routerLink directive on the anchor tag give the router control over this element. Find more info here.
A. Provide the routerTestingModule with dummy routes as:
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
beforeEach(async(() => { | |
TestBed.configureTestingModule({ | |
imports: [ | |
SharedModule, | |
RouterTestingModule.withRoutes([ | |
{path: 'vedrax-index/category/:category/demands', component: DummyComponent} | |
]) | |
], | |
declarations: [CategoryActivityComponent, DummyComponent] | |
}).compileComponents(); | |
})); |
B. Declare Router and Location for use in your test:
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
let location: Location; | |
let router: Router; | |
let debugElement: DebugElement; | |
beforeEach(() => { | |
router = TestBed.get(Router); | |
location = TestBed.get(Location); | |
fixture = TestBed.createComponent(CategoryActivityComponent); | |
debugElement = fixture.debugElement; | |
comp = fixture.componentInstance; | |
//component attribute setting here | |
fixture.detectChanges(); | |
}); |
C. Routing is an asynchronous activity so we use one of the asynchronous testing methods at our disposal, in this case the fakeAsync method.
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
it('test demands redirection', fakeAsync(() => { | |
//we trigger a click on our link | |
debugElement | |
.query(By.css('.test-btn')) | |
.nativeElement.click(); | |
//We wait for all pending promises to be resolved. | |
tick(); | |
expect(location.path()).toBe('/vedrax-index/category/api-01/demands?period=2018&size=345'); | |
})); |
Happy coding!!!
Comments
Post a Comment