Skip to content

ByLabelText does not ignore invalid IDREF in aria-labelledby #1030

Open
@silvenon

Description

@silvenon
  • @testing-library/dom version: 8.5.0
  • Testing Framework and version: jest@26.6.3
  • DOM Environment: jsdom@16.5.3

Relevant code or config:

/**
 * @jest-environment jsdom
 */

const { getByRole, getByLabelText } = require('@testing-library/dom')

test('aria-labelledby', () => {
  const container = document.createElement('div')
  container.innerHTML = `
    <label>
      Name: <input aria-labelledby="oops">
    </label>
  `
  getByRole(container, 'textbox', { name: /Name/ })
  getByLabelText(container, /Name/)
})

What you did:

npx jest

What happened:

 FAIL  ./test.js
  ✕ aria-labelledby (64 ms)

  ● aria-labelledby

    TestingLibraryElementError: Found a label with the text of: /Name/, however no form control was found associated to that label. Make sure you're using the "for" attribute or "aria-labelledby" attribute correctly.

    Ignored nodes: comments, <script />, <style />
    <div>
      
        
      <label>
        
          Name: 
        <input
          aria-labelledby="oops"
        />
        
        
      </label>
      
      
    </div>

      13 |   `
      14 |   getByRole(container, 'textbox', { name: /Name/ })
    > 15 |   getByLabelText(container, /Name/)
         |   ^
      16 | })
      17 |

Reproduction:

silvenon/dom-testing-library-template

Problem description:

Perhaps I'm wrong, but based on what I read in the ByRole docs it seems that in this context the name option should behave the same way as ByLabelText.

Suggested solution:

In that case getByRole should also throw because aria-labelledby is wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions