Skip to content

Commit 51061dc

Browse files
authored
Replace React Redux recipe with Redux's recipe (#588)
* Replace React Redux recipe with Redux's recipe * Fix missing sidebar link for React Redux
1 parent bcce957 commit 51061dc

File tree

2 files changed

+9
-124
lines changed

2 files changed

+9
-124
lines changed

docs/example-react-redux.md

Lines changed: 2 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -3,127 +3,5 @@ id: example-react-redux
33
title: React Redux
44
---
55

6-
```jsx
7-
// counter.js
8-
import React from 'react'
9-
import { connect } from 'react-redux'
10-
11-
const Counter = ({ dispatch, count }) => {
12-
const increment = () => {
13-
dispatch({ type: 'INCREMENT' })
14-
}
15-
16-
const decrement = () => {
17-
dispatch({ type: 'DECREMENT' })
18-
}
19-
20-
return (
21-
<div>
22-
<h2>Counter</h2>
23-
<div>
24-
<button onClick={decrement}>-</button>
25-
<span data-testid="count-value">{count}</span>
26-
<button onClick={increment}>+</button>
27-
</div>
28-
</div>
29-
)
30-
}
31-
32-
export default connect(state => ({ count: state.count }))(Counter)
33-
```
34-
35-
For this example, we'll have a simple reducer that tracks and updates `count`:
36-
37-
```js
38-
// reducer.js
39-
export const initialState = {
40-
count: 0,
41-
}
42-
43-
export function reducer(state = initialState, action) {
44-
switch (action.type) {
45-
case 'INCREMENT':
46-
return {
47-
count: state.count + 1,
48-
}
49-
case 'DECREMENT':
50-
return {
51-
count: state.count - 1,
52-
}
53-
default:
54-
return state
55-
}
56-
}
57-
```
58-
59-
To test our connected component we can create a custom `render` function using
60-
the `wrapper` option as explained in the
61-
[setup](./react-testing-library/setup.md) page.
62-
Our custom `render` function can look like this:
63-
64-
```js
65-
// test-utils.js
66-
import React from 'react'
67-
import { render as rtlRender } from '@testing-library/react'
68-
import { createStore } from 'redux'
69-
import { Provider } from 'react-redux'
70-
import { initialState as reducerInitialState, reducer } from './reducer'
71-
72-
function render(
73-
ui,
74-
{
75-
initialState = reducerInitialState,
76-
store = createStore(reducer, initialState),
77-
...renderOptions
78-
} = {}
79-
) {
80-
function Wrapper({ children }) {
81-
return <Provider store={store}>{children}</Provider>
82-
}
83-
return rtlRender(ui, { wrapper: Wrapper, ...renderOptions })
84-
}
85-
86-
// re-export everything
87-
export * from '@testing-library/react'
88-
89-
// override render method
90-
export { render }
91-
```
92-
93-
```jsx
94-
// counter.test.js
95-
import React from 'react'
96-
import { createStore } from 'redux'
97-
// We're using our own custom render function and not RTL's render
98-
// our custom utils also re-export everything from RTL
99-
// so we can import fireEvent and screen here as well
100-
import { render, fireEvent, screen } from './test-utils'
101-
import '@testing-library/jest-dom/extend-expect'
102-
import Counter from './counter'
103-
104-
test('can render with redux with defaults', () => {
105-
render(<Counter />)
106-
fireEvent.click(screen.getByText('+'))
107-
expect(screen.getByTestId('count-value')).toHaveTextContent('1')
108-
})
109-
110-
test('can render with redux with custom initial state', () => {
111-
render(<Counter />, {
112-
initialState: { count: 3 },
113-
})
114-
fireEvent.click(screen.getByText('-'))
115-
expect(screen.getByTestId('count-value')).toHaveTextContent('2')
116-
})
117-
118-
test('can render with redux with custom store', () => {
119-
// this is a silly store that can never be changed
120-
const store = createStore(() => ({ count: 1000 }))
121-
render(<Counter />, {
122-
store,
123-
})
124-
fireEvent.click(screen.getByText('+'))
125-
expect(screen.getByTestId('count-value')).toHaveTextContent('1000')
126-
fireEvent.click(screen.getByText('-'))
127-
expect(screen.getByTestId('count-value')).toHaveTextContent('1000')
128-
})
129-
```
6+
Moved to
7+
[Writing Tests | Redux](https://redux.js.org/recipes/writing-tests#connected-components)

netlify.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,3 +138,10 @@
138138
to = "https://testing-library.com/docs/nightwatch-testing-library/intro"
139139
status = 301
140140
force = true
141+
142+
# React Redux recipe to Redux's recipe
143+
[[redirects]]
144+
from = "https://testing-library.com/docs/example-react-redux"
145+
to = "https://redux.js.org/recipes/writing-tests#connected-components"
146+
status = 301
147+
force = true

0 commit comments

Comments
 (0)