|
| 1 | +import * as React from 'react' |
| 2 | +import {render, fireEvent} from '..' |
| 3 | + |
| 4 | +/** |
| 5 | + * Test suite for native HTML <details> element with <summary> |
| 6 | + * |
| 7 | + * Demonstrates how to test user interactions (clicking <summary>) |
| 8 | + * and verify <details> open/close behavior. |
| 9 | + */ |
| 10 | +describe('<details><summary> interaction', () => { |
| 11 | + let handleSummaryClick |
| 12 | + |
| 13 | + beforeEach(() => { |
| 14 | + handleSummaryClick = jest.fn() |
| 15 | + }) |
| 16 | + |
| 17 | + it('should toggle "open" attribute when <summary> is clicked', () => { |
| 18 | + const {container} = render( |
| 19 | + <> |
| 20 | + <details name="requirements"> |
| 21 | + <summary onClick={handleSummaryClick}> |
| 22 | + Graduation Requirements |
| 23 | + </summary> |
| 24 | + <p> |
| 25 | + Requires 40 credits, including a passing grade in health, geography, |
| 26 | + history, economics, and wood shop. |
| 27 | + </p> |
| 28 | + </details> |
| 29 | + <details name="requirements"> |
| 30 | + <summary onClick={handleSummaryClick}>System Requirements</summary> |
| 31 | + <p> |
| 32 | + Requires a computer running an operating system. The computer must |
| 33 | + have some memory and ideally some kind of long-term storage. An |
| 34 | + input device as well as some form of output device is recommended. |
| 35 | + </p> |
| 36 | + </details> |
| 37 | + <details name="requirements"> |
| 38 | + <summary onClick={handleSummaryClick}>Job Requirements</summary> |
| 39 | + <p> |
| 40 | + Requires knowledge of HTML, CSS, JavaScript, accessibility, web |
| 41 | + performance, privacy, security, and internationalization, as well as |
| 42 | + a dislike of broccoli. |
| 43 | + </p> |
| 44 | + </details> |
| 45 | + </>, |
| 46 | + ) |
| 47 | + |
| 48 | + const summaries = container.querySelectorAll('summary') |
| 49 | + expect(summaries.length).toBe(3) |
| 50 | + |
| 51 | + summaries.forEach((summary, index) => { |
| 52 | + // Initially, details should NOT be open |
| 53 | + const details = summary.closest('details') |
| 54 | + expect(details).not.toHaveAttribute('open') |
| 55 | + |
| 56 | + // Click the summary |
| 57 | + fireEvent.click(summary) |
| 58 | + |
| 59 | + // Expect the click handler to be called |
| 60 | + expect(handleSummaryClick).toHaveBeenCalledTimes(index + 1) |
| 61 | + |
| 62 | + // Details should now be open |
| 63 | + expect(details).toHaveAttribute('open') |
| 64 | + }) |
| 65 | + }) |
| 66 | +}) |
0 commit comments