/
to-be-visible.js
126 lines (106 loc) · 3.94 KB
/
to-be-visible.js
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import {render} from './helpers/test-utils'
describe('.toBeVisible', () => {
it('returns the visibility of an element', () => {
const {container} = render(`
<div>
<header>
<h1 style="display: none">Main title</h1>
<h2 style="visibility: hidden">Secondary title</h2>
<h3 style="visibility: collapse">Secondary title</h3>
<h4 style="opacity: 0">Secondary title</h4>
<h5 style="opacity: 0.1">Secondary title</h5>
</header>
<button hidden>Hidden button</button>
<section style="display: block; visibility: hidden">
<p>Hello <strong>World</strong></p>
</section>
</div>
`)
expect(container.querySelector('header')).toBeVisible()
expect(container.querySelector('h1')).not.toBeVisible()
expect(container.querySelector('h2')).not.toBeVisible()
expect(container.querySelector('h3')).not.toBeVisible()
expect(container.querySelector('h4')).not.toBeVisible()
expect(container.querySelector('h5')).toBeVisible()
expect(container.querySelector('button')).not.toBeVisible()
expect(container.querySelector('strong')).not.toBeVisible()
expect(() =>
expect(container.querySelector('header')).not.toBeVisible(),
).toThrowError()
expect(() =>
expect(container.querySelector('p')).toBeVisible(),
).toThrowError()
})
describe('with a <details /> element', () => {
let subject
afterEach(() => {
subject = undefined
})
describe('when the details is opened', () => {
beforeEach(() => {
subject = render(`
<details open>
<summary>Title of visible</summary>
<div>Visible details</div>
</details>
`)
})
it('returns true to the details content', () => {
expect(subject.container.querySelector('div')).toBeVisible()
})
it('returns true to the details summary', () => {
expect(subject.container.querySelector('summary')).toBeVisible()
})
describe('when the user clicks on the summary', () => {
beforeEach(() => subject.container.querySelector('summary').click())
it('returns false to the details content', () => {
expect(subject.container.querySelector('div')).not.toBeVisible()
})
it('returns true to the details summary', () => {
expect(subject.container.querySelector('summary')).toBeVisible()
})
})
})
describe('when the details is not opened', () => {
beforeEach(() => {
subject = render(`
<details>
<summary>Title of hidden</summary>
<div>Hidden details</div>
</details>
`)
})
it('returns false to the details content', () => {
expect(subject.container.querySelector('div')).not.toBeVisible()
})
it('returns true to the summary content', () => {
expect(subject.container.querySelector('summary')).toBeVisible()
})
describe('when the user clicks on the summary', () => {
beforeEach(() => subject.container.querySelector('summary').click())
it('returns true to the details content', () => {
expect(subject.container.querySelector('div')).toBeVisible()
})
it('returns true to the details summary', () => {
expect(subject.container.querySelector('summary')).toBeVisible()
})
})
})
describe('when the details is opened but it is hidden', () => {
beforeEach(() => {
subject = render(`
<details open hidden>
<summary>Title of visible</summary>
<div>Visible details</div>
</details>
`)
})
it('returns false to the details content', () => {
expect(subject.container.querySelector('div')).not.toBeVisible()
})
it('returns false to the details summary', () => {
expect(subject.container.querySelector('summary')).not.toBeVisible()
})
})
})
})