summaryrefslogtreecommitdiff
path: root/spec/frontend/registry/settings/store/actions_spec.js
blob: 71c815cd19c93af5207b57d09f5bd1dbe7afe361 (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
120
import Api from '~/api';
import createFlash from '~/flash';
import testAction from 'helpers/vuex_action_helper';
import * as actions from '~/registry/settings/store/actions';
import * as types from '~/registry/settings/store/mutation_types';
import {
  UPDATE_SETTINGS_ERROR_MESSAGE,
  FETCH_SETTINGS_ERROR_MESSAGE,
  UPDATE_SETTINGS_SUCCESS_MESSAGE,
} from '~/registry/settings/constants';

jest.mock('~/flash');

describe('Actions Registry Store', () => {
  describe.each`
    actionName                  | mutationName               | payload
    ${'setInitialState'}        | ${types.SET_INITIAL_STATE} | ${'foo'}
    ${'updateSettings'}         | ${types.UPDATE_SETTINGS}   | ${'foo'}
    ${'receiveSettingsSuccess'} | ${types.SET_SETTINGS}      | ${'foo'}
    ${'toggleLoading'}          | ${types.TOGGLE_LOADING}    | ${undefined}
    ${'resetSettings'}          | ${types.RESET_SETTINGS}    | ${undefined}
  `('%s action invokes %s mutation with payload %s', ({ actionName, mutationName, payload }) => {
    it('should set the initial state', done => {
      testAction(actions[actionName], payload, {}, [{ type: mutationName, payload }], [], done);
    });
  });

  describe.each`
    actionName                | message
    ${'receiveSettingsError'} | ${FETCH_SETTINGS_ERROR_MESSAGE}
    ${'updateSettingsError'}  | ${UPDATE_SETTINGS_ERROR_MESSAGE}
  `('%s action', ({ actionName, message }) => {
    it(`should call createFlash with ${message}`, done => {
      testAction(actions[actionName], null, null, [], [], () => {
        expect(createFlash).toHaveBeenCalledWith(message);
        done();
      });
    });
  });

  describe('fetchSettings', () => {
    const state = {
      projectId: 'bar',
    };

    const payload = {
      tag_expiration_policies: 'foo',
    };

    it('should fetch the data from the API', done => {
      Api.project = jest.fn().mockResolvedValue(payload);
      testAction(
        actions.fetchSettings,
        null,
        state,
        [],
        [
          { type: 'toggleLoading' },
          { type: 'receiveSettingsSuccess', payload: payload.tag_expiration_policies },
          { type: 'toggleLoading' },
        ],
        done,
      );
    });

    it('should call receiveSettingsError on error', done => {
      Api.project = jest.fn().mockRejectedValue();
      testAction(
        actions.fetchSettings,
        null,
        state,
        [],
        [{ type: 'toggleLoading' }, { type: 'receiveSettingsError' }, { type: 'toggleLoading' }],
        done,
      );
    });
  });

  describe('saveSettings', () => {
    const state = {
      projectId: 'bar',
      settings: 'baz',
    };

    const payload = {
      tag_expiration_policies: 'foo',
    };

    it('should fetch the data from the API', done => {
      Api.updateProject = jest.fn().mockResolvedValue(payload);
      testAction(
        actions.saveSettings,
        null,
        state,
        [],
        [
          { type: 'toggleLoading' },
          { type: 'receiveSettingsSuccess', payload: payload.tag_expiration_policies },
          { type: 'toggleLoading' },
        ],
        () => {
          expect(createFlash).toHaveBeenCalledWith(UPDATE_SETTINGS_SUCCESS_MESSAGE);
          done();
        },
      );
    });

    it('should call receiveSettingsError on error', done => {
      Api.updateProject = jest.fn().mockRejectedValue();
      testAction(
        actions.saveSettings,
        null,
        state,
        [],
        [{ type: 'toggleLoading' }, { type: 'updateSettingsError' }, { type: 'toggleLoading' }],
        done,
      );
    });
  });
});