Skip to content

Commit

Permalink
docs: adds a cost input example to showcase fireEvent with React (#196)
Browse files Browse the repository at this point in the history
<!--
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
alexwendte authored and Kent C. Dodds committed Oct 10, 2018
1 parent 83413d3 commit e3a50d4
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 2 deletions.
9 changes: 9 additions & 0 deletions .all-contributorsrc
Expand Up @@ -540,6 +540,15 @@
"contributions": [
"example"
]
},
{
"login": "themostcolm",
"name": "Alex Wendte",
"avatar_url": "https://avatars2.githubusercontent.com/u/5779538?v=4",
"profile": "http://www.wendtedesigns.com/",
"contributions": [
"example"
]
}
]
}
5 changes: 3 additions & 2 deletions README.md
Expand Up @@ -15,7 +15,7 @@
[![version][version-badge]][package] [![downloads][downloads-badge]][npmtrends]
[![MIT License][license-badge]][license]

[![All Contributors](https://img.shields.io/badge/all_contributors-55-orange.svg?style=flat-square)](#contributors)
[![All Contributors](https://img.shields.io/badge/all_contributors-56-orange.svg?style=flat-square)](#contributors)
[![PRs Welcome][prs-badge]][prs] [![Code of Conduct][coc-badge]][coc]
[![Join the community on Spectrum][spectrum-badge]][spectrum]

Expand Down Expand Up @@ -1236,7 +1236,8 @@ Thanks goes to these people ([emoji key][emojis]):
| [<img src="https://avatars2.githubusercontent.com/u/10452163?v=4" width="100px;"/><br /><sub><b>Richard Kotze (mobile)</b></sub>](http://www.richardkotze.com)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=rkotze "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/10819833?v=4" width="100px;"/><br /><sub><b>Brahian E. Soto Mercedes</b></sub>](https://github.com/sotobuild)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=sotobuild "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/7151559?v=4" width="100px;"/><br /><sub><b>Benoit de La Forest</b></sub>](https://github.com/bdelaforest)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=bdelaforest "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/6624197?v=4" width="100px;"/><br /><sub><b>Salah</b></sub>](https://github.com/thesalah)<br />[馃捇](https://github.com/kentcdodds/react-testing-library/commits?author=thesalah "Code") [鈿狅笍](https://github.com/kentcdodds/react-testing-library/commits?author=thesalah "Tests") | [<img src="https://avatars2.githubusercontent.com/u/370054?v=4" width="100px;"/><br /><sub><b>Adam Gordon</b></sub>](http://gordonizer.com)<br />[馃悰](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Aicfantv "Bug reports") [馃捇](https://github.com/kentcdodds/react-testing-library/commits?author=icfantv "Code") | [<img src="https://avatars2.githubusercontent.com/u/471278?v=4" width="100px;"/><br /><sub><b>Matija Marohni膰</b></sub>](https://silvenon.com)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=silvenon "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/8015514?v=4" width="100px;"/><br /><sub><b>Justice Mba</b></sub>](https://github.com/Dajust)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=Dajust "Documentation") |
| [<img src="https://avatars2.githubusercontent.com/u/5286559?v=4" width="100px;"/><br /><sub><b>Mark Pollmann</b></sub>](https://markpollmann.com/)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=MarkPollmann "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/1213123?v=4" width="100px;"/><br /><sub><b>Ehtesham Kafeel</b></sub>](https://github.com/ehteshamkafeel)<br />[馃捇](https://github.com/kentcdodds/react-testing-library/commits?author=ehteshamkafeel "Code") [馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=ehteshamkafeel "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/1493505?v=4" width="100px;"/><br /><sub><b>Julio Pav贸n</b></sub>](http://jpavon.com)<br />[馃捇](https://github.com/kentcdodds/react-testing-library/commits?author=jpavon "Code") | [<img src="https://avatars3.githubusercontent.com/u/1765048?v=4" width="100px;"/><br /><sub><b>Duncan L</b></sub>](http://www.duncanleung.com/)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=duncanleung "Documentation") [馃挕](#example-duncanleung "Examples") | [<img src="https://avatars1.githubusercontent.com/u/700778?v=4" width="100px;"/><br /><sub><b>Tiago Almeida</b></sub>](https://www.linkedin.com/in/tyagow/?locale=en_US)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=tyagow "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/4982001?v=4" width="100px;"/><br /><sub><b>Robert Smith</b></sub>](http://rbrtsmith.com/)<br />[馃悰](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Arbrtsmith "Bug reports") | [<img src="https://avatars0.githubusercontent.com/u/1700355?v=4" width="100px;"/><br /><sub><b>Zach Green</b></sub>](https://offbyone.tech)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=zgreen "Documentation") |
| [<img src="https://avatars3.githubusercontent.com/u/881986?v=4" width="100px;"/><br /><sub><b>dadamssg</b></sub>](https://github.com/dadamssg)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=dadamssg "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8734097?v=4" width="100px;"/><br /><sub><b>Yazan Aabed</b></sub>](https://www.yaabed.com/)<br />[馃摑](#blog-YazanAabeed "Blogposts") | [<img src="https://avatars0.githubusercontent.com/u/556258?v=4" width="100px;"/><br /><sub><b>Tim</b></sub>](https://github.com/timbonicus)<br />[馃悰](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Atimbonicus "Bug reports") [馃捇](https://github.com/kentcdodds/react-testing-library/commits?author=timbonicus "Code") [馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=timbonicus "Documentation") [鈿狅笍](https://github.com/kentcdodds/react-testing-library/commits?author=timbonicus "Tests") | [<img src="https://avatars3.githubusercontent.com/u/6682655?v=4" width="100px;"/><br /><sub><b>Divyanshu Maithani</b></sub>](http://divyanshu.xyz)<br />[鉁匽(#tutorial-divyanshu013 "Tutorials") [馃摴](#video-divyanshu013 "Videos") | [<img src="https://avatars2.githubusercontent.com/u/9116042?v=4" width="100px;"/><br /><sub><b>Deepak Grover</b></sub>](https://www.linkedin.com/in/metagrover)<br />[鉁匽(#tutorial-metagrover "Tutorials") [馃摴](#video-metagrover "Videos") | [<img src="https://avatars0.githubusercontent.com/u/16276358?v=4" width="100px;"/><br /><sub><b>Eyal Cohen</b></sub>](https://github.com/eyalcohen4)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=eyalcohen4 "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/7452681?v=4" width="100px;"/><br /><sub><b>Peter Makowski</b></sub>](https://github.com/petermakowski)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=petermakowski "Documentation") |
| [<img src="https://avatars2.githubusercontent.com/u/20361668?v=4" width="100px;"/><br /><sub><b>Michiel Nuyts</b></sub>](https://github.com/Michielnuyts)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=Michielnuyts "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1195863?v=4" width="100px;"/><br /><sub><b>Joe Ng'ethe</b></sub>](https://github.com/joeynimu)<br />[馃捇](https://github.com/kentcdodds/react-testing-library/commits?author=joeynimu "Code") [馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=joeynimu "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/19998290?v=4" width="100px;"/><br /><sub><b>Kate</b></sub>](https://github.com/Enikol)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=Enikol "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/11980217?v=4" width="100px;"/><br /><sub><b>Sean</b></sub>](http://www.seanrparker.com)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=SeanRParker "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/17031?v=4" width="100px;"/><br /><sub><b>James Long</b></sub>](http://jlongster.com)<br />[馃](#ideas-jlongster "Ideas, Planning, & Feedback") [馃摝](#platform-jlongster "Packaging/porting to new platform") | [<img src="https://avatars1.githubusercontent.com/u/10118777?v=4" width="100px;"/><br /><sub><b>Herb Hagely</b></sub>](https://github.com/hhagely)<br />[馃挕](#example-hhagely "Examples") |
| [<img src="https://avatars2.githubusercontent.com/u/20361668?v=4" width="100px;"/><br /><sub><b>Michiel Nuyts</b></sub>](https://github.com/Michielnuyts)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=Michielnuyts "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1195863?v=4" width="100px;"/><br /><sub><b>Joe Ng'ethe</b></sub>](https://github.com/joeynimu)<br />[馃捇](https://github.com/kentcdodds/react-testing-library/commits?author=joeynimu "Code") [馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=joeynimu "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/19998290?v=4" width="100px;"/><br /><sub><b>Kate</b></sub>](https://github.com/Enikol)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=Enikol "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/11980217?v=4" width="100px;"/><br /><sub><b>Sean</b></sub>](http://www.seanrparker.com)<br />[馃摉](https://github.com/kentcdodds/react-testing-library/commits?author=SeanRParker "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/17031?v=4" width="100px;"/><br /><sub><b>James Long</b></sub>](http://jlongster.com)<br />[馃](#ideas-jlongster "Ideas, Planning, & Feedback") [馃摝](#platform-jlongster "Packaging/porting to new platform") | [<img src="https://avatars1.githubusercontent.com/u/10118777?v=4" width="100px;"/><br /><sub><b>Herb Hagely</b></sub>](https://github.com/hhagely)<br />[馃挕](#example-hhagely "Examples") | [<img src="https://avatars2.githubusercontent.com/u/5779538?v=4" width="100px;"/><br /><sub><b>Alex Wendte</b></sub>](http://www.wendtedesigns.com/)<br />[馃挕](#example-themostcolm "Examples") |
<!-- ALL-CONTRIBUTORS-LIST:END -->
This project follows the [all-contributors][all-contributors] specification.
Expand Down
65 changes: 65 additions & 0 deletions examples/__tests__/input-event.js
@@ -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('')
})

0 comments on commit e3a50d4

Please sign in to comment.