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

Commit 9fd5e38

Browse files
committed
feat: UsingNgTemplateRef update
- send dtOptions to trigger event - add unit tests
1 parent b633d5b commit 9fd5e38

File tree

2 files changed

+82
-1
lines changed

2 files changed

+82
-1
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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 { UpperCasePipe } from '@angular/common';
13+
import { By } from '@angular/platform-browser';
14+
import { UsingNgTemplateRefComponent } from './using-ng-template-ref.component';
15+
import { DemoNgComponent } from './demo-ng-template-ref.component';
16+
17+
18+
let fixture: ComponentFixture<UsingNgTemplateRefComponent>, component: UsingNgTemplateRefComponent = null;
19+
20+
describe('UsingNgTemplateRefComponent', () => {
21+
beforeEach(() => {
22+
fixture = TestBed.configureTestingModule({
23+
declarations: [
24+
BaseDemoComponent,
25+
UsingNgTemplateRefComponent,
26+
DemoNgComponent,
27+
DataTableDirective
28+
],
29+
imports: [
30+
AppRoutingModule,
31+
RouterTestingModule,
32+
DataTablesModule.forRoot(),
33+
HttpClientModule,
34+
MarkdownModule.forRoot(
35+
{
36+
sanitize: SecurityContext.NONE
37+
}
38+
),
39+
FormsModule
40+
],
41+
schemas: [NO_ERRORS_SCHEMA],
42+
providers: [
43+
UpperCasePipe
44+
]
45+
}).createComponent(UsingNgTemplateRefComponent);
46+
47+
component = fixture.componentInstance;
48+
49+
fixture.detectChanges(); // initial binding
50+
});
51+
52+
it('should create the app', waitForAsync(() => {
53+
const app = fixture.debugElement.componentInstance;
54+
expect(app).toBeTruthy();
55+
}));
56+
57+
it('should have title "Using Angular TemplateRef"', waitForAsync(() => {
58+
const app = fixture.debugElement.componentInstance as UsingNgTemplateRefComponent;
59+
expect(app.pageTitle).toBe('Using Angular TemplateRef');
60+
}));
61+
62+
it('should have firstName, lastName columns have text in uppercase', async () => {
63+
const app = fixture.debugElement.componentInstance as UsingNgTemplateRefComponent;
64+
await fixture.whenStable();
65+
66+
const query = fixture.debugElement.query(By.directive(DataTableDirective));
67+
const dir = query.injector.get(DataTableDirective);
68+
expect(dir).toBeTruthy();
69+
70+
expect(app.message).toBe('');
71+
72+
const row: HTMLTableRowElement = fixture.nativeElement.querySelector('tbody tr:first-child');
73+
const button: HTMLButtonElement = row.querySelector('button.btn-sm');
74+
button.click();
75+
76+
expect(app.message).toBe(`Event 'action1' with data '{}`);
77+
78+
});
79+
80+
});

demo/src/app/advanced/using-ng-template-ref.component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ export class UsingNgTemplateRefComponent implements OnInit, AfterViewInit {
6060

6161
// wait before loading table
6262
setTimeout(() => {
63-
this.dtTrigger.next();
63+
// fix: unit test fails if dtOptions isn't sent with dtTrigger
64+
this.dtTrigger.next(this.dtOptions);
6465
}, 200);
6566
}
6667

0 commit comments

Comments
 (0)