Skip to content
This repository was archived by the owner on Feb 2, 2025. It is now read-only.

Commit 42a0af5

Browse files
committed
feat(demo): add unit tests for router-link.component
1 parent 8d93358 commit 42a0af5

File tree

1 file changed

+72
-0
lines changed

1 file changed

+72
-0
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/* tslint:disable:no-unused-variable */
2+
3+
import { RouterTestingModule } from '@angular/router/testing';
4+
import { HttpClientModule } from '@angular/common/http';
5+
import { NO_ERRORS_SCHEMA, SecurityContext } from '@angular/core';
6+
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
7+
import { DataTableDirective, DataTablesModule } from 'angular-datatables';
8+
import { MarkdownModule } from 'ngx-markdown';
9+
import { BaseDemoComponent } from '../base-demo/base-demo.component';
10+
import { AppRoutingModule } from '../app.routing';
11+
import { FormsModule } from '@angular/forms';
12+
import { RouterLinkComponent } from './router-link.component';
13+
import { Router } from '@angular/router';
14+
15+
16+
let fixture: ComponentFixture<RouterLinkComponent>, component: RouterLinkComponent = null, router: Router = null;
17+
18+
describe('RouterLinkComponent', () => {
19+
beforeEach(() => {
20+
fixture = TestBed.configureTestingModule({
21+
declarations: [
22+
BaseDemoComponent,
23+
RouterLinkComponent,
24+
DataTableDirective
25+
],
26+
imports: [
27+
AppRoutingModule,
28+
RouterTestingModule,
29+
DataTablesModule.forRoot(),
30+
HttpClientModule,
31+
MarkdownModule.forRoot(
32+
{
33+
sanitize: SecurityContext.NONE
34+
}
35+
),
36+
FormsModule
37+
],
38+
schemas: [NO_ERRORS_SCHEMA]
39+
}).createComponent(RouterLinkComponent);
40+
41+
component = fixture.componentInstance;
42+
router = TestBed.inject(Router);
43+
44+
fixture.detectChanges(); // initial binding
45+
});
46+
47+
it('should create the app', waitForAsync(() => {
48+
const app = fixture.debugElement.componentInstance;
49+
expect(app).toBeTruthy();
50+
}));
51+
52+
it('should have title "Router Link"', waitForAsync(() => {
53+
const app = fixture.debugElement.componentInstance as RouterLinkComponent;
54+
expect(app.pageTitle).toBe('Router Link');
55+
}));
56+
57+
it('should open Person info on click', async () => {
58+
await fixture.whenStable();
59+
60+
const rSpy = spyOn(router, 'navigate');
61+
62+
const button = document.createElement('button');
63+
button.setAttribute('view-person-id', '3');
64+
fixture.nativeElement.appendChild(button);
65+
button.click();
66+
67+
68+
fixture.detectChanges();
69+
expect(rSpy).toHaveBeenCalledWith(["/person/3"]);
70+
});
71+
72+
});

0 commit comments

Comments
 (0)