Skip to content

Commit b41b2e7

Browse files
lgandeckialexkrolick
authored andcommitted
feat(queryByValue): add get/query by value (#35)
* get/query by value * updated test
1 parent cde0cdf commit b41b2e7

File tree

4 files changed

+66
-0
lines changed

4 files changed

+66
-0
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ when a real user uses it.
7777
* [`getByText`](#getbytext)
7878
* [`getByAltText`](#getbyalttext)
7979
* [`getByTitle`](#getbytitle)
80+
* [`getByValue`](#getbyvalue)
8081
* [`getByTestId`](#getbytestid)
8182
* [`wait`](#wait)
8283
* [`waitForElement`](#waitforelement)
@@ -320,6 +321,26 @@ Returns the element that has the matching `title` attribute.
320321
const deleteElement = getByTitle(container, 'Delete')
321322
```
322323

324+
### `getByValue`
325+
326+
```typescript
327+
getByValue(
328+
container: HTMLElement,
329+
value: TextMatch,
330+
options?: {
331+
exact?: boolean = true,
332+
collapseWhitespace?: boolean = false,
333+
trim?: boolean = true,
334+
}): HTMLElement
335+
```
336+
337+
Returns the element that has the matching value.
338+
339+
```javascript
340+
// <input type="text" id="lastName" defaultValue="Norris" />
341+
const lastNameInput = getByValue('Norris')
342+
```
343+
323344
### `getByTestId`
324345

325346
```typescript

src/__tests__/__snapshots__/element-queries.js.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ exports[`get throws a useful error message 6`] = `
4848
</div>"
4949
`;
5050

51+
exports[`get throws a useful error message 7`] = `
52+
"Unable to find an element with the value: LucyRicardo.
53+
54+
<div>
55+
<div />
56+
</div>"
57+
`;
58+
5159
exports[`label with no form control 1`] = `
5260
"Found a label with the text of: /alone/, however no form control was found associated to that label. Make sure you're using the \\"for\\" attribute or \\"aria-labelledby\\" attribute correctly.
5361

src/__tests__/element-queries.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ test('get throws a useful error message', () => {
2424
getByTestId,
2525
getByAltText,
2626
getByTitle,
27+
getByValue,
2728
} = render('<div />')
2829
expect(() => getByLabelText('LucyRicardo')).toThrowErrorMatchingSnapshot()
2930
expect(() =>
@@ -33,6 +34,7 @@ test('get throws a useful error message', () => {
3334
expect(() => getByTestId('LucyRicardo')).toThrowErrorMatchingSnapshot()
3435
expect(() => getByAltText('LucyRicardo')).toThrowErrorMatchingSnapshot()
3536
expect(() => getByTitle('LucyRicardo')).toThrowErrorMatchingSnapshot()
37+
expect(() => getByValue('LucyRicardo')).toThrowErrorMatchingSnapshot()
3638
})
3739

3840
test('can get elements by matching their text content', () => {
@@ -132,6 +134,19 @@ test('query/get element by its title', () => {
132134
expect(queryByTitle('Delete').id).toEqual('2')
133135
})
134136

137+
test('query/get element by its value', () => {
138+
const {getByValue, queryByValue} = render(`
139+
<div>
140+
<input placeholder="name" type="text"/>
141+
<input placeholder="lastname" type="text" value="Norris"/>
142+
<input placeholder="email" type="text"/>
143+
</div>
144+
`)
145+
146+
expect(queryByValue('Norris').placeholder).toEqual('lastname')
147+
expect(getByValue('Norris').placeholder).toEqual('lastname')
148+
})
149+
135150
test('can get elements by data-testid attribute', () => {
136151
const {queryByTestId} = render(`<div data-testid="firstName"></div>`)
137152
expect(queryByTestId('firstName')).toBeInTheDOM()

src/queries.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,8 @@ const queryByTestId = queryByAttribute.bind(null, 'data-testid')
112112
const queryAllByTestId = queryAllByAttribute.bind(null, 'data-testid')
113113
const queryByTitle = queryByAttribute.bind(null, 'title')
114114
const queryAllByTitle = queryAllByAttribute.bind(null, 'title')
115+
const queryByValue = queryByAttribute.bind(null, 'value')
116+
const queryAllByValue = queryAllByAttribute.bind(null, 'value')
115117

116118
function queryAllByAltText(
117119
container,
@@ -166,6 +168,22 @@ function getByTitle(...args) {
166168
return firstResultOrNull(getAllByTitle, ...args)
167169
}
168170

171+
function getAllByValue(container, value, ...rest) {
172+
const els = queryAllByValue(container, value, ...rest)
173+
if (!els.length) {
174+
throw new Error(
175+
`Unable to find an element with the value: ${value}. \n\n${debugDOM(
176+
container,
177+
)}`,
178+
)
179+
}
180+
return els
181+
}
182+
183+
function getByValue(...args) {
184+
return firstResultOrNull(getAllByValue, ...args)
185+
}
186+
169187
function getAllByPlaceholderText(container, text, ...rest) {
170188
const els = queryAllByPlaceholderText(container, text, ...rest)
171189
if (!els.length) {
@@ -264,6 +282,10 @@ export {
264282
queryAllByTitle,
265283
getByTitle,
266284
getAllByTitle,
285+
queryByValue,
286+
queryAllByValue,
287+
getByValue,
288+
getAllByValue,
267289
}
268290

269291
/* eslint complexity:["error", 14] */

0 commit comments

Comments
 (0)