|
| 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 | +}); |
0 commit comments