Skip to content

Commit f792625

Browse files
Kent C. DoddsKent C. Dodds
Kent C. Dodds
authored and
Kent C. Dodds
committed
fix: remove deprecated queries (#228)
Closes #223 BREAKING CHANGE: This removes the deprecated queries `ByValue` and `BySelectText`. Use `ByDisplayValue` instead.
1 parent 393377a commit f792625

File tree

4 files changed

+40
-118
lines changed

4 files changed

+40
-118
lines changed

src/__tests__/element-queries.js

Lines changed: 28 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ beforeEach(() => {
88
test('query can return null', () => {
99
const {
1010
queryByLabelText,
11-
queryBySelectText,
11+
queryByDisplayValue,
1212
queryByPlaceholderText,
1313
queryByText,
1414
queryByTestId,
1515
queryByAltText,
1616
} = render('<div />')
1717
expect(queryByTestId('LucyRicardo')).toBeNull()
1818
expect(queryByLabelText('LucyRicardo')).toBeNull()
19-
expect(queryBySelectText('LucyRicardo')).toBeNull()
19+
expect(queryByDisplayValue('LucyRicardo')).toBeNull()
2020
expect(queryByPlaceholderText('LucyRicardo')).toBeNull()
2121
expect(queryByText('LucyRicardo')).toBeNull()
2222
expect(queryByAltText('LucyRicardo')).toBeNull()
@@ -25,27 +25,18 @@ test('query can return null', () => {
2525
test('get throws a useful error message', () => {
2626
const {
2727
getByLabelText,
28-
getBySelectText,
28+
getByDisplayValue,
2929
getByPlaceholderText,
3030
getByText,
3131
getByTestId,
3232
getByAltText,
3333
getByTitle,
34-
getByValue,
3534
getByRole,
3635
} = render('<div />')
3736
expect(() => getByLabelText('LucyRicardo'))
3837
.toThrowErrorMatchingInlineSnapshot(`
3938
"Unable to find a label with the text of: LucyRicardo
4039
41-
<div>
42-
<div />
43-
</div>"
44-
`)
45-
expect(() => getBySelectText('LucyRicardo'))
46-
.toThrowErrorMatchingInlineSnapshot(`
47-
"Unable to find a <select> element with the selected option's text: LucyRicardo
48-
4940
<div>
5041
<div />
5142
</div>"
@@ -86,7 +77,8 @@ test('get throws a useful error message', () => {
8677
<div />
8778
</div>"
8879
`)
89-
expect(() => getByValue('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(`
80+
expect(() => getByDisplayValue('LucyRicardo'))
81+
.toThrowErrorMatchingInlineSnapshot(`
9082
"Unable to find an element with the value: LucyRicardo.
9183
9284
<div>
@@ -285,20 +277,20 @@ test('query/get title element of SVG', () => {
285277
})
286278

287279
test('query/get element by its value', () => {
288-
const {getByValue, queryByValue} = render(`
280+
const {getByDisplayValue, queryByDisplayValue} = render(`
289281
<div>
290282
<input placeholder="name" type="text"/>
291283
<input placeholder="lastname" type="text" value="Norris"/>
292284
<input placeholder="email" type="text"/>
293285
</div>
294286
`)
295287

296-
expect(getByValue('Norris').placeholder).toEqual('lastname')
297-
expect(queryByValue('Norris').placeholder).toEqual('lastname')
288+
expect(getByDisplayValue('Norris').placeholder).toEqual('lastname')
289+
expect(queryByDisplayValue('Norris').placeholder).toEqual('lastname')
298290
})
299291

300292
test('query/get select by text with the default option selected', () => {
301-
const {getBySelectText, queryBySelectText} = render(`
293+
const {getByDisplayValue, queryByDisplayValue} = render(`
302294
<select id="state-select">
303295
<option value="">State</option>
304296
<option value="AL">Alabama</option>
@@ -307,12 +299,12 @@ test('query/get select by text with the default option selected', () => {
307299
</select>
308300
`)
309301

310-
expect(getBySelectText('State').id).toEqual('state-select')
311-
expect(queryBySelectText('State').id).toEqual('state-select')
302+
expect(getByDisplayValue('State').id).toEqual('state-select')
303+
expect(queryByDisplayValue('State').id).toEqual('state-select')
312304
})
313305

314306
test('query/get select by text with one option selected', () => {
315-
const {getBySelectText, queryBySelectText} = render(`
307+
const {getByDisplayValue, queryByDisplayValue} = render(`
316308
<select id="state-select">
317309
<option value="">State</option>
318310
<option value="AL">Alabama</option>
@@ -321,12 +313,12 @@ test('query/get select by text with one option selected', () => {
321313
</select>
322314
`)
323315

324-
expect(getBySelectText('Alaska').id).toEqual('state-select')
325-
expect(queryBySelectText('Alaska').id).toEqual('state-select')
316+
expect(getByDisplayValue('Alaska').id).toEqual('state-select')
317+
expect(queryByDisplayValue('Alaska').id).toEqual('state-select')
326318
})
327319

328320
test('query/get select by text with multiple options selected', () => {
329-
const {getBySelectText, queryBySelectText} = render(`
321+
const {getByDisplayValue, queryByDisplayValue} = render(`
330322
<select multiple id="state-select">
331323
<option value="">State</option>
332324
<option selected value="AL">Alabama</option>
@@ -335,8 +327,8 @@ test('query/get select by text with multiple options selected', () => {
335327
</select>
336328
`)
337329

338-
expect(getBySelectText('Alabama').id).toEqual('state-select')
339-
expect(queryBySelectText('Alaska').id).toEqual('state-select')
330+
expect(getByDisplayValue('Alabama').id).toEqual('state-select')
331+
expect(queryByDisplayValue('Alaska').id).toEqual('state-select')
340332
})
341333

342334
describe('query by test id', () => {
@@ -374,7 +366,7 @@ test('getAll* matchers return an array', () => {
374366
getAllByAltText,
375367
getAllByTestId,
376368
getAllByLabelText,
377-
getAllBySelectText,
369+
getAllByDisplayValue,
378370
getAllByPlaceholderText,
379371
getAllByText,
380372
getAllByRole,
@@ -412,8 +404,8 @@ test('getAll* matchers return an array', () => {
412404
expect(getAllByTestId('poster')).toHaveLength(3)
413405
expect(getAllByPlaceholderText(/The Rock/)).toHaveLength(1)
414406
expect(getAllByLabelText('User Name')).toHaveLength(1)
415-
expect(getAllBySelectText('Japanese cars')).toHaveLength(1)
416-
expect(getAllBySelectText(/cars$/)).toHaveLength(2)
407+
expect(getAllByDisplayValue('Japanese cars')).toHaveLength(1)
408+
expect(getAllByDisplayValue(/cars$/)).toHaveLength(2)
417409
expect(getAllByText(/^where/i)).toHaveLength(1)
418410
expect(getAllByRole(/container/i)).toHaveLength(1)
419411
})
@@ -423,11 +415,10 @@ test('getAll* matchers throw for 0 matches', () => {
423415
getAllByAltText,
424416
getAllByTestId,
425417
getAllByLabelText,
426-
getAllBySelectText,
418+
getAllByDisplayValue,
427419
getAllByPlaceholderText,
428420
getAllByText,
429421
getAllByRole,
430-
getAllByDisplayValue,
431422
} = render(`
432423
<div role="container">
433424
<label>No Matches Please</label>
@@ -438,7 +429,7 @@ test('getAll* matchers throw for 0 matches', () => {
438429
expect(() => getAllByAltText('nope')).toThrow()
439430
expect(() => getAllByLabelText('nope')).toThrow()
440431
expect(() => getAllByLabelText('no matches please')).toThrow()
441-
expect(() => getAllBySelectText('nope')).toThrow()
432+
expect(() => getAllByDisplayValue('nope')).toThrow()
442433
expect(() => getAllByPlaceholderText('nope')).toThrow()
443434
expect(() => getAllByText('nope')).toThrow()
444435
expect(() => getAllByRole('nope')).toThrow()
@@ -450,7 +441,7 @@ test('queryAll* matchers return an array for 0 matches', () => {
450441
queryAllByAltText,
451442
queryAllByTestId,
452443
queryAllByLabelText,
453-
queryAllBySelectText,
444+
queryAllByDisplayValue,
454445
queryAllByPlaceholderText,
455446
queryAllByText,
456447
queryAllByRole,
@@ -461,7 +452,7 @@ test('queryAll* matchers return an array for 0 matches', () => {
461452
expect(queryAllByTestId('nope')).toHaveLength(0)
462453
expect(queryAllByAltText('nope')).toHaveLength(0)
463454
expect(queryAllByLabelText('nope')).toHaveLength(0)
464-
expect(queryAllBySelectText('nope')).toHaveLength(0)
455+
expect(queryAllByDisplayValue('nope')).toHaveLength(0)
465456
expect(queryAllByPlaceholderText('nope')).toHaveLength(0)
466457
expect(queryAllByText('nope')).toHaveLength(0)
467458
expect(queryAllByRole('nope')).toHaveLength(0)
@@ -629,24 +620,18 @@ test('get throws a useful error message without DOM in Cypress', () => {
629620
document.defaultView.Cypress = {}
630621
const {
631622
getByLabelText,
632-
getBySelectText,
633623
getByPlaceholderText,
634624
getByText,
635625
getByTestId,
636626
getByAltText,
637627
getByTitle,
638-
getByValue,
628+
getByDisplayValue,
639629
} = render('<div />')
640630
expect(() =>
641631
getByLabelText('LucyRicardo'),
642632
).toThrowErrorMatchingInlineSnapshot(
643633
`"Unable to find a label with the text of: LucyRicardo"`,
644634
)
645-
expect(() =>
646-
getBySelectText('LucyRicardo'),
647-
).toThrowErrorMatchingInlineSnapshot(
648-
`"Unable to find a <select> element with the selected option's text: LucyRicardo"`,
649-
)
650635
expect(() =>
651636
getByPlaceholderText('LucyRicardo'),
652637
).toThrowErrorMatchingInlineSnapshot(
@@ -664,7 +649,9 @@ test('get throws a useful error message without DOM in Cypress', () => {
664649
expect(() => getByTitle('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
665650
`"Unable to find an element with the title: LucyRicardo."`,
666651
)
667-
expect(() => getByValue('LucyRicardo')).toThrowErrorMatchingInlineSnapshot(
652+
expect(() =>
653+
getByDisplayValue('LucyRicardo'),
654+
).toThrowErrorMatchingInlineSnapshot(
668655
`"Unable to find an element with the value: LucyRicardo."`,
669656
)
670657
})

src/__tests__/text-matchers.js

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,14 @@ cases(
3939
query: `Dwayne 'The Rock' Johnson`,
4040
queryFn: `queryAllByPlaceholderText`,
4141
},
42-
queryAllBySelectText: {
42+
'queryAllByDisplayValue (for select)': {
4343
dom: `
4444
<select>
4545
<option>Option 1</option>
4646
<option>Option 2</option>
4747
</select>`,
4848
query: `Option 1`,
49-
queryFn: `queryAllBySelectText`,
49+
queryFn: `queryAllByDisplayValue`,
5050
},
5151
queryAllByText: {
5252
dom: `<p>Some content</p>`,
@@ -99,14 +99,14 @@ cases(
9999
query: /^Dwayne/,
100100
queryFn: `queryAllByPlaceholderText`,
101101
},
102-
queryAllBySelectText: {
102+
'queryAllByDisplayValue (for select)': {
103103
dom: `
104104
<select>
105105
<option> Option 1 </option>
106106
<option>Option 2</option>
107107
</select>`,
108108
query: `Option 1`,
109-
queryFn: `queryAllBySelectText`,
109+
queryFn: `queryAllByDisplayValue`,
110110
},
111111
queryAllByText: {
112112
dom: `
@@ -161,14 +161,14 @@ cases(
161161
query: `Dwayne 'The Rock' Johnson`,
162162
queryFn: `queryAllByPlaceholderText`,
163163
},
164-
queryAllBySelectText: {
164+
'queryAllByDisplayValue (for select)': {
165165
dom: `
166166
<select>
167167
<option>Option 1</option>
168168
<option>Option 2</option>
169169
</select>`,
170170
query: `Option 1`,
171-
queryFn: `queryAllBySelectText`,
171+
queryFn: `queryAllByDisplayValue`,
172172
},
173173
queryAllByLabelText: {
174174
dom: `
@@ -235,9 +235,9 @@ cases(
235235
dom: `<input placeholder="User ${LRM}name" />`,
236236
queryFn: 'queryAllByPlaceholderText',
237237
},
238-
queryAllBySelectText: {
238+
'queryAllByDisplayValue (for select)': {
239239
dom: `<select><option>User ${LRM}name</option></select>`,
240-
queryFn: 'queryAllBySelectText',
240+
queryFn: 'queryAllByDisplayValue',
241241
},
242242
queryAllByText: {
243243
dom: `<div>User ${LRM}name</div>`,
@@ -251,10 +251,6 @@ cases(
251251
dom: `<div title="User ${LRM}name" />`,
252252
queryFn: 'queryAllByTitle',
253253
},
254-
queryAllByValue: {
255-
dom: `<input value="User ${LRM}name" />`,
256-
queryFn: 'queryAllByValue',
257-
},
258254
queryAllByDisplayValue: {
259255
dom: `<input value="User ${LRM}name" />`,
260256
queryFn: 'queryAllByDisplayValue',

src/queries.js

Lines changed: 0 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -137,27 +137,6 @@ function queryByTitle(...args) {
137137
return firstResultOrNull(queryAllByTitle, ...args)
138138
}
139139

140-
function queryAllBySelectText(
141-
container,
142-
text,
143-
{exact = true, collapseWhitespace, trim, normalizer} = {},
144-
) {
145-
const matcher = exact ? matches : fuzzyMatches
146-
const matchNormalizer = makeNormalizer({collapseWhitespace, trim, normalizer})
147-
return Array.from(container.querySelectorAll('select')).filter(selectNode => {
148-
const selectedOptions = Array.from(selectNode.options).filter(
149-
option => option.selected,
150-
)
151-
return selectedOptions.some(optionNode =>
152-
matcher(getNodeText(optionNode), optionNode, text, matchNormalizer),
153-
)
154-
})
155-
}
156-
157-
function queryBySelectText(...args) {
158-
return firstResultOrNull(queryAllBySelectText, ...args)
159-
}
160-
161140
function getTestIdAttribute() {
162141
return getConfig().testIdAttribute
163142
}
@@ -168,8 +147,6 @@ const queryByTestId = (...args) =>
168147
queryByAttribute(getTestIdAttribute(), ...args)
169148
const queryAllByTestId = (...args) =>
170149
queryAllByAttribute(getTestIdAttribute(), ...args)
171-
const queryByValue = queryByAttribute.bind(null, 'value')
172-
const queryAllByValue = queryAllByAttribute.bind(null, 'value')
173150
const queryByRole = queryByAttribute.bind(null, 'role')
174151
const queryAllByRole = queryAllByAttribute.bind(null, 'role')
175152

@@ -251,21 +228,6 @@ function getByTitle(...args) {
251228
return firstResultOrNull(getAllByTitle, ...args)
252229
}
253230

254-
function getAllByValue(container, value, ...rest) {
255-
const els = queryAllByValue(container, value, ...rest)
256-
if (!els.length) {
257-
throw getElementError(
258-
`Unable to find an element with the value: ${value}.`,
259-
container,
260-
)
261-
}
262-
return els
263-
}
264-
265-
function getByValue(...args) {
266-
return firstResultOrNull(getAllByValue, ...args)
267-
}
268-
269231
function getAllByPlaceholderText(container, text, ...rest) {
270232
const els = queryAllByPlaceholderText(container, text, ...rest)
271233
if (!els.length) {
@@ -346,21 +308,6 @@ function getByRole(...args) {
346308
return firstResultOrNull(getAllByRole, ...args)
347309
}
348310

349-
function getAllBySelectText(container, text, ...rest) {
350-
const els = queryAllBySelectText(container, text, ...rest)
351-
if (!els.length) {
352-
throw getElementError(
353-
`Unable to find a <select> element with the selected option's text: ${text}`,
354-
container,
355-
)
356-
}
357-
return els
358-
}
359-
360-
function getBySelectText(...args) {
361-
return firstResultOrNull(getAllBySelectText, ...args)
362-
}
363-
364311
function getAllByDisplayValue(container, value, ...rest) {
365312
const els = queryAllByDisplayValue(container, value, ...rest)
366313
if (!els.length) {
@@ -425,10 +372,6 @@ export {
425372
queryAllByAltText,
426373
getByAltText,
427374
getAllByAltText,
428-
queryBySelectText,
429-
queryAllBySelectText,
430-
getBySelectText,
431-
getAllBySelectText,
432375
queryByTestId,
433376
queryAllByTestId,
434377
getByTestId,
@@ -437,10 +380,6 @@ export {
437380
queryAllByTitle,
438381
getByTitle,
439382
getAllByTitle,
440-
queryByValue,
441-
queryAllByValue,
442-
getByValue,
443-
getAllByValue,
444383
queryByDisplayValue,
445384
queryAllByDisplayValue,
446385
getByDisplayValue,

0 commit comments

Comments
 (0)