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.

Question: How to select an option in select input (mat-select)

See original GitHub issue

Target: I am writing a module where I need to hide input elements based on some conditions. I need to select an option of material select input.

Problem: Unable to test it, the test assertion throws error. TestingLibraryElementError: Value "Partnership" not found in options

Reproduction:

  • I am loading mat options using *ngFor (below are my options)
[
    {
        value: 'Public Limited Company',
        label: 'Public Limited Company',
    },
    {
        value: 'Private Limited Company',
        label: 'Private Limited Company',
    },
    {
        value: 'Partnership',
        label: 'Partnership',
    },
    {
        value: 'Limited Liability Partnership',
        label: 'Limited Liability Partnership',
    }
]
  • Below is my assertion code.
const firmType = screen.queryByLabelText(/Firm Type/i);
const isGstPresent = screen.queryByLabelText(/Is Gst Available/i);
const gstNumber = screen.queryByLabelText(/GST Number/i);
        
userEvent.selectOptions(firmType, 'Partnership');

await waitFor(() => {
    expect(isGstPresent).toBeInTheDocument();
    expect(taxPayerType).not.toBeInTheDocument();
});

Any help is appreciated.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
VictorJSVcommented, Jun 9, 2022

This is the userEvent correct use for mat-select

const user = userEvent.setup()
...
const select = screen.getByRole('combobox', { name: /List/i })
await user.click(select)
await user.click(screen.getByText(/Minimarket/i))

... assertions ...
0reactions
tiagomsmagalhaescommented, Sep 20, 2021

The link above is dead (possible file moving?) The new link with the example

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't select a mat-option of mat-select? - Stack Overflow
Method 1. Replace [value] with value in your <mat-option [value]="nam">Nam</mat-option> when your value is not dynamic. Method 2.
Read more >
Select | Angular Material
<mat-select> is a form control for selecting a value from a set of options, similar to the native <select> element. You can read...
Read more >
<mat-select> in Angular Material - GeeksforGeeks
<mat-select> tag is used to display the options in a dropdown. ... Then use <mat-select> tag to group all the items inside this...
Read more >
Angular Material Select - Javatpoint
Angular Material Select. <mat-select> is a form control used for selecting a value from a set of options. It is same as the...
Read more >
ngx-mat-select-search - npm
Angular component providing an input field for searching / filtering MatSelect options of the Angular Material library.
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