Skip to content

Commit bd2472c

Browse files
committed
docs: add example for testing <details> element
1 parent aba5740 commit bd2472c

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

README.md

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,40 @@ function HiddenMessage({children}) {
187187
</div>
188188
)
189189
}
190+
### Testing <details> / <summary>
191+
192+
HTML `<details>` uses `<summary>` as its toggle button.
193+
When testing with React Testing Library, the `<summary>` element is exposed as a
194+
`button` role, so user interactions can be tested normally.
195+
196+
```js
197+
import { render, screen } from "@testing-library/react"
198+
import userEvent from "@testing-library/user-event"
199+
200+
function DetailsExample() {
201+
return (
202+
<details>
203+
<summary>More info</summary>
204+
<p>Hidden content</p>
205+
</details>
206+
)
207+
}
208+
209+
test("opens details when summary is clicked", async () => {
210+
const user = userEvent.setup()
211+
render(<DetailsExample />)
212+
213+
const summary = screen.getByRole("button", { name: /more info/i })
214+
215+
// initially closed
216+
expect(screen.queryByText("Hidden content")).not.toBeInTheDocument()
217+
218+
await user.click(summary)
219+
220+
// after clicking, content appears
221+
expect(screen.getByText("Hidden content")).toBeInTheDocument()
222+
})
223+
190224
191225
export default HiddenMessage
192226
```

0 commit comments

Comments
 (0)