Skip to content

Commit a34ca90

Browse files
author
Kent C. Dodds
committed
modify the examples
1 parent f5005b8 commit a34ca90

15 files changed

+96
-71
lines changed

README.md

+9-7
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,12 @@ expect(submitButton).toBeNull() // it doesn't exist
451451
## Examples
452452

453453
You'll find examples of testing with different libraries in
454-
[the test directory](https://github.com/kentcdodds/react-testing-library/blob/master/src/__tests__).
454+
[the `examples` directory](https://github.com/kentcdodds/react-testing-library/blob/master/examples).
455455
Some included are:
456456

457-
* [`react-redux`](https://github.com/kentcdodds/react-testing-library/blob/master/src/__tests__/react-redux.js)
458-
* [`react-router`](https://github.com/kentcdodds/react-testing-library/blob/master/src/__tests__/react-router.js)
459-
* [`react-context`](https://github.com/kentcdodds/react-testing-library/blob/master/src/__tests__/react-context.js)
457+
* [`react-redux`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-redux.js)
458+
* [`react-router`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-router.js)
459+
* [`react-context`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-context.js)
460460

461461
Examples of TDD with react-testing-library:
462462

@@ -541,7 +541,7 @@ render(<NumberDisplay number={2} />, {container})
541541
expect(getByTestId('number-display').textContent).toBe('2')
542542
```
543543

544-
[Open the tests](https://github.com/kentcdodds/react-testing-library/blob/master/src/__tests__/number-display.js)
544+
[Open the tests](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/update-props.js)
545545
for a full example of this.
546546

547547
</details>
@@ -584,7 +584,7 @@ test('you can mock things with jest.mock', () => {
584584
Note that because they're Jest mock functions (`jest.fn()`), you could also make
585585
assertions on those as well if you wanted.
586586

587-
[Open full test](https://github.com/kentcdodds/react-testing-library/blob/master/src/__tests__/mock.react-transition-group.js)
587+
[Open full test](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/mock.react-transition-group.js)
588588
for the full example.
589589

590590
This looks like more work that shallow rendering (and it is), but it gives you
@@ -593,7 +593,7 @@ enough.
593593

594594
If you want to make things more like shallow rendering, then you could do
595595
something more
596-
[like this](https://github.com/kentcdodds/react-testing-library/blob/master/src/__tests__/shallow.react-transition-group.js).
596+
[like this](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/shallow.react-transition-group.js).
597597

598598
Learn more about how Jest mocks work from my blog post:
599599
["But really, what is a JavaScript mock?"](https://blog.kentcdodds.com/but-really-what-is-a-javascript-mock-10d060966f7d)
@@ -754,11 +754,13 @@ light-weight, simple, and understandable.
754754
Thanks goes to these people ([emoji key][emojis]):
755755

756756
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
757+
757758
<!-- prettier-ignore -->
758759
| [<img src="https://avatars.githubusercontent.com/u/1500684?v=3" width="100px;"/><br /><sub><b>Kent C. Dodds</b></sub>](https://kentcdodds.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Documentation") [🚇](#infra-kentcdodds "Infrastructure (Hosting, Build-Tools, etc)") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=kentcdodds "Tests") | [<img src="https://avatars1.githubusercontent.com/u/2430381?v=4" width="100px;"/><br /><sub><b>Ryan Castner</b></sub>](http://audiolion.github.io)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=audiolion "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/8008023?v=4" width="100px;"/><br /><sub><b>Daniel Sandiego</b></sub>](https://www.dnlsandiego.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=dnlsandiego "Code") | [<img src="https://avatars2.githubusercontent.com/u/12592677?v=4" width="100px;"/><br /><sub><b>Paweł Mikołajczyk</b></sub>](https://github.com/Miklet)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=Miklet "Code") | [<img src="https://avatars3.githubusercontent.com/u/464978?v=4" width="100px;"/><br /><sub><b>Alejandro Ñáñez Ortiz</b></sub>](http://co.linkedin.com/in/alejandronanez/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=alejandronanez "Documentation") | [<img src="https://avatars0.githubusercontent.com/u/1402095?v=4" width="100px;"/><br /><sub><b>Matt Parrish</b></sub>](https://github.com/pbomb)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Apbomb "Bug reports") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=pbomb "Tests") | [<img src="https://avatars1.githubusercontent.com/u/1288694?v=4" width="100px;"/><br /><sub><b>Justin Hall</b></sub>](https://github.com/wKovacs64)<br />[📦](#platform-wKovacs64 "Packaging/porting to new platform") |
759760
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
760761
| [<img src="https://avatars1.githubusercontent.com/u/1241511?s=460&v=4" width="100px;"/><br /><sub><b>Anto Aravinth</b></sub>](https://github.com/antoaravinth)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Tests") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=antoaravinth "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/3462296?v=4" width="100px;"/><br /><sub><b>Jonah Moses</b></sub>](https://github.com/JonahMoses)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=JonahMoses "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/4002543?v=4" width="100px;"/><br /><sub><b>Łukasz Gandecki</b></sub>](http://team.thebrain.pro)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Code") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Tests") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=lgandecki "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/498274?v=4" width="100px;"/><br /><sub><b>Ivan Babak</b></sub>](https://sompylasar.github.io)<br />[🐛](https://github.com/kentcdodds/react-testing-library/issues?q=author%3Asompylasar "Bug reports") [🤔](#ideas-sompylasar "Ideas, Planning, & Feedback") | [<img src="https://avatars3.githubusercontent.com/u/4439618?v=4" width="100px;"/><br /><sub><b>Jesse Day</b></sub>](https://github.com/jday3)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jday3 "Code") | [<img src="https://avatars0.githubusercontent.com/u/15199?v=4" width="100px;"/><br /><sub><b>Ernesto García</b></sub>](http://gnapse.github.io)<br />[💬](#question-gnapse "Answering Questions") [💻](https://github.com/kentcdodds/react-testing-library/commits?author=gnapse "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=gnapse "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/2747424?v=4" width="100px;"/><br /><sub><b>Josef Maxx Blake</b></sub>](http://jomaxx.com)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Documentation") [⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=jomaxx "Tests") |
761762
| [<img src="https://avatars1.githubusercontent.com/u/29602306?v=4" width="100px;"/><br /><sub><b>Michal Baranowski</b></sub>](https://twitter.com/baranovskim)<br />[📝](#blog-mbaranovski "Blogposts") [✅](#tutorial-mbaranovski "Tutorials") | [<img src="https://avatars3.githubusercontent.com/u/13985684?v=4" width="100px;"/><br /><sub><b>Arthur Puthin</b></sub>](https://github.com/aputhin)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=aputhin "Documentation") | [<img src="https://avatars2.githubusercontent.com/u/21194045?v=4" width="100px;"/><br /><sub><b>Thomas Chia</b></sub>](https://github.com/thchia)<br />[💻](https://github.com/kentcdodds/react-testing-library/commits?author=thchia "Code") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=thchia "Documentation") | [<img src="https://avatars3.githubusercontent.com/u/20430611?v=4" width="100px;"/><br /><sub><b>Thiago Galvani</b></sub>](http://ilegra.com/)<br />[📖](https://github.com/kentcdodds/react-testing-library/commits?author=thiagopaiva99 "Documentation") | [<img src="https://avatars1.githubusercontent.com/u/19828824?v=4" width="100px;"/><br /><sub><b>Christian</b></sub>](http://Chriswcs.github.io)<br />[⚠️](https://github.com/kentcdodds/react-testing-library/commits?author=ChrisWcs "Tests") |
763+
762764
<!-- ALL-CONTRIBUTORS-LIST:END -->
763765

764766
This project follows the [all-contributors][all-contributors] specification.

examples/.eslintrc.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module.exports = {
2+
// we have to do this so our tests can reference 'react-testing-library'
3+
overrides: [
4+
{
5+
files: ['**/__tests__/**'],
6+
settings: {
7+
'import/resolver': {
8+
jest: {
9+
jestConfigFile: require.resolve('./jest.config.js'),
10+
},
11+
},
12+
},
13+
},
14+
],
15+
}

examples/README.md

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Examples
2+
3+
Here we have some examples for you to know how to not only use
4+
`react-testing-library` but also in general how to test common scenarios that
5+
pop up with React. Check out the `__tests__` directory for the different
6+
examples.
7+
8+
## Setup
9+
10+
The examples have a unique jest/eslint set up so the test files will resemble
11+
how they might appear in your project. (You'll see in the tests that we can
12+
`import {render} from 'react-testing-library'`).
13+
14+
Another thing that we do for the tests is we have a `setup-test-env.js` file in
15+
the `other` directory of this repo which includes
16+
`import 'jest-dom/extend-expect'` which gives us a few handy assertions. It's
17+
recommended to use [`jest-dom`][jest-dom] with `react-testing-library`.
18+
19+
## Contribute
20+
21+
We're always happy to accept contributions to the examples. Can't have too many
22+
of these as there are TONs of different ways to test React. Examples of testing
23+
components that use different and common libraries is always welcome. Try to
24+
keep examples simple enough for people to understand the main thing we're trying
25+
to demonstrate from the example.
26+
27+
Please follow the guidelines found in [CONTRIBUTING.md][contributing] to set up
28+
the project.
29+
30+
To run the tests, you can run `npm test examples`, or if you're working on a
31+
specific example, you can run `npm test name-of-your-file`. This will put you
32+
into Jest's interactive watch mode with a filter based on the name you provided.
33+
34+
[contributing]: https://github.com/kentcdodds/react-testing-library/blob/master/CONTRIBUTING.md
35+
[jest-dom]: https://github.com/gnapse/jest-dom

src/__tests__/mock.react-transition-group.js renamed to examples/__tests__/mock.react-transition-group.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import {CSSTransition} from 'react-transition-group'
3-
import {render, Simulate} from '../'
3+
import {render, Simulate} from 'react-testing-library'
44

55
function Fade({children, ...props}) {
66
return (

examples/__tests__/react-context.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react'
2-
import {render} from '../../src'
2+
import {render} from 'react-testing-library'
33
import {NameContext, NameProvider, NameConsumer} from '../react-context'
44

55
/**
@@ -8,7 +8,7 @@ import {NameContext, NameProvider, NameConsumer} from '../react-context'
88
*/
99
test('NameConsumer shows default value', () => {
1010
const {getByText} = render(<NameConsumer />)
11-
expect(getByText('My Name Is:').textContent).toBe('My Name Is: Unknown')
11+
expect(getByText('My Name Is:')).toHaveTextContent('My Name Is: Unknown')
1212
})
1313

1414
/**
@@ -17,12 +17,12 @@ test('NameConsumer shows default value', () => {
1717
*/
1818
test('NameConsumer shows value from provider', () => {
1919
const tree = (
20-
<NameContext.Provider value={'C3P0'}>
20+
<NameContext.Provider value="C3P0">
2121
<NameConsumer />
2222
</NameContext.Provider>
2323
)
2424
const {getByText} = render(tree)
25-
expect(getByText('My Name Is:').textContent).toBe('My Name Is: C3P0')
25+
expect(getByText('My Name Is:')).toHaveTextContent('My Name Is: C3P0')
2626
})
2727

2828
/**
@@ -38,7 +38,7 @@ test('NameProvider composes full name from first, last', () => {
3838
</NameProvider>
3939
)
4040
const {getByText} = render(tree)
41-
expect(getByText('Received: ').textContent).toBe('Received: Boba Fett')
41+
expect(getByText('Received:').textContent).toBe('Received: Boba Fett')
4242
})
4343

4444
/**
@@ -51,5 +51,5 @@ test('NameProvider/Consumer shows name of character', () => {
5151
</NameProvider>
5252
)
5353
const {getByText} = render(tree)
54-
expect(getByText('My Name Is: ').textContent).toBe('My Name Is: Leia Organa')
54+
expect(getByText('My Name Is:').textContent).toBe('My Name Is: Leia Organa')
5555
})

src/__tests__/react-redux.js renamed to examples/__tests__/react-redux.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React from 'react'
22
import {createStore} from 'redux'
33
import {Provider, connect} from 'react-redux'
4-
import {render, Simulate} from '../'
4+
import {render, Simulate} from 'react-testing-library'
55

66
// counter.js
77
class Counter extends React.Component {

src/__tests__/react-router.js renamed to examples/__tests__/react-router.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react'
22
import {withRouter} from 'react-router'
33
import {Link, Route, Router, Switch} from 'react-router-dom'
44
import {createMemoryHistory} from 'history'
5-
import {render, Simulate} from '../'
5+
import {render, Simulate} from 'react-testing-library'
66

77
const About = () => <div>You are on the about page</div>
88
const Home = () => <div>You are home</div>

src/__tests__/shallow.react-transition-group.js renamed to examples/__tests__/shallow.react-transition-group.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from 'react'
22
import {CSSTransition} from 'react-transition-group'
3-
import {render, Simulate} from '../'
3+
import {render, Simulate} from 'react-testing-library'
44

55
function Fade({children, ...props}) {
66
return (

src/__tests__/number-display.js renamed to examples/__tests__/update-props.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1+
// This is an example of how to update the props of a rendered component.
2+
// the basic idea is to simply call `render` again and provide the same container
3+
// that your first call created for you.
4+
15
import React from 'react'
2-
import {render} from '../'
6+
import {render} from 'react-testing-library'
37

48
let idCounter = 1
59

examples/jest.config.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
const jestConfig = require('kcd-scripts/jest')
22

33
module.exports = Object.assign(jestConfig, {
4-
testEnvironment: 'jest-environment-jsdom',
5-
roots: ['./'],
4+
rootDir: __dirname,
5+
displayName: 'example',
6+
setupTestFrameworkScriptFile: require.resolve('../other/setup-test-env'),
7+
moduleNameMapper: {
8+
// this is just here so our examples look like they would in a real project
9+
'react-testing-library': require.resolve('../src'),
10+
},
611
})

jest.config.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const jestConfig = require('kcd-scripts/jest')
22

33
module.exports = Object.assign(jestConfig, {
4-
testEnvironment: 'jest-environment-jsdom',
5-
projects: ['./examples', './src'],
4+
displayName: 'library',
5+
setupTestFrameworkScriptFile: require.resolve('./other/setup-test-env'),
66
})

other/jest.config.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module.exports = {
2+
coverageDirectory: '../coverage',
3+
collectCoverageFrom: [
4+
'**/src/**/*.js',
5+
'!**/__tests__/**',
6+
'!**/node_modules/**',
7+
],
8+
projects: ['./', './examples'],
9+
}

other/setup-test-env.js

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
import 'jest-dom/extend-expect'

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,8 @@
1111
"add-contributor": "kcd-scripts contributors add",
1212
"build": "kcd-scripts build",
1313
"lint": "kcd-scripts lint",
14-
"test": "kcd-scripts test --projects=src",
14+
"test": "kcd-scripts test --config=other/jest.config.js",
1515
"test:update": "npm test -- --updateSnapshot --coverage --projects=src",
16-
"test:examples": "kcd-scripts test --projects=examples",
1716
"validate": "kcd-scripts validate",
1817
"setup": "npm install && npm run validate -s",
1918
"precommit": "kcd-scripts precommit"
@@ -43,7 +42,9 @@
4342
"devDependencies": {
4443
"@types/react-dom": "^16.0.5",
4544
"axios": "^0.18.0",
45+
"eslint-import-resolver-jest": "^2.1.1",
4646
"history": "^4.7.2",
47+
"jest-dom": "^1.0.0",
4748
"jest-in-case": "^1.0.2",
4849
"kcd-scripts": "^0.37.0",
4950
"react": "^16.3.2",

src/__tests__/react-context.js

-47
This file was deleted.

0 commit comments

Comments
 (0)