|
| 1 | +import React from 'react' |
| 2 | +import {createStore} from 'redux' |
| 3 | +import {Provider, connect} from 'react-redux' |
| 4 | +import {render, Simulate} from '../' |
| 5 | + |
| 6 | +// counter.js |
| 7 | +class Counter extends React.Component { |
| 8 | + increment = () => { |
| 9 | + this.props.dispatch({type: 'INCREMENT'}) |
| 10 | + } |
| 11 | + |
| 12 | + decrement = () => { |
| 13 | + this.props.dispatch({type: 'DECREMENT'}) |
| 14 | + } |
| 15 | + |
| 16 | + render() { |
| 17 | + return ( |
| 18 | + <div> |
| 19 | + <h2>Counter</h2> |
| 20 | + <div> |
| 21 | + <button onClick={this.decrement} data-testid="decrementer"> |
| 22 | + - |
| 23 | + </button> |
| 24 | + <span data-testid="count-value">{this.props.count}</span> |
| 25 | + <button onClick={this.increment} data-testid="incrementer"> |
| 26 | + + |
| 27 | + </button> |
| 28 | + </div> |
| 29 | + </div> |
| 30 | + ) |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +// normally this would be: |
| 35 | +// export default connect(state => ({count: state.count}))(Counter) |
| 36 | +// but for this test we'll give it a variable name |
| 37 | +// because we're doing this all in one file |
| 38 | +const ConnectedCounter = connect(state => ({count: state.count}))(Counter) |
| 39 | + |
| 40 | +// app.js |
| 41 | +function reducer(state = {count: 0}, action) { |
| 42 | + switch (action.type) { |
| 43 | + case 'INCREMENT': |
| 44 | + return { |
| 45 | + count: state.count + 1, |
| 46 | + } |
| 47 | + case 'DECREMENT': |
| 48 | + return { |
| 49 | + count: state.count - 1, |
| 50 | + } |
| 51 | + default: |
| 52 | + return state |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// normally here you'd do: |
| 57 | +// const store = createStore(reducer) |
| 58 | +// ReactDOM.render( |
| 59 | +// <Provider store={store}> |
| 60 | +// <Counter /> |
| 61 | +// </Provider>, |
| 62 | +// document.getElementById('root'), |
| 63 | +// ) |
| 64 | +// but for this test we'll umm... not do that :) |
| 65 | + |
| 66 | +// Now here's what your test will look like: |
| 67 | + |
| 68 | +// this is a handy function that I normally make available for all my tests |
| 69 | +// that deal with connected components. |
| 70 | +// you can provide initialState or the entire store that the ui is rendered with |
| 71 | +function renderWithRedux( |
| 72 | + ui, |
| 73 | + {initialState, store = createStore(reducer, initialState)} = {}, |
| 74 | +) { |
| 75 | + return { |
| 76 | + ...render(<Provider store={store}>{ui}</Provider>), |
| 77 | + // adding `store` to the returned utilities to allow us |
| 78 | + // to reference it in our tests (just try to avoid using |
| 79 | + // this to test implementation details). |
| 80 | + store, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +test('can render with redux with defaults', () => { |
| 85 | + const {queryByTestId} = renderWithRedux(<ConnectedCounter />) |
| 86 | + Simulate.click(queryByTestId('incrementer')) |
| 87 | + expect(queryByTestId('count-value').textContent).toBe('1') |
| 88 | +}) |
| 89 | + |
| 90 | +test('can render with redux with custom initial state', () => { |
| 91 | + const {queryByTestId} = renderWithRedux(<ConnectedCounter />, { |
| 92 | + initialState: {count: 3}, |
| 93 | + }) |
| 94 | + Simulate.click(queryByTestId('decrementer')) |
| 95 | + expect(queryByTestId('count-value').textContent).toBe('2') |
| 96 | +}) |
| 97 | + |
| 98 | +test('can render with redux with custom store', () => { |
| 99 | + // this is a silly store that can never be changed |
| 100 | + const store = createStore(() => ({count: 1000})) |
| 101 | + const {queryByTestId} = renderWithRedux(<ConnectedCounter />, { |
| 102 | + store, |
| 103 | + }) |
| 104 | + Simulate.click(queryByTestId('incrementer')) |
| 105 | + expect(queryByTestId('count-value').textContent).toBe('1000') |
| 106 | + Simulate.click(queryByTestId('decrementer')) |
| 107 | + expect(queryByTestId('count-value').textContent).toBe('1000') |
| 108 | +}) |
0 commit comments