|
| 1 | +import React from 'react'; |
| 2 | +import { createStore, combineReducers } from 'redux'; |
| 3 | +import { Provider } from 'react-redux'; |
| 4 | +import { render, fireEvent, cleanup } from 'react-testing-library'; |
| 5 | + |
| 6 | +import { FCCounterConnectedExtended as ConnectedCounter } from './fc-counter-connected-extended'; |
| 7 | + |
| 8 | +const reducer = combineReducers({ |
| 9 | + counters: combineReducers({ |
| 10 | + reduxCounter: (state = 0, action: any) => { |
| 11 | + switch (action.type) { |
| 12 | + case 'counters/INCREMENT': |
| 13 | + return state + 1; // action: { type: "INCREMENT"; } |
| 14 | + |
| 15 | + default: |
| 16 | + return state; |
| 17 | + } |
| 18 | + }, |
| 19 | + }), |
| 20 | +}); |
| 21 | + |
| 22 | +afterEach(cleanup); |
| 23 | + |
| 24 | +test('can render with redux with defaults', () => { |
| 25 | + const label = 'Counter 1'; |
| 26 | + const { getByText } = renderWithRedux(<ConnectedCounter label={label} />); |
| 27 | + fireEvent.click(getByText('Increment')); |
| 28 | + expect(getByText(RegExp(label)).textContent).toBe(label + ': 1'); |
| 29 | +}); |
| 30 | + |
| 31 | +test('can render with redux with custom initial state', () => { |
| 32 | + const label = 'Counter 1'; |
| 33 | + const { getByText } = renderWithRedux(<ConnectedCounter label={label} />, { |
| 34 | + initialState: { counters: { reduxCounter: 3 } }, |
| 35 | + }); |
| 36 | + fireEvent.click(getByText('Increment')); |
| 37 | + expect(getByText(RegExp(label)).textContent).toBe(label + ': 4'); |
| 38 | +}); |
| 39 | + |
| 40 | +// TODO: move to external utils |
| 41 | +// Redux Provider utility |
| 42 | +function renderWithRedux( |
| 43 | + jsx: JSX.Element, |
| 44 | + options: { initialState?: object } = {} |
| 45 | +) { |
| 46 | + const store = createStore(reducer, options.initialState); |
| 47 | + return { |
| 48 | + ...render(<Provider store={store}>{jsx}</Provider>), |
| 49 | + store, |
| 50 | + }; |
| 51 | +} |
0 commit comments