question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug(MatSelectHarness): getOptions() returns results from another harness

See original GitHub issue

Reproduction

Use StackBlitz to reproduce your issue: https://stackblitz.com/fork/components-issue

Steps to reproduce:

  1. Add two mat-selects with the same selector (e.g. id=“test”) but different ancestor selectors to a page. Both should be multiple-selects. Add 3 options in the first select and no options in the second.
  2. Write code that sets options of second select whenever selection within first select changes, so that the second select always contains only the options that are selected in the first select.
  3. Use test harness to click one option in first select, then use following code on the second test harness to check that only the single option is available:
    await select.open();
    const optionLabels = Promise.all((await select.getOptions()).map(option => option.getText()));

Expected Behavior

What behavior were you expecting to see?

  • There should only be one option in the second select.

Actual Behavior

What behavior did you actually see?

  • The options returned by the second select are the same as in the first - i.e. 3 options - even though when inspecting the component instance, I have verified that the options are correct (just one option).

Environment

  • Angular: 9.1.7
  • CDK/Material: 9.2.3
  • Browser(s): Chrome v80
  • Operating System (e.g. Windows, macOS, Ubuntu): macOS 10.15

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
crisbetocommented, May 28, 2020

But I’m wondering, if I were to use an attribute other than ID for the selector would that really solve the problem?

Yes, that will solve the problem. We use the ID to tie a select to its overlay panel. The panel uses a scheme that looks like {{ id of the select }}-panel.

Also since we don’t have control over any of the other attributes, and as there is currently no support in the harness for e.g. finding a select by label, ID is what we have to go on.

The harnesses support any valid CSS selector so you could use an attribute or a class to distinguish between the selects.

1reaction
mmalerbacommented, Jun 25, 2020

I don’t think we want to add anything just to handle duplicate id’s. It sounds like you should open a bug against ng-dynamic-forms

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unable to test Angular Material Select Component using ...
After some tinkering with the Select Harness API, I was able to solve this. I have updated the spec to get the host...
Read more >
Create a component harness for your tests with Angular CDK
All component harness methods must return a promise. ... returned by this.getDropDown() , we have a MatSelectHarness in the select constant.
Read more >
Testing Angular components with Material's Test Harnesses
In this article you will get a brief introduction to the topic of using Angular Material's component harnesses in your tests.
Read more >
Testing with component harnesses
Using Angular Material's component harnesses in your tests ... All component harness APIs are asynchronous and return Promise objects.
Read more >
How to use Angular Material harnesses to improve your ...
Here is the same scenario using an Angular Material Harness: const select = await loader.getHarness(MatSelectHarness); await select.open();
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found