actions_spec.js 10.7 KB
Newer Older
P
Phil Hughes 已提交
1
import Vue from 'vue';
2
import * as urlUtils from '~/lib/utils/url_utility';
P
Phil Hughes 已提交
3
import store from '~/repo/stores';
P
Phil Hughes 已提交
4
import service from '~/repo/services';
P
Phil Hughes 已提交
5
import { resetStore, file } from '../helpers';
P
Phil Hughes 已提交
6 7

describe('Multi-file store actions', () => {
P
Phil Hughes 已提交
8 9
  afterEach(() => {
    resetStore(store);
P
Phil Hughes 已提交
10 11 12
  });

  describe('redirectToUrl', () => {
P
Phil Hughes 已提交
13
    it('calls visitUrl', (done) => {
14
      spyOn(urlUtils, 'visitUrl');
P
Phil Hughes 已提交
15

P
Phil Hughes 已提交
16 17
      store.dispatch('redirectToUrl', 'test')
        .then(() => {
18
          expect(urlUtils.visitUrl).toHaveBeenCalledWith('test');
P
Phil Hughes 已提交
19

P
Phil Hughes 已提交
20 21 22
          done();
        })
        .catch(done.fail);
P
Phil Hughes 已提交
23 24 25 26 27
    });
  });

  describe('setInitialData', () => {
    it('commits initial data', (done) => {
P
Phil Hughes 已提交
28 29 30 31 32 33
      store.dispatch('setInitialData', { canCommit: true })
        .then(() => {
          expect(store.state.canCommit).toBeTruthy();
          done();
        })
        .catch(done.fail);
P
Phil Hughes 已提交
34 35 36 37 38
    });
  });

  describe('closeDiscardPopup', () => {
    it('closes the discard popup', (done) => {
P
Phil Hughes 已提交
39 40 41 42 43 44 45
      store.dispatch('closeDiscardPopup', false)
        .then(() => {
          expect(store.state.discardPopupOpen).toBeFalsy();

          done();
        })
        .catch(done.fail);
P
Phil Hughes 已提交
46 47 48 49 50
    });
  });

  describe('discardAllChanges', () => {
    beforeEach(() => {
P
Phil Hughes 已提交
51 52
      store.state.openFiles.push(file());
      store.state.openFiles[0].changed = true;
P
Phil Hughes 已提交
53 54 55 56 57
    });
  });

  describe('closeAllFiles', () => {
    beforeEach(() => {
P
Phil Hughes 已提交
58 59
      store.state.openFiles.push(file());
      store.state.openFiles[0].opened = true;
P
Phil Hughes 已提交
60 61 62
    });

    it('closes all open files', (done) => {
P
Phil Hughes 已提交
63 64 65 66 67 68 69
      store.dispatch('closeAllFiles')
        .then(() => {
          expect(store.state.openFiles.length).toBe(0);

          done();
        })
        .catch(done.fail);
P
Phil Hughes 已提交
70 71 72 73
    });
  });

  describe('toggleEditMode', () => {
P
Phil Hughes 已提交
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
    it('toggles edit mode', (done) => {
      store.state.editMode = true;

      store.dispatch('toggleEditMode')
        .then(() => {
          expect(store.state.editMode).toBeFalsy();

          done();
        }).catch(done.fail);
    });

    it('sets preview mode', (done) => {
      store.state.currentBlobView = 'repo-editor';
      store.state.editMode = true;

      store.dispatch('toggleEditMode')
        .then(Vue.nextTick)
        .then(() => {
          expect(store.state.currentBlobView).toBe('repo-preview');

          done();
        }).catch(done.fail);
    });

    it('opens discard popup if there are changed files', (done) => {
      store.state.editMode = true;
      store.state.openFiles.push(file());
      store.state.openFiles[0].changed = true;

      store.dispatch('toggleEditMode')
        .then(() => {
          expect(store.state.discardPopupOpen).toBeTruthy();

          done();
        }).catch(done.fail);
    });

    it('can force closed if there are changed files', (done) => {
      store.state.editMode = true;
      store.state.openFiles.push(file());
      store.state.openFiles[0].changed = true;

      store.dispatch('toggleEditMode', true)
        .then(() => {
          expect(store.state.discardPopupOpen).toBeFalsy();
          expect(store.state.editMode).toBeFalsy();

          done();
        }).catch(done.fail);
    });

    it('discards file changes', (done) => {
      const f = file();
      store.state.editMode = true;
      store.state.tree.push(f);
      store.state.openFiles.push(f);
      f.changed = true;

      store.dispatch('toggleEditMode', true)
        .then(Vue.nextTick)
        .then(() => {
          expect(f.changed).toBeFalsy();
P
Phil Hughes 已提交
136

P
Phil Hughes 已提交
137 138 139
          done();
        }).catch(done.fail);
    });
P
Phil Hughes 已提交
140 141 142 143
  });

  describe('toggleBlobView', () => {
    it('sets edit mode view if in edit mode', (done) => {
P
Phil Hughes 已提交
144 145 146 147 148 149 150 151 152
      store.state.editMode = true;

      store.dispatch('toggleBlobView')
        .then(() => {
          expect(store.state.currentBlobView).toBe('repo-editor');

          done();
        })
        .catch(done.fail);
P
Phil Hughes 已提交
153 154 155
    });

    it('sets preview mode view if not in edit mode', (done) => {
P
Phil Hughes 已提交
156 157 158 159 160 161 162
      store.dispatch('toggleBlobView')
      .then(() => {
        expect(store.state.currentBlobView).toBe('repo-preview');

        done();
      })
      .catch(done.fail);
P
Phil Hughes 已提交
163 164 165 166 167
    });
  });

  describe('checkCommitStatus', () => {
    beforeEach(() => {
P
Phil Hughes 已提交
168 169 170
      store.state.project.id = 2;
      store.state.currentBranch = 'master';
      store.state.currentRef = '1';
P
Phil Hughes 已提交
171 172
    });

P
Phil Hughes 已提交
173
    it('calls service', (done) => {
P
Phil Hughes 已提交
174 175 176 177
      spyOn(service, 'getBranchData').and.returnValue(Promise.resolve({
        commit: { id: '123' },
      }));

P
Phil Hughes 已提交
178 179 180
      store.dispatch('checkCommitStatus')
        .then(() => {
          expect(service.getBranchData).toHaveBeenCalledWith(2, 'master');
P
Phil Hughes 已提交
181

P
Phil Hughes 已提交
182 183 184
          done();
        })
        .catch(done.fail);
P
Phil Hughes 已提交
185 186 187 188 189 190 191
    });

    it('returns true if current ref does not equal returned ID', (done) => {
      spyOn(service, 'getBranchData').and.returnValue(Promise.resolve({
        commit: { id: '123' },
      }));

P
Phil Hughes 已提交
192
      store.dispatch('checkCommitStatus')
P
Phil Hughes 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205
        .then((val) => {
          expect(val).toBeTruthy();

          done();
        })
        .catch(done.fail);
    });

    it('returns false if current ref equals returned ID', (done) => {
      spyOn(service, 'getBranchData').and.returnValue(Promise.resolve({
        commit: { id: '1' },
      }));

P
Phil Hughes 已提交
206
      store.dispatch('checkCommitStatus')
P
Phil Hughes 已提交
207 208 209 210 211 212 213 214 215 216
        .then((val) => {
          expect(val).toBeFalsy();

          done();
        })
        .catch(done.fail);
    });
  });

  describe('commitChanges', () => {
P
Phil Hughes 已提交
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
    let payload;

    beforeEach(() => {
      spyOn(window, 'scrollTo');

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

      store.state.project.id = 123;
      payload = {
        branch: 'master',
      };
    });

    afterEach(() => {
      document.querySelector('.flash-container').remove();
    });
P
Phil Hughes 已提交
233

P
Phil Hughes 已提交
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 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
    describe('success', () => {
      beforeEach(() => {
        spyOn(service, 'commit').and.returnValue(Promise.resolve({
          id: '123456',
          short_id: '123',
          message: 'test message',
          committed_date: 'date',
          stats: {
            additions: '1',
            deletions: '2',
          },
        }));
      });

      it('calls service', (done) => {
        store.dispatch('commitChanges', { payload, newMr: false })
          .then(() => {
            expect(service.commit).toHaveBeenCalledWith(123, payload);

            done();
          }).catch(done.fail);
      });

      it('shows flash notice', (done) => {
        store.dispatch('commitChanges', { payload, newMr: false })
          .then(() => {
            const alert = document.querySelector('.flash-container');

            expect(alert.querySelector('.flash-notice')).not.toBeNull();
            expect(alert.textContent.trim()).toBe(
              'Your changes have been committed. Commit 123 with 1 additions, 2 deletions.',
            );

            done();
          }).catch(done.fail);
      });

      it('adds commit data to changed files', (done) => {
        const changedFile = file();
        const f = file();
        changedFile.changed = true;

        store.state.openFiles.push(changedFile, f);

        store.dispatch('commitChanges', { payload, newMr: false })
          .then(() => {
            expect(changedFile.lastCommit.message).toBe('test message');
            expect(f.lastCommit.message).not.toBe('test message');

            done();
          }).catch(done.fail);
      });

      it('toggles edit mode', (done) => {
        store.state.editMode = true;

        store.dispatch('commitChanges', { payload, newMr: false })
          .then(() => {
            expect(store.state.editMode).toBeFalsy();

            done();
          }).catch(done.fail);
      });

      it('closes all files', (done) => {
        store.state.openFiles.push(file());
        store.state.openFiles[0].opened = true;

        store.dispatch('commitChanges', { payload, newMr: false })
          .then(Vue.nextTick)
          .then(() => {
            expect(store.state.openFiles.length).toBe(0);

            done();
          }).catch(done.fail);
      });

      it('scrolls to top of page', (done) => {
        store.dispatch('commitChanges', { payload, newMr: false })
          .then(() => {
            expect(window.scrollTo).toHaveBeenCalledWith(0, 0);

            done();
          }).catch(done.fail);
      });

      it('updates commit ref', (done) => {
        store.dispatch('commitChanges', { payload, newMr: false })
          .then(() => {
            expect(store.state.currentRef).toBe('123456');

            done();
          }).catch(done.fail);
      });

      it('redirects to new merge request page', (done) => {
        spyOn(gl.utils, 'visitUrl');

        store.state.endpoints.newMergeRequestUrl = 'newMergeRequestUrl?branch=';

        store.dispatch('commitChanges', { payload, newMr: true })
          .then(() => {
            expect(gl.utils.visitUrl).toHaveBeenCalledWith('newMergeRequestUrl?branch=master');

            done();
          }).catch(done.fail);
      });
    });

    describe('failed', () => {
      beforeEach(() => {
        spyOn(service, 'commit').and.returnValue(Promise.resolve({
          message: 'failed message',
        }));
      });

      it('shows failed message', (done) => {
        store.dispatch('commitChanges', { payload, newMr: false })
          .then(() => {
            const alert = document.querySelector('.flash-container');

            expect(alert.textContent.trim()).toBe(
              'failed message',
            );

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

  describe('createTempEntry', () => {
    it('creates a temp tree', (done) => {
P
Phil Hughes 已提交
367 368 369 370 371 372 373 374 375 376 377 378
      store.dispatch('createTempEntry', {
        name: 'test',
        type: 'tree',
      })
      .then(() => {
        expect(store.state.tree.length).toBe(1);
        expect(store.state.tree[0].tempFile).toBeTruthy();
        expect(store.state.tree[0].type).toBe('tree');

        done();
      })
      .catch(done.fail);
P
Phil Hughes 已提交
379 380 381
    });

    it('creates temp file', (done) => {
P
Phil Hughes 已提交
382 383 384 385 386 387 388 389 390 391 392 393
      store.dispatch('createTempEntry', {
        name: 'test',
        type: 'blob',
      })
      .then(() => {
        expect(store.state.tree.length).toBe(1);
        expect(store.state.tree[0].tempFile).toBeTruthy();
        expect(store.state.tree[0].type).toBe('blob');

        done();
      })
      .catch(done.fail);
P
Phil Hughes 已提交
394 395 396 397 398 399 400 401 402 403 404 405 406
    });
  });

  describe('popHistoryState', () => {

  });

  describe('scrollToTab', () => {
    it('focuses the current active element', (done) => {
      document.body.innerHTML += '<div id="tabs"><div class="active"><div class="repo-tab"></div></div></div>';
      const el = document.querySelector('.repo-tab');
      spyOn(el, 'focus');

P
Phil Hughes 已提交
407 408 409 410
      store.dispatch('scrollToTab')
        .then(() => {
          setTimeout(() => {
            expect(el.focus).toHaveBeenCalled();
P
Phil Hughes 已提交
411

P
Phil Hughes 已提交
412
            document.getElementById('tabs').remove();
P
Phil Hughes 已提交
413

P
Phil Hughes 已提交
414 415 416 417
            done();
          });
        })
        .catch(done.fail);
P
Phil Hughes 已提交
418 419 420
    });
  });
});