Skip to content

Commit fd46ca3

Browse files
crisbetojelbourn
authored andcommitted
fix(autocomplete/testing): incorrect options if multiple panels are on the page at the same time (#19114)
Fixes the autocomplete harness returning the options of the first autocomplete panel, if multiple panels are on the page at the same time.
1 parent 8f36604 commit fd46ca3

File tree

2 files changed

+49
-10
lines changed

2 files changed

+49
-10
lines changed

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

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,9 @@ import {
1616
} from '@angular/material/core/testing';
1717
import {AutocompleteHarnessFilters} from './autocomplete-harness-filters';
1818

19-
/** Selector for the autocomplete panel. */
20-
const PANEL_SELECTOR = '.mat-autocomplete-panel';
21-
2219
/** Harness for interacting with a standard mat-autocomplete in tests. */
2320
export class MatAutocompleteHarness extends ComponentHarness {
2421
private _documentRootLocator = this.documentRootLocatorFactory();
25-
private _optionalPanel = this._documentRootLocator.locatorForOptional(PANEL_SELECTOR);
2622

2723
/** The selector for the host element of a `MatAutocomplete` instance. */
2824
static hostSelector = '.mat-autocomplete-trigger';
@@ -70,7 +66,7 @@ export class MatAutocompleteHarness extends ComponentHarness {
7066
Promise<MatOptionHarness[]> {
7167
return this._documentRootLocator.locatorForAll(MatOptionHarness.with({
7268
...filters,
73-
ancestor: PANEL_SELECTOR
69+
ancestor: await this._getPanelSelector()
7470
}))();
7571
}
7672

@@ -79,7 +75,7 @@ export class MatAutocompleteHarness extends ComponentHarness {
7975
Promise<MatOptgroupHarness[]> {
8076
return this._documentRootLocator.locatorForAll(MatOptgroupHarness.with({
8177
...filters,
82-
ancestor: PANEL_SELECTOR
78+
ancestor: await this._getPanelSelector()
8379
}))();
8480
}
8581

@@ -95,7 +91,19 @@ export class MatAutocompleteHarness extends ComponentHarness {
9591

9692
/** Whether the autocomplete is open. */
9793
async isOpen(): Promise<boolean> {
98-
const panel = await this._optionalPanel();
94+
const panel = await this._getPanel();
9995
return !!panel && await panel.hasClass('mat-autocomplete-visible');
10096
}
97+
98+
/** Gets the panel associated with this autocomplete trigger. */
99+
private async _getPanel() {
100+
// Technically this is static, but it needs to be in a
101+
// function, because the autocomplete's panel ID can changed.
102+
return this._documentRootLocator.locatorForOptional(await this._getPanelSelector())();
103+
}
104+
105+
/** Gets the selector that can be used to find the autocomplete trigger's panel. */
106+
private async _getPanelSelector(): Promise<string> {
107+
return `#${(await (await this.host()).getAttribute('aria-owns'))}`;
108+
}
101109
}

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

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,21 @@ export function runHarnessTests(
101101
const options = await input.getOptions();
102102

103103
expect(groups.length).toBe(3);
104+
expect(options.length).toBe(14);
105+
});
106+
107+
it('should be able to get the autocomplete panel groups', async () => {
108+
const input = await loader.getHarness(autocompleteHarness.with({selector: '#plain'}));
109+
await input.focus();
110+
111+
const input2 = await loader.getHarness(autocompleteHarness.with({selector: '#grouped'}));
112+
await input2.focus();
113+
114+
const options = await input.getOptions();
115+
const options2 = await input2.getOptions();
116+
104117
expect(options.length).toBe(11);
118+
expect(options2.length).toBe(14);
105119
});
106120

107121
it('should be able to get filtered panel groups', async () => {
@@ -178,15 +192,32 @@ class AutocompleteHarnessTest {
178192
stateGroups = [
179193
{
180194
name: 'One',
181-
states: this.states.slice(0, 3)
195+
states: [
196+
{code: 'IA', name: 'Iowa'},
197+
{code: 'KS', name: 'Kansas'},
198+
{code: 'KY', name: 'Kentucky'},
199+
{code: 'LA', name: 'Louisiana'},
200+
{code: 'ME', name: 'Maine'}
201+
]
182202
},
183203
{
184204
name: 'Two',
185-
states: this.states.slice(3, 7)
205+
states: [
206+
{code: 'RI', name: 'Rhode Island'},
207+
{code: 'SC', name: 'South Carolina'},
208+
{code: 'SD', name: 'South Dakota'},
209+
{code: 'TN', name: 'Tennessee'},
210+
{code: 'TX', name: 'Texas'},
211+
]
186212
},
187213
{
188214
name: 'Three',
189-
states: this.states.slice(7)
215+
states: [
216+
{code: 'UT', name: 'Utah'},
217+
{code: 'WA', name: 'Washington'},
218+
{code: 'WV', name: 'West Virginia'},
219+
{code: 'WI', name: 'Wisconsin'}
220+
]
190221
}
191222
];
192223
}

0 commit comments

Comments
 (0)