/
to-have-style.js
200 lines (173 loc) · 5.71 KB
/
to-have-style.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import {render} from './helpers/test-utils'
import document from './helpers/document'
// eslint-disable-next-line max-lines-per-function
describe('.toHaveStyle', () => {
test('handles positive test cases', () => {
const {container} = render(`
<div class="label" style="background-color: blue; height: 100%">
Hello World
</div>
`)
const style = document.createElement('style')
style.innerHTML = `
.label {
align-items: center;
background-color: black;
color: white;
float: left;
transition: opacity 0.2s ease-out, top 0.3s cubic-bezier(1.175, 0.885, 0.32, 1.275);
transform: translateX(0px);
}
`
document.body.appendChild(style)
document.body.appendChild(container)
expect(container.querySelector('.label')).toHaveStyle(`
height: 100%;
color: white;
background-color: blue;
`)
expect(container.querySelector('.label')).toHaveStyle(`
background-color: blue;
color: white;
`)
expect(container.querySelector('.label')).toHaveStyle(
'transition: opacity 0.2s ease-out, top 0.3s cubic-bezier(1.175, 0.885, 0.32, 1.275)',
)
expect(container.querySelector('.label')).toHaveStyle(
'background-color:blue;color:white',
)
expect(container.querySelector('.label')).not.toHaveStyle(`
color: white;
font-weight: bold;
`)
expect(container.querySelector('.label')).toHaveStyle(`
Align-items: center;
`)
expect(container.querySelector('.label')).toHaveStyle(`
transform: translateX(0px);
`)
})
test('handles negative test cases', () => {
const {container} = render(`
<div class="label" style="background-color: blue; height: 100%">
Hello World
</div>
`)
const style = document.createElement('style')
style.innerHTML = `
.label {
background-color: black;
color: white;
float: left;
transition: opacity 0.2s ease-out, top 0.3s cubic-bezier(1.175, 0.885, 0.32, 1.275);
}
`
document.body.appendChild(style)
document.body.appendChild(container)
expect(() =>
expect(container.querySelector('.label')).toHaveStyle(
'font-weight: bold',
),
).toThrowError()
expect(() =>
expect(container.querySelector('.label')).not.toHaveStyle('color: white'),
).toThrowError()
expect(() =>
expect(container.querySelector('.label')).toHaveStyle(
'transition: all 0.7s ease, width 1.0s cubic-bezier(3, 4, 5, 6);',
),
).toThrowError()
// Make sure the test fails if the css syntax is not valid
expect(() =>
expect(container.querySelector('.label')).not.toHaveStyle(
'font-weight bold',
),
).toThrowError()
expect(() =>
expect(container.querySelector('.label')).toHaveStyle('color white'),
).toThrowError()
document.body.removeChild(style)
document.body.removeChild(container)
})
test('properly normalizes colors', () => {
const {queryByTestId} = render(`
<span data-testid="color-example" style="background-color: #123456">Hello World</span>
`)
expect(queryByTestId('color-example')).toHaveStyle(
'background-color: #123456',
)
})
test('properly normalizes colors for border', () => {
const {queryByTestId} = render(`
<span data-testid="color-example" style="border: 1px solid #fff">Hello World</span>
`)
expect(queryByTestId('color-example')).toHaveStyle('border: 1px solid #fff')
})
test('handles different color declaration formats', () => {
const {queryByTestId} = render(`
<span data-testid="color-example" style="color: rgba(0, 0, 0, 1); background-color: #000000">Hello World</span>
`)
expect(queryByTestId('color-example')).toHaveStyle('color: #000000')
expect(queryByTestId('color-example')).toHaveStyle(
'background-color: rgba(0, 0, 0, 1)',
)
})
test('handles nonexistent styles', () => {
const {container} = render(`
<div class="label" style="background-color: blue; height: 100%">
Hello World
</div>
`)
expect(container.querySelector('.label')).not.toHaveStyle(
'whatever: anything',
)
})
describe('object syntax', () => {
test('handles styles as object', () => {
const {container} = render(`
<div class="label" style="background-color: blue; height: 100%">
Hello World
</div>
`)
expect(container.querySelector('.label')).toHaveStyle({
backgroundColor: 'blue',
})
expect(container.querySelector('.label')).toHaveStyle({
backgroundColor: 'blue',
height: '100%',
})
expect(container.querySelector('.label')).not.toHaveStyle({
backgroundColor: 'red',
height: '100%',
})
expect(container.querySelector('.label')).not.toHaveStyle({
whatever: 'anything',
})
})
test('supports dash-cased property names', () => {
const {container} = render(`
<div class="label" style="background-color: blue; height: 100%">
Hello World
</div>
`)
expect(container.querySelector('.label')).toHaveStyle({
'background-color': 'blue',
})
})
test('requires strict empty properties matching', () => {
const {container} = render(`
<div class="label" style="width: 100%;height: 100%">
Hello World
</div>
`)
expect(container.querySelector('.label')).not.toHaveStyle({
width: '100%',
height: '',
})
expect(container.querySelector('.label')).not.toHaveStyle({
width: '',
height: '',
})
})
})
})