Skip to content

Commit bcd9487

Browse files
authored
test(material/chips): add disabled harness filter (#26156)
1 parent 9f4557a commit bcd9487

File tree

9 files changed

+71
-7
lines changed

9 files changed

+71
-7
lines changed

src/material/chips/testing/chip-grid-harness.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,19 @@ describe('MatChipGridHarness', () => {
2626
expect(harnesses.length).toBe(1);
2727
});
2828

29+
it('should load chip grids with disabled state match', async () => {
30+
let enabledChips = await loader.getAllHarnesses(MatChipGridHarness.with({disabled: false}));
31+
let disabledChips = await loader.getAllHarnesses(MatChipGridHarness.with({disabled: true}));
32+
expect(enabledChips.length).toBe(1);
33+
expect(disabledChips.length).toBe(0);
34+
35+
fixture.componentInstance.control.disable();
36+
enabledChips = await loader.getAllHarnesses(MatChipGridHarness.with({disabled: false}));
37+
disabledChips = await loader.getAllHarnesses(MatChipGridHarness.with({disabled: true}));
38+
expect(enabledChips.length).toBe(0);
39+
expect(disabledChips.length).toBe(1);
40+
});
41+
2942
it('should get correct number of rows', async () => {
3043
const harness = await loader.getHarness(MatChipGridHarness);
3144
const rows = await harness.getRows();

src/material/chips/testing/chip-grid-harness.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,13 @@ export class MatChipGridHarness extends ComponentHarness {
3232
this: ComponentHarnessConstructor<T>,
3333
options: ChipGridHarnessFilters = {},
3434
): HarnessPredicate<T> {
35-
return new HarnessPredicate(this, options);
35+
return new HarnessPredicate(this, options).addOption(
36+
'disabled',
37+
options.disabled,
38+
async (harness, disabled) => {
39+
return (await harness.isDisabled()) === disabled;
40+
},
41+
);
3642
}
3743

3844
/** Gets whether the chip grid is disabled. */

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,33 @@ import {BaseHarnessFilters} from '@angular/cdk/testing';
1111
export interface ChipHarnessFilters extends BaseHarnessFilters {
1212
/** Only find instances whose text matches the given value. */
1313
text?: string | RegExp;
14+
/** Only find instances which match the given disabled state. */
15+
disabled?: boolean;
1416
}
1517

1618
export interface ChipInputHarnessFilters extends BaseHarnessFilters {
1719
/** Filters based on the value of the input. */
1820
value?: string | RegExp;
1921
/** Filters based on the placeholder text of the input. */
2022
placeholder?: string | RegExp;
23+
/** Only find instances which match the given disabled state. */
24+
disabled?: boolean;
2125
}
2226

23-
export interface ChipListboxHarnessFilters extends BaseHarnessFilters {}
27+
export interface ChipListboxHarnessFilters extends BaseHarnessFilters {
28+
/** Only find instances which match the given disabled state. */
29+
disabled?: boolean;
30+
}
2431

2532
export interface ChipOptionHarnessFilters extends ChipHarnessFilters {
2633
/** Only find chip instances whose selected state matches the given value. */
2734
selected?: boolean;
2835
}
2936

30-
export interface ChipGridHarnessFilters extends BaseHarnessFilters {}
37+
export interface ChipGridHarnessFilters extends BaseHarnessFilters {
38+
/** Only find instances which match the given disabled state. */
39+
disabled?: boolean;
40+
}
3141

3242
export interface ChipRowHarnessFilters extends ChipHarnessFilters {}
3343

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,13 @@ export class MatChipHarness extends ContentContainerComponentHarness {
3535
this: ComponentHarnessConstructor<T>,
3636
options: ChipHarnessFilters = {},
3737
): HarnessPredicate<T> {
38-
return new HarnessPredicate(this, options).addOption('text', options.text, (harness, label) => {
39-
return HarnessPredicate.stringMatches(harness.getText(), label);
40-
});
38+
return new HarnessPredicate(this, options)
39+
.addOption('text', options.text, (harness, label) => {
40+
return HarnessPredicate.stringMatches(harness.getText(), label);
41+
})
42+
.addOption('disabled', options.disabled, async (harness, disabled) => {
43+
return (await harness.isDisabled()) === disabled;
44+
});
4145
}
4246

4347
/** Gets a promise for the text content the option. */

src/material/chips/testing/chip-input-harness.spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ describe('MatChipInputHarness', () => {
2525
expect(harnesses.length).toBe(2);
2626
});
2727

28+
it('should load chip inputs with disabled state match', async () => {
29+
const enabledChips = await loader.getAllHarnesses(MatChipInputHarness.with({disabled: false}));
30+
const disabledChips = await loader.getAllHarnesses(MatChipInputHarness.with({disabled: true}));
31+
expect(enabledChips.length).toBe(1);
32+
expect(disabledChips.length).toBe(1);
33+
});
34+
2835
it('should get the disabled state', async () => {
2936
const harnesses = await loader.getAllHarnesses(MatChipInputHarness);
3037
expect(await harnesses[0].isDisabled()).toBe(false);

src/material/chips/testing/chip-input-harness.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export class MatChipInputHarness extends ComponentHarness {
3434
})
3535
.addOption('placeholder', options.placeholder, async (harness, placeholder) => {
3636
return (await harness.getPlaceholder()) === placeholder;
37+
})
38+
.addOption('disabled', options.disabled, async (harness, disabled) => {
39+
return (await harness.isDisabled()) === disabled;
3740
});
3841
}
3942

src/material/chips/testing/chip-listbox-harness.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,17 @@ describe('MatChipListboxHarness', () => {
3838
expect(await harness.isMultiple()).toBe(true);
3939
});
4040

41+
it('should load chip inputs with disabled state match', async () => {
42+
let enabledChips = await loader.getAllHarnesses(MatChipListboxHarness.with({disabled: false}));
43+
let disabledChips = await loader.getAllHarnesses(MatChipListboxHarness.with({disabled: true}));
44+
expect(enabledChips.length).toBe(1);
45+
expect(disabledChips.length).toBe(0);
46+
47+
fixture.componentInstance.disabled = true;
48+
enabledChips = await loader.getAllHarnesses(MatChipListboxHarness.with({disabled: false}));
49+
disabledChips = await loader.getAllHarnesses(MatChipListboxHarness.with({disabled: true}));
50+
});
51+
4152
it('should get whether the listbox is disabled', async () => {
4253
const harness = await loader.getHarness(MatChipListboxHarness);
4354
expect(await harness.isDisabled()).toBe(false);

src/material/chips/testing/chip-listbox-harness.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ export class MatChipListboxHarness extends ComponentHarness {
2929
this: ComponentHarnessConstructor<T>,
3030
options: ChipListboxHarnessFilters = {},
3131
): HarnessPredicate<T> {
32-
return new HarnessPredicate(this, options);
32+
return new HarnessPredicate(this, options).addOption(
33+
'disabled',
34+
options.disabled,
35+
async (harness, disabled) => {
36+
return (await harness.isDisabled()) === disabled;
37+
},
38+
);
3339
}
3440

3541
/** Gets whether the chip listbox is disabled. */

tools/public_api_guard/material/chips-testing.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,25 @@ export interface ChipAvatarHarnessFilters extends BaseHarnessFilters {
1919

2020
// @public (undocumented)
2121
export interface ChipGridHarnessFilters extends BaseHarnessFilters {
22+
disabled?: boolean;
2223
}
2324

2425
// @public (undocumented)
2526
export interface ChipHarnessFilters extends BaseHarnessFilters {
27+
disabled?: boolean;
2628
text?: string | RegExp;
2729
}
2830

2931
// @public (undocumented)
3032
export interface ChipInputHarnessFilters extends BaseHarnessFilters {
33+
disabled?: boolean;
3134
placeholder?: string | RegExp;
3235
value?: string | RegExp;
3336
}
3437

3538
// @public (undocumented)
3639
export interface ChipListboxHarnessFilters extends BaseHarnessFilters {
40+
disabled?: boolean;
3741
}
3842

3943
// @public (undocumented)

0 commit comments

Comments
 (0)