From 91d6425520f6d1704a9416f7086b8f38fe38239d Mon Sep 17 00:00:00 2001 From: MattAgn Date: Sat, 23 Jan 2021 15:29:02 +0100 Subject: [PATCH 1/2] fix: make getByDisplayValue check the default value of text inputs --- .../__snapshots__/render.test.js.snap | 35 +++++++++++++++++++ src/__tests__/render.test.js | 13 +++++++ src/helpers/getByAPI.js | 5 ++- 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/__tests__/__snapshots__/render.test.js.snap b/src/__tests__/__snapshots__/render.test.js.snap index 11881c3e8..1562b1549 100644 --- a/src/__tests__/__snapshots__/render.test.js.snap +++ b/src/__tests__/__snapshots__/render.test.js.snap @@ -20,12 +20,19 @@ exports[`debug 1`] = ` /> + + + + + { render() { @@ -67,7 +69,9 @@ class Banana extends React.Component { testID="bananaChef" placeholder={PLACEHOLDER_CHEF} value={INPUT_CHEF} + defaultValue={DEFAULT_INPUT_CHEF} /> + Change freshness! @@ -201,6 +205,15 @@ test('getByDisplayValue, queryByDisplayValue', () => { expect(() => queryByDisplayValue(/fresh/i)).toThrow('Expected 1 but found 2'); }); +test('getByDisplayValue, queryByDisplayValue get element by default value only when value is undefined', () => { + const { getByDisplayValue, queryByDisplayValue } = render(); + expect(() => getByDisplayValue(DEFAULT_INPUT_CHEF)).toThrow(); + expect(queryByDisplayValue(DEFAULT_INPUT_CHEF)).toBeNull(); + + expect(getByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy(); + expect(queryByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy(); +}); + test('getAllByDisplayValue, queryAllByDisplayValue', () => { const { getAllByDisplayValue, queryAllByDisplayValue } = render(); const inputs = getAllByDisplayValue(/fresh/i); diff --git a/src/helpers/getByAPI.js b/src/helpers/getByAPI.js index 9f6a0d5c6..a7fe23fcc 100644 --- a/src/helpers/getByAPI.js +++ b/src/helpers/getByAPI.js @@ -107,11 +107,10 @@ const getTextInputNodeByPlaceholderText = (node, placeholder) => { const getTextInputNodeByDisplayValue = (node, value) => { try { const { TextInput } = require('react-native'); + const nodeValue = node.props.value || node.props.defaultValue; return ( filterNodeByType(node, TextInput) && - (typeof value === 'string' - ? value === node.props.value - : value.test(node.props.value)) + (typeof value === 'string' ? value === nodeValue : value.test(nodeValue)) ); } catch (error) { throw createLibraryNotSupportedError(error); From 57bd13b9bb4a9eebf6c29319c2773f77c1a5344f Mon Sep 17 00:00:00 2001 From: MattAgn Date: Mon, 25 Jan 2021 13:39:51 +0100 Subject: [PATCH 2/2] fix: make getByDisplayValue only use defaultValue if value is undefined if the value of TextInput is an empty string, then the TextInput will not use the defaultValue and display the empty string. Our tests now do the same --- .../__snapshots__/render.test.js.snap | 35 +++++++++++++++++++ src/__tests__/render.test.js | 4 +++ src/helpers/getByAPI.js | 5 ++- 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/__tests__/__snapshots__/render.test.js.snap b/src/__tests__/__snapshots__/render.test.js.snap index 1562b1549..13462ca90 100644 --- a/src/__tests__/__snapshots__/render.test.js.snap +++ b/src/__tests__/__snapshots__/render.test.js.snap @@ -33,6 +33,13 @@ exports[`debug 1`] = ` rejectResponderTermination={true} underlineColorAndroid=\\"transparent\\" /> + + + + + { defaultValue={DEFAULT_INPUT_CHEF} /> + Change freshness! @@ -210,6 +211,9 @@ test('getByDisplayValue, queryByDisplayValue get element by default value only w expect(() => getByDisplayValue(DEFAULT_INPUT_CHEF)).toThrow(); expect(queryByDisplayValue(DEFAULT_INPUT_CHEF)).toBeNull(); + expect(() => getByDisplayValue('hello')).toThrow(); + expect(queryByDisplayValue('hello')).toBeNull(); + expect(getByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy(); expect(queryByDisplayValue(DEFAULT_INPUT_CUSTOMER)).toBeTruthy(); }); diff --git a/src/helpers/getByAPI.js b/src/helpers/getByAPI.js index a7fe23fcc..41e74bdd5 100644 --- a/src/helpers/getByAPI.js +++ b/src/helpers/getByAPI.js @@ -107,7 +107,10 @@ const getTextInputNodeByPlaceholderText = (node, placeholder) => { const getTextInputNodeByDisplayValue = (node, value) => { try { const { TextInput } = require('react-native'); - const nodeValue = node.props.value || node.props.defaultValue; + const nodeValue = + node.props.value !== undefined + ? node.props.value + : node.props.defaultValue; return ( filterNodeByType(node, TextInput) && (typeof value === 'string' ? value === nodeValue : value.test(nodeValue))