actions_spec.js 10.8 KB
Newer Older
P
Phil Hughes 已提交
1
import Vue from 'vue';
2
import * as urlUtils from '~/lib/utils/url_utility';
3 4
import store from '~/ide/stores';
import service from '~/ide/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(() => {
T
Tim Zallmann 已提交
51
      store.state.openFiles.push(file('discardAll'));
P
Phil Hughes 已提交
52
      store.state.openFiles[0].changed = true;
P
Phil Hughes 已提交
53 54 55 56 57
    });
  });

  describe('closeAllFiles', () => {
    beforeEach(() => {
T
Tim Zallmann 已提交
58
      store.state.openFiles.push(file('closeAll'));
P
Phil Hughes 已提交
59
      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
    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;
T
Tim Zallmann 已提交
100
      store.state.openFiles.push(file('discardChanges'));
P
Phil Hughes 已提交
101 102 103 104 105 106 107 108 109 110 111 112
      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;
113

T
Tim Zallmann 已提交
114
      store.state.openFiles.push(file('forceClose'));
P
Phil Hughes 已提交
115 116 117 118 119 120 121 122 123 124 125 126
      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) => {
T
Tim Zallmann 已提交
127
      const f = file('discard');
P
Phil Hughes 已提交
128 129 130 131 132 133 134 135
      store.state.editMode = true;
      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
      store.dispatch('toggleBlobView')
        .then(() => {
          expect(store.state.currentBlobView).toBe('repo-editor');

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

    it('sets preview mode view if not in edit mode', (done) => {
154 155
      store.state.editMode = false;

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(() => {
168 169 170 171 172 173 174 175 176
      store.state.currentProjectId = 'abcproject';
      store.state.currentBranchId = 'master';
      store.state.projects.abcproject = {
        branches: {
          master: {
            workingReference: '1',
          },
        },
      };
P
Phil Hughes 已提交
177 178
    });

P
Phil Hughes 已提交
179
    it('calls service', (done) => {
P
Phil Hughes 已提交
180 181 182 183
      spyOn(service, 'getBranchData').and.returnValue(Promise.resolve({
        commit: { id: '123' },
      }));

P
Phil Hughes 已提交
184 185
      store.dispatch('checkCommitStatus')
        .then(() => {
186
          expect(service.getBranchData).toHaveBeenCalledWith('abcproject', 'master');
P
Phil Hughes 已提交
187

P
Phil Hughes 已提交
188 189 190
          done();
        })
        .catch(done.fail);
P
Phil Hughes 已提交
191 192 193 194 195 196 197
    });

    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 已提交
198
      store.dispatch('checkCommitStatus')
P
Phil Hughes 已提交
199 200 201 202 203 204 205 206 207 208 209 210 211
        .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 已提交
212
      store.dispatch('checkCommitStatus')
P
Phil Hughes 已提交
213 214 215 216 217 218 219 220 221 222
        .then((val) => {
          expect(val).toBeFalsy();

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

  describe('commitChanges', () => {
P
Phil Hughes 已提交
223 224 225 226 227 228 229
    let payload;

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

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

230 231 232 233 234 235 236 237 238 239 240
      store.state.currentProjectId = 'abcproject';
      store.state.currentBranchId = 'master';
      store.state.projects.abcproject = {
        web_url: 'webUrl',
        branches: {
          master: {
            workingReference: '1',
          },
        },
      };

P
Phil Hughes 已提交
241 242 243 244 245 246 247 248
      payload = {
        branch: 'master',
      };
    });

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

P
Phil Hughes 已提交
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266
    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(() => {
267
            expect(service.commit).toHaveBeenCalledWith('abcproject', payload);
P
Phil Hughes 已提交
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287

            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) => {
T
Tim Zallmann 已提交
288 289
        const changedFile = file('changed');
        const f = file('newfile');
P
Phil Hughes 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312
        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('scrolls to top of page', (done) => {
        store.dispatch('commitChanges', { payload, newMr: false })
          .then(() => {
            expect(window.scrollTo).toHaveBeenCalledWith(0, 0);

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

      it('redirects to new merge request page', (done) => {
P
Phil Hughes 已提交
313
        spyOn(urlUtils, 'visitUrl');
P
Phil Hughes 已提交
314 315 316

        store.dispatch('commitChanges', { payload, newMr: true })
          .then(() => {
317
            expect(urlUtils.visitUrl).toHaveBeenCalledWith('webUrl/merge_requests/new?merge_request%5Bsource_branch%5D=master');
P
Phil Hughes 已提交
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

            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 已提交
344 345 346
  });

  describe('createTempEntry', () => {
347 348 349 350 351 352 353 354 355
    beforeEach(() => {
      store.state.trees['abcproject/mybranch'] = {
        tree: [],
      };
      store.state.projects.abcproject = {
        web_url: '',
      };
    });

P
Phil Hughes 已提交
356
    it('creates a temp tree', (done) => {
357 358
      const projectTree = store.state.trees['abcproject/mybranch'];

P
Phil Hughes 已提交
359
      store.dispatch('createTempEntry', {
360 361 362
        projectId: 'abcproject',
        branchId: 'mybranch',
        parent: projectTree,
P
Phil Hughes 已提交
363 364 365 366
        name: 'test',
        type: 'tree',
      })
      .then(() => {
367 368 369 370
        const baseTree = projectTree.tree;
        expect(baseTree.length).toBe(1);
        expect(baseTree[0].tempFile).toBeTruthy();
        expect(baseTree[0].type).toBe('tree');
P
Phil Hughes 已提交
371 372 373 374

        done();
      })
      .catch(done.fail);
P
Phil Hughes 已提交
375 376 377
    });

    it('creates temp file', (done) => {
378 379
      const projectTree = store.state.trees['abcproject/mybranch'];

P
Phil Hughes 已提交
380
      store.dispatch('createTempEntry', {
381 382 383
        projectId: 'abcproject',
        branchId: 'mybranch',
        parent: projectTree,
P
Phil Hughes 已提交
384 385 386 387
        name: 'test',
        type: 'blob',
      })
      .then(() => {
388 389 390 391
        const baseTree = projectTree.tree;
        expect(baseTree.length).toBe(1);
        expect(baseTree[0].tempFile).toBeTruthy();
        expect(baseTree[0].type).toBe('blob');
P
Phil Hughes 已提交
392 393 394 395

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

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

P
Phil Hughes 已提交
414
            document.getElementById('tabs').remove();
P
Phil Hughes 已提交
415

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