Skip to content

Infra/render tests - TextField #1684

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 10 commits into from
Dec 1, 2021
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
17 changes: 5 additions & 12 deletions src/components/hint/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,15 @@ describe('Hint Screen component test', () => {
});

it('Test Hint modal is not visible when showHint is false', async () => {
const element = <HintTestComponent showHint={false}/>;

const {queryByTestId} = render(element);

const modal = queryByTestId('Hint.modal');

const renderTree = render(<HintTestComponent showHint={false}/>);
const modal = renderTree.queryByTestId('Hint.modal');
expect(modal).toBeNull();
});

it('Test Hint modal is visible when showHint is true', async () => {
const element = <HintTestComponent showHint/>;

const {getByTestId, queryAllByTestId} = render(element);
const hint = getByTestId('Hint');
const renderTree = render(<HintTestComponent showHint/>);
const hint = renderTree.getByTestId('Hint');
expect(hint).toBeTruthy();

expect(queryAllByTestId('Hint.modal')).toBeTruthy();
expect(renderTree.queryAllByTestId('Hint.modal')).toBeTruthy();
});
});
86 changes: 86 additions & 0 deletions src/incubator/TextField/__tests__/index.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import React, {useState} from 'react';
import {fireEvent, render} from '@testing-library/react-native';
import TextField from '../index';

const defaultProps = {
testID: 'field',
placeholder: 'Placeholder'
};

const TestCase = props => {
const [value, setValue] = useState(props.value);
return <TextField {...defaultProps} {...props} onChangeText={setValue} value={value}/>;
};

describe('TextField', () => {
describe('hint prop', () => {
it('should hint text replace placeholder when input is focused', () => {
const renderTree = render(<TestCase hint={'Hint'}/>);

const input = renderTree.getByTestId('field');
renderTree.getByPlaceholderText(defaultProps.placeholder);

fireEvent(input, 'focus');
renderTree.getByPlaceholderText('Hint');
});

it('should not show hint when hint prop not passed', () => {
const renderTree = render(<TestCase/>);

const input = renderTree.getByTestId('field');
renderTree.getByPlaceholderText(defaultProps.placeholder);

fireEvent(input, 'focus');
renderTree.getByPlaceholderText(defaultProps.placeholder);
});
});

describe('formatter prop', () => {
const priceFormatter = Intl.NumberFormat('en-US');

const props = {
value: '10000',
formatter: value => priceFormatter.format(Number(value))
};

it('should format value while not focused based on formatter prop', () => {
const renderTree = render(<TestCase {...props}/>);
renderTree.getByDisplayValue('10,000');
});

it('should not format value while focused', () => {
const renderTree = render(<TestCase {...props}/>);
const input = renderTree.getByTestId('field');
fireEvent(input, 'focus');
renderTree.getByDisplayValue('10000');
});
});

describe('validation', () => {
const props = {
testID: 'field',
enableErrors: true,
validate: 'email',
validationMessage: 'email is invalid'
};

it('should display validation error message when validation fail after blur', () => {
const renderTree = render(<TestCase {...props} validateOnBlur/>);
const input = renderTree.getByTestId('field');
fireEvent.changeText(input, 'invalidEmail');
fireEvent(input, 'blur');
renderTree.getByText(props.validationMessage);
});

it('should remove validation error message after entering a valid input', async () => {
const renderTree = render(<TestCase {...props} validateOnStart validateOnChange value={'invalid'}/>);
const input = renderTree.getByTestId('field');

renderTree.getByText(props.validationMessage);

fireEvent.changeText(input, '[email protected]');
const validationMessageElement = renderTree.queryByText(props.validationMessage);
expect(validationMessageElement).toBe(null);
});
});
});