|
| 1 | +import React from 'react' |
| 2 | +import { createStore } from 'redux' |
| 3 | +import { mount } from 'enzyme' |
| 4 | +import { memoize } from 'lodash' |
| 5 | + |
| 6 | +import Redux from '../redux' |
| 7 | + |
| 8 | +const reducer = (state = { test: 0 }, action) => { |
| 9 | + switch (action.type) { |
| 10 | + case 'INCREMENT': |
| 11 | + return { test: state.test + 1 } |
| 12 | + default: |
| 13 | + return state |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +const store = createStore(reducer) |
| 18 | + |
| 19 | +describe('Redux', function() { |
| 20 | + const wrapper = mount( |
| 21 | + <Redux selector={state => state.test}> |
| 22 | + {(test, dispatch) => ( |
| 23 | + <div onClick={() => dispatch({ type: 'INCREMENT' })}>{test}</div> |
| 24 | + )} |
| 25 | + </Redux>, |
| 26 | + { context: { reduxRenderStore: store } } |
| 27 | + ) |
| 28 | + |
| 29 | + it('should render without throwing an error', () => { |
| 30 | + expect(wrapper.exists()).toBe(true) |
| 31 | + }) |
| 32 | + |
| 33 | + it('should contain the text from the store', () => { |
| 34 | + expect(wrapper.html()).toBe('<div>0</div>') |
| 35 | + }) |
| 36 | + |
| 37 | + it('should change when dispatch is called', () => { |
| 38 | + wrapper.find('div').simulate('click') |
| 39 | + expect(wrapper.html()).toBe('<div>1</div>') |
| 40 | + }) |
| 41 | + |
| 42 | + it('should not re-render when state is same', () => { |
| 43 | + let renderCount = 0 |
| 44 | + |
| 45 | + const memoizedSelector = memoize(state => state.test) |
| 46 | + const wrapper = mount( |
| 47 | + <Redux selector={memoizedSelector}> |
| 48 | + {(test, dispatch) => { |
| 49 | + // Increment the render count to show that a side effect occurred |
| 50 | + renderCount += 1 |
| 51 | + return ( |
| 52 | + <div onClick={() => dispatch({ type: 'NOT_INCREMENT' })}> |
| 53 | + {test} |
| 54 | + </div> |
| 55 | + ) |
| 56 | + }} |
| 57 | + </Redux>, |
| 58 | + { context: { reduxRenderStore: store } } |
| 59 | + ) |
| 60 | + |
| 61 | + expect(renderCount).toBe(1) |
| 62 | + |
| 63 | + wrapper.find('div').simulate('click') |
| 64 | + expect(renderCount).toBe(1) |
| 65 | + }) |
| 66 | +}) |
0 commit comments