Skip to content

Commit 00cf4a2

Browse files
authored
feat(material/autocomplete): add clear method to test harness (#24752)
Adds a `clear` method to the autocomplete harness, because currently it's only possible to enter text into it. Fixes #24751.
1 parent 8946ce2 commit 00cf4a2

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

src/material/autocomplete/testing/autocomplete-harness.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ export abstract class _MatAutocompleteHarnessBase<
7070
return (await this.host()).sendKeys(value);
7171
}
7272

73+
/** Clears the input value. */
74+
async clear(): Promise<void> {
75+
return (await this.host()).clear();
76+
}
77+
7378
/** Gets the options inside the autocomplete panel. */
7479
async getOptions(filters?: Omit<OptionFilters, 'ancestor'>): Promise<Option[]> {
7580
if (!(await this.isOpen())) {

src/material/autocomplete/testing/shared.spec.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,14 @@ export function runHarnessTests(
6767
expect(await input.getValue()).toBe('Hello there');
6868
});
6969

70+
it('should be able to clear the input', async () => {
71+
const input = await loader.getHarness(autocompleteHarness.with({selector: '#plain'}));
72+
await input.enterText('Hello there');
73+
expect(await input.getValue()).toBe('Hello there');
74+
await input.clear();
75+
expect(await input.getValue()).toBe('');
76+
});
77+
7078
it('should be able to get the autocomplete panel options', async () => {
7179
const input = await loader.getHarness(autocompleteHarness.with({selector: '#plain'}));
7280
await input.focus();

tools/public_api_guard/material/autocomplete-testing.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export abstract class _MatAutocompleteHarnessBase<OptionType extends ComponentHa
3939
with: (options?: OptionGroupFilters) => HarnessPredicate<OptionGroup>;
4040
}, OptionGroup extends ComponentHarness, OptionGroupFilters extends BaseHarnessFilters> extends ComponentHarness {
4141
blur(): Promise<void>;
42+
clear(): Promise<void>;
4243
enterText(value: string): Promise<void>;
4344
focus(): Promise<void>;
4445
getOptionGroups(filters?: Omit<OptionGroupFilters, 'ancestor'>): Promise<OptionGroup[]>;

0 commit comments

Comments
 (0)