summaryrefslogtreecommitdiff
path: root/spec/frontend/admin/statistics_panel/store/actions_spec.js
blob: c7481b664b31593054eebd72d8cbddacde5ca320 (plain)
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
114
115
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import testAction from 'helpers/vuex_action_helper';
import * as actions from '~/admin/statistics_panel/store/actions';
import * as types from '~/admin/statistics_panel/store/mutation_types';
import getInitialState from '~/admin/statistics_panel/store/state';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import mockStatistics from '../mock_data';

describe('Admin statistics panel actions', () => {
  let mock;
  let state;

  beforeEach(() => {
    state = getInitialState();
    mock = new MockAdapter(axios);
  });

  describe('fetchStatistics', () => {
    describe('success', () => {
      beforeEach(() => {
        mock.onGet(/api\/(.*)\/application\/statistics/).replyOnce(200, mockStatistics);
      });

      it('dispatches success with received data', (done) =>
        testAction(
          actions.fetchStatistics,
          null,
          state,
          [],
          [
            { type: 'requestStatistics' },
            {
              type: 'receiveStatisticsSuccess',
              payload: expect.objectContaining(
                convertObjectPropsToCamelCase(mockStatistics, { deep: true }),
              ),
            },
          ],
          done,
        ));
    });

    describe('error', () => {
      beforeEach(() => {
        mock.onGet(/api\/(.*)\/application\/statistics/).replyOnce(500);
      });

      it('dispatches error', (done) =>
        testAction(
          actions.fetchStatistics,
          null,
          state,
          [],
          [
            {
              type: 'requestStatistics',
            },
            {
              type: 'receiveStatisticsError',
              payload: new Error('Request failed with status code 500'),
            },
          ],
          done,
        ));
    });
  });

  describe('requestStatistic', () => {
    it('should commit the request mutation', (done) =>
      testAction(
        actions.requestStatistics,
        null,
        state,
        [{ type: types.REQUEST_STATISTICS }],
        [],
        done,
      ));
  });

  describe('receiveStatisticsSuccess', () => {
    it('should commit received data', (done) =>
      testAction(
        actions.receiveStatisticsSuccess,
        mockStatistics,
        state,
        [
          {
            type: types.RECEIVE_STATISTICS_SUCCESS,
            payload: mockStatistics,
          },
        ],
        [],
        done,
      ));
  });

  describe('receiveStatisticsError', () => {
    it('should commit error', (done) => {
      testAction(
        actions.receiveStatisticsError,
        500,
        state,
        [
          {
            type: types.RECEIVE_STATISTICS_ERROR,
            payload: 500,
          },
        ],
        [],
        done,
      );
    });
  });
});