Skip to content

Commit e46afe8

Browse files
authored
fix(select/testing): incorrect options if multiple selects are on the page at the same time (#19112)
Fixes the test harness always retrieving the options for the first select when there are multiple selects on the page. Fixes #19075.
1 parent 0bf1a07 commit e46afe8

File tree

3 files changed

+52
-9
lines changed

3 files changed

+52
-9
lines changed

src/material/select/select.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
<div class="mat-select-panel-wrap" [@transformPanelWrap]>
3333
<div
3434
#panel
35+
[attr.id]="id + '-panel'"
3536
class="mat-select-panel {{ _getPanelTheme() }}"
3637
[ngClass]="panelClass"
3738
[@transformPanel]="multiple ? 'showing-multiple' : 'showing'"

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

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

19-
const PANEL_SELECTOR = '.mat-select-panel';
2019

2120
/** Harness for interacting with a standard mat-select in tests. */
2221
export class MatSelectHarness extends MatFormFieldControlHarness {
2322
private _documentRootLocator = this.documentRootLocatorFactory();
2423
private _backdrop = this._documentRootLocator.locatorFor('.cdk-overlay-backdrop');
25-
private _optionalPanel = this._documentRootLocator.locatorForOptional(PANEL_SELECTOR);
2624
private _trigger = this.locatorFor('.mat-select-trigger');
2725
private _value = this.locatorFor('.mat-select-value');
2826

@@ -84,7 +82,7 @@ export class MatSelectHarness extends MatFormFieldControlHarness {
8482
Promise<MatOptionHarness[]> {
8583
return this._documentRootLocator.locatorForAll(MatOptionHarness.with({
8684
...filter,
87-
ancestor: PANEL_SELECTOR
85+
ancestor: await this._getPanelSelector()
8886
}))();
8987
}
9088

@@ -93,13 +91,13 @@ export class MatSelectHarness extends MatFormFieldControlHarness {
9391
Promise<MatOptgroupHarness[]> {
9492
return this._documentRootLocator.locatorForAll(MatOptgroupHarness.with({
9593
...filter,
96-
ancestor: PANEL_SELECTOR
94+
ancestor: await this._getPanelSelector()
9795
}))();
9896
}
9997

10098
/** Gets whether the select is open. */
10199
async isOpen(): Promise<boolean> {
102-
return !!(await this._optionalPanel());
100+
return !!await this._documentRootLocator.locatorForOptional(await this._getPanelSelector())();
103101
}
104102

105103
/** Opens the select's panel. */
@@ -138,4 +136,10 @@ export class MatSelectHarness extends MatFormFieldControlHarness {
138136
return (await this._backdrop()).click();
139137
}
140138
}
139+
140+
/** Gets the selector that should be used to find this select's panel. */
141+
private async _getPanelSelector(): Promise<string> {
142+
const id = await (await this.host()).getAttribute('id');
143+
return `#${id}-panel`;
144+
}
141145
}

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

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,28 @@ export function runHarnessTests(
152152
const options = await select.getOptions();
153153

154154
expect(groups.length).toBe(3);
155-
expect(options.length).toBe(11);
155+
expect(options.length).toBe(14);
156+
});
157+
158+
it('should be able to get the select options when there are multiple open selects', async () => {
159+
const singleSelect = await loader.getHarness(selectHarness.with({
160+
selector: '#single-selection'
161+
}));
162+
await singleSelect.open();
163+
164+
const groupedSelect = await loader.getHarness(selectHarness.with({selector: '#grouped'}));
165+
await groupedSelect.open();
166+
167+
const [singleOptions, groupedOptions] = await Promise.all([
168+
singleSelect.getOptions(),
169+
groupedSelect.getOptions()
170+
]);
171+
172+
expect(await singleOptions[0].getText()).toBe('Alabama');
173+
expect(singleOptions.length).toBe(11);
174+
175+
expect(await groupedOptions[0].getText()).toBe('Iowa');
176+
expect(groupedOptions.length).toBe(14);
156177
});
157178

158179
it('should be able to get the value text from a single-selection select', async () => {
@@ -268,15 +289,32 @@ class SelectHarnessTest {
268289
stateGroups = [
269290
{
270291
name: 'One',
271-
states: this.states.slice(0, 3)
292+
states: [
293+
{code: 'IA', name: 'Iowa'},
294+
{code: 'KS', name: 'Kansas'},
295+
{code: 'KY', name: 'Kentucky'},
296+
{code: 'LA', name: 'Louisiana'},
297+
{code: 'ME', name: 'Maine'}
298+
]
272299
},
273300
{
274301
name: 'Two',
275-
states: this.states.slice(3, 7)
302+
states: [
303+
{code: 'RI', name: 'Rhode Island'},
304+
{code: 'SC', name: 'South Carolina'},
305+
{code: 'SD', name: 'South Dakota'},
306+
{code: 'TN', name: 'Tennessee'},
307+
{code: 'TX', name: 'Texas'},
308+
]
276309
},
277310
{
278311
name: 'Three',
279-
states: this.states.slice(7)
312+
states: [
313+
{code: 'UT', name: 'Utah'},
314+
{code: 'WA', name: 'Washington'},
315+
{code: 'WV', name: 'West Virginia'},
316+
{code: 'WI', name: 'Wisconsin'}
317+
]
280318
}
281319
];
282320
}

0 commit comments

Comments
 (0)