-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce routing tester to test with the RouterTestingHarness
- Loading branch information
Showing
5 changed files
with
223 additions
and
36 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,68 @@ | ||
import { Component } from '@angular/core'; | ||
import { ActivatedRoute, provideRouter, RouterLink } from '@angular/router'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { RoutingTester } from './routing-tester'; | ||
import { RouterTestingHarness } from '@angular/router/testing'; | ||
import { speculoosMatchers } from './matchers'; | ||
|
||
@Component({ | ||
standalone: true, | ||
imports: [RouterLink], | ||
template: ` | ||
<h1>Current page: {{ page }}</h1> | ||
<a routerLink="." [queryParams]="{ page: page + 1 }">Next</a> | ||
` | ||
}) | ||
class PageComponent { | ||
page!: number; | ||
|
||
constructor(route: ActivatedRoute) { | ||
route.queryParamMap.subscribe(map => (this.page = parseInt(map.get('page')))); | ||
} | ||
} | ||
|
||
class PageComponentTester extends RoutingTester { | ||
constructor(harness: RouterTestingHarness) { | ||
super(harness); | ||
} | ||
|
||
get title() { | ||
return this.element('h1'); | ||
} | ||
|
||
get link() { | ||
return this.element('a'); | ||
} | ||
} | ||
|
||
describe('RoutingTester', () => { | ||
beforeEach(() => { | ||
jasmine.addMatchers(speculoosMatchers); | ||
TestBed.configureTestingModule({ | ||
providers: [provideRouter([{ path: 'list', component: PageComponent }])] | ||
}); | ||
}); | ||
|
||
it('should display the page of the query params', async () => { | ||
const tester = new PageComponentTester(await RouterTestingHarness.create('/list?page=42')); | ||
|
||
expect(tester.title).toHaveText('Current page: 42'); | ||
|
||
await tester.harness.navigateByUrl('/list?page=54'); | ||
|
||
expect(tester.title).toHaveText('Current page: 54'); | ||
}); | ||
|
||
it('should navigate to the next page when clicking the link', async () => { | ||
const tester = new PageComponentTester(await RouterTestingHarness.create('/list?page=42')); | ||
|
||
expect(tester.title).toHaveText('Current page: 42'); | ||
|
||
tester.link.click(); | ||
await tester.stable(); | ||
|
||
expect(tester.urlTree.queryParamMap.get('page')).toBe('43'); | ||
expect(tester.url).toBe('/list?page=43'); | ||
expect(tester.title).toHaveText('Current page: 43'); | ||
}); | ||
}); |
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,45 @@ | ||
import { ComponentTester } from './component-tester'; | ||
import { RouterTestingHarness } from '@angular/router/testing'; | ||
import { Router, UrlTree } from '@angular/router'; | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
/** | ||
* A thin wrapper around Angular RouterTestingHarness which helps testing routed components. | ||
* It allows, based on a configured testing module where the router is provided, to initially navigate | ||
* to a given route, then test the routed component. It's then possible to either navigate again with | ||
* the wrapped harness, or to use the component to trigger navigation, and test that the URL has changed | ||
* for example. | ||
*/ | ||
export class RoutingTester extends ComponentTester<unknown> { | ||
constructor(readonly harness: RouterTestingHarness) { | ||
super(harness.fixture); | ||
} | ||
|
||
/** | ||
* Creates a RouterTestngHarness and uses it to navigate to the given URL | ||
* @param url the URL to initially navigate to. | ||
* @return a promise which resolves to a RoutingTester which wraps the harness | ||
* and its fixture. | ||
*/ | ||
static async forUrl(url: string) { | ||
const harness = await RouterTestingHarness.create(url); | ||
return new RoutingTester(harness); | ||
} | ||
|
||
/** | ||
* Gets the current URL of the Router service as a string | ||
*/ | ||
get url(): string { | ||
const router = TestBed.inject(Router); | ||
return router.url; | ||
} | ||
|
||
/** | ||
* Gets the current URL of the Router service as an UrlTree, to be able to test its | ||
* elements (query params, etc.) | ||
*/ | ||
get urlTree(): UrlTree { | ||
const router = TestBed.inject(Router); | ||
return router.parseUrl(router.url); | ||
} | ||
} |
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