Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: adds a cost input example to showcase fireEvent with React (#196)
<!-- Thanks for your interest in the project. Bugs filed and PRs submitted are appreciated! Please make sure that you are familiar with and follow the Code of Conduct for this project (found in the CODE_OF_CONDUCT.md file). Also, please make sure you're familiar with and follow the instructions in the contributing guidelines (found in the CONTRIBUTING.md file). If you're new to contributing to open source projects, you might find this free video course helpful: http://kcd.im/pull-request Please fill out the information below to expedite the review and (hopefully) merge of your pull request! --> <!-- What changes are being made? (What feature/bug is being fixed here?) --> **What**: I added a new example <!-- Why are these changes necessary? --> **Why**: When I started using fireEvent, I found it very confusing and I also didn't understand how React handled events. I believe an example showing the testing of a simple component will help people with this concept greatly <!-- How were these changes implemented? --> **How**: I added a stateful component: CostInput that keeps a $ in front of the input and keeps non-numeric values out. <!-- Have you done all of these things? --> **Checklist**: <!-- add "N/A" to the end of each line that's irrelevant to your changes --> <!-- to check an item, place an "x" in the box like so: "- [x] Documentation" --> - [ ] Documentation N/A - [x] Tests - [x] Ready to be merged <!-- In your opinion, is this ready to be merged as soon as it's reviewed? --> - [x] Added myself to contributors table <!-- this is optional, see the contributing guidelines for instructions --> <!-- feel free to add additional comments -->
- Loading branch information
1 parent
83413d3
commit e3a50d4
Showing
3 changed files
with
77 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,65 @@ | ||
import React from 'react' | ||
import {render, fireEvent, cleanup} from 'react-testing-library' | ||
|
||
class CostInput extends React.Component { | ||
state = { | ||
value: '', | ||
} | ||
|
||
removeDollarSign = value => (value[0] === '$' ? value.slice(1) : value) | ||
getReturnValue = value => (value === '' ? '' : `$${value}`) | ||
handleChange = ev => { | ||
ev.preventDefault() | ||
const inputtedValue = ev.currentTarget.value | ||
const noDollarSign = this.removeDollarSign(inputtedValue) | ||
if (isNaN(noDollarSign)) return | ||
this.setState({value: this.getReturnValue(noDollarSign)}) | ||
} | ||
|
||
render() { | ||
return ( | ||
<input | ||
value={this.state.value} | ||
aria-label="cost-input" | ||
onChange={this.handleChange} | ||
/> | ||
) | ||
} | ||
} | ||
|
||
const setup = () => { | ||
const utils = render(<CostInput />) | ||
const input = utils.getByLabelText('cost-input') | ||
return { | ||
input, | ||
...utils, | ||
} | ||
} | ||
|
||
afterEach(cleanup) | ||
|
||
test('It should keep a $ in front of the input', () => { | ||
const {input} = setup() | ||
fireEvent.change(input, {target: {value: '23'}}) | ||
expect(input.value).toBe('$23') | ||
}) | ||
test('It should allow a $ to be in the input when the value is changed', () => { | ||
const {input} = setup() | ||
fireEvent.change(input, {target: {value: '$23.0'}}) | ||
expect(input.value).toBe('$23.0') | ||
}) | ||
|
||
test('It should not allow letters to be inputted', () => { | ||
const {input} = setup() | ||
expect(input.value).toBe('') // empty before | ||
fireEvent.change(input, {target: {value: 'Good Day'}}) | ||
expect(input.value).toBe('') //empty after | ||
}) | ||
|
||
test('It should allow the $ to be deleted', () => { | ||
const {input} = setup() | ||
fireEvent.change(input, {target: {value: '23'}}) | ||
expect(input.value).toBe('$23') // need to make a change so React registers "" as a change | ||
fireEvent.change(input, {target: {value: ''}}) | ||
expect(input.value).toBe('') | ||
}) |