summaryrefslogtreecommitdiff
path: root/spec/frontend/vuex_shared/modules/members/actions_spec.js
blob: 833bd4cc175669acfaa132b2743d598f5c6e1216 (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
import { noop } from 'lodash';
import axios from 'axios';
import MockAdapter from 'axios-mock-adapter';
import { members, group } from 'jest/vue_shared/components/members/mock_data';
import testAction from 'helpers/vuex_action_helper';
import httpStatusCodes from '~/lib/utils/http_status';
import * as types from '~/vuex_shared/modules/members/mutation_types';
import {
  updateMemberRole,
  showRemoveGroupLinkModal,
  hideRemoveGroupLinkModal,
} from '~/vuex_shared/modules/members/actions';

describe('Vuex members actions', () => {
  let mock;

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

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

  describe('updateMemberRole', () => {
    const memberId = members[0].id;
    const accessLevel = { integerValue: 30, stringValue: 'Developer' };

    const payload = {
      memberId,
      accessLevel,
    };
    const state = {
      members,
      memberPath: '/groups/foo-bar/-/group_members/:id',
      requestFormatter: noop,
      removeGroupLinkModalVisible: false,
      groupLinkToRemove: null,
    };

    describe('successful request', () => {
      it(`commits ${types.RECEIVE_MEMBER_ROLE_SUCCESS} mutation`, async () => {
        let requestPath;
        mock.onPut().replyOnce(config => {
          requestPath = config.url;
          return [httpStatusCodes.OK, {}];
        });

        await testAction(updateMemberRole, payload, state, [
          {
            type: types.RECEIVE_MEMBER_ROLE_SUCCESS,
            payload,
          },
        ]);

        expect(requestPath).toBe('/groups/foo-bar/-/group_members/238');
      });
    });

    describe('unsuccessful request', () => {
      beforeEach(() => {
        mock.onPut().replyOnce(httpStatusCodes.BAD_REQUEST, { message: 'Bad request' });
      });

      it(`commits ${types.RECEIVE_MEMBER_ROLE_ERROR} mutation`, async () => {
        try {
          await testAction(updateMemberRole, payload, state, [
            {
              type: types.RECEIVE_MEMBER_ROLE_SUCCESS,
            },
          ]);
        } catch {
          // Do nothing
        }
      });

      it('throws error', async () => {
        await expect(testAction(updateMemberRole, payload, state)).rejects.toThrowError();
      });
    });
  });

  describe('Group Link Modal', () => {
    const state = {
      removeGroupLinkModalVisible: false,
      groupLinkToRemove: null,
    };

    describe('showRemoveGroupLinkModal', () => {
      it(`commits ${types.SHOW_REMOVE_GROUP_LINK_MODAL} mutation`, () => {
        testAction(showRemoveGroupLinkModal, group, state, [
          {
            type: types.SHOW_REMOVE_GROUP_LINK_MODAL,
            payload: group,
          },
        ]);
      });
    });

    describe('hideRemoveGroupLinkModal', () => {
      it(`commits ${types.HIDE_REMOVE_GROUP_LINK_MODAL} mutation`, () => {
        testAction(hideRemoveGroupLinkModal, group, state, [
          {
            type: types.HIDE_REMOVE_GROUP_LINK_MODAL,
          },
        ]);
      });
    });
  });
});