actions_spec.js 12.2 KB
Newer Older
1
import actions from '~/ide/stores/actions';
P
Phil Hughes 已提交
2 3 4 5
import store from '~/ide/stores';
import service from '~/ide/services';
import router from '~/ide/ide_router';
import eventHub from '~/ide/eventhub';
S
Sam Bigelow 已提交
6
import consts from '~/ide/stores/modules/commit/constants';
7
import { commitActionTypes } from '~/ide/constants';
P
Phil Hughes 已提交
8 9 10 11 12 13 14 15 16 17 18 19
import { resetStore, file } from 'spec/ide/helpers';

describe('IDE commit module actions', () => {
  beforeEach(() => {
    spyOn(router, 'push');
  });

  afterEach(() => {
    resetStore(store);
  });

  describe('updateCommitMessage', () => {
P
Phil Hughes 已提交
20 21 22
    it('updates store with new commit message', done => {
      store
        .dispatch('commit/updateCommitMessage', 'testing')
P
Phil Hughes 已提交
23 24 25 26 27 28 29 30 31
        .then(() => {
          expect(store.state.commit.commitMessage).toBe('testing');
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('discardDraft', () => {
P
Phil Hughes 已提交
32
    it('resets commit message to blank', done => {
P
Phil Hughes 已提交
33 34
      store.state.commit.commitMessage = 'testing';

P
Phil Hughes 已提交
35 36
      store
        .dispatch('commit/discardDraft')
P
Phil Hughes 已提交
37 38 39 40 41 42 43 44 45
        .then(() => {
          expect(store.state.commit.commitMessage).not.toBe('testing');
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('updateCommitAction', () => {
P
Phil Hughes 已提交
46 47 48
    it('updates store with new commit action', done => {
      store
        .dispatch('commit/updateCommitAction', '1')
P
Phil Hughes 已提交
49 50 51 52 53 54 55 56 57
        .then(() => {
          expect(store.state.commit.commitAction).toBe('1');
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('updateBranchName', () => {
P
Phil Hughes 已提交
58 59 60
    it('updates store with new branch name', done => {
      store
        .dispatch('commit/updateBranchName', 'branch-name')
P
Phil Hughes 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
        .then(() => {
          expect(store.state.commit.newBranchName).toBe('branch-name');
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('setLastCommitMessage', () => {
    beforeEach(() => {
      Object.assign(store.state, {
        currentProjectId: 'abcproject',
        projects: {
          abcproject: {
            web_url: 'http://testing',
          },
        },
      });
    });

P
Phil Hughes 已提交
81 82 83
    it('updates commit message with short_id', done => {
      store
        .dispatch('commit/setLastCommitMessage', { short_id: '123' })
P
Phil Hughes 已提交
84 85 86 87 88 89 90 91 92
        .then(() => {
          expect(store.state.lastCommitMsg).toContain(
            'Your changes have been committed. Commit <a href="http://testing/commit/123" class="commit-sha">123</a>',
          );
        })
        .then(done)
        .catch(done.fail);
    });

P
Phil Hughes 已提交
93 94 95 96 97 98 99 100 101
    it('updates commit message with stats', done => {
      store
        .dispatch('commit/setLastCommitMessage', {
          short_id: '123',
          stats: {
            additions: '1',
            deletions: '2',
          },
        })
P
Phil Hughes 已提交
102
        .then(() => {
P
Phil Hughes 已提交
103 104 105
          expect(store.state.lastCommitMsg).toBe(
            'Your changes have been committed. Commit <a href="http://testing/commit/123" class="commit-sha">123</a> with 1 additions, 2 deletions.',
          );
P
Phil Hughes 已提交
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
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('updateFilesAfterCommit', () => {
    const data = {
      id: '123',
      message: 'testing commit message',
      committed_date: '123',
      committer_name: 'root',
    };
    const branch = 'master';
    let f;

    beforeEach(() => {
      spyOn(eventHub, '$emit');

      f = file('changedFile');
      Object.assign(f, {
        active: true,
        changed: true,
        content: 'file content',
      });

      store.state.currentProjectId = 'abcproject';
      store.state.currentBranchId = 'master';
      store.state.projects.abcproject = {
        web_url: 'web_url',
        branches: {
          master: {
            workingReference: '',
          },
        },
      };
P
Phil Hughes 已提交
142
      store.state.stagedFiles.push(f, {
P
Phil Hughes 已提交
143 144 145
        ...file('changedFile2'),
        changed: true,
      });
P
Phil Hughes 已提交
146
      store.state.openFiles = store.state.stagedFiles;
P
Phil Hughes 已提交
147

P
Phil Hughes 已提交
148 149
      store.state.stagedFiles.forEach(stagedFile => {
        store.state.entries[stagedFile.path] = stagedFile;
P
Phil Hughes 已提交
150 151 152
      });
    });

P
Phil Hughes 已提交
153 154 155 156 157 158
    it('updates stores working reference', done => {
      store
        .dispatch('commit/updateFilesAfterCommit', {
          data,
          branch,
        })
P
Phil Hughes 已提交
159
        .then(() => {
160
          expect(store.state.projects.abcproject.branches.master.workingReference).toBe(data.id);
P
Phil Hughes 已提交
161 162 163 164 165
        })
        .then(done)
        .catch(done.fail);
    });

P
Phil Hughes 已提交
166 167 168 169 170 171
    it('resets all files changed status', done => {
      store
        .dispatch('commit/updateFilesAfterCommit', {
          data,
          branch,
        })
P
Phil Hughes 已提交
172
        .then(() => {
P
Phil Hughes 已提交
173
          store.state.openFiles.forEach(entry => {
P
Phil Hughes 已提交
174 175 176 177 178 179 180
            expect(entry.changed).toBeFalsy();
          });
        })
        .then(done)
        .catch(done.fail);
    });

P
Phil Hughes 已提交
181 182 183 184 185 186
    it('sets files commit data', done => {
      store
        .dispatch('commit/updateFilesAfterCommit', {
          data,
          branch,
        })
P
Phil Hughes 已提交
187
        .then(() => {
188
          expect(f.lastCommitSha).toBe(data.id);
P
Phil Hughes 已提交
189 190 191 192 193
        })
        .then(done)
        .catch(done.fail);
    });

P
Phil Hughes 已提交
194 195 196 197 198 199
    it('updates raw content for changed file', done => {
      store
        .dispatch('commit/updateFilesAfterCommit', {
          data,
          branch,
        })
P
Phil Hughes 已提交
200 201 202 203 204 205 206
        .then(() => {
          expect(f.raw).toBe(f.content);
        })
        .then(done)
        .catch(done.fail);
    });

P
Phil Hughes 已提交
207 208 209 210 211 212
    it('emits changed event for file', done => {
      store
        .dispatch('commit/updateFilesAfterCommit', {
          data,
          branch,
        })
P
Phil Hughes 已提交
213
        .then(() => {
214 215 216 217
          expect(eventHub.$emit).toHaveBeenCalledWith(`editor.update.model.content.${f.key}`, {
            content: f.content,
            changed: false,
          });
P
Phil Hughes 已提交
218 219 220 221 222 223 224
        })
        .then(done)
        .catch(done.fail);
    });
  });

  describe('commitChanges', () => {
225 226
    let visitUrl;

P
Phil Hughes 已提交
227
    beforeEach(() => {
228
      visitUrl = spyOnDependency(actions, 'visitUrl');
P
Phil Hughes 已提交
229 230 231 232 233 234 235 236 237 238 239 240 241

      document.body.innerHTML += '<div class="flash-container"></div>';

      store.state.currentProjectId = 'abcproject';
      store.state.currentBranchId = 'master';
      store.state.projects.abcproject = {
        web_url: 'webUrl',
        branches: {
          master: {
            workingReference: '1',
          },
        },
      };
P
Phil Hughes 已提交
242 243 244 245 246

      const f = {
        ...file('changed'),
        type: 'blob',
        active: true,
247
        lastCommitSha: '123456789',
P
Phil Hughes 已提交
248 249 250 251 252 253 254
      };
      store.state.stagedFiles.push(f);
      store.state.changedFiles = [
        {
          ...f,
        },
      ];
P
Phil Hughes 已提交
255 256
      store.state.openFiles = store.state.changedFiles;

P
Phil Hughes 已提交
257 258
      store.state.openFiles.forEach(localF => {
        store.state.entries[localF.path] = localF;
P
Phil Hughes 已提交
259 260 261 262 263 264 265 266 267 268 269
      });

      store.state.commit.commitAction = '2';
      store.state.commit.commitMessage = 'testing 123';
    });

    afterEach(() => {
      document.querySelector('.flash-container').remove();
    });

    describe('success', () => {
270 271 272 273 274 275 276 277 278 279 280
      const COMMIT_RESPONSE = {
        id: '123456',
        short_id: '123',
        message: 'test message',
        committed_date: 'date',
        stats: {
          additions: '1',
          deletions: '2',
        },
      };

P
Phil Hughes 已提交
281
      beforeEach(() => {
P
Phil Hughes 已提交
282 283
        spyOn(service, 'commit').and.returnValue(
          Promise.resolve({
284
            data: COMMIT_RESPONSE,
P
Phil Hughes 已提交
285 286
          }),
        );
P
Phil Hughes 已提交
287 288
      });

P
Phil Hughes 已提交
289 290 291
      it('calls service', done => {
        store
          .dispatch('commit/commitChanges')
P
Phil Hughes 已提交
292 293 294 295
          .then(() => {
            expect(service.commit).toHaveBeenCalledWith('abcproject', {
              branch: jasmine.anything(),
              commit_message: 'testing 123',
P
Phil Hughes 已提交
296 297
              actions: [
                {
298
                  action: commitActionTypes.update,
P
Phil Hughes 已提交
299
                  file_path: jasmine.anything(),
P
Phil Hughes 已提交
300
                  content: undefined,
P
Phil Hughes 已提交
301
                  encoding: jasmine.anything(),
302
                  last_commit_id: undefined,
P
Phil Hughes 已提交
303
                  previous_path: undefined,
P
Phil Hughes 已提交
304 305
                },
              ],
P
Phil Hughes 已提交
306 307 308
              start_branch: 'master',
            });

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
            done();
          })
          .catch(done.fail);
      });

      it('sends lastCommit ID when not creating new branch', done => {
        store.state.commit.commitAction = '1';

        store
          .dispatch('commit/commitChanges')
          .then(() => {
            expect(service.commit).toHaveBeenCalledWith('abcproject', {
              branch: jasmine.anything(),
              commit_message: 'testing 123',
              actions: [
                {
325
                  action: commitActionTypes.update,
326
                  file_path: jasmine.anything(),
P
Phil Hughes 已提交
327
                  content: undefined,
328 329
                  encoding: jasmine.anything(),
                  last_commit_id: '123456789',
P
Phil Hughes 已提交
330
                  previous_path: undefined,
331 332 333 334 335
                },
              ],
              start_branch: undefined,
            });

P
Phil Hughes 已提交
336
            done();
P
Phil Hughes 已提交
337 338
          })
          .catch(done.fail);
P
Phil Hughes 已提交
339 340
      });

P
Phil Hughes 已提交
341 342 343
      it('sets last Commit Msg', done => {
        store
          .dispatch('commit/commitChanges')
P
Phil Hughes 已提交
344 345 346 347 348 349
          .then(() => {
            expect(store.state.lastCommitMsg).toBe(
              'Your changes have been committed. Commit <a href="webUrl/commit/123" class="commit-sha">123</a> with 1 additions, 2 deletions.',
            );

            done();
P
Phil Hughes 已提交
350 351
          })
          .catch(done.fail);
P
Phil Hughes 已提交
352 353
      });

P
Phil Hughes 已提交
354
      it('adds commit data to files', done => {
P
Phil Hughes 已提交
355 356
        store
          .dispatch('commit/commitChanges')
P
Phil Hughes 已提交
357
          .then(() => {
358 359
            expect(store.state.entries[store.state.openFiles[0].path].lastCommitSha).toBe(
              COMMIT_RESPONSE.id,
360
            );
P
Phil Hughes 已提交
361 362

            done();
P
Phil Hughes 已提交
363 364
          })
          .catch(done.fail);
P
Phil Hughes 已提交
365 366
      });

P
Phil Hughes 已提交
367 368
      it('resets stores commit actions', done => {
        store.state.commit.commitAction = consts.COMMIT_TO_NEW_BRANCH;
P
Phil Hughes 已提交
369

P
Phil Hughes 已提交
370 371
        store
          .dispatch('commit/commitChanges')
P
Phil Hughes 已提交
372
          .then(() => {
P
Phil Hughes 已提交
373
            expect(store.state.commit.commitAction).not.toBe(consts.COMMIT_TO_NEW_BRANCH);
P
Phil Hughes 已提交
374
          })
P
Phil Hughes 已提交
375
          .then(done)
P
Phil Hughes 已提交
376
          .catch(done.fail);
P
Phil Hughes 已提交
377
      });
P
Phil Hughes 已提交
378 379 380 381 382 383 384 385 386 387

      it('removes all staged files', done => {
        store
          .dispatch('commit/commitChanges')
          .then(() => {
            expect(store.state.stagedFiles.length).toBe(0);
          })
          .then(done)
          .catch(done.fail);
      });
388

389 390 391
      describe('merge request', () => {
        it('redirects to new merge request page', done => {
          spyOn(eventHub, '$on');
P
Phil Hughes 已提交
392

S
Sam Bigelow 已提交
393 394
          store.state.commit.commitAction = consts.COMMIT_TO_NEW_BRANCH;
          store.state.commit.shouldCreateMR = true;
P
Phil Hughes 已提交
395

396 397 398
          store
            .dispatch('commit/commitChanges')
            .then(() => {
399
              expect(visitUrl).toHaveBeenCalledWith(
400
                `webUrl/merge_requests/new?merge_request[source_branch]=${
401
                  store.getters['commit/placeholderBranchName']
402 403
                }&merge_request[target_branch]=master`,
              );
P
Phil Hughes 已提交
404

405 406 407
              done();
            })
            .catch(done.fail);
S
Sam Bigelow 已提交
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
        });

        it('does not redirect to new merge request page when shouldCreateMR is not checked', done => {
          spyOn(eventHub, '$on');

          store.state.commit.commitAction = consts.COMMIT_TO_NEW_BRANCH;
          store.state.commit.shouldCreateMR = false;

          store
            .dispatch('commit/commitChanges')
            .then(() => {
              expect(visitUrl).not.toHaveBeenCalled();
              done();
            })
            .catch(done.fail);
423 424 425 426 427 428 429 430 431 432
        });

        it('resets changed files before redirecting', done => {
          spyOn(eventHub, '$on');

          store.state.commit.commitAction = '3';

          store
            .dispatch('commit/commitChanges')
            .then(() => {
433
              expect(store.state.stagedFiles.length).toBe(0);
434 435 436 437 438

              done();
            })
            .catch(done.fail);
        });
P
Phil Hughes 已提交
439 440 441 442 443
      });
    });

    describe('failed', () => {
      beforeEach(() => {
P
Phil Hughes 已提交
444 445 446 447 448 449 450
        spyOn(service, 'commit').and.returnValue(
          Promise.resolve({
            data: {
              message: 'failed message',
            },
          }),
        );
P
Phil Hughes 已提交
451 452
      });

P
Phil Hughes 已提交
453 454 455
      it('shows failed message', done => {
        store
          .dispatch('commit/commitChanges')
P
Phil Hughes 已提交
456 457 458
          .then(() => {
            const alert = document.querySelector('.flash-container');

P
Phil Hughes 已提交
459
            expect(alert.textContent.trim()).toBe('failed message');
P
Phil Hughes 已提交
460 461

            done();
P
Phil Hughes 已提交
462 463
          })
          .catch(done.fail);
P
Phil Hughes 已提交
464 465 466 467
      });
    });
  });
});