-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add react testing library example (#7346)
* docs: add react-testing-library example * chore: rename test files to match comments in docs * docs: show react-testing-library example first then enzyme
- Loading branch information
1 parent
49d0840
commit 5b7c75d
Showing
9 changed files
with
132 additions
and
3 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
File renamed without changes.
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,3 @@ | ||
{ | ||
"presets": ["env", "react"] | ||
} |
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,31 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
import React from 'react'; | ||
|
||
export default class CheckboxWithLabel extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = {isChecked: false}; | ||
|
||
// bind manually because React class components don't auto-bind | ||
// http://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#autobinding | ||
this.onChange = this.onChange.bind(this); | ||
} | ||
|
||
onChange() { | ||
this.setState({isChecked: !this.state.isChecked}); | ||
} | ||
|
||
render() { | ||
return ( | ||
<label> | ||
<input | ||
type="checkbox" | ||
checked={this.state.isChecked} | ||
onChange={this.onChange} | ||
/> | ||
{this.state.isChecked ? this.props.labelOn : this.props.labelOff} | ||
</label> | ||
); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
examples/react-testing-library/__tests__/CheckboxWithLabel-test.js
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,20 @@ | ||
// Copyright 2004-present Facebook. All Rights Reserved. | ||
|
||
import React from 'react'; | ||
import {render, fireEvent, cleanup} from 'react-testing-library'; | ||
import CheckboxWithLabel from '../CheckboxWithLabel'; | ||
|
||
// automatically unmount and cleanup DOM after the test is finished. | ||
afterEach(cleanup); | ||
|
||
it('CheckboxWithLabel changes the text after click', () => { | ||
const {queryByLabelText, getByLabelText} = render( | ||
<CheckboxWithLabel labelOn="On" labelOff="Off" /> | ||
); | ||
|
||
expect(queryByLabelText(/off/i)).toBeTruthy(); | ||
|
||
fireEvent.click(getByLabelText(/off/i)); | ||
|
||
expect(queryByLabelText(/on/i)).toBeTruthy(); | ||
}); |
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,19 @@ | ||
{ | ||
"private": true, | ||
"version": "0.0.0", | ||
"name": "example-react-testing-library", | ||
"dependencies": { | ||
"react": "16.4.1", | ||
"react-dom": "16.4.1" | ||
}, | ||
"devDependencies": { | ||
"babel-jest": "*", | ||
"babel-preset-env": "*", | ||
"babel-preset-react": "*", | ||
"react-testing-library": "*", | ||
"jest": "*" | ||
}, | ||
"scripts": { | ||
"test": "jest" | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
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