Skip to content

Commit c61de92

Browse files
alexkrolickKent C. Dodds
authored and
Kent C. Dodds
committed
feat(examples): Add context examples (#68)
* feat(examples): Add context examples * modify the examples * fix example stuff
1 parent 46c3a7e commit c61de92

15 files changed

+163
-62
lines changed

README.md

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

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

462-
* [`react-redux`](https://github.com/kentcdodds/react-testing-library/blob/master/src/__tests__/react-redux.js)
463-
* [`react-router`](https://github.com/kentcdodds/react-testing-library/blob/master/src/__tests__/react-router.js)
464-
* [`react-context`](https://github.com/kentcdodds/react-testing-library/blob/master/src/__tests__/react-context.js)
462+
* [`react-redux`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-redux.js)
463+
* [`react-router`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-router.js)
464+
* [`react-context`](https://github.com/kentcdodds/react-testing-library/blob/master/examples/__tests__/react-context.js)
465465

466466
Examples of TDD with react-testing-library:
467467

@@ -546,7 +546,7 @@ render(<NumberDisplay number={2} />, {container})
546546
expect(getByTestId('number-display').textContent).toBe('2')
547547
```
548548

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

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

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

595595
This looks like more work that shallow rendering (and it is), but it gives you
@@ -598,7 +598,7 @@ enough.
598598

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

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

761761
<!-- ALL-CONTRIBUTORS-LIST:START - Do not remove or modify this section -->
762-
763762
<!-- prettier-ignore -->
764763
| [<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") |
765764
| :---: | :---: | :---: | :---: | :---: | :---: | :---: |
766765
| [<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") |
767766
| [<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") | [<img src="https://avatars3.githubusercontent.com/u/1571667?v=4" width="100px;"/><br /><sub><b>Alex Krolick</b></sub>](https://alexkrolick.com)<br />[💬](#question-alexkrolick "Answering Questions") [📖](https://github.com/kentcdodds/react-testing-library/commits?author=alexkrolick "Documentation") [💡](#example-alexkrolick "Examples") [🤔](#ideas-alexkrolick "Ideas, Planning, & Feedback") |
768-
769767
<!-- ALL-CONTRIBUTORS-LIST:END -->
770768

771769
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

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
## Contribute
15+
16+
We're always happy to accept contributions to the examples. Can't have too many
17+
of these as there are TONs of different ways to test React. Examples of testing
18+
components that use different and common libraries is always welcome. Try to
19+
keep examples simple enough for people to understand the main thing we're trying
20+
to demonstrate from the example.
21+
22+
Please follow the guidelines found in [CONTRIBUTING.md][contributing] to set up
23+
the project.
24+
25+
To run the tests, you can run `npm test examples`, or if you're working on a
26+
specific example, you can run `npm test name-of-your-file`. This will put you
27+
into Jest's interactive watch mode with a filter based on the name you provided.
28+
29+
[contributing]: https://github.com/kentcdodds/react-testing-library/blob/master/CONTRIBUTING.md
30+
[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

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import React from 'react'
2+
import {render} from 'react-testing-library'
3+
import 'jest-dom/extend-expect'
4+
import {NameContext, NameProvider, NameConsumer} from '../react-context'
5+
6+
/**
7+
* Test default values by rendering a context consumer without a
8+
* matching provider
9+
*/
10+
test('NameConsumer shows default value', () => {
11+
const {getByText} = render(<NameConsumer />)
12+
expect(getByText('My Name Is:')).toHaveTextContent('My Name Is: Unknown')
13+
})
14+
15+
/**
16+
* To test a component tree that uses a context consumer but not the provider,
17+
* wrap the tree with a matching provider
18+
*/
19+
test('NameConsumer shows value from provider', () => {
20+
const tree = (
21+
<NameContext.Provider value="C3P0">
22+
<NameConsumer />
23+
</NameContext.Provider>
24+
)
25+
const {getByText} = render(tree)
26+
expect(getByText('My Name Is:')).toHaveTextContent('My Name Is: C3P0')
27+
})
28+
29+
/**
30+
* To test a component that provides a context value, render a matching
31+
* consumer as the child
32+
*/
33+
test('NameProvider composes full name from first, last', () => {
34+
const tree = (
35+
<NameProvider first="Boba" last="Fett">
36+
<NameContext.Consumer>
37+
{value => <span>Received: {value}</span>}
38+
</NameContext.Consumer>
39+
</NameProvider>
40+
)
41+
const {getByText} = render(tree)
42+
expect(getByText('Received:').textContent).toBe('Received: Boba Fett')
43+
})
44+
45+
/**
46+
* A tree containing both a providers and consumer can be rendered normally
47+
*/
48+
test('NameProvider/Consumer shows name of character', () => {
49+
const tree = (
50+
<NameProvider first="Leia" last="Organa">
51+
<NameConsumer />
52+
</NameProvider>
53+
)
54+
const {getByText} = render(tree)
55+
expect(getByText('My Name Is:').textContent).toBe('My Name Is: Leia Organa')
56+
})

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

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
const jestConfig = require('kcd-scripts/jest')
2+
3+
module.exports = Object.assign(jestConfig, {
4+
rootDir: __dirname,
5+
roots: [__dirname],
6+
displayName: 'example',
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+
},
11+
})

examples/react-context.js

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React from 'react'
2+
3+
const NameContext = React.createContext('Unknown')
4+
5+
const NameProvider = ({children, first, last}) => {
6+
const fullName = `${first} ${last}`
7+
return (
8+
<NameContext.Provider value={fullName}>{children}</NameContext.Provider>
9+
)
10+
}
11+
12+
const NameConsumer = () => (
13+
<NameContext.Consumer>
14+
{value => <div>My Name Is: {value}</div>}
15+
</NameContext.Consumer>
16+
)
17+
18+
export {NameContext, NameConsumer, NameProvider}

jest.config.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const jestConfig = require('kcd-scripts/jest')
2+
3+
module.exports = Object.assign(jestConfig, {
4+
displayName: 'library',
5+
})

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+
}

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"add-contributor": "kcd-scripts contributors add",
1212
"build": "kcd-scripts build",
1313
"lint": "kcd-scripts lint",
14-
"test": "kcd-scripts test",
14+
"test": "kcd-scripts test --config=other/jest.config.js",
1515
"test:update": "npm test -- --updateSnapshot --coverage",
1616
"validate": "kcd-scripts validate",
1717
"setup": "npm install && npm run validate -s",
@@ -42,7 +42,9 @@
4242
"devDependencies": {
4343
"@types/react-dom": "^16.0.5",
4444
"axios": "^0.18.0",
45+
"eslint-import-resolver-jest": "^2.1.1",
4546
"history": "^4.7.2",
47+
"jest-dom": "^1.0.0",
4648
"jest-in-case": "^1.0.2",
4749
"kcd-scripts": "^0.37.0",
4850
"react": "^16.3.2",

src/__tests__/react-context.js

-47
This file was deleted.

0 commit comments

Comments
 (0)