@@ -3,127 +3,5 @@ id: example-react-redux
3
3
title : React Redux
4
4
---
5
5
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 )
0 commit comments