Skip to content

[v7] *ByLabelText finding additional elements when descendants of label #482

Closed
@wKovacs64

Description

@wKovacs64
  • dom-testing-library version: 7.0.3
  • node version: 12.16.1
  • npm (or yarn) version: yarn v1.22.0

Relevant code or config:

test('getByLabelText and label descendants', () => {
  render(
    <label htmlFor="text-input">
      <h2>Enter your text here:</h2>
      <div>
        {/* should just find this input */}
        <input id="text-input" name="text" type="text" />
        {/* but v7 also finds this button */}
        <button type="submit">Submit</button>
      </div>
    </label>,
  );

  screen.getByLabelText(/your text here/i);
});

What you did:

I upgraded @testing-library/react from v.9.50 to v10.0.1 (and thus, @testing-library/dom from v6 to v7).

What happened:

A particular class of tests started to fail with the following message:

TestingLibraryElementError: Found multiple elements with the text of: /your text here/

(If this is intentional, then use the `*AllBy*` variant of the query (like `queryAllByText`, `getAllByText`, or `findAllByText`)).

Reproduction repository:

https://codesandbox.io/s/dom-testing-library-v7-label-descendants-issue-cw8mb?module=%2Fsrc%2F__tests__%2Findex.test.js&previewwindow=tests

Problem description:

The *ByLabelText queries used to find just the input but now they find the input and the button when nested under a label. I'm not using the selector option, but I think this might have been introduced in #373.

Suggested solution:

Not sure yet, ran out of time. It's quite possible I'm just doin' it wrong™️ , too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions