|
| 1 | +import React from 'react' |
| 2 | +import { Provider } from 'react-redux' |
| 3 | +import { mount } from 'enzyme' |
| 4 | +import configureStore from 'redux-mock-store' |
| 5 | + |
| 6 | +import { actionTypes } from '../../features/counter' |
| 7 | +import Counter from './Counter' |
| 8 | + |
| 9 | +describe('Counter', () => { |
| 10 | + const mockStore = configureStore([]) |
| 11 | + const store = mockStore({ |
| 12 | + count: { |
| 13 | + value: 42, |
| 14 | + }, |
| 15 | + }) |
| 16 | + |
| 17 | + // Add jest mock spy to watch for store.dispatch method. See https://jestjs.io/docs/en/jest-object#jestspyonobject-methodname for more info |
| 18 | + jest.spyOn(store, 'dispatch') |
| 19 | + |
| 20 | + beforeEach(() => { |
| 21 | + // Clear any saved mock data from previous tests, because jest saves calls data for spies and mocks, https://jestjs.io/docs/en/mock-function-api#mockfnmockclear |
| 22 | + store.dispatch.mockClear() |
| 23 | + }) |
| 24 | + |
| 25 | + it('renders without crashing.', () => { |
| 26 | + const wrapper = mount( |
| 27 | + <Provider store={store}> |
| 28 | + <Counter /> |
| 29 | + </Provider> |
| 30 | + ) |
| 31 | + |
| 32 | + const countValue = wrapper.find('strong').text() |
| 33 | + expect(countValue).toBe('42') |
| 34 | + }) |
| 35 | + |
| 36 | + it('should be possible to increment counter.', () => { |
| 37 | + const wrapper = mount( |
| 38 | + <Provider store={store}> |
| 39 | + <Counter /> |
| 40 | + </Provider> |
| 41 | + ) |
| 42 | + |
| 43 | + wrapper |
| 44 | + .find('button') |
| 45 | + .filter({ 'data-qa': 'increment-counter' }) |
| 46 | + .simulate('click') |
| 47 | + |
| 48 | + expect(store.dispatch).toBeCalledTimes(1) |
| 49 | + |
| 50 | + expect(store.dispatch).toBeCalledWith({ |
| 51 | + type: actionTypes.INCREMENT_COUNTER, |
| 52 | + }) |
| 53 | + }) |
| 54 | + |
| 55 | + it('should be possible to decrement counter.', () => { |
| 56 | + const wrapper = mount( |
| 57 | + <Provider store={store}> |
| 58 | + <Counter /> |
| 59 | + </Provider> |
| 60 | + ) |
| 61 | + |
| 62 | + wrapper |
| 63 | + .find('button') |
| 64 | + .filter({ 'data-qa': 'decrement-counter' }) |
| 65 | + .simulate('click') |
| 66 | + |
| 67 | + expect(store.dispatch).toHaveBeenCalledTimes(1) |
| 68 | + |
| 69 | + expect(store.dispatch).toHaveBeenCalledWith({ |
| 70 | + type: actionTypes.DECREMENT_COUNTER, |
| 71 | + }) |
| 72 | + }) |
| 73 | +}) |
0 commit comments