Skip to content

Commit af85d8c

Browse files
committed
Break React-Redux example into multiple "files" (code blocks) (#167)
* Break example into multiple "files" The previous example put all the necessary variables into one file. However, this is unrealistic and I found it confusing when first reading through the docs. Instead, I've broken each chunk of this example into its own "file", which I think reads better and will be more applicable to a user's code base. * Specify JavaScript language on code block Per @kentcdodds' PR comment: testing-library/testing-library-docs#167 (comment)
1 parent e1ec8ad commit af85d8c

File tree

1 file changed

+26
-24
lines changed

1 file changed

+26
-24
lines changed

docs/example-react-redux.md

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ title: React Redux
44
---
55

66
```jsx
7+
// counter.js
78
import React from 'react'
8-
import { createStore } from 'redux'
9-
import { Provider, connect } from 'react-redux'
10-
import { render, fireEvent, cleanup } from '@testing-library/react'
9+
import { connect } from 'react-redux'
1110

12-
// counter.js
1311
class Counter extends React.Component {
1412
increment = () => {
1513
this.props.dispatch({ type: 'INCREMENT' })
@@ -33,14 +31,18 @@ class Counter extends React.Component {
3331
}
3432
}
3533

36-
// normally this would be:
37-
// export default connect(state => ({count: state.count}))(Counter)
38-
// but for this test we'll give it a variable name
39-
// because we're doing this all in one file
40-
const ConnectedCounter = connect(state => ({ count: state.count }))(Counter)
34+
export default connect(state => ({count: state.count}))(Counter)
35+
```
36+
37+
For this example, we'll have a simple reducer that tracks and updates `count`:
4138

42-
// app.js
43-
function reducer(state = { count: 0 }, action) {
39+
```js
40+
// reducer.js
41+
export const initialState = {
42+
count: 0
43+
}
44+
45+
export function reducer(state = initialState, action) {
4446
switch (action.type) {
4547
case 'INCREMENT':
4648
return {
@@ -54,18 +56,18 @@ function reducer(state = { count: 0 }, action) {
5456
return state
5557
}
5658
}
59+
```
5760

58-
// normally here you'd do:
59-
// const store = createStore(reducer)
60-
// ReactDOM.render(
61-
// <Provider store={store}>
62-
// <Counter />
63-
// </Provider>,
64-
// document.getElementById('root'),
65-
// )
66-
// but for this test we'll umm... not do that :)
61+
Now here's what your test will look like:
6762

68-
// Now here's what your test will look like:
63+
```jsx
64+
// counter.test.js
65+
import React from 'react'
66+
import { createStore } from 'redux'
67+
import { Provider, connect } from 'react-redux'
68+
import { render, fireEvent, cleanup } from '@testing-library/react'
69+
import { initialState, reducer } from './reducer.js'
70+
import Counter from './counter.js'
6971

7072
afterEach(cleanup)
7173

@@ -86,13 +88,13 @@ function renderWithRedux(
8688
}
8789

8890
test('can render with redux with defaults', () => {
89-
const { getByTestId, getByText } = renderWithRedux(<ConnectedCounter />)
91+
const { getByTestId, getByText } = renderWithRedux(<Counter />)
9092
fireEvent.click(getByText('+'))
9193
expect(getByTestId('count-value').textContent).toBe('1')
9294
})
9395

9496
test('can render with redux with custom initial state', () => {
95-
const { getByTestId, getByText } = renderWithRedux(<ConnectedCounter />, {
97+
const { getByTestId, getByText } = renderWithRedux(<Counter />, {
9698
initialState: { count: 3 },
9799
})
98100
fireEvent.click(getByText('-'))
@@ -102,7 +104,7 @@ test('can render with redux with custom initial state', () => {
102104
test('can render with redux with custom store', () => {
103105
// this is a silly store that can never be changed
104106
const store = createStore(() => ({ count: 1000 }))
105-
const { getByTestId, getByText } = renderWithRedux(<ConnectedCounter />, {
107+
const { getByTestId, getByText } = renderWithRedux(<Counter />, {
106108
store,
107109
})
108110
fireEvent.click(getByText('+'))

0 commit comments

Comments
 (0)