This repository has been archived by the owner on May 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathside-bar.component.spec.ts
59 lines (48 loc) · 2.2 KB
/
side-bar.component.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {SideBarComponent} from './side-bar.component';
import {By} from '@angular/platform-browser';
import {provideRouter, RouterLink} from '@angular/router';
import {Component} from '@angular/core';
import {RouterTestingHarness} from '@angular/router/testing';
@Component({
template: ``
})
export class DummyComponent {
}
describe('SideBarComponent', () => {
let component: SideBarComponent;
let fixture: ComponentFixture<SideBarComponent>;
beforeEach(async () => {
await TestBed.configureTestingModule({
imports: [SideBarComponent],
providers: [provideRouter([
{path: '', component: DummyComponent},
{path: 'edit', component: DummyComponent}
])]
})
.compileComponents()
fixture = TestBed.createComponent(SideBarComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
it('can link to main pages', () => {
const anchorElements = fixture.debugElement.queryAll(By.css('a'));
expect(anchorElements.length).withContext('should have 2 links').toBe(2);
expect(anchorElements[0]?.attributes['href']).withContext('1st link should go to Home').toBe('/');
expect(anchorElements[1]?.attributes['href']).withContext('2nd link should go to Edit').toBe('/edit');
});
it('can show the active link', async () => {
let routerTestingHarness: RouterTestingHarness
await fixture.ngZone?.run(async () => routerTestingHarness = await RouterTestingHarness.create('/'))
let activeLinks = fixture.debugElement.queryAll(By.css('.active'))
expect(activeLinks.length).withContext('should only have 1 active link').toBe(1);
expect(activeLinks[0]?.attributes['href']).withContext('active link should be for Home').toBe('/');
await fixture.ngZone?.run(async () => routerTestingHarness.navigateByUrl('/edit'))
activeLinks = fixture.debugElement.queryAll(By.css('.active'))
expect(activeLinks.length).withContext('should only have 1 active link after navigating').toBe(1);
expect(activeLinks[0]?.attributes['href']).withContext('active link should be for Edit').toBe('/edit');
});
});