file_spec.js 10.2 KB
Newer Older
P
Phil Hughes 已提交
1 2
import mutations from '~/ide/stores/mutations/file';
import state from '~/ide/stores/state';
P
Phil Hughes 已提交
3 4
import { file } from '../../helpers';

5
describe('IDE store file mutations', () => {
P
Phil Hughes 已提交
6 7 8 9 10
  let localState;
  let localFile;

  beforeEach(() => {
    localState = state();
P
Phil Hughes 已提交
11 12 13 14
    localFile = {
      ...file(),
      type: 'blob',
    };
P
Phil Hughes 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27

    localState.entries[localFile.path] = localFile;
  });

  describe('SET_FILE_ACTIVE', () => {
    it('sets the file active', () => {
      mutations.SET_FILE_ACTIVE(localState, {
        path: localFile.path,
        active: true,
      });

      expect(localFile.active).toBeTruthy();
    });
P
Phil Hughes 已提交
28 29 30 31 32

    it('sets pending tab as not active', () => {
      localState.openFiles.push({
        ...localFile,
        pending: true,
P
Phil Hughes 已提交
33
        active: true,
P
Phil Hughes 已提交
34 35 36 37 38 39 40 41 42
      });

      mutations.SET_FILE_ACTIVE(localState, {
        path: localFile.path,
        active: true,
      });

      expect(localState.openFiles[0].active).toBe(false);
    });
P
Phil Hughes 已提交
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
  });

  describe('TOGGLE_FILE_OPEN', () => {
    beforeEach(() => {
      mutations.TOGGLE_FILE_OPEN(localState, localFile.path);
    });

    it('adds into opened files', () => {
      expect(localFile.opened).toBeTruthy();
      expect(localState.openFiles.length).toBe(1);
    });

    it('removes from opened files', () => {
      mutations.TOGGLE_FILE_OPEN(localState, localFile.path);

      expect(localFile.opened).toBeFalsy();
      expect(localState.openFiles.length).toBe(0);
    });
  });

  describe('SET_FILE_DATA', () => {
    it('sets extra file data', () => {
      mutations.SET_FILE_DATA(localState, {
        data: {
          blame_path: 'blame',
          commits_path: 'commits',
          permalink: 'permalink',
          raw_path: 'raw',
          binary: true,
          render_error: 'render_error',
        },
        file: localFile,
      });

      expect(localFile.blamePath).toBe('blame');
      expect(localFile.commitsPath).toBe('commits');
      expect(localFile.permalink).toBe('permalink');
      expect(localFile.rawPath).toBe('raw');
      expect(localFile.binary).toBeTruthy();
      expect(localFile.renderError).toBe('render_error');
83 84
      expect(localFile.raw).toBeNull();
      expect(localFile.baseRaw).toBeNull();
P
Phil Hughes 已提交
85 86 87 88 89 90 91 92 93 94 95 96
    });
  });

  describe('SET_FILE_RAW_DATA', () => {
    it('sets raw data', () => {
      mutations.SET_FILE_RAW_DATA(localState, {
        file: localFile,
        raw: 'testing',
      });

      expect(localFile.raw).toBe('testing');
    });
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

    it('adds raw data to open pending file', () => {
      localState.openFiles.push({
        ...localFile,
        pending: true,
      });

      mutations.SET_FILE_RAW_DATA(localState, {
        file: localFile,
        raw: 'testing',
      });

      expect(localState.openFiles[0].raw).toBe('testing');
    });

    it('does not add raw data to open pending tempFile file', () => {
      localState.openFiles.push({
        ...localFile,
        pending: true,
        tempFile: true,
      });

      mutations.SET_FILE_RAW_DATA(localState, {
        file: localFile,
        raw: 'testing',
      });

      expect(localState.openFiles[0].raw).not.toBe('testing');
    });
P
Phil Hughes 已提交
126 127
  });

128 129 130 131 132 133 134 135 136 137 138
  describe('SET_FILE_BASE_RAW_DATA', () => {
    it('sets raw data from base branch', () => {
      mutations.SET_FILE_BASE_RAW_DATA(localState, {
        file: localFile,
        baseRaw: 'testing',
      });

      expect(localFile.baseRaw).toBe('testing');
    });
  });

P
Phil Hughes 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
  describe('UPDATE_FILE_CONTENT', () => {
    beforeEach(() => {
      localFile.raw = 'test';
    });

    it('sets content', () => {
      mutations.UPDATE_FILE_CONTENT(localState, {
        path: localFile.path,
        content: 'test',
      });

      expect(localFile.content).toBe('test');
    });

    it('sets changed if content does not match raw', () => {
      mutations.UPDATE_FILE_CONTENT(localState, {
        path: localFile.path,
        content: 'testing',
      });

      expect(localFile.content).toBe('testing');
      expect(localFile.changed).toBeTruthy();
    });

    it('sets changed if file is a temp file', () => {
      localFile.tempFile = true;

      mutations.UPDATE_FILE_CONTENT(localState, {
        path: localFile.path,
        content: '',
      });

      expect(localFile.changed).toBeTruthy();
    });
  });

T
Tim Zallmann 已提交
175
  describe('SET_FILE_MERGE_REQUEST_CHANGE', () => {
176
    it('sets file mr change', () => {
T
Tim Zallmann 已提交
177
      mutations.SET_FILE_MERGE_REQUEST_CHANGE(localState, {
178 179 180 181 182 183
        file: localFile,
        mrChange: { diff: 'ABC' },
      });

      expect(localFile.mrChange.diff).toBe('ABC');
    });
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230

    it('has diffMode replaced by default', () => {
      mutations.SET_FILE_MERGE_REQUEST_CHANGE(localState, {
        file: localFile,
        mrChange: {
          diff: 'ABC',
        },
      });

      expect(localFile.mrChange.diffMode).toBe('replaced');
    });

    it('has diffMode new', () => {
      mutations.SET_FILE_MERGE_REQUEST_CHANGE(localState, {
        file: localFile,
        mrChange: {
          diff: 'ABC',
          new_file: true,
        },
      });

      expect(localFile.mrChange.diffMode).toBe('new');
    });

    it('has diffMode deleted', () => {
      mutations.SET_FILE_MERGE_REQUEST_CHANGE(localState, {
        file: localFile,
        mrChange: {
          diff: 'ABC',
          deleted_file: true,
        },
      });

      expect(localFile.mrChange.diffMode).toBe('deleted');
    });

    it('has diffMode renamed', () => {
      mutations.SET_FILE_MERGE_REQUEST_CHANGE(localState, {
        file: localFile,
        mrChange: {
          diff: 'ABC',
          renamed_file: true,
        },
      });

      expect(localFile.mrChange.diffMode).toBe('renamed');
    });
231 232
  });

P
Phil Hughes 已提交
233 234 235 236
  describe('DISCARD_FILE_CHANGES', () => {
    beforeEach(() => {
      localFile.content = 'test';
      localFile.changed = true;
237 238 239 240 241
      localState.currentProjectId = 'gitlab-ce';
      localState.currentBranchId = 'master';
      localState.trees['gitlab-ce/master'] = {
        tree: [],
      };
P
Phil Hughes 已提交
242 243 244 245 246 247 248 249
    });

    it('resets content and changed', () => {
      mutations.DISCARD_FILE_CHANGES(localState, localFile.path);

      expect(localFile.content).toBe('');
      expect(localFile.changed).toBeFalsy();
    });
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

    it('adds to root tree if deleted', () => {
      localFile.deleted = true;

      mutations.DISCARD_FILE_CHANGES(localState, localFile.path);

      expect(localState.trees['gitlab-ce/master'].tree).toEqual([
        {
          ...localFile,
          deleted: false,
        },
      ]);
    });

    it('adds to parent tree if deleted', () => {
      localFile.deleted = true;
      localFile.parentPath = 'parentPath';
      localState.entries.parentPath = {
        tree: [],
      };

      mutations.DISCARD_FILE_CHANGES(localState, localFile.path);

      expect(localState.entries.parentPath.tree).toEqual([
        {
          ...localFile,
          deleted: false,
        },
      ]);
    });
P
Phil Hughes 已提交
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
  });

  describe('ADD_FILE_TO_CHANGED', () => {
    it('adds file into changed files array', () => {
      mutations.ADD_FILE_TO_CHANGED(localState, localFile.path);

      expect(localState.changedFiles.length).toBe(1);
    });
  });

  describe('REMOVE_FILE_FROM_CHANGED', () => {
    it('removes files from changed files array', () => {
      localState.changedFiles.push(localFile);

      mutations.REMOVE_FILE_FROM_CHANGED(localState, localFile.path);

      expect(localState.changedFiles.length).toBe(0);
    });
  });

P
Phil Hughes 已提交
300 301
  describe('STAGE_CHANGE', () => {
    it('adds file into stagedFiles array', () => {
P
Phil Hughes 已提交
302
      mutations.STAGE_CHANGE(localState, localFile.path);
P
Phil Hughes 已提交
303 304

      expect(localState.stagedFiles.length).toBe(1);
P
Phil Hughes 已提交
305
      expect(localState.stagedFiles[0]).toEqual(localFile);
P
Phil Hughes 已提交
306 307 308
    });

    it('updates stagedFile if it is already staged', () => {
P
Phil Hughes 已提交
309
      mutations.STAGE_CHANGE(localState, localFile.path);
P
Phil Hughes 已提交
310

P
Phil Hughes 已提交
311
      localFile.raw = 'testing 123';
P
Phil Hughes 已提交
312

P
Phil Hughes 已提交
313
      mutations.STAGE_CHANGE(localState, localFile.path);
P
Phil Hughes 已提交
314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

      expect(localState.stagedFiles.length).toBe(1);
      expect(localState.stagedFiles[0].raw).toEqual('testing 123');
    });
  });

  describe('UNSTAGE_CHANGE', () => {
    let f;

    beforeEach(() => {
      f = {
        ...file(),
        type: 'blob',
        staged: true,
      };

      localState.stagedFiles.push(f);
      localState.changedFiles.push(f);
P
Phil Hughes 已提交
332
      localState.entries[f.path] = f;
P
Phil Hughes 已提交
333 334 335
    });

    it('removes from stagedFiles array', () => {
P
Phil Hughes 已提交
336
      mutations.UNSTAGE_CHANGE(localState, f.path);
P
Phil Hughes 已提交
337 338

      expect(localState.stagedFiles.length).toBe(0);
P
Phil Hughes 已提交
339
      expect(localState.changedFiles.length).toBe(1);
P
Phil Hughes 已提交
340 341 342
    });
  });

P
Phil Hughes 已提交
343 344 345 346 347 348 349 350 351 352
  describe('TOGGLE_FILE_CHANGED', () => {
    it('updates file changed status', () => {
      mutations.TOGGLE_FILE_CHANGED(localState, {
        file: localFile,
        changed: true,
      });

      expect(localFile.changed).toBeTruthy();
    });
  });
P
Phil Hughes 已提交
353

T
Tim Zallmann 已提交
354 355 356 357 358 359 360 361 362 363 364
  describe('SET_FILE_VIEWMODE', () => {
    it('updates file view mode', () => {
      mutations.SET_FILE_VIEWMODE(localState, {
        file: localFile,
        viewMode: 'preview',
      });

      expect(localFile.viewMode).toBe('preview');
    });
  });

P
Phil Hughes 已提交
365 366 367 368 369 370 371 372 373 374 375 376 377 378
  describe('ADD_PENDING_TAB', () => {
    beforeEach(() => {
      const f = {
        ...file('openFile'),
        path: 'openFile',
        active: true,
        opened: true,
      };

      localState.entries[f.path] = f;
      localState.openFiles.push(f);
    });

    it('adds file into openFiles as pending', () => {
379
      mutations.ADD_PENDING_TAB(localState, { file: localFile });
P
Phil Hughes 已提交
380 381

      expect(localState.openFiles.length).toBe(1);
P
Phil Hughes 已提交
382 383
      expect(localState.openFiles[0].pending).toBe(true);
      expect(localState.openFiles[0].key).toBe(`pending-${localFile.key}`);
P
Phil Hughes 已提交
384 385
    });

P
Phil Hughes 已提交
386 387 388
    it('only allows 1 open pending file', () => {
      const newFile = file('test');
      localState.entries[newFile.path] = newFile;
P
Phil Hughes 已提交
389

390
      mutations.ADD_PENDING_TAB(localState, { file: localFile });
P
Phil Hughes 已提交
391

P
Phil Hughes 已提交
392
      expect(localState.openFiles.length).toBe(1);
P
Phil Hughes 已提交
393

P
Phil Hughes 已提交
394
      mutations.ADD_PENDING_TAB(localState, { file: file('test') });
P
Phil Hughes 已提交
395

P
Phil Hughes 已提交
396 397
      expect(localState.openFiles.length).toBe(1);
      expect(localState.openFiles[0].name).toBe('test');
P
Phil Hughes 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410
    });
  });

  describe('REMOVE_PENDING_TAB', () => {
    it('removes pending tab from openFiles', () => {
      localFile.key = 'testing';
      localState.openFiles.push(localFile);

      mutations.REMOVE_PENDING_TAB(localState, localFile);

      expect(localState.openFiles.length).toBe(0);
    });
  });
P
Phil Hughes 已提交
411
});