File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff 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
191225export default HiddenMessage
192226` ` `
You can’t perform that action at this time.
0 commit comments