@@ -4,12 +4,10 @@ title: React Redux
44---
55
66``` jsx
7+ // counter.js
78import 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
1311class 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
7072afterEach (cleanup)
7173
@@ -86,13 +88,13 @@ function renderWithRedux(
8688}
8789
8890test (' 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
9496test (' 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', () => {
102104test (' 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