Skip to content

Commit 0cbdeb9

Browse files
julienwKent C. Dodds
authored and
Kent C. Dodds
committed
fix(getByText): support inputs where type is either button or submit (#185)
* Make getByText support inputs where type is either `button` or `submit` Closes testing-library/react-testing-library#248 * change quotes
1 parent 228e91f commit 0cbdeb9

File tree

3 files changed

+36
-0
lines changed

3 files changed

+36
-0
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,14 @@ matching the given [`TextMatch`](#textmatch).
296296
const aboutAnchorNode = getByText(container, /about/i)
297297
```
298298

299+
It also works properly with `input`s whose `type` attribute is either `submit`
300+
or `button`:
301+
302+
```javascript
303+
// <input type="submit" value="Send data" />
304+
const submitButton = getByText(container, /send data/i)
305+
```
306+
299307
> NOTE: see [`getByLabelText`](#getbylabeltext) for more details on how and when to use the `selector` option
300308
301309
The `ignore` option accepts a query selector. If the
@@ -702,6 +710,17 @@ This function is also used internally when querying nodes by their text content.
702710
This enables functions like `getByText` and `queryByText` to work as expected,
703711
finding elements in the DOM similarly to how users would do.
704712

713+
The function has a special behavior for some input elements:
714+
715+
```javascript
716+
// <input type="submit" value="Send data" />
717+
// <input type="button" value="Push me" />
718+
const submitText = getNodeText(container.querySelector('input[type=submit]')) // "Send data"
719+
const buttonText = getNodeText(container.querySelector('input[type=button]')) // "Push me"
720+
721+
These elements use the attribute `value` and display its value to the user.
722+
```
723+
705724
## Custom Jest Matchers
706725

707726
When using [jest][], it is convenient to import a set of custom matchers that

src/__tests__/element-queries.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ test('can get elements by matching their text across adjacent text nodes', () =>
7676
expect(queryByText('£24.99')).toBeTruthy()
7777
})
7878

79+
test('can get input elements with type submit or button', () => {
80+
const {queryByText} = render(`
81+
<div>
82+
<input type="submit" value="Send data"/>
83+
<input type="button" value="Push me!"/>
84+
<input type="text" value="user data" />
85+
</div>
86+
`)
87+
expect(queryByText('Send data')).toBeTruthy()
88+
expect(queryByText('Push me!')).toBeTruthy()
89+
expect(queryByText('user data')).toBeFalsy()
90+
})
91+
7992
test('matches case with RegExp matcher', () => {
8093
const {queryByText} = render(`
8194
<span>STEP 1 of 4</span>

src/get-node-text.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
function getNodeText(node) {
22
const window = node.ownerDocument.defaultView
33

4+
if (node.matches('input[type=submit], input[type=button]')) {
5+
return node.value;
6+
}
7+
48
return Array.from(node.childNodes)
59
.filter(
610
child =>

0 commit comments

Comments
 (0)