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

Commit 2fb311a

Browse files
feat(#1579): add unit tests to demo project
* fix: tests can't find DOM types * feat: update AppComponent tests * feat: Add tests for zero-config component * feat: add tests for with-options component * feat: tests for with-ajax component * WIP feat: server-side-angular-way test * feat(demo): Upgrade demo project to Angular v12 * fix(tests): fix up server-side-angular-way, with-ajax * fix(tests): tsconfig.spec.json bug fixes - extends from `tsconfig.json` - fix esModuleInterop flag error * feat: tests for angular-way component * feat: add tests for custom-range-search component * feat: add tests for dt-instance component * feat: add tests for individual column filter component * feat: add tests for load-dt-options-with-promise component * feat: add tests for multiple-tables component * feat: add tests for rerender component * feat(demo): add unit tests for router-link.component * feat(demo): add unit tests for row-click-event component * feat(demo): add unit tests for using-ng-pipe component * feat: UsingNgTemplateRef update - send dtOptions to trigger event - add unit tests * fix(tests): remove redunant tslint comment * fix(tests): update comment in ng-template-ref component * fix: remove tslint comment in spec files - part 2 of d6fbd0b
1 parent 5a874e4 commit 2fb311a

19 files changed

+2697
-2278
lines changed

demo/package-lock.json

Lines changed: 1609 additions & 2262 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import { RouterTestingModule } from '@angular/router/testing';
2+
import { HttpClientModule } from '@angular/common/http';
3+
import { NO_ERRORS_SCHEMA, SecurityContext } from '@angular/core';
4+
import { ComponentFixture, TestBed, tick, waitForAsync } from '@angular/core/testing';
5+
import { DataTableDirective, DataTablesModule } from 'angular-datatables';
6+
import { MarkdownModule } from 'ngx-markdown';
7+
import { BaseDemoComponent } from '../base-demo/base-demo.component';
8+
import { AppRoutingModule } from '../app.routing';
9+
import { By } from '@angular/platform-browser';
10+
import { CustomRangeSearchComponent } from './custom-range-search.component';
11+
import { FormsModule } from '@angular/forms';
12+
13+
14+
let fixture: ComponentFixture<CustomRangeSearchComponent>, component: CustomRangeSearchComponent = null;
15+
16+
describe('CustomRangeSearchComponent', () => {
17+
beforeEach(() => {
18+
fixture = TestBed.configureTestingModule({
19+
declarations: [
20+
BaseDemoComponent,
21+
CustomRangeSearchComponent,
22+
DataTableDirective
23+
],
24+
imports: [
25+
AppRoutingModule,
26+
RouterTestingModule,
27+
DataTablesModule.forRoot(),
28+
HttpClientModule,
29+
MarkdownModule.forRoot(
30+
{
31+
sanitize: SecurityContext.NONE
32+
}
33+
),
34+
FormsModule
35+
],
36+
schemas: [NO_ERRORS_SCHEMA]
37+
}).createComponent(CustomRangeSearchComponent);
38+
39+
component = fixture.componentInstance;
40+
41+
fixture.detectChanges(); // initial binding
42+
});
43+
44+
it('should create the app', waitForAsync(() => {
45+
const app = fixture.debugElement.componentInstance;
46+
expect(app).toBeTruthy();
47+
}));
48+
49+
it('should have title "Custom filtering - Range search"', waitForAsync(() => {
50+
const app = fixture.debugElement.componentInstance as CustomRangeSearchComponent;
51+
expect(app.pageTitle).toBe('Custom filtering - Range search');
52+
}));
53+
54+
it('should have data filtered when min, max values change', async () => {
55+
const app = fixture.componentInstance as CustomRangeSearchComponent;
56+
await fixture.whenStable();
57+
58+
const query = fixture.debugElement.query(By.directive(DataTableDirective));
59+
const dir = query.injector.get(DataTableDirective);
60+
expect(dir).toBeTruthy();
61+
const instance = await dir.dtInstance;
62+
63+
const inputFieldMin: HTMLInputElement = fixture.nativeElement.querySelector('input[name="min"]');
64+
const inputFieldMax: HTMLInputElement = fixture.nativeElement.querySelector('input[name="max"]');
65+
66+
// # Test 1
67+
68+
inputFieldMin.value = '1';
69+
inputFieldMax.value = '5';
70+
71+
inputFieldMin.dispatchEvent(new Event('input'));
72+
inputFieldMax.dispatchEvent(new Event('input'));
73+
74+
instance.draw();
75+
fixture.detectChanges();
76+
77+
expect(instance.rows({ page: 'current' }).count()).toBe(1);
78+
79+
// # Test 2
80+
81+
inputFieldMin.value = '1';
82+
inputFieldMax.value = '15';
83+
84+
inputFieldMin.dispatchEvent(new Event('input'));
85+
inputFieldMax.dispatchEvent(new Event('input'));
86+
87+
instance.draw();
88+
fixture.detectChanges();
89+
90+
expect(instance.rows({ page: 'current' }).count()).toBe(3);
91+
92+
});
93+
94+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { RouterTestingModule } from '@angular/router/testing';
2+
import { HttpClientModule } from '@angular/common/http';
3+
import { NO_ERRORS_SCHEMA, SecurityContext } from '@angular/core';
4+
import { ComponentFixture, TestBed, tick, waitForAsync } from '@angular/core/testing';
5+
import { DataTableDirective, DataTablesModule } from 'angular-datatables';
6+
import { MarkdownModule } from 'ngx-markdown';
7+
import { BaseDemoComponent } from '../base-demo/base-demo.component';
8+
import { AppRoutingModule } from '../app.routing';
9+
import { By } from '@angular/platform-browser';
10+
import { FormsModule } from '@angular/forms';
11+
import { DtInstanceComponent } from './dt-instance.component';
12+
13+
14+
let fixture: ComponentFixture<DtInstanceComponent>, component: DtInstanceComponent = null;
15+
16+
describe('DtInstanceComponent', () => {
17+
beforeEach(() => {
18+
fixture = TestBed.configureTestingModule({
19+
declarations: [
20+
BaseDemoComponent,
21+
DtInstanceComponent,
22+
DataTableDirective
23+
],
24+
imports: [
25+
AppRoutingModule,
26+
RouterTestingModule,
27+
DataTablesModule.forRoot(),
28+
HttpClientModule,
29+
MarkdownModule.forRoot(
30+
{
31+
sanitize: SecurityContext.NONE
32+
}
33+
),
34+
FormsModule
35+
],
36+
schemas: [NO_ERRORS_SCHEMA]
37+
}).createComponent(DtInstanceComponent);
38+
39+
component = fixture.componentInstance;
40+
41+
fixture.detectChanges(); // initial binding
42+
});
43+
44+
it('should create the app', waitForAsync(() => {
45+
const app = fixture.debugElement.componentInstance;
46+
expect(app).toBeTruthy();
47+
}));
48+
49+
it('should have title "Getting the DataTable instance"', waitForAsync(() => {
50+
const app = fixture.debugElement.componentInstance as DtInstanceComponent;
51+
expect(app.pageTitle).toBe('Getting the DataTable instance');
52+
}));
53+
54+
it('should retrieve Table instance', async () => {
55+
const app = fixture.componentInstance as DtInstanceComponent;
56+
await fixture.whenStable();
57+
58+
const query = fixture.debugElement.query(By.directive(DataTableDirective));
59+
const dir = query.injector.get(DataTableDirective);
60+
expect(dir).toBeTruthy();
61+
62+
const instance = await dir.dtInstance;
63+
expect(instance).toBeTruthy();
64+
});
65+
66+
});
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import { RouterTestingModule } from '@angular/router/testing';
2+
import { HttpClientModule } from '@angular/common/http';
3+
import { NO_ERRORS_SCHEMA, SecurityContext } from '@angular/core';
4+
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
5+
import { DataTableDirective, DataTablesModule } from 'angular-datatables';
6+
import { MarkdownModule } from 'ngx-markdown';
7+
import { BaseDemoComponent } from '../base-demo/base-demo.component';
8+
import { AppRoutingModule } from '../app.routing';
9+
import { By } from '@angular/platform-browser';
10+
import { FormsModule } from '@angular/forms';
11+
import { IndividualColumnFilteringComponent } from './individual-column-filtering.component';
12+
13+
14+
let fixture: ComponentFixture<IndividualColumnFilteringComponent>, component: IndividualColumnFilteringComponent = null;
15+
16+
function applyValueToInput(inputElement: HTMLInputElement, value: string, table: DataTables.Api) {
17+
inputElement.value = value;
18+
inputElement.dispatchEvent(new Event('input'));
19+
inputElement.dispatchEvent(new Event('change'));
20+
table.draw();
21+
}
22+
23+
describe('IndividualColumnFilteringComponent', () => {
24+
beforeEach(() => {
25+
fixture = TestBed.configureTestingModule({
26+
declarations: [
27+
BaseDemoComponent,
28+
IndividualColumnFilteringComponent,
29+
DataTableDirective
30+
],
31+
imports: [
32+
AppRoutingModule,
33+
RouterTestingModule,
34+
DataTablesModule.forRoot(),
35+
HttpClientModule,
36+
MarkdownModule.forRoot(
37+
{
38+
sanitize: SecurityContext.NONE
39+
}
40+
),
41+
FormsModule
42+
],
43+
schemas: [NO_ERRORS_SCHEMA]
44+
}).createComponent(IndividualColumnFilteringComponent);
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 "Individual column searching"', waitForAsync(() => {
57+
const app = fixture.debugElement.componentInstance as IndividualColumnFilteringComponent;
58+
expect(app.pageTitle).toBe('Individual column searching');
59+
}));
60+
61+
it('should filter contents acc. to column', async () => {
62+
const app = fixture.componentInstance as IndividualColumnFilteringComponent;
63+
app.dtOptions.paging = false;
64+
65+
await fixture.whenStable();
66+
67+
const query = fixture.debugElement.query(By.directive(DataTableDirective));
68+
const dir = query.injector.get(DataTableDirective);
69+
expect(dir).toBeTruthy();
70+
71+
const instance = await dir.dtInstance;
72+
73+
const inputFields = Array.from(fixture.nativeElement.querySelectorAll('input')) as HTMLInputElement[];
74+
const inputFieldID = inputFields.find(e => e.name == "search-id");
75+
const inputFieldFirstName = inputFields.find(e => e.name == "search-first-name");
76+
const inputFieldLastName = inputFields.find(e => e.name == "search-last-name");
77+
78+
// # Test 1
79+
applyValueToInput(inputFieldID, '113', instance);
80+
expect(instance.rows({ page: 'current' }).count()).toBe(1);
81+
82+
// # Test 2
83+
84+
// reset prev. field
85+
applyValueToInput(inputFieldID, '', instance);
86+
applyValueToInput(inputFieldFirstName, 'Batman', instance);
87+
expect(instance.rows({ page: 'current' }).count()).toBe(30);
88+
89+
// # Test 3
90+
// reset prev. field
91+
applyValueToInput(inputFieldFirstName, '', instance);
92+
applyValueToInput(inputFieldLastName, 'Titi', instance);
93+
expect(instance.rows({ page: 'current' }).count()).toBe(28);
94+
95+
});
96+
97+
});
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { RouterTestingModule } from '@angular/router/testing';
2+
import { HttpClientModule } from '@angular/common/http';
3+
import { NO_ERRORS_SCHEMA, SecurityContext } from '@angular/core';
4+
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
5+
import { DataTableDirective, DataTablesModule } from 'angular-datatables';
6+
import { MarkdownModule } from 'ngx-markdown';
7+
import { BaseDemoComponent } from '../base-demo/base-demo.component';
8+
import { AppRoutingModule } from '../app.routing';
9+
import { By } from '@angular/platform-browser';
10+
import { FormsModule } from '@angular/forms';
11+
import { LoadDtOptionsWithPromiseComponent } from './load-dt-options-with-promise.component';
12+
13+
14+
let fixture: ComponentFixture<LoadDtOptionsWithPromiseComponent>, component: LoadDtOptionsWithPromiseComponent = null;
15+
16+
describe('LoadDtOptionsWithPromiseComponent', () => {
17+
beforeEach(() => {
18+
fixture = TestBed.configureTestingModule({
19+
declarations: [
20+
BaseDemoComponent,
21+
LoadDtOptionsWithPromiseComponent,
22+
DataTableDirective
23+
],
24+
imports: [
25+
AppRoutingModule,
26+
RouterTestingModule,
27+
DataTablesModule.forRoot(),
28+
HttpClientModule,
29+
MarkdownModule.forRoot(
30+
{
31+
sanitize: SecurityContext.NONE
32+
}
33+
),
34+
FormsModule
35+
],
36+
schemas: [NO_ERRORS_SCHEMA]
37+
}).createComponent(LoadDtOptionsWithPromiseComponent);
38+
39+
component = fixture.componentInstance;
40+
41+
fixture.detectChanges(); // initial binding
42+
});
43+
44+
it('should create the app', waitForAsync(() => {
45+
const app = fixture.debugElement.componentInstance;
46+
expect(app).toBeTruthy();
47+
}));
48+
49+
it('should have title "Load DataTables Options with Promise"', waitForAsync(() => {
50+
const app = fixture.debugElement.componentInstance as LoadDtOptionsWithPromiseComponent;
51+
expect(app.pageTitle).toBe('Load DataTables Options with Promise');
52+
}));
53+
54+
it('should render table from dtOptions as a Promise', async () => {
55+
const app = fixture.componentInstance as LoadDtOptionsWithPromiseComponent;
56+
await fixture.whenStable();
57+
58+
const query = fixture.debugElement.query(By.directive(DataTableDirective));
59+
const dir = query.injector.get(DataTableDirective);
60+
expect(dir).toBeTruthy();
61+
62+
const instance = await dir.dtInstance;
63+
expect(instance.rows().count()).toBeGreaterThan(0);
64+
});
65+
66+
});
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { RouterTestingModule } from '@angular/router/testing';
2+
import { HttpClientModule } from '@angular/common/http';
3+
import { NO_ERRORS_SCHEMA, QueryList, SecurityContext } from '@angular/core';
4+
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
5+
import { DataTableDirective, DataTablesModule } from 'angular-datatables';
6+
import { MarkdownModule } from 'ngx-markdown';
7+
import { BaseDemoComponent } from '../base-demo/base-demo.component';
8+
import { AppRoutingModule } from '../app.routing';
9+
import { By } from '@angular/platform-browser';
10+
import { FormsModule } from '@angular/forms';
11+
import { MultipleTablesComponent } from './multiple-tables.component';
12+
13+
14+
let fixture: ComponentFixture<MultipleTablesComponent>, component: MultipleTablesComponent = null;
15+
16+
describe('MultipleTablesComponent', () => {
17+
beforeEach(() => {
18+
fixture = TestBed.configureTestingModule({
19+
declarations: [
20+
BaseDemoComponent,
21+
MultipleTablesComponent,
22+
DataTableDirective
23+
],
24+
imports: [
25+
AppRoutingModule,
26+
RouterTestingModule,
27+
DataTablesModule.forRoot(),
28+
HttpClientModule,
29+
MarkdownModule.forRoot(
30+
{
31+
sanitize: SecurityContext.NONE
32+
}
33+
),
34+
FormsModule
35+
],
36+
schemas: [NO_ERRORS_SCHEMA]
37+
}).createComponent(MultipleTablesComponent);
38+
39+
component = fixture.componentInstance;
40+
41+
fixture.detectChanges(); // initial binding
42+
});
43+
44+
it('should create the app', waitForAsync(() => {
45+
const app = fixture.debugElement.componentInstance;
46+
expect(app).toBeTruthy();
47+
}));
48+
49+
it('should have title "Multiple DataTables in the same page"', waitForAsync(() => {
50+
const app = fixture.debugElement.componentInstance as MultipleTablesComponent;
51+
expect(app.pageTitle).toBe('Multiple DataTables in the same page');
52+
}));
53+
54+
it('should have two table instances in dtElements', async () => {
55+
const app = fixture.componentInstance as MultipleTablesComponent;
56+
await fixture.whenStable();
57+
58+
expect(app.dtElements.length).toBe(2);
59+
});
60+
61+
});

0 commit comments

Comments
 (0)