actions_spec.js 10.9 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
      spyOn(service, 'getBranchData').and.returnValue(Promise.resolve({
181 182 183
        data: {
          commit: { id: '123' },
        },
P
Phil Hughes 已提交
184 185
      }));

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

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

    it('returns true if current ref does not equal returned ID', (done) => {
      spyOn(service, 'getBranchData').and.returnValue(Promise.resolve({
197 198 199
        data: {
          commit: { id: '123' },
        },
P
Phil Hughes 已提交
200 201
      }));

P
Phil Hughes 已提交
202
      store.dispatch('checkCommitStatus')
P
Phil Hughes 已提交
203 204 205 206 207 208 209 210 211 212
        .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({
213 214 215
        data: {
          commit: { id: '1' },
        },
P
Phil Hughes 已提交
216 217
      }));

P
Phil Hughes 已提交
218
      store.dispatch('checkCommitStatus')
P
Phil Hughes 已提交
219 220 221 222 223 224 225 226 227 228
        .then((val) => {
          expect(val).toBeFalsy();

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

  describe('commitChanges', () => {
P
Phil Hughes 已提交
229 230 231 232 233 234 235
    let payload;

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

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

236 237 238 239 240 241 242 243 244 245 246
      store.state.currentProjectId = 'abcproject';
      store.state.currentBranchId = 'master';
      store.state.projects.abcproject = {
        web_url: 'webUrl',
        branches: {
          master: {
            workingReference: '1',
          },
        },
      };

P
Phil Hughes 已提交
247 248 249 250 251 252 253 254
      payload = {
        branch: 'master',
      };
    });

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

P
Phil Hughes 已提交
256 257 258
    describe('success', () => {
      beforeEach(() => {
        spyOn(service, 'commit').and.returnValue(Promise.resolve({
P
Phil Hughes 已提交
259 260 261 262 263 264 265 266 267
          data: {
            id: '123456',
            short_id: '123',
            message: 'test message',
            committed_date: 'date',
            stats: {
              additions: '1',
              deletions: '2',
            },
P
Phil Hughes 已提交
268 269 270 271 272 273 274
          },
        }));
      });

      it('calls service', (done) => {
        store.dispatch('commitChanges', { payload, newMr: false })
          .then(() => {
275
            expect(service.commit).toHaveBeenCalledWith('abcproject', payload);
P
Phil Hughes 已提交
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295

            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 已提交
296 297
        const changedFile = file('changed');
        const f = file('newfile');
P
Phil Hughes 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
        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 已提交
321
        spyOn(urlUtils, 'visitUrl');
P
Phil Hughes 已提交
322 323 324

        store.dispatch('commitChanges', { payload, newMr: true })
          .then(() => {
325
            expect(urlUtils.visitUrl).toHaveBeenCalledWith('webUrl/merge_requests/new?merge_request%5Bsource_branch%5D=master');
P
Phil Hughes 已提交
326 327 328 329 330 331 332 333 334

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

    describe('failed', () => {
      beforeEach(() => {
        spyOn(service, 'commit').and.returnValue(Promise.resolve({
P
Phil Hughes 已提交
335 336 337
          data: {
            message: 'failed message',
          },
P
Phil Hughes 已提交
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
        }));
      });

      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 已提交
354 355 356
  });

  describe('createTempEntry', () => {
357 358 359 360 361 362 363 364 365
    beforeEach(() => {
      store.state.trees['abcproject/mybranch'] = {
        tree: [],
      };
      store.state.projects.abcproject = {
        web_url: '',
      };
    });

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

P
Phil Hughes 已提交
369
      store.dispatch('createTempEntry', {
370 371 372
        projectId: 'abcproject',
        branchId: 'mybranch',
        parent: projectTree,
P
Phil Hughes 已提交
373 374 375 376
        name: 'test',
        type: 'tree',
      })
      .then(() => {
377 378 379 380
        const baseTree = projectTree.tree;
        expect(baseTree.length).toBe(1);
        expect(baseTree[0].tempFile).toBeTruthy();
        expect(baseTree[0].type).toBe('tree');
P
Phil Hughes 已提交
381 382 383 384

        done();
      })
      .catch(done.fail);
P
Phil Hughes 已提交
385 386 387
    });

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

P
Phil Hughes 已提交
390
      store.dispatch('createTempEntry', {
391 392 393
        projectId: 'abcproject',
        branchId: 'mybranch',
        parent: projectTree,
P
Phil Hughes 已提交
394 395 396 397
        name: 'test',
        type: 'blob',
      })
      .then(() => {
398 399 400 401
        const baseTree = projectTree.tree;
        expect(baseTree.length).toBe(1);
        expect(baseTree[0].tempFile).toBeTruthy();
        expect(baseTree[0].type).toBe('blob');
P
Phil Hughes 已提交
402 403 404 405

        done();
      })
      .catch(done.fail);
P
Phil Hughes 已提交
406 407 408 409 410 411 412 413 414 415 416 417 418
    });
  });

  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 已提交
419 420 421 422
      store.dispatch('scrollToTab')
        .then(() => {
          setTimeout(() => {
            expect(el.focus).toHaveBeenCalled();
P
Phil Hughes 已提交
423

P
Phil Hughes 已提交
424
            document.getElementById('tabs').remove();
P
Phil Hughes 已提交
425

P
Phil Hughes 已提交
426 427 428 429
            done();
          });
        })
        .catch(done.fail);
P
Phil Hughes 已提交
430 431 432
    });
  });
});