Skip to content

Commit

Permalink
test(material/chips): add disabled harness filter (#26156)
Browse files Browse the repository at this point in the history
  • Loading branch information
wagnermaciel committed Dec 12, 2022
1 parent 9f4557a commit bcd9487
Show file tree
Hide file tree
Showing 9 changed files with 71 additions and 7 deletions.
13 changes: 13 additions & 0 deletions src/material/chips/testing/chip-grid-harness.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ describe('MatChipGridHarness', () => {
expect(harnesses.length).toBe(1);
});

it('should load chip grids with disabled state match', async () => {
let enabledChips = await loader.getAllHarnesses(MatChipGridHarness.with({disabled: false}));
let disabledChips = await loader.getAllHarnesses(MatChipGridHarness.with({disabled: true}));
expect(enabledChips.length).toBe(1);
expect(disabledChips.length).toBe(0);

fixture.componentInstance.control.disable();
enabledChips = await loader.getAllHarnesses(MatChipGridHarness.with({disabled: false}));
disabledChips = await loader.getAllHarnesses(MatChipGridHarness.with({disabled: true}));
expect(enabledChips.length).toBe(0);
expect(disabledChips.length).toBe(1);
});

it('should get correct number of rows', async () => {
const harness = await loader.getHarness(MatChipGridHarness);
const rows = await harness.getRows();
Expand Down
8 changes: 7 additions & 1 deletion src/material/chips/testing/chip-grid-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,13 @@ export class MatChipGridHarness extends ComponentHarness {
this: ComponentHarnessConstructor<T>,
options: ChipGridHarnessFilters = {},
): HarnessPredicate<T> {
return new HarnessPredicate(this, options);
return new HarnessPredicate(this, options).addOption(
'disabled',
options.disabled,
async (harness, disabled) => {
return (await harness.isDisabled()) === disabled;
},
);
}

/** Gets whether the chip grid is disabled. */
Expand Down
14 changes: 12 additions & 2 deletions src/material/chips/testing/chip-harness-filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,33 @@ import {BaseHarnessFilters} from '@angular/cdk/testing';
export interface ChipHarnessFilters extends BaseHarnessFilters {
/** Only find instances whose text matches the given value. */
text?: string | RegExp;
/** Only find instances which match the given disabled state. */
disabled?: boolean;
}

export interface ChipInputHarnessFilters extends BaseHarnessFilters {
/** Filters based on the value of the input. */
value?: string | RegExp;
/** Filters based on the placeholder text of the input. */
placeholder?: string | RegExp;
/** Only find instances which match the given disabled state. */
disabled?: boolean;
}

export interface ChipListboxHarnessFilters extends BaseHarnessFilters {}
export interface ChipListboxHarnessFilters extends BaseHarnessFilters {
/** Only find instances which match the given disabled state. */
disabled?: boolean;
}

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

export interface ChipGridHarnessFilters extends BaseHarnessFilters {}
export interface ChipGridHarnessFilters extends BaseHarnessFilters {
/** Only find instances which match the given disabled state. */
disabled?: boolean;
}

export interface ChipRowHarnessFilters extends ChipHarnessFilters {}

Expand Down
10 changes: 7 additions & 3 deletions src/material/chips/testing/chip-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,13 @@ export class MatChipHarness extends ContentContainerComponentHarness {
this: ComponentHarnessConstructor<T>,
options: ChipHarnessFilters = {},
): HarnessPredicate<T> {
return new HarnessPredicate(this, options).addOption('text', options.text, (harness, label) => {
return HarnessPredicate.stringMatches(harness.getText(), label);
});
return new HarnessPredicate(this, options)
.addOption('text', options.text, (harness, label) => {
return HarnessPredicate.stringMatches(harness.getText(), label);
})
.addOption('disabled', options.disabled, async (harness, disabled) => {
return (await harness.isDisabled()) === disabled;
});
}

/** Gets a promise for the text content the option. */
Expand Down
7 changes: 7 additions & 0 deletions src/material/chips/testing/chip-input-harness.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ describe('MatChipInputHarness', () => {
expect(harnesses.length).toBe(2);
});

it('should load chip inputs with disabled state match', async () => {
const enabledChips = await loader.getAllHarnesses(MatChipInputHarness.with({disabled: false}));
const disabledChips = await loader.getAllHarnesses(MatChipInputHarness.with({disabled: true}));
expect(enabledChips.length).toBe(1);
expect(disabledChips.length).toBe(1);
});

it('should get the disabled state', async () => {
const harnesses = await loader.getAllHarnesses(MatChipInputHarness);
expect(await harnesses[0].isDisabled()).toBe(false);
Expand Down
3 changes: 3 additions & 0 deletions src/material/chips/testing/chip-input-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export class MatChipInputHarness extends ComponentHarness {
})
.addOption('placeholder', options.placeholder, async (harness, placeholder) => {
return (await harness.getPlaceholder()) === placeholder;
})
.addOption('disabled', options.disabled, async (harness, disabled) => {
return (await harness.isDisabled()) === disabled;
});
}

Expand Down
11 changes: 11 additions & 0 deletions src/material/chips/testing/chip-listbox-harness.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ describe('MatChipListboxHarness', () => {
expect(await harness.isMultiple()).toBe(true);
});

it('should load chip inputs with disabled state match', async () => {
let enabledChips = await loader.getAllHarnesses(MatChipListboxHarness.with({disabled: false}));
let disabledChips = await loader.getAllHarnesses(MatChipListboxHarness.with({disabled: true}));
expect(enabledChips.length).toBe(1);
expect(disabledChips.length).toBe(0);

fixture.componentInstance.disabled = true;
enabledChips = await loader.getAllHarnesses(MatChipListboxHarness.with({disabled: false}));
disabledChips = await loader.getAllHarnesses(MatChipListboxHarness.with({disabled: true}));
});

it('should get whether the listbox is disabled', async () => {
const harness = await loader.getHarness(MatChipListboxHarness);
expect(await harness.isDisabled()).toBe(false);
Expand Down
8 changes: 7 additions & 1 deletion src/material/chips/testing/chip-listbox-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ export class MatChipListboxHarness extends ComponentHarness {
this: ComponentHarnessConstructor<T>,
options: ChipListboxHarnessFilters = {},
): HarnessPredicate<T> {
return new HarnessPredicate(this, options);
return new HarnessPredicate(this, options).addOption(
'disabled',
options.disabled,
async (harness, disabled) => {
return (await harness.isDisabled()) === disabled;
},
);
}

/** Gets whether the chip listbox is disabled. */
Expand Down
4 changes: 4 additions & 0 deletions tools/public_api_guard/material/chips-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,25 @@ export interface ChipAvatarHarnessFilters extends BaseHarnessFilters {

// @public (undocumented)
export interface ChipGridHarnessFilters extends BaseHarnessFilters {
disabled?: boolean;
}

// @public (undocumented)
export interface ChipHarnessFilters extends BaseHarnessFilters {
disabled?: boolean;
text?: string | RegExp;
}

// @public (undocumented)
export interface ChipInputHarnessFilters extends BaseHarnessFilters {
disabled?: boolean;
placeholder?: string | RegExp;
value?: string | RegExp;
}

// @public (undocumented)
export interface ChipListboxHarnessFilters extends BaseHarnessFilters {
disabled?: boolean;
}

// @public (undocumented)
Expand Down

0 comments on commit bcd9487

Please sign in to comment.