Skip to content

Commit 2b6dc31

Browse files
authored
TextField - handle value change to undefined (#3676)
* TextField - handle value change to undefined * Fix test
1 parent 333d7d2 commit 2b6dc31

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/components/textField/__tests__/index.driver.spec.tsx

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React, {useState} from 'react';
1+
import React, {useEffect, useState} from 'react';
22
import {render} from '@testing-library/react-native';
33
import Constants from '../../../commons/Constants';
44
import Assets from '../../../assets';
@@ -15,14 +15,19 @@ const helperText = 'Helper Text';
1515

1616
function TestCase(textFieldProps?: TextFieldProps) {
1717
const [value, setValue] = useState(textFieldProps?.value);
18+
19+
useEffect(() => {
20+
setValue(textFieldProps?.value);
21+
}, [textFieldProps?.value]);
22+
1823
return (
1924
<View>
2025
<TextField {...textFieldProps} testID={TEXT_FIELD_TEST_ID} value={value} onChangeText={setValue}/>
2126
</View>
2227
);
2328
}
2429

25-
const validate = jest.fn((value: string) => {
30+
const validate = jest.fn((value?: string) => {
2631
return !!value;
2732
});
2833

@@ -214,6 +219,18 @@ describe('TextField', () => {
214219
expect(textFieldDriver.getValidationMessage().getText()).toEqual('mock message');
215220
});
216221

222+
it('should render validationMessage when input is requires after changing the value to undefined', () => {
223+
const renderTree = render(<TestCase {...defaultProps} value={'Some text'} validate={'required'} validationMessage={'mock message'} enableErrors validateOnChange/>);
224+
const textFieldDriver = TextFieldDriver({renderTree, testID: TEXT_FIELD_TEST_ID});
225+
226+
expect(textFieldDriver.getValidationMessage().exists()).toBe(false);
227+
expect(textFieldDriver.getValidationMessage().getText()).toEqual('');
228+
229+
renderTree.rerender(<TestCase {...defaultProps} validate={'required'} validationMessage={'mock message'} enableErrors validateOnChange/>);
230+
expect(textFieldDriver.getValidationMessage().exists()).toBe(true);
231+
expect(textFieldDriver.getValidationMessage().getText()).toEqual('mock message');
232+
});
233+
217234
it('should display validation error message when validation fail after blur', () => {
218235
const renderTree = render(<TestCase {...defaultProps} validate={'email'} validationMessage={'email is invalid'} enableErrors validateOnBlur/>);
219236
const textFieldDriver = TextFieldDriver({renderTree, testID: TEXT_FIELD_TEST_ID});
@@ -490,7 +507,7 @@ describe('TextField', () => {
490507
const props = {
491508
...defaultProps,
492509
value: '10000',
493-
formatter: value => priceFormatter.format(Number(value))
510+
formatter: (value?: string) => priceFormatter.format(Number(value))
494511
};
495512

496513
it('should format value while not focused based on formatter prop', () => {

src/components/textField/useFieldState.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@ export default function useFieldState({
6565

6666
if (validateOnChange) {
6767
if (validationDebounceTime) {
68-
debouncedValidateField(propsValue);
68+
debouncedValidateField(propsValue ?? '');
6969
} else {
70-
validateField(propsValue);
70+
validateField(propsValue ?? '');
7171
}
7272
}
7373
}

0 commit comments

Comments
 (0)