summaryrefslogtreecommitdiff
path: root/spec/javascripts/ide/stores/modules/commit/getters_spec.js
blob: 07445c229178dc06f95edff23c8eb9492cc5eeae (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import commitState from '~/ide/stores/modules/commit/state';
import * as getters from '~/ide/stores/modules/commit/getters';
import consts from '~/ide/stores/modules/commit/constants';

describe('IDE commit module getters', () => {
  let state;

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

  describe('discardDraftButtonDisabled', () => {
    it('returns true when commitMessage is empty', () => {
      expect(getters.discardDraftButtonDisabled(state)).toBeTruthy();
    });

    it('returns false when commitMessage is not empty & loading is false', () => {
      state.commitMessage = 'test';
      state.submitCommitLoading = false;

      expect(getters.discardDraftButtonDisabled(state)).toBeFalsy();
    });

    it('returns true when commitMessage is not empty & loading is true', () => {
      state.commitMessage = 'test';
      state.submitCommitLoading = true;

      expect(getters.discardDraftButtonDisabled(state)).toBeTruthy();
    });
  });

  describe('placeholderBranchName', () => {
    it('includes username, currentBranchId, patch & random number', () => {
      gon.current_username = 'username';

      const branch = getters.placeholderBranchName(state, null, {
        currentBranchId: 'testing',
      });

      expect(branch).toMatch(/username-testing-patch-\d{5}$/);
    });
  });

  describe('branchName', () => {
    const rootState = {
      currentBranchId: 'master',
    };
    const localGetters = {
      placeholderBranchName: 'placeholder-branch-name',
    };

    beforeEach(() => {
      Object.assign(state, {
        newBranchName: 'state-newBranchName',
      });
    });

    it('defaults to currentBranchId when not committing to a new branch', () => {
      localGetters.isCreatingNewBranch = false;

      expect(getters.branchName(state, localGetters, rootState)).toBe('master');
    });

    describe('commit to a new branch', () => {
      beforeEach(() => {
        localGetters.isCreatingNewBranch = true;
      });

      it('uses newBranchName when not empty', () => {
        const newBranchName = 'nonempty-branch-name';
        Object.assign(state, {
          newBranchName,
        });

        expect(getters.branchName(state, localGetters, rootState)).toBe(newBranchName);
      });

      it('uses placeholderBranchName when state newBranchName is empty', () => {
        Object.assign(state, {
          newBranchName: '',
        });

        expect(getters.branchName(state, localGetters, rootState)).toBe('placeholder-branch-name');
      });
    });
  });

  describe('preBuiltCommitMessage', () => {
    let rootState = {};

    beforeEach(() => {
      rootState.changedFiles = [];
      rootState.stagedFiles = [];
    });

    afterEach(() => {
      rootState = {};
    });

    it('returns commitMessage when set', () => {
      state.commitMessage = 'test commit message';

      expect(getters.preBuiltCommitMessage(state, null, rootState)).toBe('test commit message');
    });

    ['changedFiles', 'stagedFiles'].forEach(key => {
      it('returns commitMessage with updated file', () => {
        rootState[key].push({
          path: 'test-file',
        });

        expect(getters.preBuiltCommitMessage(state, null, rootState)).toBe('Update test-file');
      });

      it('returns commitMessage with updated files', () => {
        rootState[key].push(
          {
            path: 'test-file',
          },
          {
            path: 'index.js',
          },
        );

        expect(getters.preBuiltCommitMessage(state, null, rootState)).toBe(
          'Update test-file, index.js files',
        );
      });

      it('returns commitMessage with deleted files', () => {
        rootState[key].push(
          {
            path: 'test-file',
            deleted: true,
          },
          {
            path: 'index.js',
          },
        );

        expect(getters.preBuiltCommitMessage(state, null, rootState)).toBe(
          'Update index.js\nDeleted test-file',
        );
      });
    });
  });

  describe('isCreatingNewBranch', () => {
    it('returns false if NOT creating a new branch', () => {
      state.commitAction = consts.COMMIT_TO_CURRENT_BRANCH;

      expect(getters.isCreatingNewBranch(state)).toBeFalsy();
    });

    it('returns true if creating a new branch', () => {
      state.commitAction = consts.COMMIT_TO_NEW_BRANCH;

      expect(getters.isCreatingNewBranch(state)).toBeTruthy();
    });
  });

  describe('shouldHideNewMrOption', () => {
    let localGetters = {};
    let rootGetters = {};

    beforeEach(() => {
      localGetters = {
        isCreatingNewBranch: null,
      };
      rootGetters = {
        isOnDefaultBranch: null,
        hasMergeRequest: null,
        canPushToBranch: null,
      };
    });

    describe('NO existing MR for the branch', () => {
      beforeEach(() => {
        rootGetters.hasMergeRequest = false;
      });

      it('should never hide "New MR" option', () => {
        expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy();
      });
    });

    describe('existing MR for the branch', () => {
      beforeEach(() => {
        rootGetters.hasMergeRequest = true;
      });

      it('should NOT hide "New MR" option if user can NOT push to the current branch', () => {
        rootGetters.canPushToBranch = false;

        expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy();
      });

      it('should hide "New MR" option if user can push to the current branch', () => {
        rootGetters.canPushToBranch = true;

        expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeTruthy();
      });
    });

    describe('user can NOT push the branch', () => {
      beforeEach(() => {
        rootGetters.canPushToBranch = false;
      });

      it('should never hide "New MR" option', () => {
        expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy();
      });
    });

    describe('user can push to the branch', () => {
      beforeEach(() => {
        rootGetters.canPushToBranch = true;
      });

      it('should NOT hide "New MR" option if there is NO existing MR for the current branch', () => {
        rootGetters.hasMergeRequest = false;

        expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy();
      });

      it('should hide "New MR" option if there is existing MR for the current branch', () => {
        rootGetters.hasMergeRequest = true;

        expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeTruthy();
      });
    });

    describe('default branch', () => {
      beforeEach(() => {
        rootGetters.isOnDefaultBranch = true;
      });

      describe('committing to the same branch', () => {
        beforeEach(() => {
          localGetters.isCreatingNewBranch = false;
          rootGetters.canPushToBranch = true;
        });

        it('should hide "New MR" when there is an existing MR', () => {
          rootGetters.hasMergeRequest = true;

          expect(
            getters.shouldHideNewMrOption(state, localGetters, null, rootGetters),
          ).toBeTruthy();
        });

        it('should hide "New MR" when there is no existing MR', () => {
          rootGetters.hasMergeRequest = false;

          expect(
            getters.shouldHideNewMrOption(state, localGetters, null, rootGetters),
          ).toBeTruthy();
        });
      });

      describe('creating a new branch', () => {
        beforeEach(() => {
          localGetters.isCreatingNewBranch = true;
        });

        it('should NOT hide "New MR" option no matter existence of an MR or write access', () => {
          rootGetters.hasMergeRequest = false;
          rootGetters.canPushToBranch = true;

          expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy();

          rootGetters.hasMergeRequest = true;
          rootGetters.canPushToBranch = true;

          expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy();

          rootGetters.hasMergeRequest = false;
          rootGetters.canPushToBranch = false;

          expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy();
        });
      });
    });

    it('should never hide "New MR" option when creating a new branch', () => {
      localGetters.isCreatingNewBranch = true;

      rootGetters.isOnDefaultBranch = false;
      rootGetters.hasMergeRequest = true;
      rootGetters.canPushToBranch = true;

      expect(getters.shouldHideNewMrOption(state, localGetters, null, rootGetters)).toBeFalsy();
    });
  });
});