-
Notifications
You must be signed in to change notification settings - Fork 734
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
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
7b5b850
WIP - add render tests for TextField
ethanshar 9f2166a
Merge branch 'master' into infra/renderTests
ethanshar 5e29d7d
WIP - keep working on TextField tests
ethanshar 581ca87
Add tests for validations
ethanshar a962306
Merge branch 'master' into infra/renderTests
ethanshar de09c49
cleanup tests
ethanshar ea94207
move render test case into the same file of the test
ethanshar 89eb398
minor changes in Hint render tests
ethanshar a5d2eaa
Code review fixes
ethanshar f4b0958
Add test that check when hint not passed
ethanshar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.