@@ -4,12 +4,10 @@ title: React Redux
4
4
---
5
5
6
6
``` jsx
7
+ // counter.js
7
8
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'
11
10
12
- // counter.js
13
11
class Counter extends React .Component {
14
12
increment = () => {
15
13
this .props .dispatch ({ type: ' INCREMENT' })
@@ -33,14 +31,18 @@ class Counter extends React.Component {
33
31
}
34
32
}
35
33
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 ` :
41
38
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 ) {
44
46
switch (action .type ) {
45
47
case ' INCREMENT' :
46
48
return {
@@ -54,18 +56,18 @@ function reducer(state = { count: 0 }, action) {
54
56
return state
55
57
}
56
58
}
59
+ ```
57
60
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:
67
62
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'
69
71
70
72
afterEach (cleanup)
71
73
@@ -86,13 +88,13 @@ function renderWithRedux(
86
88
}
87
89
88
90
test (' can render with redux with defaults' , () => {
89
- const { getByTestId , getByText } = renderWithRedux (< ConnectedCounter / > )
91
+ const { getByTestId , getByText } = renderWithRedux (< Counter / > )
90
92
fireEvent .click (getByText (' +' ))
91
93
expect (getByTestId (' count-value' ).textContent ).toBe (' 1' )
92
94
})
93
95
94
96
test (' can render with redux with custom initial state' , () => {
95
- const { getByTestId , getByText } = renderWithRedux (< ConnectedCounter / > , {
97
+ const { getByTestId , getByText } = renderWithRedux (< Counter / > , {
96
98
initialState: { count: 3 },
97
99
})
98
100
fireEvent .click (getByText (' -' ))
@@ -102,7 +104,7 @@ test('can render with redux with custom initial state', () => {
102
104
test (' can render with redux with custom store' , () => {
103
105
// this is a silly store that can never be changed
104
106
const store = createStore (() => ({ count: 1000 }))
105
- const { getByTestId , getByText } = renderWithRedux (< ConnectedCounter / > , {
107
+ const { getByTestId , getByText } = renderWithRedux (< Counter / > , {
106
108
store,
107
109
})
108
110
fireEvent .click (getByText (' +' ))
0 commit comments