Welcome to mirror list, hosted at ThFree Co, Russian Federation.

actions_spec.js « store « subscriptions « jira_connect « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 53b5d8e70af4dc23abe18aa870c1abce58383243 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
import testAction from 'helpers/vuex_action_helper';

import * as types from '~/jira_connect/subscriptions/store/mutation_types';
import {
  fetchSubscriptions,
  loadCurrentUser,
  addSubscription,
} from '~/jira_connect/subscriptions/store/actions';
import state from '~/jira_connect/subscriptions/store/state';
import * as api from '~/jira_connect/subscriptions/api';
import * as userApi from '~/api/user_api';
import * as integrationsApi from '~/api/integrations_api';
import {
  I18N_DEFAULT_SUBSCRIPTIONS_ERROR_MESSAGE,
  I18N_ADD_SUBSCRIPTION_SUCCESS_ALERT_TITLE,
  I18N_ADD_SUBSCRIPTION_SUCCESS_ALERT_MESSAGE,
  INTEGRATIONS_DOC_LINK,
} from '~/jira_connect/subscriptions/constants';
import * as utils from '~/jira_connect/subscriptions/utils';

describe('JiraConnect actions', () => {
  let mockedState;

  beforeEach(() => {
    mockedState = state();
  });

  describe('fetchSubscriptions', () => {
    const mockUrl = '/mock-url';

    describe('when API request is successful', () => {
      it('should commit SET_SUBSCRIPTIONS_LOADING and SET_SUBSCRIPTIONS mutations', async () => {
        jest.spyOn(api, 'fetchSubscriptions').mockResolvedValue({ data: { subscriptions: [] } });

        await testAction(
          fetchSubscriptions,
          mockUrl,
          mockedState,
          [
            { type: types.SET_SUBSCRIPTIONS_LOADING, payload: true },
            { type: types.SET_SUBSCRIPTIONS, payload: [] },
            { type: types.SET_SUBSCRIPTIONS_LOADING, payload: false },
          ],
          [],
        );

        expect(api.fetchSubscriptions).toHaveBeenCalledWith(mockUrl);
      });
    });

    describe('when API request fails', () => {
      it('should commit SET_SUBSCRIPTIONS_LOADING, SET_SUBSCRIPTIONS_ERROR and SET_ALERT mutations', async () => {
        jest.spyOn(api, 'fetchSubscriptions').mockRejectedValue();

        await testAction(
          fetchSubscriptions,
          mockUrl,
          mockedState,
          [
            { type: types.SET_SUBSCRIPTIONS_LOADING, payload: true },
            { type: types.SET_SUBSCRIPTIONS_ERROR, payload: true },
            {
              type: types.SET_ALERT,
              payload: { message: I18N_DEFAULT_SUBSCRIPTIONS_ERROR_MESSAGE, variant: 'danger' },
            },
            { type: types.SET_SUBSCRIPTIONS_LOADING, payload: false },
          ],
          [],
        );

        expect(api.fetchSubscriptions).toHaveBeenCalledWith(mockUrl);
      });
    });
  });

  describe('loadCurrentUser', () => {
    const mockAccessToken = 'abcd1234';

    describe('when API request succeeds', () => {
      it('commits the SET_ACCESS_TOKEN and SET_CURRENT_USER mutations', async () => {
        const mockUser = { name: 'root' };
        jest.spyOn(userApi, 'getCurrentUser').mockResolvedValue({ data: mockUser });

        await testAction(
          loadCurrentUser,
          mockAccessToken,
          mockedState,
          [{ type: types.SET_CURRENT_USER, payload: mockUser }],
          [],
        );

        expect(userApi.getCurrentUser).toHaveBeenCalledWith({
          headers: { Authorization: `Bearer ${mockAccessToken}` },
        });
      });
    });

    describe('when API request fails', () => {
      it('commits the SET_CURRENT_USER_ERROR mutation', async () => {
        jest.spyOn(userApi, 'getCurrentUser').mockRejectedValue();

        await testAction(
          loadCurrentUser,
          mockAccessToken,
          mockedState,
          [{ type: types.SET_CURRENT_USER_ERROR }],
          [],
        );
      });
    });
  });

  describe('addSubscription', () => {
    const mockNamespace = 'gitlab-org/gitlab';
    const mockSubscriptionsPath = '/subscriptions';

    beforeEach(() => {
      jest.spyOn(utils, 'getJwt').mockReturnValue('1234');
    });

    describe('when API request succeeds', () => {
      it('commits the SET_ACCESS_TOKEN and SET_CURRENT_USER mutations', async () => {
        jest
          .spyOn(integrationsApi, 'addJiraConnectSubscription')
          .mockResolvedValue({ success: true });

        await testAction(
          addSubscription,
          { namespacePath: mockNamespace, subscriptionsPath: mockSubscriptionsPath },
          mockedState,
          [
            { type: types.ADD_SUBSCRIPTION_LOADING, payload: true },
            {
              type: types.SET_ALERT,
              payload: {
                title: I18N_ADD_SUBSCRIPTION_SUCCESS_ALERT_TITLE,
                message: I18N_ADD_SUBSCRIPTION_SUCCESS_ALERT_MESSAGE,
                linkUrl: INTEGRATIONS_DOC_LINK,
                variant: 'success',
              },
            },
            { type: types.ADD_SUBSCRIPTION_LOADING, payload: false },
          ],
          [{ type: 'fetchSubscriptions', payload: mockSubscriptionsPath }],
        );

        expect(integrationsApi.addJiraConnectSubscription).toHaveBeenCalledWith(mockNamespace, {
          accessToken: null,
          jwt: '1234',
        });
      });
    });

    describe('when API request fails', () => {
      it('commits the SET_CURRENT_USER_ERROR mutation', async () => {
        jest.spyOn(integrationsApi, 'addJiraConnectSubscription').mockRejectedValue();

        await testAction(
          addSubscription,
          mockNamespace,
          mockedState,
          [
            { type: types.ADD_SUBSCRIPTION_LOADING, payload: true },
            { type: types.ADD_SUBSCRIPTION_ERROR },
            { type: types.ADD_SUBSCRIPTION_LOADING, payload: false },
          ],
          [],
        );
      });
    });
  });
});