Skip to content

Commit ec43ff2

Browse files
committed
docs(material/list): add harness example for list (#21450)
(cherry picked from commit 15c4175)
1 parent 10f1115 commit ec43ff2

File tree

5 files changed

+100
-0
lines changed

5 files changed

+100
-0
lines changed

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

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

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,18 @@ import {ListOverviewExample} from './list-overview/list-overview-example';
66
import {ListSectionsExample} from './list-sections/list-sections-example';
77
import {ListSelectionExample} from './list-selection/list-selection-example';
88
import {ListSingleSelectionExample} from './list-single-selection/list-single-selection-example';
9+
import {ListHarnessExample} from './list-harness/list-harness-example';
910

1011
export {
12+
ListHarnessExample,
1113
ListOverviewExample,
1214
ListSectionsExample,
1315
ListSelectionExample,
1416
ListSingleSelectionExample,
1517
};
1618

1719
const EXAMPLES = [
20+
ListHarnessExample,
1821
ListOverviewExample,
1922
ListSectionsExample,
2023
ListSelectionExample,
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<mat-list>
2+
<mat-list-item>
3+
<div matLine>Item </div>
4+
<div matLine>1</div>
5+
<div matListIcon>icon</div>
6+
<div matListAvatar>Avatar</div>
7+
</mat-list-item>
8+
<div matSubheader>Section 1</div>
9+
<a mat-list-item>
10+
<span>Item 2</span>
11+
</a>
12+
<button mat-list-item>Item 3</button>
13+
<div matSubheader>Section 2</div>
14+
</mat-list>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import {HarnessLoader, parallel} from '@angular/cdk/testing';
2+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
3+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
4+
import {MatListHarness} from '@angular/material/list/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {MatListModule} from '@angular/material/list';
8+
import {ListHarnessExample} from './list-harness-example';
9+
10+
describe('ListHarnessExample', () => {
11+
let fixture: ComponentFixture<ListHarnessExample>;
12+
let loader: HarnessLoader;
13+
14+
beforeAll(() => {
15+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
16+
});
17+
18+
beforeEach(
19+
waitForAsync(() => {
20+
TestBed.configureTestingModule({
21+
imports: [MatListModule],
22+
declarations: [ListHarnessExample]
23+
}).compileComponents();
24+
fixture = TestBed.createComponent(ListHarnessExample);
25+
fixture.detectChanges();
26+
loader = TestbedHarnessEnvironment.loader(fixture);
27+
})
28+
);
29+
30+
it('should get all items', async () => {
31+
const list = await loader.getHarness(MatListHarness);
32+
const items = await list.getItems();
33+
expect(await parallel(() => items.map(i => i.getText())))
34+
.toEqual(['Item 1', 'Item 2', 'Item 3']);
35+
});
36+
37+
it('should get all items matching text', async () => {
38+
const list = await loader.getHarness(MatListHarness);
39+
const items = await list.getItems({text: /[13]/});
40+
expect(await parallel(() => items.map(i => i.getText()))).toEqual(['Item 1', 'Item 3']);
41+
});
42+
43+
it('should get items by subheader', async () => {
44+
const list = await loader.getHarness(MatListHarness);
45+
const sections = await list.getItemsGroupedBySubheader();
46+
expect(sections.length).toBe(3);
47+
expect(sections[0].heading).toBeUndefined();
48+
expect(await parallel(() => sections[0].items.map(i => i.getText()))).toEqual(['Item 1']);
49+
expect(sections[1].heading).toBe('Section 1');
50+
expect(await parallel(() => sections[1].items.map(i => i.getText())))
51+
.toEqual(['Item 2', 'Item 3']);
52+
expect(sections[2].heading).toBe('Section 2');
53+
expect(sections[2].items.length).toEqual(0);
54+
});
55+
56+
it('should get list item text and lines', async () => {
57+
const list = await loader.getHarness(MatListHarness);
58+
const items = await list.getItems();
59+
expect(items.length).toBe(3);
60+
expect(await items[0].getText()).toBe('Item 1');
61+
expect(await items[0].getLinesText()).toEqual(['Item', '1']);
62+
expect(await items[1].getText()).toBe('Item 2');
63+
expect(await items[1].getLinesText()).toEqual([]);
64+
expect(await items[2].getText()).toBe('Item 3');
65+
expect(await items[2].getLinesText()).toEqual([]);
66+
});
67+
});
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Testing with MatListHarness
5+
*/
6+
@Component({
7+
selector: 'list-harness-example',
8+
templateUrl: 'list-harness-example.html',
9+
})
10+
export class ListHarnessExample {}

0 commit comments

Comments
 (0)