diff options
Diffstat (limited to 'spec/frontend/jira_connect/subscriptions/store')
-rw-r--r-- | spec/frontend/jira_connect/subscriptions/store/actions_spec.js | 172 | ||||
-rw-r--r-- | spec/frontend/jira_connect/subscriptions/store/mutations_spec.js | 67 |
2 files changed, 239 insertions, 0 deletions
diff --git a/spec/frontend/jira_connect/subscriptions/store/actions_spec.js b/spec/frontend/jira_connect/subscriptions/store/actions_spec.js new file mode 100644 index 00000000000..53b5d8e70af --- /dev/null +++ b/spec/frontend/jira_connect/subscriptions/store/actions_spec.js @@ -0,0 +1,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 }, + ], + [], + ); + }); + }); + }); +}); diff --git a/spec/frontend/jira_connect/subscriptions/store/mutations_spec.js b/spec/frontend/jira_connect/subscriptions/store/mutations_spec.js index 84a33dbf0b5..aeb136a76b9 100644 --- a/spec/frontend/jira_connect/subscriptions/store/mutations_spec.js +++ b/spec/frontend/jira_connect/subscriptions/store/mutations_spec.js @@ -25,4 +25,71 @@ describe('JiraConnect store mutations', () => { }); }); }); + + describe('SET_SUBSCRIPTIONS', () => { + it('sets subscriptions loading flag', () => { + const mockSubscriptions = [{ name: 'test' }]; + mutations.SET_SUBSCRIPTIONS(localState, mockSubscriptions); + + expect(localState.subscriptions).toBe(mockSubscriptions); + }); + }); + + describe('SET_SUBSCRIPTIONS_LOADING', () => { + it('sets subscriptions loading flag', () => { + mutations.SET_SUBSCRIPTIONS_LOADING(localState, true); + + expect(localState.subscriptionsLoading).toBe(true); + }); + }); + + describe('SET_SUBSCRIPTIONS_ERROR', () => { + it('sets subscriptions error', () => { + mutations.SET_SUBSCRIPTIONS_ERROR(localState, true); + + expect(localState.subscriptionsError).toBe(true); + }); + }); + + describe('ADD_SUBSCRIPTION_LOADING', () => { + it('sets addSubscriptionLoading', () => { + mutations.ADD_SUBSCRIPTION_LOADING(localState, true); + + expect(localState.addSubscriptionLoading).toBe(true); + }); + }); + + describe('ADD_SUBSCRIPTION_ERROR', () => { + it('sets addSubscriptionError', () => { + mutations.ADD_SUBSCRIPTION_ERROR(localState, true); + + expect(localState.addSubscriptionError).toBe(true); + }); + }); + + describe('SET_CURRENT_USER', () => { + it('sets currentUser', () => { + const mockUser = { name: 'root' }; + mutations.SET_CURRENT_USER(localState, mockUser); + + expect(localState.currentUser).toBe(mockUser); + }); + }); + + describe('SET_CURRENT_USER_ERROR', () => { + it('sets currentUserError', () => { + mutations.SET_CURRENT_USER_ERROR(localState, true); + + expect(localState.currentUserError).toBe(true); + }); + }); + + describe('SET_ACCESS_TOKEN', () => { + it('sets accessToken', () => { + const mockAccessToken = 'asdf1234'; + mutations.SET_ACCESS_TOKEN(localState, mockAccessToken); + + expect(localState.accessToken).toBe(mockAccessToken); + }); + }); }); |