Skip to content

Commit d29ef76

Browse files
authored
feat(material-experimental/mdc-chips): add remove method to MDC chip harness (#20831)
This allows MDC chips to be removed in testing environments. Note that the harness must not only click on the remove button, but also dispatch a `transitionend` event. Without the dispatch of this event, the chip would not fire it's `removed` output. This PR also includes a basic MDC ChipRemoveHarness that mirrors the non-MDC version. This was necessary to find the ChipRemove button in the chip harness' `remove` method.
1 parent 57a3661 commit d29ef76

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

src/material-experimental/mdc-chips/testing/chip-harness-filters.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,5 @@ export interface ChipOptionHarnessFilters extends ChipHarnessFilters {}
2323
export interface ChipRowHarnessFilters extends ChipHarnessFilters {}
2424

2525
export interface ChipSetHarnessFilters extends BaseHarnessFilters {}
26+
27+
export interface ChipRemoveHarnessFilters extends BaseHarnessFilters {}

src/material-experimental/mdc-chips/testing/chip-harness.spec.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe('MatChipHarness', () => {
2222

2323
it('should get correct number of chip harnesses', async () => {
2424
const harnesses = await loader.getAllHarnesses(MatChipHarness);
25-
expect(harnesses.length).toBe(4);
25+
expect(harnesses.length).toBe(5);
2626
});
2727

2828
it('should get the chip text content', async () => {
@@ -31,6 +31,16 @@ describe('MatChipHarness', () => {
3131
expect(await harnesses[1].getText()).toBe('Chip');
3232
expect(await harnesses[2].getText()).toBe('Chip with avatar');
3333
expect(await harnesses[3].getText()).toBe('Disabled Chip');
34+
expect(await harnesses[4].getText()).toBe('Chip Row');
35+
});
36+
37+
it('should be able to remove a mat-chip-row', async () => {
38+
const removeChipSpy = spyOn(fixture.componentInstance, 'removeChip');
39+
40+
const harnesses = await loader.getAllHarnesses(MatChipHarness);
41+
await harnesses[4].remove();
42+
43+
expect(removeChipSpy).toHaveBeenCalledTimes(1);
3444
});
3545
});
3646

@@ -40,7 +50,9 @@ describe('MatChipHarness', () => {
4050
<mat-chip>Chip <span matChipTrailingIcon>trailing_icon</span></mat-chip>
4151
<mat-chip><mat-chip-avatar>B</mat-chip-avatar>Chip with avatar</mat-chip>
4252
<mat-chip disabled>Disabled Chip <span matChipRemove>remove_icon</span></mat-chip>
53+
<mat-chip-row (removed)="removeChip()">Chip Row</mat-chip-row>
4354
`
4455
})
45-
class ChipHarnessTest {}
46-
56+
class ChipHarnessTest {
57+
removeChip() {}
58+
}

src/material-experimental/mdc-chips/testing/chip-harness.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.io/license
77
*/
88

9-
import {ComponentHarness, HarnessPredicate} from '@angular/cdk/testing';
9+
import {ComponentHarness, HarnessPredicate, TestKey} from '@angular/cdk/testing';
1010
import {ChipHarnessFilters} from './chip-harness-filters';
1111

1212
/** Harness for interacting with a mat-chip in tests. */
@@ -30,4 +30,11 @@ export class MatChipHarness extends ComponentHarness {
3030
exclude: '.mat-mdc-chip-avatar, .mat-mdc-chip-trailing-icon, .mat-icon'
3131
});
3232
}
33+
34+
/** Delete a chip from the set. */
35+
async remove(): Promise<void> {
36+
const hostEl = await this.host();
37+
await hostEl.sendKeys!(TestKey.DELETE);
38+
await hostEl.dispatchEvent!('transitionend', {propertyName: 'width'});
39+
}
3340
}

0 commit comments

Comments
 (0)