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

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

  describe('redirectToUrl', () => {
P
Phil Hughes 已提交
12
    it('calls visitUrl', (done) => {
P
Phil Hughes 已提交
13 14
      spyOn(gl.utils, 'visitUrl');

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

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

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

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

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

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

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

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

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

  describe('toggleEditMode', () => {
P
Phil Hughes 已提交
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
    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 已提交
135

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

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

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

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

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

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

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

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

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

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

    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 已提交
191
      store.dispatch('checkCommitStatus')
P
Phil Hughes 已提交
192 193 194 195 196 197 198 199 200 201 202 203 204
        .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 已提交
205
      store.dispatch('checkCommitStatus')
P
Phil Hughes 已提交
206 207 208 209 210 211 212 213 214 215
        .then((val) => {
          expect(val).toBeFalsy();

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

  describe('commitChanges', () => {
P
Phil Hughes 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
    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 已提交
232

P
Phil Hughes 已提交
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 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
    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 已提交
362 363 364 365
  });

  describe('createTempEntry', () => {
    it('creates a temp tree', (done) => {
P
Phil Hughes 已提交
366 367 368 369 370 371 372 373 374 375 376 377
      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 已提交
378 379 380
    });

    it('creates temp file', (done) => {
P
Phil Hughes 已提交
381 382 383 384 385 386 387 388 389 390 391 392
      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 已提交
393 394 395 396 397 398 399 400 401 402 403 404 405
    });
  });

  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 已提交
406 407 408 409
      store.dispatch('scrollToTab')
        .then(() => {
          setTimeout(() => {
            expect(el.focus).toHaveBeenCalled();
P
Phil Hughes 已提交
410

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

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