summaryrefslogtreecommitdiff
path: root/spec/frontend/integrations/edit/store/actions_spec.js
blob: b413de2b286829b5b5d223c9a317f7cd8aaa8a9a (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
116
117
118
119
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import testAction from 'helpers/vuex_action_helper';
import { I18N_FETCH_TEST_SETTINGS_DEFAULT_ERROR_MESSAGE } from '~/integrations/constants';
import {
  setOverride,
  setIsResetting,
  requestResetIntegration,
  receiveResetIntegrationSuccess,
  receiveResetIntegrationError,
  requestJiraIssueTypes,
  receiveJiraIssueTypesSuccess,
  receiveJiraIssueTypesError,
} from '~/integrations/edit/store/actions';
import * as types from '~/integrations/edit/store/mutation_types';
import createState from '~/integrations/edit/store/state';
import { refreshCurrentPage } from '~/lib/utils/url_utility';
import { mockJiraIssueTypes } from '../mock_data';

jest.mock('~/lib/utils/url_utility');

describe('Integration form store actions', () => {
  let state;
  let mockAxios;

  beforeEach(() => {
    state = createState();
    mockAxios = new MockAdapter(axios);
  });

  afterEach(() => {
    mockAxios.restore();
  });

  describe('setOverride', () => {
    it('should commit override mutation', () => {
      return testAction(setOverride, true, state, [{ type: types.SET_OVERRIDE, payload: true }]);
    });
  });

  describe('setIsResetting', () => {
    it('should commit isResetting mutation', () => {
      return testAction(setIsResetting, true, state, [
        { type: types.SET_IS_RESETTING, payload: true },
      ]);
    });
  });

  describe('requestResetIntegration', () => {
    it('should commit REQUEST_RESET_INTEGRATION mutation', () => {
      return testAction(requestResetIntegration, null, state, [
        { type: types.REQUEST_RESET_INTEGRATION },
      ]);
    });
  });

  describe('receiveResetIntegrationSuccess', () => {
    it('should call refreshCurrentPage()', () => {
      return testAction(receiveResetIntegrationSuccess, null, state, [], [], () => {
        expect(refreshCurrentPage).toHaveBeenCalled();
      });
    });
  });

  describe('receiveResetIntegrationError', () => {
    it('should commit RECEIVE_RESET_INTEGRATION_ERROR mutation', () => {
      return testAction(receiveResetIntegrationError, null, state, [
        { type: types.RECEIVE_RESET_INTEGRATION_ERROR },
      ]);
    });
  });

  describe('requestJiraIssueTypes', () => {
    describe.each`
      scenario                              | responseCode | response                              | action
      ${'when successful'}                  | ${200}       | ${{ issuetypes: mockJiraIssueTypes }} | ${{ type: 'receiveJiraIssueTypesSuccess', payload: mockJiraIssueTypes }}
      ${'when response has no issue types'} | ${200}       | ${{ issuetypes: [] }}                 | ${{ type: 'receiveJiraIssueTypesError', payload: I18N_FETCH_TEST_SETTINGS_DEFAULT_ERROR_MESSAGE }}
      ${'when response includes error'}     | ${200}       | ${{ error: new Error() }}             | ${{ type: 'receiveJiraIssueTypesError', payload: I18N_FETCH_TEST_SETTINGS_DEFAULT_ERROR_MESSAGE }}
      ${'when error occurs'}                | ${500}       | ${{}}                                 | ${{ type: 'receiveJiraIssueTypesError', payload: expect.any(String) }}
    `('$scenario', ({ responseCode, response, action }) => {
      it(`should commit SET_JIRA_ISSUE_TYPES_ERROR_MESSAGE and SET_IS_LOADING_JIRA_ISSUE_TYPES mutations, and dispatch ${action.type}`, () => {
        mockAxios.onPut('/test').replyOnce(responseCode, response);

        return testAction(
          requestJiraIssueTypes,
          new FormData(),
          { propsSource: { testPath: '/test' } },
          [
            // should clear the error messages and set the loading state
            { type: types.SET_JIRA_ISSUE_TYPES_ERROR_MESSAGE, payload: '' },
            { type: types.SET_IS_LOADING_JIRA_ISSUE_TYPES, payload: true },
          ],
          [action],
        );
      });
    });
  });

  describe('receiveJiraIssueTypesSuccess', () => {
    it('should commit SET_IS_LOADING_JIRA_ISSUE_TYPES and SET_JIRA_ISSUE_TYPES mutations', () => {
      const issueTypes = ['issue', 'epic'];
      return testAction(receiveJiraIssueTypesSuccess, issueTypes, state, [
        { type: types.SET_IS_LOADING_JIRA_ISSUE_TYPES, payload: false },
        { type: types.SET_JIRA_ISSUE_TYPES, payload: issueTypes },
      ]);
    });
  });

  describe('receiveJiraIssueTypesError', () => {
    it('should commit SET_IS_LOADING_JIRA_ISSUE_TYPES, SET_JIRA_ISSUE_TYPES and SET_JIRA_ISSUE_TYPES_ERROR_MESSAGE mutations', () => {
      const errorMessage = 'something went wrong';
      return testAction(receiveJiraIssueTypesError, errorMessage, state, [
        { type: types.SET_IS_LOADING_JIRA_ISSUE_TYPES, payload: false },
        { type: types.SET_JIRA_ISSUE_TYPES, payload: [] },
        { type: types.SET_JIRA_ISSUE_TYPES_ERROR_MESSAGE, payload: errorMessage },
      ]);
    });
  });
});