File size: 3,034 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { act, renderHook } from '@testing-library/react-hooks';
import logger from 'redux-logger';
import thunk from 'redux-thunk';
import createReducer from '../src/factory/createReducer';

it('should init reducer hook function', () => {
  const useSomeReducer = createReducer();
  expect(useSomeReducer).toBeInstanceOf(Function);
});

/**
 * This test suite implements the special demo in storybook that creates a
 * reducer with thunk and logger for using a simple counter
 */
describe('when using created reducer hook', () => {
  const initialCount = 1;
  let originalLog;
  let originalGroup;
  const mockLog = jest.fn();
  const mockGroup = jest.fn();

  function reducer(state, action) {
    switch (action.type) {
      case 'increment':
        return { count: state.count + 1 };
      case 'decrement':
        return { count: state.count - 1 };
      case 'reset':
        return { count: action.payload };
      default:
        throw new Error();
    }
  }

  // Action creator to increment count, wait a second and then reset
  const addAndReset = () => {
    return (dispatch) => {
      dispatch({ type: 'increment' });

      setTimeout(() => {
        dispatch({ type: 'reset', payload: initialCount });
      }, 1000);
    };
  };

  const setUp = () => {
    const useThunkReducer = createReducer(thunk, logger);
    return renderHook(() => useThunkReducer(reducer, { count: initialCount }));
  };

  beforeAll(() => {
    originalLog = console.log;
    originalGroup = console.group;
    console.log = mockLog;
    console.group = mockGroup;
  });

  beforeEach(() => {
    jest.useFakeTimers();
  });

  afterAll(() => {
    console.log = originalLog;
    console.group = originalGroup;
  });

  it('should init state and dispatcher', () => {
    const { result } = setUp();
    const [state, dispatch] = result.current;

    expect(state).toEqual({ count: 1 });
    expect(dispatch).toBeInstanceOf(Function);
  });

  it.each`
    actionType     | expectedCount | payload
    ${'increment'} | ${2}          | ${undefined}
    ${'decrement'} | ${0}          | ${undefined}
    ${'reset'}     | ${1}          | ${1}
  `('should handle "$actionType" action', ({ actionType, expectedCount, payload }) => {
    const { result } = setUp();
    const [, dispatch] = result.current;
    expect(mockLog).not.toHaveBeenCalled();

    act(() => {
      dispatch({ type: actionType, payload });
    });

    expect(result.current[0]).toEqual({ count: expectedCount });
    expect(mockLog).toHaveBeenCalled();
  });

  it('should handle async action with several middlewares', () => {
    const { result } = setUp();
    const [, dispatch] = result.current;
    expect(mockLog).not.toHaveBeenCalled();

    act(() => {
      dispatch(addAndReset());
    });

    expect(result.current[0]).toEqual({ count: 2 });
    expect(mockLog).toHaveBeenCalled();

    // fast-forward until all timers have been executed
    act(() => {
      jest.runAllTimers();
    });

    expect(result.current[0]).toEqual({ count: 1 });
  });
});