-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add navigate for router tests (#48)
- Loading branch information
1 parent
0f4022e
commit 656aa69
Showing
7 changed files
with
218 additions
and
7 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
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,82 @@ | ||
import { render } from '@testing-library/angular'; | ||
|
||
import { DetailComponent, MasterComponent, HiddenDetailComponent } from './09-router'; | ||
import { TestBed } from '@angular/core/testing'; | ||
import { Router } from '@angular/router'; | ||
|
||
test('it can navigate to routes', async () => { | ||
const component = await render(MasterComponent, { | ||
declarations: [DetailComponent, HiddenDetailComponent], | ||
routes: [ | ||
{ | ||
path: '', | ||
children: [ | ||
{ | ||
path: 'detail/:id', | ||
component: DetailComponent, | ||
}, | ||
{ | ||
path: 'hidden-detail', | ||
component: HiddenDetailComponent, | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
expect(component.queryByText(/Detail one/i)).not.toBeInTheDocument(); | ||
|
||
await component.navigate(component.getByText(/Load one/)); | ||
expect(component.queryByText(/Detail one/i)).toBeInTheDocument(); | ||
|
||
await component.navigate(component.getByText(/Load three/)); | ||
expect(component.queryByText(/Detail one/i)).not.toBeInTheDocument(); | ||
expect(component.queryByText(/Detail three/i)).toBeInTheDocument(); | ||
|
||
await component.navigate(component.getByText(/Back to parent/)); | ||
expect(component.queryByText(/Detail three/i)).not.toBeInTheDocument(); | ||
|
||
await component.navigate(component.getByText(/Load two/)); | ||
expect(component.queryByText(/Detail two/i)).toBeInTheDocument(); | ||
await component.navigate(component.getByText(/hidden x/)); | ||
expect(component.queryByText(/You found the treasure!/i)).toBeInTheDocument(); | ||
}); | ||
|
||
test('it can navigate to routes with a base path', async () => { | ||
const basePath = 'base'; | ||
const component = await render(MasterComponent, { | ||
declarations: [DetailComponent, HiddenDetailComponent], | ||
routes: [ | ||
{ | ||
path: basePath, | ||
children: [ | ||
{ | ||
path: 'detail/:id', | ||
component: DetailComponent, | ||
}, | ||
{ | ||
path: 'hidden-detail', | ||
component: HiddenDetailComponent, | ||
}, | ||
], | ||
}, | ||
], | ||
}); | ||
|
||
expect(component.queryByText(/Detail one/i)).not.toBeInTheDocument(); | ||
|
||
await component.navigate(component.getByText(/Load one/), basePath); | ||
expect(component.queryByText(/Detail one/i)).toBeInTheDocument(); | ||
|
||
await component.navigate(component.getByText(/Load three/), basePath); | ||
expect(component.queryByText(/Detail one/i)).not.toBeInTheDocument(); | ||
expect(component.queryByText(/Detail three/i)).toBeInTheDocument(); | ||
|
||
await component.navigate(component.getByText(/Back to parent/)); | ||
expect(component.queryByText(/Detail three/i)).not.toBeInTheDocument(); | ||
|
||
await component.navigate('base/detail/two'); // possible to just use strings | ||
expect(component.queryByText(/Detail two/i)).toBeInTheDocument(); | ||
await component.navigate('/hidden-detail', basePath); | ||
expect(component.queryByText(/You found the treasure!/i)).toBeInTheDocument(); | ||
}); |
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,38 @@ | ||
import { OnInit, Component } from '@angular/core'; | ||
import { ActivatedRoute } from '@angular/router'; | ||
import { map } from 'rxjs/operators'; | ||
|
||
@Component({ | ||
selector: 'app-master', | ||
template: ` | ||
<a [routerLink]="'./detail/one'">Load one</a> | <a [routerLink]="'./detail/two'">Load two</a> | | ||
<a [routerLink]="'./detail/three'">Load three</a> | | ||
<hr /> | ||
<router-outlet></router-outlet> | ||
`, | ||
}) | ||
export class MasterComponent {} | ||
|
||
@Component({ | ||
selector: 'app-detail', | ||
template: ` | ||
<h2>Detail {{ id | async }}</h2> | ||
<a [routerLink]="'../..'">Back to parent</a> | ||
<a routerLink="/hidden-detail">hidden x</a> | ||
`, | ||
}) | ||
export class DetailComponent { | ||
id = this.route.paramMap.pipe(map(params => params.get('id'))); | ||
constructor(private route: ActivatedRoute) {} | ||
} | ||
|
||
@Component({ | ||
selector: 'app-detail-hidden', | ||
template: ` | ||
You found the treasure! | ||
`, | ||
}) | ||
export class HiddenDetailComponent {} |