summaryrefslogtreecommitdiff
path: root/spec/frontend/members/store/mutations_spec.js
blob: 7ad7034eb6dfe2ca6593792a1ac9faa7fa236435 (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
import { members, group } from 'jest/members/mock_data';
import * as types from '~/members/store/mutation_types';
import mutations from '~/members/store/mutations';

describe('Vuex members mutations', () => {
  describe('update member mutations', () => {
    let state;

    beforeEach(() => {
      state = {
        members,
        showError: false,
        errorMessage: '',
      };
    });

    describe(types.RECEIVE_MEMBER_ROLE_SUCCESS, () => {
      it('updates member', () => {
        const accessLevel = { integerValue: 30, stringValue: 'Developer' };

        mutations[types.RECEIVE_MEMBER_ROLE_SUCCESS](state, {
          memberId: members[0].id,
          accessLevel,
        });

        expect(state.members[0].accessLevel).toEqual(accessLevel);
      });
    });

    describe(types.RECEIVE_MEMBER_ROLE_ERROR, () => {
      describe('when error does not have a message', () => {
        it('shows default error message', () => {
          mutations[types.RECEIVE_MEMBER_ROLE_ERROR](state, {
            error: new Error('Network Error'),
          });

          expect(state.showError).toBe(true);
          expect(state.errorMessage).toBe(
            "An error occurred while updating the member's role, please try again.",
          );
        });
      });

      describe('when error has a message', () => {
        it('shows error message', () => {
          const error = new Error('Request failed with status code 422');
          const message =
            'User email "john.smith@gmail.com" does not match the allowed domain of example.com';

          error.response = {
            data: { message },
          };
          mutations[types.RECEIVE_MEMBER_ROLE_ERROR](state, { error });

          expect(state.showError).toBe(true);
          expect(state.errorMessage).toBe(message);
        });
      });
    });

    describe(types.RECEIVE_MEMBER_EXPIRATION_SUCCESS, () => {
      it('updates member', () => {
        const expiresAt = '2020-03-17T00:00:00Z';

        mutations[types.RECEIVE_MEMBER_EXPIRATION_SUCCESS](state, {
          memberId: members[0].id,
          expiresAt,
        });

        expect(state.members[0].expiresAt).toEqual(expiresAt);
      });
    });

    describe(types.RECEIVE_MEMBER_EXPIRATION_ERROR, () => {
      describe('when error does not have a message', () => {
        it('shows default error message', () => {
          mutations[types.RECEIVE_MEMBER_EXPIRATION_ERROR](state, {
            error: new Error('Network Error'),
          });

          expect(state.showError).toBe(true);
          expect(state.errorMessage).toBe(
            "An error occurred while updating the member's expiration date, please try again.",
          );
        });
      });

      describe('when error has a message', () => {
        it('shows error message', () => {
          const error = new Error('Request failed with status code 422');
          const message =
            'User email "john.smith@gmail.com" does not match the allowed domain of example.com';

          error.response = {
            data: { message },
          };
          mutations[types.RECEIVE_MEMBER_EXPIRATION_ERROR](state, { error });

          expect(state.showError).toBe(true);
          expect(state.errorMessage).toBe(message);
        });
      });
    });
  });

  describe(types.HIDE_ERROR, () => {
    it('sets `showError` to `false`', () => {
      const state = {
        showError: true,
        errorMessage: 'foo bar',
      };

      mutations[types.HIDE_ERROR](state);

      expect(state.showError).toBe(false);
    });

    it('sets `errorMessage` to an empty string', () => {
      const state = {
        showError: true,
        errorMessage: 'foo bar',
      };

      mutations[types.HIDE_ERROR](state);

      expect(state.errorMessage).toBe('');
    });
  });

  describe(types.SHOW_REMOVE_GROUP_LINK_MODAL, () => {
    it('sets `removeGroupLinkModalVisible` and `groupLinkToRemove`', () => {
      const state = {
        removeGroupLinkModalVisible: false,
        groupLinkToRemove: null,
      };

      mutations[types.SHOW_REMOVE_GROUP_LINK_MODAL](state, group);

      expect(state).toEqual({
        removeGroupLinkModalVisible: true,
        groupLinkToRemove: group,
      });
    });
  });

  describe(types.HIDE_REMOVE_GROUP_LINK_MODAL, () => {
    it('sets `removeGroupLinkModalVisible` to `false`', () => {
      const state = {
        removeGroupLinkModalVisible: false,
      };

      mutations[types.HIDE_REMOVE_GROUP_LINK_MODAL](state);

      expect(state.removeGroupLinkModalVisible).toBe(false);
    });
  });
});