/
to-have-value.js
208 lines (173 loc) · 6.75 KB
/
to-have-value.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
201
202
203
204
205
206
207
208
import {render} from './helpers/test-utils'
describe('.toHaveValue', () => {
test('handles value of text input', () => {
const {queryByTestId} = render(`
<input type="text" value="foo" data-testid="value" />
<input type="text" value="" data-testid="empty" />
<input type="text" data-testid="without" />
`)
expect(queryByTestId('value')).toHaveValue('foo')
expect(queryByTestId('value')).toHaveValue()
expect(queryByTestId('value')).not.toHaveValue('bar')
expect(queryByTestId('value')).not.toHaveValue('')
expect(queryByTestId('empty')).toHaveValue('')
expect(queryByTestId('empty')).not.toHaveValue()
expect(queryByTestId('empty')).not.toHaveValue('foo')
expect(queryByTestId('without')).toHaveValue('')
expect(queryByTestId('without')).not.toHaveValue()
expect(queryByTestId('without')).not.toHaveValue('foo')
queryByTestId('without').value = 'bar'
expect(queryByTestId('without')).toHaveValue('bar')
})
test('handles value of number input', () => {
const {queryByTestId} = render(`
<input type="number" value="5" data-testid="number" />
<input type="number" value="" data-testid="empty" />
<input type="number" data-testid="without" />
`)
expect(queryByTestId('number')).toHaveValue(5)
expect(queryByTestId('number')).toHaveValue()
expect(queryByTestId('number')).not.toHaveValue(4)
expect(queryByTestId('number')).not.toHaveValue('5')
expect(queryByTestId('empty')).toHaveValue(null)
expect(queryByTestId('empty')).not.toHaveValue()
expect(queryByTestId('empty')).not.toHaveValue('5')
expect(queryByTestId('without')).toHaveValue(null)
expect(queryByTestId('without')).not.toHaveValue()
expect(queryByTestId('without')).not.toHaveValue('10')
queryByTestId('without').value = 10
expect(queryByTestId('without')).toHaveValue(10)
})
test('handles value of select element', () => {
const {queryByTestId} = render(`
<select data-testid="single">
<option value="first">First Value</option>
<option value="second" selected>Second Value</option>
<option value="third">Third Value</option>
</select>
<select data-testid="multiple" multiple>
<option value="first">First Value</option>
<option value="second" selected>Second Value</option>
<option value="third" selected>Third Value</option>
</select>
<select data-testid="not-selected" >
<option value="" disabled selected>- Select some value - </option>
<option value="first">First Value</option>
<option value="second">Second Value</option>
<option value="third">Third Value</option>
</select>
`)
expect(queryByTestId('single')).toHaveValue('second')
expect(queryByTestId('single')).toHaveValue()
expect(queryByTestId('multiple')).toHaveValue(['second', 'third'])
expect(queryByTestId('multiple')).toHaveValue()
expect(queryByTestId('not-selected')).not.toHaveValue()
expect(queryByTestId('not-selected')).toHaveValue('')
queryByTestId('single').children[0].setAttribute('selected', true)
expect(queryByTestId('single')).toHaveValue('first')
})
test('handles value of textarea element', () => {
const {queryByTestId} = render(`
<textarea data-testid="textarea">text value</textarea>
`)
expect(queryByTestId('textarea')).toHaveValue('text value')
})
test('throws when passed checkbox or radio', () => {
const {queryByTestId} = render(`
<input data-testid="checkbox" type="checkbox" name="checkbox" value="val" checked />
<input data-testid="radio" type="radio" name="radio" value="val" checked />
`)
expect(() => {
expect(queryByTestId('checkbox')).toHaveValue('')
}).toThrow()
expect(() => {
expect(queryByTestId('radio')).toHaveValue('')
}).toThrow()
})
test('throws when the expected input value does not match', () => {
const {container} = render(`<input data-testid="one" value="foo" />`)
const input = container.firstChild
let errorMessage
try {
expect(input).toHaveValue('something else')
} catch (error) {
errorMessage = error.message
}
expect(errorMessage).toMatchInlineSnapshot(`
"<dim>expect(</><red>element</><dim>).toHaveValue(</><green>something else</><dim>)</>
Expected the element to have value:
<green> something else</>
Received:
<red> foo</>"
`)
})
test('throws with type information when the expected text input value has loose equality with received value', () => {
const {container} = render(`<input data-testid="one" value="8" />`)
const input = container.firstChild
let errorMessage
try {
expect(input).toHaveValue(8)
} catch (error) {
errorMessage = error.message
}
expect(errorMessage).toMatchInlineSnapshot(`
"<dim>expect(</><red>element</><dim>).toHaveValue(</><green>8</><dim>)</>
Expected the element to have value:
<green> 8 (number)</>
Received:
<red> 8 (string)</>"
`)
})
test('throws when using not but the expected input value does match', () => {
const {container} = render(`<input data-testid="one" value="foo" />`)
const input = container.firstChild
let errorMessage
try {
expect(input).not.toHaveValue('foo')
} catch (error) {
errorMessage = error.message
}
expect(errorMessage).toMatchInlineSnapshot(`
"<dim>expect(</><red>element</><dim>).not.toHaveValue(</><green>foo</><dim>)</>
Expected the element not to have value:
<green> foo</>
Received:
<red> foo</>"
`)
})
test('throws when the form has no a value but a value is expected', () => {
const {container} = render(`<input data-testid="one" />`)
const input = container.firstChild
let errorMessage
try {
expect(input).toHaveValue()
} catch (error) {
errorMessage = error.message
}
expect(errorMessage).toMatchInlineSnapshot(`
"<dim>expect(</><red>element</><dim>).toHaveValue(</><green>expected</><dim>)</>
Expected the element to have value:
<green> (any)</>
Received:
"
`)
})
test('throws when the form has a value but none is expected', () => {
const {container} = render(`<input data-testid="one" value="foo" />`)
const input = container.firstChild
let errorMessage
try {
expect(input).not.toHaveValue()
} catch (error) {
errorMessage = error.message
}
expect(errorMessage).toMatchInlineSnapshot(`
"<dim>expect(</><red>element</><dim>).not.toHaveValue(</><green>expected</><dim>)</>
Expected the element not to have value:
<green> (any)</>
Received:
<red> foo</>"
`)
})
})
/* eslint max-lines-per-function:0 */