Skip to content

Commit 065aae3

Browse files
annieywmmalerba
authored andcommitted
docs(material/chips): add chips harness example (#20942)
(cherry picked from commit 554c775)
1 parent c374c98 commit 065aae3

File tree

5 files changed

+93
-0
lines changed

5 files changed

+93
-0
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,17 @@ ng_module(
1212
module_name = "@angular/components-examples/material/chips",
1313
deps = [
1414
"//src/cdk/drag-drop",
15+
"//src/cdk/testing",
16+
"//src/cdk/testing/testbed",
1517
"//src/material/autocomplete",
1618
"//src/material/chips",
19+
"//src/material/chips/testing",
1720
"//src/material/form-field",
1821
"//src/material/icon",
1922
"@npm//@angular/forms",
23+
"@npm//@angular/platform-browser",
24+
"@npm//@angular/platform-browser-dynamic",
25+
"@npm//@types/jasmine",
2026
],
2127
)
2228

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<mat-chip-list
2+
[disabled]="isDisabled"
3+
[aria-orientation]="'horizontal'">
4+
<mat-chip (removed)="remove()">Chip 1</mat-chip>
5+
<mat-chip (removed)="remove()">Chip 2 <span matChipRemove>remove_icon</span></mat-chip>
6+
<mat-chip (removed)="remove()"><mat-chip-avatar>C</mat-chip-avatar>Chip 4</mat-chip>
7+
</mat-chip-list>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import {TestBed, ComponentFixture, waitForAsync} from '@angular/core/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk/testing/testbed';
3+
import {MatChipHarness, MatChipListHarness} from '@angular/material/chips/testing';
4+
import {HarnessLoader} from '@angular/cdk/testing';
5+
import {BrowserDynamicTestingModule, platformBrowserDynamicTesting}
6+
from '@angular/platform-browser-dynamic/testing';
7+
import {ChipsHarnessExample} from './chips-harness-example';
8+
import {NoopAnimationsModule} from '@angular/platform-browser/animations';
9+
import {MatChipsModule} from '@angular/material/chips';
10+
11+
describe('ChipsHarnessExample', () => {
12+
let fixture: ComponentFixture<ChipsHarnessExample>;
13+
let loader: HarnessLoader;
14+
beforeAll(() => {
15+
TestBed.initTestEnvironment(BrowserDynamicTestingModule, platformBrowserDynamicTesting());
16+
});
17+
beforeEach(
18+
waitForAsync(() => {
19+
TestBed.configureTestingModule({
20+
imports: [MatChipsModule, NoopAnimationsModule],
21+
declarations: [ChipsHarnessExample]
22+
}).compileComponents();
23+
fixture = TestBed.createComponent(ChipsHarnessExample);
24+
fixture.detectChanges();
25+
loader = TestbedHarnessEnvironment.loader(fixture);
26+
})
27+
);
28+
29+
it('should get whether a chip list is disabled', async () => {
30+
const chipList = await loader.getHarness(MatChipListHarness);
31+
32+
expect(await chipList.isDisabled()).toBeFalse();
33+
34+
fixture.componentInstance.isDisabled = true;
35+
fixture.detectChanges();
36+
37+
expect(await chipList.isDisabled()).toBeTrue();
38+
});
39+
40+
it('should get the orientation of a chip list', async () => {
41+
const chipList = await loader.getHarness(MatChipListHarness);
42+
43+
expect(await chipList.getOrientation()).toEqual('horizontal');
44+
});
45+
46+
it('should be able to get the selected chips in a list', async () => {
47+
const chipList = await loader.getHarness(MatChipListHarness);
48+
const chips = await chipList.getChips();
49+
50+
expect((await chipList.getChips({selected: true})).length).toBe(0);
51+
await chips[1].select();
52+
53+
const selectedChips = await chipList.getChips({selected: true});
54+
expect(await Promise.all(selectedChips.map(chip => chip.getText()))).toEqual(['Chip 2']);
55+
});
56+
57+
it('should be able to trigger chip removal', async () => {
58+
const chip = await loader.getHarness(MatChipHarness);
59+
expect(fixture.componentInstance.remove).not.toHaveBeenCalled();
60+
await chip.remove();
61+
expect(fixture.componentInstance.remove).toHaveBeenCalled();
62+
});
63+
});
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import {Component} from '@angular/core';
2+
3+
/**
4+
* @title Testing with MatChipsHarness
5+
*/
6+
@Component({
7+
selector: 'chips-harness-example',
8+
templateUrl: 'chips-harness-example.html',
9+
})
10+
export class ChipsHarnessExample {
11+
isDisabled = false;
12+
remove = jasmine.createSpy('remove spy');
13+
add = jasmine.createSpy('add spy');
14+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,15 @@ import {ChipsDragDropExample} from './chips-drag-drop/chips-drag-drop-example';
1111
import {ChipsInputExample} from './chips-input/chips-input-example';
1212
import {ChipsOverviewExample} from './chips-overview/chips-overview-example';
1313
import {ChipsStackedExample} from './chips-stacked/chips-stacked-example';
14+
import {ChipsHarnessExample} from './chips-harness/chips-harness-example';
1415

1516
export {
1617
ChipsAutocompleteExample,
1718
ChipsDragDropExample,
1819
ChipsInputExample,
1920
ChipsOverviewExample,
2021
ChipsStackedExample,
22+
ChipsHarnessExample,
2123
};
2224

2325
const EXAMPLES = [
@@ -26,6 +28,7 @@ const EXAMPLES = [
2628
ChipsInputExample,
2729
ChipsOverviewExample,
2830
ChipsStackedExample,
31+
ChipsHarnessExample,
2932
];
3033

3134
@NgModule({

0 commit comments

Comments
 (0)