Skip to content

TextField - handle value change to undefined #3676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions src/components/textField/__tests__/index.driver.spec.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, {useState} from 'react';
import React, {useEffect, useState} from 'react';
import {render} from '@testing-library/react-native';
import Constants from '../../../commons/Constants';
import Assets from '../../../assets';
Expand All @@ -15,14 +15,19 @@ const helperText = 'Helper Text';

function TestCase(textFieldProps?: TextFieldProps) {
const [value, setValue] = useState(textFieldProps?.value);

useEffect(() => {
setValue(textFieldProps?.value);
}, [textFieldProps?.value]);

return (
<View>
<TextField {...textFieldProps} testID={TEXT_FIELD_TEST_ID} value={value} onChangeText={setValue}/>
</View>
);
}

const validate = jest.fn((value: string) => {
const validate = jest.fn((value?: string) => {
return !!value;
});

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

it('should render validationMessage when input is requires after changing the value to undefined', () => {
const renderTree = render(<TestCase {...defaultProps} value={'Some text'} validate={'required'} validationMessage={'mock message'} enableErrors validateOnChange/>);
const textFieldDriver = TextFieldDriver({renderTree, testID: TEXT_FIELD_TEST_ID});

expect(textFieldDriver.getValidationMessage().exists()).toBe(false);
expect(textFieldDriver.getValidationMessage().getText()).toEqual('');

renderTree.rerender(<TestCase {...defaultProps} validate={'required'} validationMessage={'mock message'} enableErrors validateOnChange/>);
expect(textFieldDriver.getValidationMessage().exists()).toBe(true);
expect(textFieldDriver.getValidationMessage().getText()).toEqual('mock message');
});

it('should display validation error message when validation fail after blur', () => {
const renderTree = render(<TestCase {...defaultProps} validate={'email'} validationMessage={'email is invalid'} enableErrors validateOnBlur/>);
const textFieldDriver = TextFieldDriver({renderTree, testID: TEXT_FIELD_TEST_ID});
Expand Down Expand Up @@ -490,7 +507,7 @@ describe('TextField', () => {
const props = {
...defaultProps,
value: '10000',
formatter: value => priceFormatter.format(Number(value))
formatter: (value?: string) => priceFormatter.format(Number(value))
};

it('should format value while not focused based on formatter prop', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/textField/useFieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ export default function useFieldState({

if (validateOnChange) {
if (validationDebounceTime) {
debouncedValidateField(propsValue);
debouncedValidateField(propsValue ?? '');
} else {
validateField(propsValue);
validateField(propsValue ?? '');
}
}
}
Expand Down