|
1 |
| -import {Component, NgModule} from '@angular/core'; |
2 |
| -import {render, screen} from '@testing-library/angular'; |
3 |
| -import {Router, RouterModule, Routes} from "@angular/router"; |
4 |
| -import {RouterTestingModule} from "@angular/router/testing"; |
5 |
| -import userEvent from "@testing-library/user-event"; |
6 |
| -import {Location} from "@angular/common"; |
7 |
| -import {TestBed} from "@angular/core/testing"; |
| 1 | +import { Location } from '@angular/common'; |
| 2 | +import { Component, NgModule } from '@angular/core'; |
| 3 | +import { RouterModule, Routes } from '@angular/router'; |
| 4 | +import { RouterTestingModule } from '@angular/router/testing'; |
| 5 | +import userEvent from '@testing-library/user-event'; |
| 6 | +import { render, screen } from '../../src/public_api'; |
8 | 7 |
|
9 | 8 | @Component({
|
10 |
| - template: `<div>Navigate</div> <router-outlet></router-outlet>`, |
| 9 | + template: `<div>Navigate</div> |
| 10 | + <router-outlet></router-outlet>`, |
11 | 11 | })
|
12 | 12 | class MainComponent {}
|
13 | 13 |
|
14 | 14 | @Component({
|
15 |
| - template: `<div>first page</div><a routerLink="/second">go to second</a>` |
| 15 | + template: `<div>first page</div> |
| 16 | + <a routerLink="/second">go to second</a>`, |
16 | 17 | })
|
17 | 18 | class FirstComponent {}
|
18 | 19 |
|
19 | 20 | @Component({
|
20 |
| - template: `<div>second page</div><button (click)="goBack()">navigate back</button>` |
| 21 | + template: `<div>second page</div> |
| 22 | + <button (click)="goBack()">navigate back</button>`, |
21 | 23 | })
|
22 | 24 | class SecondComponent {
|
23 |
| - constructor(private location: Location) { } |
24 |
| - goBack() {this.location.back();} |
| 25 | + constructor(private location: Location) {} |
| 26 | + goBack() { |
| 27 | + this.location.back(); |
| 28 | + } |
25 | 29 | }
|
26 | 30 |
|
27 | 31 | const routes: Routes = [
|
28 |
| - {path: '', redirectTo: '/first', pathMatch: 'full'}, |
29 |
| - {path: 'first', component: FirstComponent}, |
30 |
| - {path: 'second', component: SecondComponent} |
| 32 | + { path: '', redirectTo: '/first', pathMatch: 'full' }, |
| 33 | + { path: 'first', component: FirstComponent }, |
| 34 | + { path: 'second', component: SecondComponent }, |
31 | 35 | ];
|
32 | 36 |
|
33 | 37 | @NgModule({
|
34 | 38 | declarations: [FirstComponent, SecondComponent],
|
35 | 39 | imports: [RouterModule.forRoot(routes)],
|
36 |
| - exports: [RouterModule] |
| 40 | + exports: [RouterModule], |
37 | 41 | })
|
38 | 42 | class AppRoutingModule {}
|
39 | 43 |
|
40 |
| - |
41 |
| -// test('navigate to second page and back', async () => { |
42 |
| -// const subject = await render(MainComponent, {imports: [AppRoutingModule, RouterTestingModule]}); |
43 |
| -// await subject.navigate('/'); |
44 |
| -// |
45 |
| -// expect(await screen.findByText('Navigate')).toBeInTheDocument(); |
46 |
| -// expect(await screen.findByText('first page')).toBeInTheDocument(); |
47 |
| -// |
48 |
| -// userEvent.click(await screen.findByText('go to second')); |
49 |
| -// |
50 |
| -// expect(await screen.findByText('second page')).toBeInTheDocument(); |
51 |
| -// expect(await screen.findByText('navigate back')).toBeInTheDocument(); |
52 |
| -// |
53 |
| -// userEvent.click(await screen.findByText('navigate back')); |
54 |
| -// |
55 |
| -// expect(await screen.findByText('first page')).toBeInTheDocument(); |
56 |
| -// }); |
57 |
| - |
58 | 44 | test('navigate to second page and back', async () => {
|
59 |
| - const subject = await render(MainComponent, {imports: [AppRoutingModule, RouterTestingModule]}); |
60 |
| - |
61 |
| - TestBed.inject(Router).initialNavigation(); |
62 |
| - |
63 |
| - await subject.navigate('/'); |
| 45 | + await render(MainComponent, { imports: [AppRoutingModule, RouterTestingModule] }); |
64 | 46 |
|
65 | 47 | expect(await screen.findByText('Navigate')).toBeInTheDocument();
|
66 | 48 | expect(await screen.findByText('first page')).toBeInTheDocument();
|
|
0 commit comments