import { async, ComponentFixture, TestBed, fakeAsync, tick } from '@angular/core/testing'; import { RouterTestingModule } from '@angular/router/testing'; import { Router, Route, ActivatedRoute } from '@angular/router'; import { NgBreadcrumbComponent } from './ng-breadcrumb.component'; import { MockComponent } from '@mocks'; describe('NgBreadcrumbComponent', () => { let router: Router; let activatedRoute: ActivatedRoute; let component: NgBreadcrumbComponent; let fixture: ComponentFixture; beforeEach(async(() => { TestBed.configureTestingModule({ imports: [RouterTestingModule.withRoutes([])], declarations: [NgBreadcrumbComponent, MockComponent], }) .compileComponents(); })); beforeEach(() => { router = TestBed.get(Router); activatedRoute = TestBed.get(ActivatedRoute); fixture = TestBed.createComponent(NgBreadcrumbComponent); component = fixture.componentInstance; fixture.detectChanges(); }); it('creates the component', () => { expect(component).toBeTruthy(); }); describe('buildBreadCrumb()', () => { it('should create the expected breadcrumb', fakeAsync(() => { component.baseRoute = 'Test Home' fixture.ngZone.run(() => { const testRoutes: Route[] = [ { path: 'test-page', component: MockComponent, data: { breadcrumb: 'Test Page', }, children: [{ path: 'developer-reference', component: MockComponent, data: { breadcrumb: 'Developer Reference', }, }], }, ] router.resetConfig(testRoutes); router.navigate(['/test-page/developer-reference']); tick(); fixture.detectChanges(); expect(component.buildBreadCrumb(activatedRoute)).toEqual([ { label: 'Test Home', url: '/', }, { label: 'Test Page', url: '/test-page/', }, { label: 'Developer Reference', url: '/test-page/developer-reference/', }, ]); }) })); }) });