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