From 00d14e8fc65bdbc4a2c10172f4043dcf286fb0de Mon Sep 17 00:00:00 2001 From: Julien Wajsberg Date: Thu, 20 Dec 2018 19:50:32 +0100 Subject: [PATCH 1/2] Make getByText support inputs where type is either `button` or `submit` Fixes kentcdodds/react-testing-library#248 --- README.md | 19 +++++++++++++++++++ src/__tests__/element-queries.js | 13 +++++++++++++ src/get-node-text.js | 4 ++++ 3 files changed, 36 insertions(+) diff --git a/README.md b/README.md index 119237c8..0f1c20ad 100644 --- a/README.md +++ b/README.md @@ -296,6 +296,14 @@ matching the given [`TextMatch`](#textmatch). const aboutAnchorNode = getByText(container, /about/i) ``` +It also works properly with `input`s whose `type` attribute is either `submit` +or `button`: + +```javascript +// +const submitButton = getByText(container, /send data/i) +``` + > NOTE: see [`getByLabelText`](#getbylabeltext) for more details on how and when to use the `selector` option 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. This enables functions like `getByText` and `queryByText` to work as expected, finding elements in the DOM similarly to how users would do. +The function has a special behavior for some input elements: + +```javascript +// +// +const submitText = getNodeText(container.querySelector('input[type=submit]')) // "Send data" +const buttonText = getNodeText(container.querySelector('input[type=button]')) // "Push me" + +These elements use the attribute `value` and display its value to the user. +``` + ## Custom Jest Matchers When using [jest][], it is convenient to import a set of custom matchers that diff --git a/src/__tests__/element-queries.js b/src/__tests__/element-queries.js index 0f9b2535..bc30089a 100644 --- a/src/__tests__/element-queries.js +++ b/src/__tests__/element-queries.js @@ -76,6 +76,19 @@ test('can get elements by matching their text across adjacent text nodes', () => expect(queryByText('£24.99')).toBeTruthy() }) +test('can get input elements with type submit or button', () => { + const {queryByText} = render(` +
+ + + +
+ `) + expect(queryByText('Send data')).toBeTruthy() + expect(queryByText('Push me!')).toBeTruthy() + expect(queryByText('user data')).toBeFalsy() +}) + test('matches case with RegExp matcher', () => { const {queryByText} = render(` STEP 1 of 4 diff --git a/src/get-node-text.js b/src/get-node-text.js index 1f7c3ecd..59a42d4b 100644 --- a/src/get-node-text.js +++ b/src/get-node-text.js @@ -1,6 +1,10 @@ function getNodeText(node) { const window = node.ownerDocument.defaultView + if (node.matches("input[type=submit], input[type=button]")) { + return node.value; + } + return Array.from(node.childNodes) .filter( child => From 79470f23ea50f47e76fe285cc30e684a123f6a53 Mon Sep 17 00:00:00 2001 From: "Kent C. Dodds" Date: Thu, 20 Dec 2018 12:25:38 -0700 Subject: [PATCH 2/2] change quotes --- src/get-node-text.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/get-node-text.js b/src/get-node-text.js index 59a42d4b..82af6b67 100644 --- a/src/get-node-text.js +++ b/src/get-node-text.js @@ -1,7 +1,7 @@ function getNodeText(node) { const window = node.ownerDocument.defaultView - if (node.matches("input[type=submit], input[type=button]")) { + if (node.matches('input[type=submit], input[type=button]')) { return node.value; }