Skip to content

Update documentation for toBeVisible() when using details elements #395

Open
@kschow

Description

@kschow

I was working on something using the <details /> element and was looking to test whether the inner text was visible or not. What I found out while looking through the code/tests for toBeVisible() is that:

  1. The inner text needs to be inside another element. e.g.
<details>
    <summary>Title</summary>
    <div>Explanation text</div>
</details>
  1. This is necessary because of the previousElement check in isAttributeVisible
    (element.nodeName === 'DETAILS' && previousElement.nodeName !== 'SUMMARY'
  2. When a <details /> element without enclosed inner text is used in conjunction with React Testing Library, you'll receive TypeError: Cannot read property 'nodeName' of undefined. E.g.
<details>
    <summary>This will not work</summary>
    Unenclosed Inner Text
</details>
expect(getByText('Unenclosed Inner Text')).not.toBeVisible();
  1. While the documentation does mention that toBeVisible() works with <details /> elements with the open attribute, it neither mentions the above or give an example.

Since <details /> elements do not need to have an enclosing element for their inner text, see the first example https://developer.mozilla.org/en-US/docs/Web/HTML/Element/details, it should be clearer that this is necessary. It's probably ok if this acts as it is, I just think it should be clearer that it's explicitly necessary and there should probably be an example for <details /> regardless.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinggood first issueGood for newcomers

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions