Skip to content

Not able to select an optgroup with getByRole #940

Closed
@runephilosof-abtion

Description

@runephilosof-abtion

"@testing-library/dom": "^7.30.4",
"@testing-library/jest-dom": "^5.11.10",
"@testing-library/react": "^11.2.6",

Relevant code or config:

const App = () => (
  <select style={styles}>
    <optgroup label="foo">
      <option value="1">baz</option>
    </optgroup>
    <optgroup label="bar">
      <option value="2">baz</option>
    </optgroup>
  </select>
)

render(<App />, document.getElementById('root'))

screen.getByRole("group", { name: /bar/i })

What you did:

screen.getByRole("group", { name: /bar/i })

What happened:

TestingLibraryElementError: Unable to find an accessible element with the role "group" and name `/bar/i`

Here are the accessible roles:

  group:

  Name "":
  <optgroup
    label="foo"
  />

  Name "":
  <optgroup
    label="bar"
  />

Reproduction:

Just paste the above code into https://codesandbox.io/s/5z6x4r7n0p

Problem description:

It would be useful to be able to verify that a certain group is present.
It would also make it easier to select options within that group.

Suggested solution:

The name argument should match against the label attribute of optgroup.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions