Skip to content

Commit 52c33c7

Browse files
andrewseguinjelbourn
authored andcommitted
feat(material-experimental/mdc-chips): add harness skeleton (#16737)
1 parent 9027c6f commit 52c33c7

15 files changed

+576
-2
lines changed

src/material-experimental/mdc-chips/BUILD.bazel

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package(default_visibility = ["//visibility:public"])
22

33
load("@io_bazel_rules_sass//:defs.bzl", "sass_binary", "sass_library")
4-
load("//tools:defaults.bzl", "ng_module", "ng_test_library", "ng_web_test_suite")
4+
load("//tools:defaults.bzl", "ng_module", "ng_test_library", "ng_web_test_suite", "ts_library")
55

66
ng_module(
77
name = "mdc-chips",
88
srcs = glob(
99
["**/*.ts"],
10-
exclude = ["**/*.spec.ts"],
10+
exclude = [
11+
"**/*.spec.ts",
12+
"harness/**",
13+
],
1114
),
1215
assets = [":chips_scss"] + glob(["**/*.html"]),
1316
module_name = "@angular/material-experimental/mdc-chips",
@@ -23,6 +26,17 @@ ng_module(
2326
],
2427
)
2528

29+
ts_library(
30+
name = "harness",
31+
srcs = glob(
32+
["harness/**/*.ts"],
33+
exclude = ["**/*.spec.ts"],
34+
),
35+
deps = [
36+
"//src/cdk-experimental/testing",
37+
],
38+
)
39+
2640
sass_library(
2741
name = "mdc_chips_scss_lib",
2842
srcs = glob(["**/_*.scss"]),
@@ -52,7 +66,10 @@ ng_test_library(
5266
exclude = ["**/*.e2e.spec.ts"],
5367
),
5468
deps = [
69+
":harness",
5570
":mdc-chips",
71+
"//src/cdk-experimental/testing",
72+
"//src/cdk-experimental/testing/testbed",
5673
"//src/cdk/a11y",
5774
"//src/cdk/bidi",
5875
"//src/cdk/keycodes",
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import {HarnessLoader} from '@angular/cdk-experimental/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed';
3+
import {Component} from '@angular/core';
4+
import {ComponentFixture, TestBed} from '@angular/core/testing';
5+
import {MatChipsModule} from '../index';
6+
import {MatChipGridHarness} from './chip-grid-harness';
7+
8+
let fixture: ComponentFixture<ChipGridHarnessTest>;
9+
let loader: HarnessLoader;
10+
11+
describe('MatChipGridHarness', () => {
12+
beforeEach(async () => {
13+
await TestBed.configureTestingModule({
14+
imports: [MatChipsModule],
15+
declarations: [ChipGridHarnessTest],
16+
}).compileComponents();
17+
18+
fixture = TestBed.createComponent(ChipGridHarnessTest);
19+
fixture.detectChanges();
20+
loader = TestbedHarnessEnvironment.loader(fixture);
21+
});
22+
23+
it('should get correct number of grid harnesses', async () => {
24+
const harnesses = await loader.getAllHarnesses(MatChipGridHarness);
25+
expect(harnesses.length).toBe(1);
26+
});
27+
28+
it('should get correct number of rows', async () => {
29+
const harnesses = await loader.getAllHarnesses(MatChipGridHarness);
30+
const rows = await harnesses[0].getRows();
31+
expect(rows.length).toBe(3);
32+
});
33+
34+
it('should get the chip input harness', async () => {
35+
const harnesses = await loader.getAllHarnesses(MatChipGridHarness);
36+
const input = await harnesses[0].getTextInput();
37+
expect(await input).not.toBe(null);
38+
});
39+
});
40+
41+
@Component({
42+
template: `
43+
<mat-chip-grid #grid>
44+
<mat-chip-row> Chip A </mat-chip-row>
45+
<mat-chip-row> Chip B </mat-chip-row>
46+
<mat-chip-row> Chip C </mat-chip-row>
47+
<input [matChipInputFor]="grid" />
48+
</mat-chip-grid>
49+
`
50+
})
51+
class ChipGridHarnessTest {}
52+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {ComponentHarness} from '@angular/cdk-experimental/testing';
10+
import {MatChipRowHarness} from './chip-row-harness';
11+
import {MatChipInputHarness} from './chip-input';
12+
13+
/**
14+
* Harness for interacting with a mat-chip-grid in tests.
15+
* @dynamic
16+
*/
17+
export class MatChipGridHarness extends ComponentHarness {
18+
static hostSelector = 'mat-chip-grid';
19+
20+
private _rows = this.locatorForAll(MatChipRowHarness);
21+
private _input = this.locatorFor(MatChipInputHarness);
22+
23+
/** Gets promise of the harnesses for the chip rows. */
24+
async getRows(): Promise<MatChipRowHarness[]> {
25+
return await this._rows();
26+
}
27+
28+
/** Gets promise of the chip text input harness. */
29+
async getTextInput(): Promise<MatChipInputHarness|null> {
30+
return await this._input();
31+
}
32+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import {HarnessLoader} from '@angular/cdk-experimental/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed';
3+
import {Component} from '@angular/core';
4+
import {ComponentFixture, TestBed} from '@angular/core/testing';
5+
import {MatChipsModule} from '../index';
6+
import {MatChipHarness} from './chip-harness';
7+
8+
let fixture: ComponentFixture<ChipHarnessTest>;
9+
let loader: HarnessLoader;
10+
11+
describe('MatChipHarness', () => {
12+
beforeEach(async () => {
13+
await TestBed.configureTestingModule({
14+
imports: [MatChipsModule],
15+
declarations: [ChipHarnessTest],
16+
}).compileComponents();
17+
18+
fixture = TestBed.createComponent(ChipHarnessTest);
19+
fixture.detectChanges();
20+
loader = TestbedHarnessEnvironment.loader(fixture);
21+
});
22+
23+
it('should get correct number of chip harnesses', async () => {
24+
const harnesses = await loader.getAllHarnesses(MatChipHarness);
25+
expect(harnesses.length).toBe(3);
26+
});
27+
28+
it('should get the chip text content', async () => {
29+
const harnesses = await loader.getAllHarnesses(MatChipHarness);
30+
expect(await harnesses[0].getText()).toBe('Basic Chip');
31+
expect(await harnesses[1].getText()).toBe('Chip');
32+
expect(await harnesses[2].getText()).toBe('Disabled Chip');
33+
});
34+
});
35+
36+
@Component({
37+
template: `
38+
<mat-basic-chip> Basic Chip </mat-basic-chip>
39+
<mat-chip> Chip </mat-chip>
40+
<mat-chip disabled> Disabled Chip </mat-chip>
41+
`
42+
})
43+
class ChipHarnessTest {}
44+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {ComponentHarness} from '@angular/cdk-experimental/testing';
10+
11+
/**
12+
* Harness for interacting with a mat-chip in tests.
13+
* @dynamic
14+
*/
15+
export class MatChipHarness extends ComponentHarness {
16+
static hostSelector = 'mat-basic-chip, mat-chip';
17+
18+
/** Gets a promise for the text content the option. */
19+
async getText(): Promise<string> {
20+
return (await this.host()).text();
21+
}
22+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import {HarnessLoader} from '@angular/cdk-experimental/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed';
3+
import {Component} from '@angular/core';
4+
import {ComponentFixture, TestBed} from '@angular/core/testing';
5+
import {MatChipsModule} from '../index';
6+
import {MatChipInputHarness} from './chip-input';
7+
8+
let fixture: ComponentFixture<ChipInputHarnessTest>;
9+
let loader: HarnessLoader;
10+
11+
describe('MatChipInputHarness', () => {
12+
beforeEach(async () => {
13+
await TestBed.configureTestingModule({
14+
imports: [MatChipsModule],
15+
declarations: [ChipInputHarnessTest],
16+
}).compileComponents();
17+
18+
fixture = TestBed.createComponent(ChipInputHarnessTest);
19+
fixture.detectChanges();
20+
loader = TestbedHarnessEnvironment.loader(fixture);
21+
});
22+
23+
it('should get correct number of chip input harnesses', async () => {
24+
const harnesses = await loader.getAllHarnesses(MatChipInputHarness);
25+
expect(harnesses.length).toBe(2);
26+
});
27+
28+
it('should get the disabled state', async () => {
29+
const harnesses = await loader.getAllHarnesses(MatChipInputHarness);
30+
expect(await harnesses[0].isDisabled()).toBe(false);
31+
expect(await harnesses[1].isDisabled()).toBe(true);
32+
});
33+
});
34+
35+
@Component({
36+
template: `
37+
<mat-chip-grid #grid1>
38+
<input [matChipInputFor]="grid1" />
39+
</mat-chip-grid>
40+
41+
<mat-chip-grid #grid2>
42+
<input [matChipInputFor]="grid2" disabled />
43+
</mat-chip-grid>
44+
`
45+
})
46+
class ChipInputHarnessTest {}
47+
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* @license
3+
* Copyright Google LLC All Rights Reserved.
4+
*
5+
* Use of this source code is governed by an MIT-style license that can be
6+
* found in the LICENSE file at https://angular.io/license
7+
*/
8+
9+
import {ComponentHarness} from '@angular/cdk-experimental/testing';
10+
11+
/**
12+
* Harness for interacting with a grid's chip input in tests.
13+
* @dynamic
14+
*/
15+
export class MatChipInputHarness extends ComponentHarness {
16+
static hostSelector = '.mat-mdc-chip-input';
17+
18+
/** Gets a promise for the disabled state. */
19+
async isDisabled(): Promise<boolean> {
20+
return await ((await this.host()).getAttribute('disabled')) === 'true';
21+
}
22+
23+
/** Gets a promise for the placeholder text. */
24+
async getPlaceholder(): Promise<string|null> {
25+
return (await this.host()).getAttribute('placeholder');
26+
}
27+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import {HarnessLoader} from '@angular/cdk-experimental/testing';
2+
import {TestbedHarnessEnvironment} from '@angular/cdk-experimental/testing/testbed';
3+
import {Component} from '@angular/core';
4+
import {ComponentFixture, TestBed} from '@angular/core/testing';
5+
import {MatChipsModule} from '../index';
6+
import {MatChipListboxHarness} from './chip-listbox-harness';
7+
8+
let fixture: ComponentFixture<ChipListboxHarnessTest>;
9+
let loader: HarnessLoader;
10+
11+
describe('MatChipListboxHarness', () => {
12+
beforeEach(async () => {
13+
await TestBed.configureTestingModule({
14+
imports: [MatChipsModule],
15+
declarations: [ChipListboxHarnessTest],
16+
}).compileComponents();
17+
18+
fixture = TestBed.createComponent(ChipListboxHarnessTest);
19+
fixture.detectChanges();
20+
loader = TestbedHarnessEnvironment.loader(fixture);
21+
});
22+
23+
it('should get correct number of listbox harnesses', async () => {
24+
const harnesses = await loader.getAllHarnesses(MatChipListboxHarness);
25+
expect(harnesses.length).toBe(1);
26+
});
27+
28+
it('should get the number of options', async () => {
29+
const harnesses = await loader.getAllHarnesses(MatChipListboxHarness);
30+
expect ((await harnesses[0].getOptions()).length).toBe(4);
31+
});
32+
33+
describe('should get selection', async () => {
34+
it('with no selected options', async () => {
35+
const harnesses = await loader.getAllHarnesses(MatChipListboxHarness);
36+
const selectedOption = await harnesses[0].getSelected();
37+
expect(await selectedOption.length).toBe(0);
38+
});
39+
40+
it('with a single selected option', async () => {
41+
fixture.componentInstance.options[0].selected = true;
42+
fixture.detectChanges();
43+
44+
const harnesses = await loader.getAllHarnesses(MatChipListboxHarness);
45+
const selectedOption = await harnesses[0].getSelected();
46+
expect(await selectedOption.length).toBe(1);
47+
expect(await selectedOption[0].getText()).toContain('Blue');
48+
});
49+
50+
it('with multiple selected options', async () => {
51+
fixture.componentInstance.enableMultipleSelection = true;
52+
fixture.componentInstance.options[0].selected = true;
53+
fixture.componentInstance.options[1].selected = true;
54+
fixture.detectChanges();
55+
56+
const harnesses = (await loader.getAllHarnesses(MatChipListboxHarness));
57+
const selectedOption = await harnesses[0].getSelected();
58+
expect(await selectedOption.length).toBe(2);
59+
expect(await selectedOption[0].getText()).toContain('Blue');
60+
expect(await selectedOption[1].getText()).toContain('Green');
61+
});
62+
});
63+
});
64+
65+
interface ExampleOption {
66+
selected: boolean;
67+
text: string;
68+
}
69+
70+
@Component({
71+
template: `
72+
<mat-chip-listbox [multiple]="enableMultipleSelection">
73+
<mat-chip-option *ngFor="let option of options" [selected]="option.selected">
74+
{{option.text}}
75+
</mat-chip-option>
76+
</mat-chip-listbox>
77+
`
78+
})
79+
class ChipListboxHarnessTest {
80+
enableMultipleSelection = false;
81+
options: ExampleOption[] = [
82+
{text: 'Blue', selected: false},
83+
{text: 'Green', selected: false},
84+
{text: 'Red', selected: false},
85+
{text: 'Yellow', selected: false},
86+
];
87+
}
88+

0 commit comments

Comments
 (0)