Skip to content

Commit 5c4b102

Browse files
test: refactor router test (#287)
1 parent d758b94 commit 5c4b102

File tree

4 files changed

+49
-9
lines changed

4 files changed

+49
-9
lines changed

apps/example-app/src/app/app-routing.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import { MaterialFormsComponent } from './examples/04-forms-with-material';
99
import { ComponentWithProviderComponent } from './examples/05-component-provider';
1010
import { WithNgRxStoreComponent } from './examples/06-with-ngrx-store';
1111
import { WithNgRxMockStoreComponent } from './examples/07-with-ngrx-mock-store';
12-
import { MasterComponent, DetailComponent, HiddenDetailComponent } from './examples/09-router';
12+
import { RootComponent, DetailComponent, HiddenDetailComponent } from './examples/09-router';
1313

1414
export const examples = [
1515
{
@@ -70,7 +70,7 @@ export const examples = [
7070
},
7171
{
7272
path: 'with-router',
73-
component: MasterComponent,
73+
component: RootComponent,
7474
data: {
7575
name: 'Router',
7676
},

apps/example-app/src/app/app.module.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { MaterialFormsComponent } from './examples/04-forms-with-material';
2020
import { ComponentWithProviderComponent } from './examples/05-component-provider';
2121
import { WithNgRxStoreComponent, reducer } from './examples/06-with-ngrx-store';
2222
import { WithNgRxMockStoreComponent } from './examples/07-with-ngrx-mock-store';
23-
import { MasterComponent, DetailComponent, HiddenDetailComponent } from './examples/09-router';
23+
import { RootComponent, DetailComponent, HiddenDetailComponent } from './examples/09-router';
2424
import { ScrollingModule } from '@angular/cdk/scrolling';
2525

2626
function reducerItems() {
@@ -40,7 +40,7 @@ function reducerItems() {
4040
ComponentWithProviderComponent,
4141
WithNgRxStoreComponent,
4242
WithNgRxMockStoreComponent,
43-
MasterComponent,
43+
RootComponent,
4444
DetailComponent,
4545
HiddenDetailComponent,
4646
],

apps/example-app/src/app/examples/09-router.spec.ts

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,49 @@
1-
import { render, screen } from '@testing-library/angular';
1+
import { render, screen, waitForElementToBeRemoved } from '@testing-library/angular';
2+
import userEvent from '@testing-library/user-event';
23

3-
import { DetailComponent, MasterComponent, HiddenDetailComponent } from './09-router';
4+
import { DetailComponent, RootComponent, HiddenDetailComponent } from './09-router';
45

56
test('it can navigate to routes', async () => {
6-
const { navigate } = await render(MasterComponent, {
7+
await render(RootComponent, {
8+
declarations: [DetailComponent, HiddenDetailComponent],
9+
routes: [
10+
{
11+
path: '',
12+
children: [
13+
{
14+
path: 'detail/:id',
15+
component: DetailComponent,
16+
},
17+
{
18+
path: 'hidden-detail',
19+
component: HiddenDetailComponent,
20+
},
21+
],
22+
},
23+
],
24+
});
25+
26+
expect(screen.queryByText(/Detail one/i)).not.toBeInTheDocument();
27+
28+
userEvent.click(screen.getByRole('link', { name: /load one/i }));
29+
expect(await screen.findByRole('heading', { name: /Detail one/i })).toBeInTheDocument();
30+
31+
userEvent.click(screen.getByRole('link', { name: /load three/i }));
32+
await waitForElementToBeRemoved(() => screen.queryByRole('heading', { name: /Detail one/i }));
33+
expect(await screen.findByRole('heading', { name: /Detail three/i })).toBeInTheDocument();
34+
35+
userEvent.click(screen.getByRole('link', { name: /back to parent/i }));
36+
await waitForElementToBeRemoved(() => screen.queryByRole('heading', { name: /Detail three/i }));
37+
38+
userEvent.click(screen.getByRole('link', { name: /load two/i }));
39+
expect(await screen.findByRole('heading', { name: /Detail two/i })).toBeInTheDocument();
40+
41+
userEvent.click(screen.getByRole('link', { name: /hidden x/i }));
42+
expect(await screen.findByText(/You found the treasure!/i)).toBeInTheDocument();
43+
});
44+
45+
test('it can navigate to routes - workaround', async () => {
46+
const { navigate } = await render(RootComponent, {
747
declarations: [DetailComponent, HiddenDetailComponent],
848
routes: [
949
{
@@ -42,7 +82,7 @@ test('it can navigate to routes', async () => {
4282

4383
test('it can navigate to routes with a base path', async () => {
4484
const basePath = 'base';
45-
const { navigate } = await render(MasterComponent, {
85+
const { navigate } = await render(RootComponent, {
4686
declarations: [DetailComponent, HiddenDetailComponent],
4787
routes: [
4888
{

apps/example-app/src/app/examples/09-router.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { map } from 'rxjs/operators';
1313
<router-outlet></router-outlet>
1414
`,
1515
})
16-
export class MasterComponent {}
16+
export class RootComponent {}
1717

1818
@Component({
1919
selector: 'app-detail',

0 commit comments

Comments
 (0)