-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathsort.spec.ts
703 lines (592 loc) · 24.7 KB
/
sort.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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
import {CollectionViewer, DataSource} from '@angular/cdk/collections';
import {CdkTableModule} from '@angular/cdk/table';
import {dispatchMouseEvent, wrappedErrorMessage} from '@angular/cdk/testing/private';
import {Component, ElementRef, ViewChild, inject} from '@angular/core';
import {ComponentFixture, TestBed, fakeAsync, tick, waitForAsync} from '@angular/core/testing';
import {MatTableModule} from '../table';
import {By} from '@angular/platform-browser';
import {Observable} from 'rxjs';
import {map} from 'rxjs/operators';
import {
MAT_SORT_DEFAULT_OPTIONS,
MatSort,
MatSortHeader,
MatSortModule,
Sort,
SortDirection,
} from './index';
import {
getSortDuplicateSortableIdError,
getSortHeaderMissingIdError,
getSortHeaderNotContainedWithinSortError,
getSortInvalidDirectionError,
} from './sort-errors';
describe('MatSort', () => {
describe('without default options', () => {
let fixture: ComponentFixture<SimpleMatSortApp>;
let component: SimpleMatSortApp;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [
MatSortModule,
MatTableModule,
CdkTableModule,
SimpleMatSortApp,
CdkTableMatSortApp,
MatTableMatSortApp,
MatSortHeaderMissingMatSortApp,
MatSortDuplicateMatSortableIdsApp,
MatSortableMissingIdApp,
MatSortableInvalidDirection,
MatSortableInvalidDirection,
MatSortWithArrowPosition,
],
});
}));
beforeEach(() => {
fixture = TestBed.createComponent(SimpleMatSortApp);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should have the sort headers register and deregister themselves', () => {
const sortables = component.matSort.sortables;
expect(sortables.size).toBe(4);
expect(sortables.get('defaultA')).toBe(component.defaultA);
expect(sortables.get('defaultB')).toBe(component.defaultB);
fixture.destroy();
expect(sortables.size).toBe(0);
});
it('should mark itself as initialized', fakeAsync(() => {
let isMarkedInitialized = false;
component.matSort.initialized.subscribe(() => (isMarkedInitialized = true));
tick();
expect(isMarkedInitialized).toBeTruthy();
}));
it('should use the column definition if used within a cdk table', () => {
const cdkTableMatSortAppFixture = TestBed.createComponent(CdkTableMatSortApp);
const cdkTableMatSortAppComponent = cdkTableMatSortAppFixture.componentInstance;
cdkTableMatSortAppFixture.detectChanges();
const sortables = cdkTableMatSortAppComponent.matSort.sortables;
expect(sortables.size).toBe(3);
expect(sortables.has('column_a')).toBe(true);
expect(sortables.has('column_b')).toBe(true);
expect(sortables.has('column_c')).toBe(true);
});
it('should use the column definition if used within an mat table', () => {
const matTableMatSortAppFixture = TestBed.createComponent(MatTableMatSortApp);
const matTableMatSortAppComponent = matTableMatSortAppFixture.componentInstance;
matTableMatSortAppFixture.detectChanges();
const sortables = matTableMatSortAppComponent.matSort.sortables;
expect(sortables.size).toBe(3);
expect(sortables.has('column_a')).toBe(true);
expect(sortables.has('column_b')).toBe(true);
expect(sortables.has('column_c')).toBe(true);
});
it('should be able to cycle from asc -> desc from either start point', () => {
component.disableClear = true;
component.start = 'asc';
fixture.changeDetectorRef.markForCheck();
testSingleColumnSortDirectionSequence(fixture, ['asc', 'desc']);
// Reverse directions
component.start = 'desc';
fixture.changeDetectorRef.markForCheck();
testSingleColumnSortDirectionSequence(fixture, ['desc', 'asc']);
});
it('should be able to cycle asc -> desc -> [none]', () => {
component.start = 'asc';
fixture.changeDetectorRef.markForCheck();
testSingleColumnSortDirectionSequence(fixture, ['asc', 'desc', '']);
});
it('should be able to cycle desc -> asc -> [none]', () => {
component.start = 'desc';
fixture.changeDetectorRef.markForCheck();
testSingleColumnSortDirectionSequence(fixture, ['desc', 'asc', '']);
});
it('should allow for the cycling the sort direction to be disabled per column', () => {
const container = fixture.nativeElement.querySelector('#defaultA .mat-sort-header-container');
component.sort('defaultA');
expect(component.matSort.direction).toBe('asc');
expect(container.getAttribute('tabindex')).toBe('0');
expect(container.getAttribute('role')).toBe('button');
component.disabledColumnSort = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
component.sort('defaultA');
expect(component.matSort.direction).toBe('asc');
expect(container.hasAttribute('tabindex')).toBe(false);
expect(container.hasAttribute('role')).toBe(false);
});
it('should allow for the cycling the sort direction to be disabled for all columns', () => {
const container = fixture.nativeElement.querySelector('#defaultA .mat-sort-header-container');
component.sort('defaultA');
expect(component.matSort.active).toBe('defaultA');
expect(component.matSort.direction).toBe('asc');
expect(container.getAttribute('tabindex')).toBe('0');
component.disableAllSort = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
component.sort('defaultA');
expect(component.matSort.active).toBe('defaultA');
expect(component.matSort.direction).toBe('asc');
expect(container.getAttribute('tabindex')).toBeFalsy();
component.sort('defaultB');
expect(component.matSort.active).toBe('defaultA');
expect(component.matSort.direction).toBe('asc');
expect(container.getAttribute('tabindex')).toBeFalsy();
});
it('should reset sort direction when a different column is sorted', () => {
component.sort('defaultA');
expect(component.matSort.active).toBe('defaultA');
expect(component.matSort.direction).toBe('asc');
component.sort('defaultA');
expect(component.matSort.active).toBe('defaultA');
expect(component.matSort.direction).toBe('desc');
component.sort('defaultB');
expect(component.matSort.active).toBe('defaultB');
expect(component.matSort.direction).toBe('asc');
});
it(
'should throw an error if an MatSortable is not contained within an MatSort ' + 'directive',
() => {
expect(() =>
TestBed.createComponent(MatSortHeaderMissingMatSortApp).detectChanges(),
).toThrowError(wrappedErrorMessage(getSortHeaderNotContainedWithinSortError()));
},
);
it('should throw an error if two MatSortables have the same id', () => {
expect(() =>
TestBed.createComponent(MatSortDuplicateMatSortableIdsApp).detectChanges(),
).toThrowError(wrappedErrorMessage(getSortDuplicateSortableIdError('duplicateId')));
});
it('should throw an error if an MatSortable is missing an id', () => {
expect(() => TestBed.createComponent(MatSortableMissingIdApp).detectChanges()).toThrowError(
wrappedErrorMessage(getSortHeaderMissingIdError()),
);
});
it('should throw an error if the provided direction is invalid', () => {
expect(() =>
TestBed.createComponent(MatSortableInvalidDirection).detectChanges(),
).toThrowError(wrappedErrorMessage(getSortInvalidDirectionError('ascending')));
});
it('should allow let MatSortable override the default sort parameters', () => {
testSingleColumnSortDirectionSequence(fixture, ['asc', 'desc', '']);
testSingleColumnSortDirectionSequence(fixture, ['desc', 'asc', ''], 'overrideStart');
testSingleColumnSortDirectionSequence(fixture, ['asc', 'desc'], 'overrideDisableClear');
});
it('should apply the aria-sort label to the header when sorted', () => {
const sortHeaderElement = fixture.nativeElement.querySelector('#defaultA');
expect(sortHeaderElement.getAttribute('aria-sort')).toBe('none');
component.sort('defaultA');
fixture.detectChanges();
expect(sortHeaderElement.getAttribute('aria-sort')).toBe('ascending');
component.sort('defaultA');
fixture.detectChanges();
expect(sortHeaderElement.getAttribute('aria-sort')).toBe('descending');
component.sort('defaultA');
fixture.detectChanges();
expect(sortHeaderElement.getAttribute('aria-sort')).toBe('none');
});
it('should not render the arrow if sorting is disabled for that column', fakeAsync(() => {
const sortHeaderElement = fixture.nativeElement.querySelector('#defaultA');
// Switch sorting to a different column before asserting.
component.sort('defaultB');
fixture.componentInstance.disabledColumnSort = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(sortHeaderElement.querySelector('.mat-sort-header-arrow')).toBeFalsy();
}));
it('should render the arrow if a disabled column is being sorted by', fakeAsync(() => {
const sortHeaderElement = fixture.nativeElement.querySelector('#defaultA');
component.sort('defaultA');
fixture.componentInstance.disabledColumnSort = true;
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
tick();
fixture.detectChanges();
expect(sortHeaderElement.querySelector('.mat-sort-header-arrow')).toBeTruthy();
}));
it('should have a focus indicator', () => {
const headerNativeElement = fixture.debugElement.query(
By.directive(MatSortHeader),
)!.nativeElement;
const container = headerNativeElement.querySelector('.mat-sort-header-container');
expect(container.classList.contains('mat-focus-indicator')).toBe(true);
});
it('should add a default aria description to sort buttons', () => {
const sortButton = fixture.nativeElement.querySelector('.mat-sort-header-container');
const descriptionId = sortButton.getAttribute('aria-describedby');
expect(descriptionId).toBeDefined();
const descriptionElement = document.getElementById(descriptionId);
expect(descriptionElement?.textContent).toBe('Sort');
});
it('should add a custom aria description to sort buttons', () => {
const sortButton = fixture.nativeElement.querySelector(
'#defaultB .mat-sort-header-container',
);
let descriptionId = sortButton.getAttribute('aria-describedby');
expect(descriptionId).toBeDefined();
let descriptionElement = document.getElementById(descriptionId);
expect(descriptionElement?.textContent).toBe('Sort second column');
fixture.componentInstance.secondColumnDescription = 'Sort 2nd column';
fixture.changeDetectorRef.markForCheck();
fixture.detectChanges();
descriptionId = sortButton.getAttribute('aria-describedby');
descriptionElement = document.getElementById(descriptionId);
expect(descriptionElement?.textContent).toBe('Sort 2nd column');
});
it('should render arrows after sort header by default', () => {
const matSortWithArrowPositionFixture = TestBed.createComponent(MatSortWithArrowPosition);
matSortWithArrowPositionFixture.detectChanges();
const containerA = matSortWithArrowPositionFixture.nativeElement.querySelector(
'#defaultA .mat-sort-header-container',
);
const containerB = matSortWithArrowPositionFixture.nativeElement.querySelector(
'#defaultB .mat-sort-header-container',
);
expect(containerA.classList.contains('mat-sort-header-position-before')).toBe(false);
expect(containerB.classList.contains('mat-sort-header-position-before')).toBe(false);
});
it('should render arrows before if appropriate parameter passed', () => {
const matSortWithArrowPositionFixture = TestBed.createComponent(MatSortWithArrowPosition);
const matSortWithArrowPositionComponent = matSortWithArrowPositionFixture.componentInstance;
matSortWithArrowPositionComponent.arrowPosition = 'before';
matSortWithArrowPositionFixture.detectChanges();
const containerA = matSortWithArrowPositionFixture.nativeElement.querySelector(
'#defaultA .mat-sort-header-container',
);
const containerB = matSortWithArrowPositionFixture.nativeElement.querySelector(
'#defaultB .mat-sort-header-container',
);
expect(containerA.classList.contains('mat-sort-header-position-before')).toBe(true);
expect(containerB.classList.contains('mat-sort-header-position-before')).toBe(true);
});
it('should render arrows in proper position based on arrowPosition parameter', () => {
const matSortWithArrowPositionFixture = TestBed.createComponent(MatSortWithArrowPosition);
const matSortWithArrowPositionComponent = matSortWithArrowPositionFixture.componentInstance;
matSortWithArrowPositionFixture.detectChanges();
const containerA = matSortWithArrowPositionFixture.nativeElement.querySelector(
'#defaultA .mat-sort-header-container',
);
const containerB = matSortWithArrowPositionFixture.nativeElement.querySelector(
'#defaultB .mat-sort-header-container',
);
expect(containerA.classList.contains('mat-sort-header-position-before')).toBe(false);
expect(containerB.classList.contains('mat-sort-header-position-before')).toBe(false);
matSortWithArrowPositionComponent.arrowPosition = 'before';
matSortWithArrowPositionFixture.changeDetectorRef.markForCheck();
matSortWithArrowPositionFixture.detectChanges();
expect(containerA.classList.contains('mat-sort-header-position-before')).toBe(true);
expect(containerB.classList.contains('mat-sort-header-position-before')).toBe(true);
});
});
describe('with default options', () => {
let fixture: ComponentFixture<MatSortWithoutExplicitInputs>;
let component: MatSortWithoutExplicitInputs;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatSortModule, MatTableModule, CdkTableModule, MatSortWithoutExplicitInputs],
providers: [
{
provide: MAT_SORT_DEFAULT_OPTIONS,
useValue: {
disableClear: true,
},
},
],
});
}));
beforeEach(() => {
fixture = TestBed.createComponent(MatSortWithoutExplicitInputs);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should be able to cycle from asc -> desc from either start point', () => {
component.start = 'asc';
testSingleColumnSortDirectionSequence(fixture, ['asc', 'desc']);
// Reverse directions
component.start = 'desc';
testSingleColumnSortDirectionSequence(fixture, ['desc', 'asc']);
});
});
describe('with default arrowPosition', () => {
let fixture: ComponentFixture<MatSortWithoutInputs>;
beforeEach(waitForAsync(() => {
TestBed.configureTestingModule({
imports: [MatSortModule, MatTableModule, CdkTableModule, MatSortWithoutInputs],
providers: [
{
provide: MAT_SORT_DEFAULT_OPTIONS,
useValue: {
disableClear: true,
arrowPosition: 'before',
},
},
],
});
}));
beforeEach(() => {
fixture = TestBed.createComponent(MatSortWithoutInputs);
fixture.detectChanges();
});
it('should render arrows in proper position', () => {
const containerA = fixture.nativeElement.querySelector(
'#defaultA .mat-sort-header-container',
);
const containerB = fixture.nativeElement.querySelector(
'#defaultB .mat-sort-header-container',
);
expect(containerA.classList.contains('mat-sort-header-position-before')).toBe(true);
expect(containerB.classList.contains('mat-sort-header-position-before')).toBe(true);
});
});
});
/**
* Performs a sequence of sorting on a single column to see if the sort directions are
* consistent with expectations. Detects any changes in the fixture to reflect any changes in
* the inputs and resets the MatSort to remove any side effects from previous tests.
*/
function testSingleColumnSortDirectionSequence(
fixture: ComponentFixture<SimpleMatSortApp | MatSortWithoutExplicitInputs>,
expectedSequence: SortDirection[],
id: SimpleMatSortAppColumnIds = 'defaultA',
) {
// Detect any changes that were made in preparation for this sort sequence
fixture.detectChanges();
// Reset the sort to make sure there are no side affects from previous tests
const component = fixture.componentInstance;
component.matSort.active = '';
component.matSort.direction = '';
// Run through the sequence to confirm the order
const actualSequence = expectedSequence.map(() => {
component.sort(id);
// Check that the sort event's active sort is consistent with the MatSort
expect(component.matSort.active).toBe(id);
expect(component.latestSortEvent.active).toBe(id);
// Check that the sort event's direction is consistent with the MatSort
expect(component.matSort.direction).toBe(component.latestSortEvent.direction);
return component.matSort.direction;
});
expect(actualSequence).toEqual(expectedSequence);
// Expect that performing one more sort will loop it back to the beginning.
component.sort(id);
expect(component.matSort.direction).toBe(expectedSequence[0]);
}
/** Column IDs of the SimpleMatSortApp for typing of function params in the component (e.g. sort) */
type SimpleMatSortAppColumnIds = 'defaultA' | 'defaultB' | 'overrideStart' | 'overrideDisableClear';
@Component({
template: `
<div matSort
[matSortActive]="active"
[matSortDisabled]="disableAllSort"
[matSortStart]="start"
[matSortDirection]="direction"
[matSortDisableClear]="disableClear"
(matSortChange)="latestSortEvent = $event">
<div id="defaultA"
#defaultA
mat-sort-header="defaultA"
[disabled]="disabledColumnSort">
A
</div>
<div id="defaultB"
#defaultB
mat-sort-header="defaultB"
[sortActionDescription]="secondColumnDescription">
B
</div>
<div id="overrideStart"
#overrideStart
mat-sort-header="overrideStart" start="desc">
D
</div>
<div id="overrideDisableClear"
#overrideDisableClear
mat-sort-header="overrideDisableClear"
disableClear>
E
</div>
</div>
`,
imports: [MatSortModule, MatTableModule, CdkTableModule],
})
class SimpleMatSortApp {
elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
latestSortEvent: Sort;
active: string;
start: SortDirection = 'asc';
direction: SortDirection = '';
disableClear: boolean;
disabledColumnSort = false;
disableAllSort = false;
secondColumnDescription = 'Sort second column';
@ViewChild(MatSort) matSort: MatSort;
@ViewChild('defaultA') defaultA: MatSortHeader;
@ViewChild('defaultB') defaultB: MatSortHeader;
@ViewChild('overrideStart') overrideStart: MatSortHeader;
@ViewChild('overrideDisableClear') overrideDisableClear: MatSortHeader;
sort(id: SimpleMatSortAppColumnIds) {
this.dispatchMouseEvent(id, 'click');
}
dispatchMouseEvent(id: SimpleMatSortAppColumnIds, event: string) {
const sortElement = this.elementRef.nativeElement.querySelector(`#${id}`)!;
dispatchMouseEvent(sortElement, event);
}
}
class FakeDataSource extends DataSource<never> {
connect(collectionViewer: CollectionViewer): Observable<never[]> {
return collectionViewer.viewChange.pipe(map(() => []));
}
disconnect() {}
}
@Component({
template: `
<cdk-table [dataSource]="dataSource" matSort>
<ng-container cdkColumnDef="column_a">
<cdk-header-cell *cdkHeaderCellDef #sortHeaderA mat-sort-header> Column A </cdk-header-cell>
<cdk-cell *cdkCellDef="let row"> {{row.a}} </cdk-cell>
</ng-container>
<ng-container cdkColumnDef="column_b">
<cdk-header-cell *cdkHeaderCellDef #sortHeaderB mat-sort-header> Column B </cdk-header-cell>
<cdk-cell *cdkCellDef="let row"> {{row.b}} </cdk-cell>
</ng-container>
<ng-container cdkColumnDef="column_c">
<cdk-header-cell *cdkHeaderCellDef #sortHeaderC mat-sort-header> Column C </cdk-header-cell>
<cdk-cell *cdkCellDef="let row"> {{row.c}} </cdk-cell>
</ng-container>
<cdk-header-row *cdkHeaderRowDef="columnsToRender"></cdk-header-row>
<cdk-row *cdkRowDef="let row; columns: columnsToRender"></cdk-row>
</cdk-table>
`,
imports: [MatSortModule, MatTableModule, CdkTableModule],
})
class CdkTableMatSortApp {
@ViewChild(MatSort) matSort: MatSort;
dataSource = new FakeDataSource();
columnsToRender = ['column_a', 'column_b', 'column_c'];
}
@Component({
template: `
<mat-table [dataSource]="dataSource" matSort>
<ng-container matColumnDef="column_a">
<mat-header-cell *matHeaderCellDef #sortHeaderA mat-sort-header> Column A </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.a}} </mat-cell>
</ng-container>
<ng-container matColumnDef="column_b">
<mat-header-cell *matHeaderCellDef #sortHeaderB mat-sort-header> Column B </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.b}} </mat-cell>
</ng-container>
<ng-container matColumnDef="column_c">
<mat-header-cell *matHeaderCellDef #sortHeaderC mat-sort-header> Column C </mat-header-cell>
<mat-cell *matCellDef="let row"> {{row.c}} </mat-cell>
</ng-container>
<mat-header-row *matHeaderRowDef="columnsToRender"></mat-header-row>
<mat-row *matRowDef="let row; columns: columnsToRender"></mat-row>
</mat-table>
`,
imports: [MatSortModule, MatTableModule, CdkTableModule],
})
class MatTableMatSortApp {
@ViewChild(MatSort) matSort: MatSort;
dataSource = new FakeDataSource();
columnsToRender = ['column_a', 'column_b', 'column_c'];
}
@Component({
template: `<div mat-sort-header="a"> A </div>`,
imports: [MatSortModule, MatTableModule, CdkTableModule],
})
class MatSortHeaderMissingMatSortApp {}
@Component({
template: `
<div matSort>
<div mat-sort-header="duplicateId"> A </div>
<div mat-sort-header="duplicateId"> A </div>
</div>
`,
imports: [MatSortModule, MatTableModule, CdkTableModule],
})
class MatSortDuplicateMatSortableIdsApp {}
@Component({
template: `
<div matSort>
<div mat-sort-header> A </div>
</div>
`,
imports: [MatSortModule, MatTableModule, CdkTableModule],
})
class MatSortableMissingIdApp {}
@Component({
template: `
<div matSort matSortDirection="ascending">
<div mat-sort-header="a"> A </div>
</div>
`,
imports: [MatSortModule, MatTableModule, CdkTableModule],
})
class MatSortableInvalidDirection {}
@Component({
template: `
<div matSort
[matSortActive]="active"
[matSortStart]="start"
(matSortChange)="latestSortEvent = $event">
<div id="defaultA" #defaultA mat-sort-header="defaultA">
A
</div>
</div>
`,
imports: [MatSortModule, MatTableModule, CdkTableModule],
})
class MatSortWithoutExplicitInputs {
elementRef = inject<ElementRef<HTMLElement>>(ElementRef);
latestSortEvent: Sort;
active: string;
start: SortDirection = 'asc';
@ViewChild(MatSort) matSort: MatSort;
@ViewChild('defaultA') defaultA: MatSortHeader;
sort(id: SimpleMatSortAppColumnIds) {
this.dispatchMouseEvent(id, 'click');
}
dispatchMouseEvent(id: SimpleMatSortAppColumnIds, event: string) {
const sortElement = this.elementRef.nativeElement.querySelector(`#${id}`)!;
dispatchMouseEvent(sortElement, event);
}
}
@Component({
template: `
<div matSort>
<div id="defaultA" #defaultA mat-sort-header="defaultA" [arrowPosition]="arrowPosition">
A
</div>
<div id="defaultB" #defaultB mat-sort-header="defaultB" [arrowPosition]="arrowPosition">
B
</div>
</div>
`,
imports: [MatSortModule, MatTableModule, CdkTableModule],
})
class MatSortWithArrowPosition {
arrowPosition?: 'before' | 'after';
@ViewChild(MatSort) matSort: MatSort;
@ViewChild('defaultA') defaultA: MatSortHeader;
@ViewChild('defaultB') defaultB: MatSortHeader;
}
@Component({
template: `
<div matSort>
<div id="defaultA" #defaultA mat-sort-header="defaultA">
A
</div>
<div id="defaultB" #defaultB mat-sort-header="defaultB">
B
</div>
</div>
`,
imports: [MatSortModule, MatTableModule, CdkTableModule],
})
class MatSortWithoutInputs {
@ViewChild(MatSort) matSort: MatSort;
@ViewChild('defaultA') defaultA: MatSortHeader;
@ViewChild('defaultB') defaultB: MatSortHeader;
}