import { async, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { Router, Route } from '@angular/router'; import { MockComponent } from '@mocks'; import { PackageNamePipe } from '@pipes'; import { HeroComponent } from './hero.component'; describe('HeroCatalogComponent', () => { let router: Router; let component: HeroComponent; let fixture: ComponentFixture; beforeEach(async(() => { TestBed.configureTestingModule({ imports: [RouterTestingModule.withRoutes([])], declarations: [HeroComponent, MockComponent, PackageNamePipe], }) .compileComponents(); })); beforeEach(() => { router = TestBed.get(Router); fixture = TestBed.createComponent(HeroComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('creates the component', () => { expect(component).toBeTruthy(); }); describe('ngOnInit()', () => { it('leaves "showDevRef" false if the "developer-reference" route does not exist', () => { expect(component.showDevRef).toBeFalsy(); }); it('sets "showDevRef" to true if the "developer-reference" route exists', fakeAsync(() => { fixture.ngZone.run(() => { const testRoutes: Route[] = [ { path: 'test', component: MockComponent, children: [{ path: 'developer-reference', component: MockComponent, }], }, ] router.resetConfig(testRoutes); router.navigate(['test']); tick(); fixture = TestBed.createComponent(HeroComponent); component = fixture.componentInstance; fixture.detectChanges(); expect(component.showDevRef).toBeTruthy(); }); })); it('leaves "showStyle" false if the "style" route does not exist', () => { expect(component.showStyle).toBeFalsy(); }); it('sets "showStyle" to true if the "style" route exists', fakeAsync(() => { fixture.ngZone.run(() => { const testRoutes: Route[] = [ { path: 'test', component: MockComponent, children: [{ path: 'style', component: MockComponent, }], }, ] router.resetConfig(testRoutes); router.navigate(['test']); tick(); fixture = TestBed.createComponent(HeroComponent); component = fixture.componentInstance; fixture.detectChanges(); expect(component.showStyle).toBeTruthy(); }); })); it('leaves "showUsage" false if the "usage" route does not exist', () => { expect(component.showDevRef).toBeFalsy(); }); it('sets "showUsage" to true if the "usage" route exists', fakeAsync(() => { fixture.ngZone.run(() => { const testRoutes: Route[] = [ { path: 'test', component: MockComponent, children: [{ path: 'usage', component: MockComponent, }], }, ] router.resetConfig(testRoutes); router.navigate(['test']); tick(); fixture = TestBed.createComponent(HeroComponent); component = fixture.componentInstance; fixture.detectChanges(); expect(component.showUsage).toBeTruthy(); }); })); it('leaves "showComponentStyles" false if the "component-styles" route does not exist', () => { expect(component.showDevRef).toBeFalsy(); }); it('sets "showComponentStyles" to true if the "component-styles" route exists', fakeAsync(() => { fixture.ngZone.run(() => { const testRoutes: Route[] = [ { path: 'test', component: MockComponent, children: [{ path: 'component-styles', component: MockComponent, }], }, ] router.resetConfig(testRoutes); router.navigate(['test']); tick(); fixture = TestBed.createComponent(HeroComponent); component = fixture.componentInstance; fixture.detectChanges(); expect(component.showComponentStyles).toBeTruthy(); }); })); it('leaves "showLibrary" false if the "library" route does not exist', () => { expect(component.showLibrary).toBeFalsy(); }); it('sets "showLibrary" to true if the "library" route exists', fakeAsync(() => { fixture.ngZone.run(() => { const testRoutes: Route[] = [ { path: 'test', component: MockComponent, children: [{ path: 'library', component: MockComponent, }], }, ] router.resetConfig(testRoutes); router.navigate(['test']); tick(); fixture = TestBed.createComponent(HeroComponent); component = fixture.componentInstance; fixture.detectChanges(); expect(component.showLibrary).toBeTruthy(); }); })); it('leaves "showResponsive" false if the "responsive" route does not exist', () => { expect(component.showResponsive).toBeFalsy(); }); it('sets "showResponsive" to true if the "responsive" route exists', fakeAsync(() => { fixture.ngZone.run(() => { const testRoutes: Route[] = [ { path: 'test', component: MockComponent, children: [{ path: 'responsive', component: MockComponent, }], }, ] router.resetConfig(testRoutes); router.navigate(['test']); tick(); fixture = TestBed.createComponent(HeroComponent); component = fixture.componentInstance; fixture.detectChanges(); expect(component.showResponsive).toBeTruthy(); }); })); it('leaves children checks false if there is a root route without children', fakeAsync(() => { fixture.ngZone.run(() => { const testRoutes: Route[] = [ { path: 'test', component: MockComponent, }, ] router.resetConfig(testRoutes); router.navigate(['test']); tick(); fixture = TestBed.createComponent(HeroComponent); component = fixture.componentInstance; fixture.detectChanges(); expect(component.showDevRef).toBeFalsy(); expect(component.showUsage).toBeFalsy(); expect(component.showComponentStyles).toBeFalsy(); expect(component.showLibrary).toBeFalsy(); }); })); }); });