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

Commit a5bfd08

Browse files
committed
feat(demo): add unit tests for using-ng-pipe component
1 parent 5672f4e commit a5bfd08

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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 { UsingNgPipeComponent } from './using-ng-pipe.component';
13+
import { UpperCasePipe } from '@angular/common';
14+
import { By } from '@angular/platform-browser';
15+
import { Person } from 'app/person';
16+
17+
18+
let fixture: ComponentFixture<UsingNgPipeComponent>, component: UsingNgPipeComponent = null;
19+
20+
describe('UsingNgPipeComponent', () => {
21+
beforeEach(() => {
22+
fixture = TestBed.configureTestingModule({
23+
declarations: [
24+
BaseDemoComponent,
25+
UsingNgPipeComponent,
26+
DataTableDirective
27+
],
28+
imports: [
29+
AppRoutingModule,
30+
RouterTestingModule,
31+
DataTablesModule.forRoot(),
32+
HttpClientModule,
33+
MarkdownModule.forRoot(
34+
{
35+
sanitize: SecurityContext.NONE
36+
}
37+
),
38+
FormsModule
39+
],
40+
schemas: [NO_ERRORS_SCHEMA],
41+
providers: [
42+
UpperCasePipe
43+
]
44+
}).createComponent(UsingNgPipeComponent);
45+
46+
component = fixture.componentInstance;
47+
48+
fixture.detectChanges(); // initial binding
49+
});
50+
51+
it('should create the app', waitForAsync(() => {
52+
const app = fixture.debugElement.componentInstance;
53+
expect(app).toBeTruthy();
54+
}));
55+
56+
it('should have title "Using Angular Pipe"', waitForAsync(() => {
57+
const app = fixture.debugElement.componentInstance as UsingNgPipeComponent;
58+
expect(app.pageTitle).toBe('Using Angular Pipe');
59+
}));
60+
61+
it('should have firstName, lastName columns have text in uppercase', async () => {
62+
const app = fixture.debugElement.componentInstance as UsingNgPipeComponent;
63+
await fixture.whenStable();
64+
65+
const query = fixture.debugElement.query(By.directive(DataTableDirective));
66+
const dir = query.injector.get(DataTableDirective);
67+
expect(dir).toBeTruthy();
68+
69+
const instance = await dir.dtInstance;
70+
71+
const rows = fixture.nativeElement.querySelectorAll('tbody tr');
72+
73+
const testsArray = [0, 3, 6];
74+
const expectedArray = testsArray.map(_ => true);
75+
76+
expect(testsArray.map(index => {
77+
const dataRow = rows[index];
78+
79+
const firstNameFromData = (instance.row(dataRow).data() as Person).firstName;
80+
const firstNameFromTable = $('td:nth-child(2)', dataRow).text();
81+
82+
const lastNameFromData = (instance.row(dataRow).data() as Person).lastName;
83+
const lastNameFromTable = $('td:nth-child(3)', dataRow).text();
84+
return firstNameFromTable == firstNameFromData.toUpperCase() && lastNameFromTable == lastNameFromData.toUpperCase();
85+
}))
86+
.toEqual(expectedArray);
87+
});
88+
89+
});

0 commit comments

Comments
 (0)