Skip to content

feat(material/autocomplete): test harness should throw when options are requested but panel is closed #24494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/material/autocomplete/testing/autocomplete-harness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
ComponentHarness,
ComponentHarnessConstructor,
HarnessPredicate,
TestElement,
} from '@angular/cdk/testing';
import {
MatOptgroupHarness,
Expand Down Expand Up @@ -71,6 +72,10 @@ export abstract class _MatAutocompleteHarnessBase<

/** Gets the options inside the autocomplete panel. */
async getOptions(filters?: Omit<OptionFilters, 'ancestor'>): Promise<Option[]> {
if (!(await this.isOpen())) {
throw new Error('Unable to retrieve options for autocomplete. Autocomplete panel is closed.');
}

return this._documentRootLocator.locatorForAll(
this._optionClass.with({
...(filters || {}),
Expand All @@ -81,6 +86,12 @@ export abstract class _MatAutocompleteHarnessBase<

/** Gets the option groups inside the autocomplete panel. */
async getOptionGroups(filters?: Omit<OptionGroupFilters, 'ancestor'>): Promise<OptionGroup[]> {
if (!(await this.isOpen())) {
throw new Error(
'Unable to retrieve option groups for autocomplete. Autocomplete panel is closed.',
);
}

return this._documentRootLocator.locatorForAll(
this._optionGroupClass.with({
...(filters || {}),
Expand All @@ -106,7 +117,7 @@ export abstract class _MatAutocompleteHarnessBase<
}

/** Gets the panel associated with this autocomplete trigger. */
private async _getPanel() {
private async _getPanel(): Promise<TestElement | null> {
// Technically this is static, but it needs to be in a
// function, because the autocomplete's panel ID can changed.
return this._documentRootLocator.locatorForOptional(await this._getPanelSelector())();
Expand Down