Skip to content

Commit d9342d1

Browse files
committed
docs(material/sort): add harness example for sort (#21469)
(cherry picked from commit 5f9ef44)
1 parent 7390486 commit d9342d1

File tree

5 files changed

+141
-1
lines changed

5 files changed

+141
-1
lines changed

src/components-examples/material/sort/BUILD.bazel

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,13 @@ ng_module(
1111
]),
1212
module_name = "@angular/components-examples/material/sort",
1313
deps = [
14+
"//src/cdk/testing",
15+
"//src/cdk/testing/testbed",
1416
"//src/material/sort",
17+
"//src/material/sort/testing",
18+
"@npm//@angular/platform-browser",
19+
"@npm//@angular/platform-browser-dynamic",
20+
"@npm//@types/jasmine",
1521
],
1622
)
1723

src/components-examples/material/sort/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ import {CommonModule} from '@angular/common';
22
import {NgModule} from '@angular/core';
33
import {MatSortModule} from '@angular/material/sort';
44
import {SortOverviewExample} from './sort-overview/sort-overview-example';
5+
import {SortHarnessExample} from './sort-harness/sort-harness-example';
56

6-
export {SortOverviewExample};
7+
export {SortHarnessExample, SortOverviewExample};
78

89
const EXAMPLES = [
10+
SortHarnessExample,
911
SortOverviewExample,
1012
];
1113

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<table matSort (matSortChange)="sortData($event)">
2+
<tr>
3+
<th mat-sort-header="name">Dessert</th>
4+
<th mat-sort-header="calories">Calories</th>
5+
<th mat-sort-header="fat" [disabled]="disableThirdHeader">Fat</th>
6+
<th mat-sort-header="carbs">Carbs</th>
7+
<th mat-sort-header="protein">Protein</th>
8+
</tr>
9+
10+
<tr *ngFor="let dessert of sortedData">
11+
<td>{{dessert.name}}</td>
12+
<td>{{dessert.calories}}</td>
13+
<td>{{dessert.fat}}</td>
14+
<td>{{dessert.carbs}}</td>
15+
<td>{{dessert.protein}}</td>
16+
</tr>
17+
</table>
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatSortHarness} from '@angular/material/sort/testing';
4+
import {HarnessLoader, parallel} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatSortModule} from '@angular/material/sort';
8+
import {SortHarnessExample} from './sort-harness-example';
9+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
10+
11+
describe('SortHarnessExample', () => {
12+
let fixture: ComponentFixture<SortHarnessExample>;
13+
let loader: HarnessLoader;
14+
15+
beforeAll(() => {
16+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
17+
});
18+
19+
beforeEach(
20+
waitForAsync(() => {
21+
TestBed.configureTestingModule({
22+
imports: [MatSortModule, NoopAnimationsModule],
23+
declarations: [SortHarnessExample]
24+
}).compileComponents();
25+
fixture = TestBed.createComponent(SortHarnessExample);
26+
fixture.detectChanges();
27+
loader = TestbedHarnessEnvironment.loader(fixture);
28+
})
29+
);
30+
31+
it('should load harness for mat-sort', async () => {
32+
const sorts = await loader.getAllHarnesses(MatSortHarness);
33+
expect(sorts.length).toBe(1);
34+
});
35+
36+
it('should be able to filter headers by their sorted state', async () => {
37+
const sort = await loader.getHarness(MatSortHarness);
38+
let headers = await sort.getSortHeaders({sortDirection: ''});
39+
expect(headers.length).toBe(5);
40+
41+
await headers[0].click();
42+
43+
headers = await sort.getSortHeaders({sortDirection: 'asc'});
44+
45+
expect(headers.length).toBe(1);
46+
});
47+
48+
it('should be able to get the label of a header', async () => {
49+
const sort = await loader.getHarness(MatSortHarness);
50+
const headers = await sort.getSortHeaders();
51+
const labels = await parallel(() => headers.map(header => header.getLabel()));
52+
expect(labels).toEqual(['Dessert', 'Calories', 'Fat', 'Carbs', 'Protein']);
53+
});
54+
55+
it('should get the disabled state of a header', async () => {
56+
const sort = await loader.getHarness(MatSortHarness);
57+
const thirdHeader = (await sort.getSortHeaders())[2];
58+
59+
expect(await thirdHeader.isDisabled()).toBe(false);
60+
61+
fixture.componentInstance.disableThirdHeader = true;
62+
fixture.detectChanges();
63+
64+
expect(await thirdHeader.isDisabled()).toBe(true);
65+
});
66+
67+
it('should get the sorted direction of a header', async () => {
68+
const sort = await loader.getHarness(MatSortHarness);
69+
const secondHeader = (await sort.getSortHeaders())[1];
70+
71+
expect(await secondHeader.getSortDirection()).toBe('');
72+
73+
await secondHeader.click();
74+
expect(await secondHeader.getSortDirection()).toBe('asc');
75+
76+
await secondHeader.click();
77+
expect(await secondHeader.getSortDirection()).toBe('desc');
78+
});
79+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import {Component} from '@angular/core';
2+
import {Sort} from '@angular/material/sort';
3+
4+
/**
5+
* @title Testing with MatSortHarness
6+
*/
7+
@Component({
8+
selector: 'sort-harness-example',
9+
templateUrl: 'sort-harness-example.html',
10+
})
11+
export class SortHarnessExample {
12+
disableThirdHeader = false;
13+
desserts = [
14+
{name: 'Frozen yogurt', calories: 159, fat: 6, carbs: 24, protein: 4},
15+
{name: 'Ice cream sandwich', calories: 237, fat: 9, carbs: 37, protein: 4},
16+
{name: 'Eclair', calories: 262, fat: 16, carbs: 24, protein: 6},
17+
{name: 'Cupcake', calories: 305, fat: 4, carbs: 67, protein: 4},
18+
{name: 'Gingerbread', calories: 356, fat: 16, carbs: 49, protein: 4},
19+
];
20+
21+
sortedData = this.desserts.slice();
22+
23+
sortData(sort: Sort) {
24+
const data = this.desserts.slice();
25+
26+
if (!sort.active || sort.direction === '') {
27+
this.sortedData = data;
28+
} else {
29+
this.sortedData = data.sort((a, b) => {
30+
const aValue = (a as any)[sort.active];
31+
const bValue = (b as any)[sort.active];
32+
return (aValue < bValue ? -1 : 1) * (sort.direction === 'asc' ? 1 : -1);
33+
});
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)