/
th.spec.ts
256 lines (242 loc) · 9.5 KB
/
th.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import { ApplicationRef, Component, DebugElement, ViewChild } from '@angular/core';
import { ComponentFixture, fakeAsync, TestBed } from '@angular/core/testing';
import { By } from '@angular/platform-browser';
import { NoopAnimationsModule } from '@angular/platform-browser/animations';
import { NzSafeAny } from 'ng-zorro-antd/core/types';
import { NzIconTestModule } from 'ng-zorro-antd/icon/testing';
import { NzThAddOnComponent } from '../cell/th-addon.component';
import { NzTableModule } from '../table.module';
describe('nz-th', () => {
beforeEach(fakeAsync(() => {
TestBed.configureTestingModule({
imports: [NzTableModule, NzIconTestModule, NoopAnimationsModule],
declarations: [NzThTestNzTableComponent, NzThTestTableDefaultFilterComponent]
});
TestBed.compileComponents();
}));
describe('nz-th addon in nz-table', () => {
let fixture: ComponentFixture<NzThTestNzTableComponent>;
let testComponent: NzThTestNzTableComponent;
let th: DebugElement;
beforeEach(() => {
fixture = TestBed.createComponent(NzThTestNzTableComponent);
fixture.detectChanges();
testComponent = fixture.debugElement.componentInstance;
th = fixture.debugElement.query(By.directive(NzThAddOnComponent));
});
it('should showSort work', () => {
fixture.detectChanges();
expect(th.nativeElement.classList).toContain('ant-table-column-has-sorters');
expect(th.nativeElement.querySelector('.ant-table-column-sorter')).toBeDefined();
});
it('should sort change work', () => {
fixture.detectChanges();
expect(th.nativeElement.querySelector('.ant-table-column-sorter-up').classList).not.toContain('active');
expect(th.nativeElement.querySelector('.ant-table-column-sorter-down').classList).not.toContain('active');
expect(th.nativeElement.classList).not.toContain('ant-table-column-sort');
testComponent.sort = 'ascend';
fixture.detectChanges();
expect(th.nativeElement.querySelector('.ant-table-column-sorter-up').classList).toContain('active');
expect(th.nativeElement.querySelector('.ant-table-column-sorter-down').classList).not.toContain('active');
expect(th.nativeElement.classList).toContain('ant-table-column-sort');
testComponent.sort = 'descend';
fixture.detectChanges();
expect(th.nativeElement.querySelector('.ant-table-column-sorter-up').classList).not.toContain('active');
expect(th.nativeElement.querySelector('.ant-table-column-sorter-down').classList).toContain('active');
expect(th.nativeElement.classList).toContain('ant-table-column-sort');
testComponent.sort = null;
fixture.detectChanges();
expect(th.nativeElement.querySelector('.ant-table-column-sorter-up').classList).not.toContain('active');
expect(th.nativeElement.querySelector('.ant-table-column-sorter-down').classList).not.toContain('active');
expect(th.nativeElement.classList).not.toContain('ant-table-column-sort');
expect(testComponent.sortChange).toHaveBeenCalledTimes(0);
});
it('should sort click work', () => {
fixture.detectChanges();
expect(th.nativeElement.querySelector('.ant-table-column-sorter-up').classList).not.toContain('active');
expect(th.nativeElement.querySelector('.ant-table-column-sorter-down').classList).not.toContain('active');
th.nativeElement.querySelector('.ant-table-column-sorters').firstElementChild.click();
fixture.detectChanges();
expect(testComponent.sort).toBe('ascend');
expect(testComponent.sortChange).toHaveBeenCalledTimes(1);
expect(th.nativeElement.querySelector('.ant-table-column-sorter-up').classList).toContain('active');
expect(th.nativeElement.querySelector('.ant-table-column-sorter-down').classList).not.toContain('active');
th.nativeElement.querySelector('.ant-table-column-sorters').firstElementChild.click();
fixture.detectChanges();
expect(testComponent.sortChange).toHaveBeenCalledTimes(2);
expect(testComponent.sort).toBe('descend');
expect(th.nativeElement.querySelector('.ant-table-column-sorter-up').classList).not.toContain('active');
expect(th.nativeElement.querySelector('.ant-table-column-sorter-down').classList).toContain('active');
th.nativeElement.querySelector('.ant-table-column-sorters').firstElementChild.click();
fixture.detectChanges();
expect(testComponent.sortChange).toHaveBeenCalledTimes(3);
expect(testComponent.sort).toBe(null);
});
it('should left work', () => {
testComponent.left = '20px';
fixture.detectChanges();
expect(th.nativeElement.classList).toContain('ant-table-cell-fix-left');
expect(th.nativeElement.style.left).toBe('20px');
});
it('should right work', () => {
testComponent.right = '20px';
fixture.detectChanges();
expect(th.nativeElement.classList).toContain('ant-table-cell-fix-right');
expect(th.nativeElement.style.right).toBe('20px');
});
it('should be throw error when use specific class name', () => {
expect(() => {
TestBed.configureTestingModule({
declarations: [NzTestDisableThComponent]
}).createComponent(NzTestDisableThComponent);
}).toThrow();
});
it('should not run change detection on click events for the `nz-filter-trigger`', () => {
const appRef = TestBed.inject(ApplicationRef);
const event = new MouseEvent('click');
spyOn(appRef, 'tick');
spyOn(event, 'stopPropagation').and.callThrough();
fixture.debugElement.nativeElement
.querySelector('nz-filter-trigger .ant-table-filter-trigger')
.dispatchEvent(event);
expect(appRef.tick).not.toHaveBeenCalled();
expect(event.stopPropagation).toHaveBeenCalled();
});
});
});
@Component({
template: `
<nz-table *ngIf="!destroy">
<th
[nzLeft]="left"
[nzRight]="right"
[nzWidth]="width"
[(nzSortOrder)]="sort"
(nzSortOrderChange)="sortChange($event)"
[nzFilters]="filters"
(nzFilterChange)="filterChange($event)"
[nzFilterMultiple]="filterMultiple"
></th>
</nz-table>
`
})
export class NzThTestNzTableComponent {
@ViewChild(NzThAddOnComponent, { static: false }) nzThComponent!: NzThAddOnComponent<NzSafeAny>;
destroy = false;
left?: string | number;
right?: string | number;
width?: string | number;
sort: string | null = null;
sortChange = jasmine.createSpy('sort change');
filters = [
{ text: 'filter1', value: '1' },
{ text: 'filter2', value: '2' }
];
filterChange = jasmine.createSpy('filter change');
filterMultiple = true;
}
interface DataItem {
name: string;
age: number;
address: string;
}
@Component({
template: `
<nz-table #filterTable [nzData]="displayData">
<thead (nzSortOrderChange)="sort($event)">
<tr>
<th nzColumnKey="name" [nzFilters]="nameList" (nzFilterChange)="filter($event, searchAddress)">Name</th>
<th nzColumnKey="age">Age</th>
<th
nzColumnKey="address"
[nzFilterMultiple]="false"
[nzFilters]="addressList"
(nzFilterChange)="filter(listOfSearchName, $event)"
>
Address
</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of filterTable.data">
<td>{{ data.name }}</td>
<td>{{ data.age }}</td>
<td>{{ data.address }}</td>
</tr>
</tbody>
</nz-table>
`
})
export class NzThTestTableDefaultFilterComponent {
nameList = [
{ text: 'Joe', value: 'Joe', byDefault: true },
{ text: 'Jim', value: 'Jim' }
];
addressList = [
{ text: 'London', value: 'London', byDefault: true },
{ text: 'Sidney', value: 'Sidney' }
];
sortName: keyof DataItem | null = null;
sortValue: string | null = null;
listOfSearchName = ['Joe', 'London'];
searchAddress!: string;
data: DataItem[] = [
{
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park'
},
{
name: 'Jim Green',
age: 42,
address: 'London No. 1 Lake Park'
},
{
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park'
},
{
name: 'Jim Red',
age: 32,
address: 'London No. 2 Lake Park'
}
];
displayData: DataItem[] = [];
@ViewChild(NzThAddOnComponent, { static: false }) nzThComponent!: NzThAddOnComponent<DataItem>;
sort(sort: { key: keyof DataItem; value: string }): void {
this.sortName = sort.key;
this.sortValue = sort.value;
this.search();
}
filter(listOfSearchName: string[], searchAddress: string): void {
this.listOfSearchName = listOfSearchName;
this.searchAddress = searchAddress;
this.search();
}
search(): void {
/** filter data **/
const filterFunc = (item: { name: string; address: string; age: number }): boolean =>
(this.searchAddress ? item.address.indexOf(this.searchAddress) !== -1 : true) &&
(this.listOfSearchName.length ? this.listOfSearchName.some(name => item.name.indexOf(name) !== -1) : true);
const data = this.data.filter(item => filterFunc(item));
/** sort data **/
if (this.sortName && this.sortValue) {
this.displayData = data.sort((a: DataItem, b: DataItem) =>
this.sortValue === 'ascend'
? a[this.sortName!] > b[this.sortName!]
? 1
: -1
: b[this.sortName!] > a[this.sortName!]
? 1
: -1
);
} else {
this.displayData = data;
}
}
}
@Component({
template: ` <th class="nz-disable-th"></th> `
})
export class NzTestDisableThComponent {}