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

Commit e01d147

Browse files
committed
ci: unit tests changes
- add unit tests for new components - fix broken unit tests due to updates
1 parent 32e10ee commit e01d147

7 files changed

+119
-33
lines changed

demo/src/app/advanced/dt-instance.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ describe('DtInstanceComponent', () => {
4646
expect(app).toBeTruthy();
4747
}));
4848

49-
it('should have title "Getting the DataTable instance"', waitForAsync(() => {
49+
it('should have title "Finding DataTable instance"', waitForAsync(() => {
5050
const app = fixture.debugElement.componentInstance as DtInstanceComponent;
51-
expect(app.pageTitle).toBe('Getting the DataTable instance');
51+
expect(app.pageTitle).toBe('Finding DataTable instance');
5252
}));
5353

5454
it('should retrieve Table instance', async () => {

demo/src/app/advanced/load-dt-options-with-promise.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ describe('LoadDtOptionsWithPromiseComponent', () => {
4646
expect(app).toBeTruthy();
4747
}));
4848

49-
it('should have title "Load DataTables Options with Promise"', waitForAsync(() => {
49+
it('should have title "Load DT Options with Promise"', waitForAsync(() => {
5050
const app = fixture.debugElement.componentInstance as LoadDtOptionsWithPromiseComponent;
51-
expect(app.pageTitle).toBe('Load DataTables Options with Promise');
51+
expect(app.pageTitle).toBe('Load DT Options with Promise');
5252
}));
5353

5454
it('should render table from dtOptions as a Promise', async () => {

demo/src/app/advanced/multiple-tables.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ describe('MultipleTablesComponent', () => {
4646
expect(app).toBeTruthy();
4747
}));
4848

49-
it('should have title "Multiple DataTables in the same page"', waitForAsync(() => {
49+
it('should have title "Multiple tables in the same page"', waitForAsync(() => {
5050
const app = fixture.debugElement.componentInstance as MultipleTablesComponent;
51-
expect(app.pageTitle).toBe('Multiple DataTables in the same page');
51+
expect(app.pageTitle).toBe('Multiple tables in the same page');
5252
}));
5353

5454
it('should have two table instances in dtElements', async () => {

demo/src/app/advanced/router-link.component.spec.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import { AppRoutingModule } from '../app.routing';
99
import { FormsModule } from '@angular/forms';
1010
import { RouterLinkComponent } from './router-link.component';
1111
import { Router } from '@angular/router';
12+
import { By } from '@angular/platform-browser';
13+
import { DemoNgComponent } from './demo-ng-template-ref.component';
1214

1315

1416
let fixture: ComponentFixture<RouterLinkComponent>, component: RouterLinkComponent = null, router: Router = null;
@@ -18,6 +20,7 @@ describe('RouterLinkComponent', () => {
1820
fixture = TestBed.configureTestingModule({
1921
declarations: [
2022
BaseDemoComponent,
23+
DemoNgComponent,
2124
RouterLinkComponent,
2225
DataTableDirective
2326
],
@@ -52,19 +55,20 @@ describe('RouterLinkComponent', () => {
5255
expect(app.pageTitle).toBe('Router Link');
5356
}));
5457

55-
it('should open Person info on click', async () => {
58+
it('should respond to button click event inside TemplateRef', async () => {
5659
await fixture.whenStable();
5760

61+
const query = fixture.debugElement.query(By.directive(DataTableDirective));
62+
const dir = query.injector.get(DataTableDirective);
63+
expect(dir).toBeTruthy();
64+
5865
const rSpy = spyOn(router, 'navigate');
5966

60-
const button = document.createElement('button');
61-
button.setAttribute('view-person-id', '3');
62-
fixture.nativeElement.appendChild(button);
67+
const row: HTMLTableRowElement = fixture.nativeElement.querySelector('tbody tr:first-child');
68+
const button: HTMLButtonElement = row.querySelector('button.btn-sm');
6369
button.click();
6470

65-
66-
fixture.detectChanges();
67-
expect(rSpy).toHaveBeenCalledWith(["/person/3"]);
71+
expect(rSpy).toHaveBeenCalled();
6872
});
6973

7074
});
Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,64 @@
1-
import { ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { HttpClientModule } from '@angular/common/http';
2+
import { SecurityContext, NO_ERRORS_SCHEMA } from '@angular/core';
3+
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
4+
import { By } from '@angular/platform-browser';
5+
import { RouterTestingModule } from '@angular/router/testing';
6+
import { DataTableDirective, DataTablesModule } from 'angular-datatables';
7+
import { AppRoutingModule } from 'app/app.routing';
8+
import { BaseDemoComponent } from 'app/base-demo/base-demo.component';
9+
import { MarkdownModule } from 'ngx-markdown';
210

311
import { NewServerSideComponent } from './new-server-side.component';
412

513
describe('NewServerSideComponent', () => {
614
let component: NewServerSideComponent;
715
let fixture: ComponentFixture<NewServerSideComponent>;
816

9-
beforeEach(async () => {
10-
await TestBed.configureTestingModule({
11-
declarations: [ NewServerSideComponent ]
12-
})
13-
.compileComponents();
17+
beforeEach(() => {
18+
fixture = TestBed.configureTestingModule({
19+
declarations: [
20+
BaseDemoComponent,
21+
NewServerSideComponent,
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+
],
35+
schemas: [NO_ERRORS_SCHEMA]
36+
}).createComponent(NewServerSideComponent);
1437

15-
fixture = TestBed.createComponent(NewServerSideComponent);
1638
component = fixture.componentInstance;
17-
fixture.detectChanges();
39+
40+
fixture.detectChanges(); // initial binding
1841
});
1942

2043
it('should create', () => {
2144
expect(component).toBeTruthy();
2245
});
46+
47+
48+
it('should have title "Server-side processing"', waitForAsync(() => {
49+
const app = fixture.debugElement.componentInstance as NewServerSideComponent;
50+
expect(app.pageTitle).toBe('Server-side processing');
51+
}));
52+
53+
it('should have table populated via AJAX', async () => {
54+
const app = fixture.debugElement.componentInstance as NewServerSideComponent;
55+
await fixture.whenStable();
56+
expect(app.dtOptions.columns).toBeDefined();
57+
const query = fixture.debugElement.query(By.directive(DataTableDirective));
58+
const dir = query.injector.get(DataTableDirective);
59+
expect(dir).toBeTruthy();
60+
const instance = await dir.dtInstance;
61+
fixture.detectChanges();
62+
expect(instance.rows().length).toBeGreaterThan(0);
63+
});
2364
});
Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,64 @@
1-
import { ComponentFixture, TestBed } from '@angular/core/testing';
1+
import { HttpClientModule } from '@angular/common/http';
2+
import { SecurityContext, NO_ERRORS_SCHEMA } from '@angular/core';
3+
import { ComponentFixture, TestBed, waitForAsync } from '@angular/core/testing';
4+
import { By } from '@angular/platform-browser';
5+
import { RouterTestingModule } from '@angular/router/testing';
6+
import { DataTableDirective, DataTablesModule } from 'angular-datatables';
7+
import { AppRoutingModule } from 'app/app.routing';
8+
import { BaseDemoComponent } from 'app/base-demo/base-demo.component';
9+
import { MarkdownModule } from 'ngx-markdown';
210

311
import { WithAjaxCallbackComponent } from './with-ajax-callback.component';
412

513
describe('WithAjaxCallbackComponent', () => {
614
let component: WithAjaxCallbackComponent;
715
let fixture: ComponentFixture<WithAjaxCallbackComponent>;
816

9-
beforeEach(async () => {
10-
await TestBed.configureTestingModule({
11-
declarations: [ WithAjaxCallbackComponent ]
12-
})
13-
.compileComponents();
17+
beforeEach(() => {
18+
fixture = TestBed.configureTestingModule({
19+
declarations: [
20+
BaseDemoComponent,
21+
WithAjaxCallbackComponent,
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+
],
35+
schemas: [NO_ERRORS_SCHEMA]
36+
}).createComponent(WithAjaxCallbackComponent);
1437

15-
fixture = TestBed.createComponent(WithAjaxCallbackComponent);
1638
component = fixture.componentInstance;
17-
fixture.detectChanges();
39+
40+
fixture.detectChanges(); // initial binding
1841
});
1942

20-
it('should create', () => {
21-
expect(component).toBeTruthy();
43+
it('should create the app', waitForAsync(() => {
44+
const app = fixture.debugElement.componentInstance;
45+
expect(app).toBeTruthy();
46+
}));
47+
48+
it('should have title "AJAX with callback"', waitForAsync(() => {
49+
const app = fixture.debugElement.componentInstance as WithAjaxCallbackComponent;
50+
expect(app.pageTitle).toBe('AJAX with callback');
51+
}));
52+
53+
it('should have table populated via AJAX', async () => {
54+
const app = fixture.debugElement.componentInstance as WithAjaxCallbackComponent;
55+
await fixture.whenStable();
56+
expect(app.dtOptions.columns).toBeDefined();
57+
const query = fixture.debugElement.query(By.directive(DataTableDirective));
58+
const dir = query.injector.get(DataTableDirective);
59+
expect(dir).toBeTruthy();
60+
const instance = await dir.dtInstance;
61+
fixture.detectChanges();
62+
expect(instance.rows().length).toBeGreaterThan(0);
2263
});
2364
});

demo/src/app/basic/with-ajax.component.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ describe('WithAjaxComponent', () => {
4444
expect(app).toBeTruthy();
4545
}));
4646

47-
it('should have title "With Ajax"', waitForAsync(() => {
47+
it('should have title "Quickstart"', waitForAsync(() => {
4848
const app = fixture.debugElement.componentInstance as WithAjaxComponent;
49-
expect(app.pageTitle).toBe('With Ajax');
49+
expect(app.pageTitle).toBe('Quickstart');
5050
}));
5151

5252
it('should have table populated via AJAX', async () => {

0 commit comments

Comments
 (0)