|
| 1 | +import { ComponentRef } from '@angular/core'; |
1 | 2 | import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
|
2 |
| -import { provideRouter, Router } from '@angular/router'; |
| 3 | +import { provideRouter, Route } from '@angular/router'; |
| 4 | +import { RouterTestingHarness } from '@angular/router/testing'; |
| 5 | +import { take } from 'rxjs'; |
3 | 6 |
|
4 | 7 | import { BreadcrumbRouterComponent } from './breadcrumb-router.component';
|
5 | 8 | import { BreadcrumbRouterService } from './breadcrumb-router.service';
|
6 | 9 |
|
7 | 10 | describe('BreadcrumbComponent', () => {
|
8 | 11 | let component: BreadcrumbRouterComponent;
|
| 12 | + let componentRef: ComponentRef<BreadcrumbRouterComponent>; |
9 | 13 | let fixture: ComponentFixture<BreadcrumbRouterComponent>;
|
10 |
| - let router: Router; |
| 14 | + let harness: RouterTestingHarness; |
| 15 | + |
| 16 | + const routes: Route[] = [ |
| 17 | + { path: 'home', component: BreadcrumbRouterComponent, data: { title: 'Home' } }, |
| 18 | + { path: 'color', component: BreadcrumbRouterComponent, title: 'Color' }, |
| 19 | + { path: '', component: BreadcrumbRouterComponent } |
| 20 | + ]; |
11 | 21 |
|
12 | 22 | beforeEach(waitForAsync(() => {
|
13 | 23 | TestBed.configureTestingModule({
|
14 |
| - imports: [ |
15 |
| - BreadcrumbRouterComponent |
16 |
| - ], |
17 |
| - providers: [BreadcrumbRouterService, provideRouter([])] |
| 24 | + imports: [BreadcrumbRouterComponent], |
| 25 | + providers: [BreadcrumbRouterService, provideRouter(routes)] |
18 | 26 | }).compileComponents();
|
19 | 27 | }));
|
20 | 28 |
|
21 |
| - beforeEach(() => { |
| 29 | + beforeEach(async () => { |
22 | 30 | fixture = TestBed.createComponent(BreadcrumbRouterComponent);
|
23 |
| - router = TestBed.inject(Router); |
24 | 31 | component = fixture.componentInstance;
|
| 32 | + componentRef = fixture.componentRef; |
| 33 | + |
| 34 | + harness = await RouterTestingHarness.create(); |
25 | 35 | fixture.detectChanges();
|
26 | 36 | });
|
27 | 37 |
|
28 | 38 | it('should create', () => {
|
29 | 39 | expect(component).toBeTruthy();
|
30 | 40 | });
|
| 41 | + |
| 42 | + it('should have breadcrumbs', () => { |
| 43 | + expect(component.breadcrumbs).toBeDefined(); |
| 44 | + }); |
| 45 | + |
| 46 | + it('should get breadcrumbs from service', async () => { |
| 47 | + const comp = await harness.navigateByUrl('/home'); |
| 48 | + component.breadcrumbs?.pipe(take(1)).subscribe((breadcrumbs) => { |
| 49 | + expect(breadcrumbs).toEqual([{ label: 'Home', url: '/home', queryParams: {} }]); |
| 50 | + }); |
| 51 | + }); |
| 52 | + it('should get breadcrumbs from service', async () => { |
| 53 | + const comp = await harness.navigateByUrl('/color?id=1&test=2'); |
| 54 | + component.breadcrumbs?.pipe(take(1)).subscribe((breadcrumbs) => { |
| 55 | + expect(breadcrumbs).toEqual([{ label: 'Color', url: '/color', queryParams: { id: '1', test: '2' } }]); |
| 56 | + }); |
| 57 | + }); |
| 58 | + it('should get breadcrumbs from service', async () => { |
| 59 | + const comp = await harness.navigateByUrl('/'); |
| 60 | + component.breadcrumbs?.pipe(take(1)).subscribe((breadcrumbs) => { |
| 61 | + expect(breadcrumbs).toEqual([{ label: '', url: '/', queryParams: {} }]); |
| 62 | + }); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should emit breadcrumbs on items change', () => { |
| 66 | + componentRef.setInput('items', [{ label: 'test' }]); |
| 67 | + fixture.detectChanges(); |
| 68 | + component.breadcrumbs?.pipe(take(1)).subscribe((breadcrumbs) => { |
| 69 | + expect(breadcrumbs).toEqual([{ label: 'test' }]); |
| 70 | + }); |
| 71 | + }); |
31 | 72 | });
|
0 commit comments