Skip to content

Commit 4e6e572

Browse files
committed
feat(routing): add routing tests
1 parent a0046ce commit 4e6e572

10 files changed

+194
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { Location } from '@angular/common';
2+
import {
3+
TestBed,
4+
fakeAsync,
5+
tick,
6+
async,
7+
ComponentFixture
8+
} from '@angular/core/testing';
9+
import { RouterTestingModule } from '@angular/router/testing';
10+
import { Router } from '@angular/router';
11+
import { AppRoutingModule, appRoutes } from './app-routing.module';
12+
import { AppModule } from './app.module';
13+
import { Component } from '@angular/core';
14+
import { HomeComponent } from './home/home.component';
15+
import { SearchComponent } from './search/search.component';
16+
import { By } from '@angular/platform-browser';
17+
18+
@Component({
19+
selector: 'routing-test-cmp',
20+
template: `<router-outlet></router-outlet>`
21+
})
22+
class RoutingTestComponent {}
23+
24+
describe('The App Routing (with custom cmp)', () => {
25+
let routingComponentFixture: ComponentFixture<RoutingTestComponent>;
26+
let routingComponent: RoutingTestComponent;
27+
let router: Router;
28+
let location: Location;
29+
30+
beforeEach(() => {
31+
TestBed.configureTestingModule({
32+
imports: [RouterTestingModule.withRoutes(appRoutes)],
33+
declarations: [HomeComponent, SearchComponent, RoutingTestComponent]
34+
});
35+
36+
routingComponentFixture = TestBed.createComponent(RoutingTestComponent);
37+
routingComponent = routingComponentFixture.componentInstance;
38+
39+
router = TestBed.get(Router) as Router;
40+
location = TestBed.get(Location) as Location;
41+
});
42+
43+
it(
44+
'should properly navigate back from search to home again',
45+
fakeAsync(() => {
46+
router.navigate(['']);
47+
tick();
48+
49+
router.navigate(['/search']);
50+
tick();
51+
52+
const searchCmpEl = routingComponentFixture.debugElement.query(
53+
By.css('app-search')
54+
);
55+
expect(searchCmpEl).not.toBeNull();
56+
const searchCmp = searchCmpEl.componentInstance as SearchComponent;
57+
58+
searchCmp.onGoBack();
59+
tick();
60+
expect(location.path()).toBe('/home');
61+
})
62+
);
63+
});
+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { Location } from '@angular/common';
2+
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
3+
import { Router } from '@angular/router';
4+
import { RouterTestingModule } from '@angular/router/testing';
5+
import { appRoutes } from './app-routing.module';
6+
import { AppModule } from './app.module';
7+
8+
describe('The App Routing', () => {
9+
let router: Router;
10+
let location: Location;
11+
12+
beforeEach(() => {
13+
TestBed.configureTestingModule({
14+
imports: [AppModule, RouterTestingModule.withRoutes(appRoutes)]
15+
});
16+
17+
router = TestBed.get(Router) as Router;
18+
location = TestBed.get(Location) as Location;
19+
});
20+
21+
it(
22+
'automatically redirects to home when the app starts',
23+
fakeAsync(() => {
24+
router.navigate(['']);
25+
tick();
26+
expect(location.path()).toBe('/home');
27+
})
28+
);
29+
30+
it(
31+
'automatically redirects to search when invoking /search',
32+
fakeAsync(() => {
33+
router.navigate(['/search']);
34+
tick();
35+
expect(location.path()).toBe('/search');
36+
})
37+
);
38+
});

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

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { NgModule } from '@angular/core';
2+
import { Routes, RouterModule } from '@angular/router';
3+
import { HomeComponent } from './home/home.component';
4+
import { SearchComponent } from './search/search.component';
5+
6+
export const appRoutes: Routes = [
7+
{
8+
path: '',
9+
redirectTo: 'home',
10+
pathMatch: 'full'
11+
},
12+
{
13+
path: 'home',
14+
component: HomeComponent
15+
},
16+
{
17+
path: 'search',
18+
component: SearchComponent
19+
}
20+
];
21+
22+
@NgModule({
23+
imports: [RouterModule.forRoot(appRoutes)],
24+
exports: [RouterModule]
25+
})
26+
export class AppRoutingModule {}

src/app/routing/app.component.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-root',
5+
template: `<router-outlet></router-outlet>`
6+
})
7+
export class RoutingAppComponent {}

src/app/routing/app.module.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NgModule } from '@angular/core';
2+
import { AppRoutingModule } from './app-routing.module';
3+
import { RoutingAppComponent } from './app.component';
4+
import { HomeModule } from './home/home.module';
5+
import { SearchModule } from './search/search.module';
6+
7+
@NgModule({
8+
declarations: [RoutingAppComponent],
9+
imports: [HomeModule, SearchModule, AppRoutingModule]
10+
})
11+
export class AppModule {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { NgModule } from '@angular/core';
2+
import { Routes, RouterModule } from '@angular/router';
3+
4+
const routes: Routes = [];
5+
6+
@NgModule({
7+
imports: [RouterModule.forChild(routes)],
8+
exports: [RouterModule]
9+
})
10+
export class HomeRoutingModule { }
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Component } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-home',
5+
template: `home component`
6+
})
7+
export class HomeComponent {}

src/app/routing/home/home.module.ts

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { NgModule } from '@angular/core';
2+
import { HomeComponent } from './home.component';
3+
4+
@NgModule({
5+
declarations: [HomeComponent]
6+
})
7+
export class HomeModule {}
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Component } from '@angular/core';
2+
import { Location } from '@angular/common';
3+
4+
@Component({
5+
selector: 'app-search',
6+
template: `
7+
search component
8+
9+
<button (click)="onGoBack()">Go back</button>
10+
`
11+
})
12+
export class SearchComponent {
13+
constructor(private location: Location) {}
14+
15+
onGoBack() {
16+
this.location.back();
17+
}
18+
}
+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { NgModule } from '@angular/core';
2+
import { SearchComponent } from './search.component';
3+
4+
@NgModule({
5+
declarations: [SearchComponent]
6+
})
7+
export class SearchModule {}

0 commit comments

Comments
 (0)