utils_spec.js 21.3 KB
Newer Older
Felipe Artur's avatar
Felipe Artur committed
1 2 3 4 5
import * as utils from '~/diffs/store/utils';
import {
  LINE_POSITION_LEFT,
  LINE_POSITION_RIGHT,
  TEXT_DIFF_POSITION_TYPE,
6
  LEGACY_DIFF_NOTE_TYPE,
Felipe Artur's avatar
Felipe Artur committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20
  DIFF_NOTE_TYPE,
  NEW_LINE_TYPE,
  OLD_LINE_TYPE,
  MATCH_LINE_TYPE,
  PARALLEL_DIFF_VIEW_TYPE,
} from '~/diffs/constants';
import { MERGE_REQUEST_NOTEABLE_TYPE } from '~/notes/constants';
import diffFileMockData from '../mock_data/diff_file';
import { noteableDataMock } from '../../notes/mock_data';

const getDiffFileMock = () => Object.assign({}, diffFileMockData);

describe('DiffsStoreUtils', () => {
  describe('findDiffFile', () => {
21
    const files = [{ file_hash: 1, name: 'one' }];
Felipe Artur's avatar
Felipe Artur committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43

    it('should return correct file', () => {
      expect(utils.findDiffFile(files, 1).name).toEqual('one');
      expect(utils.findDiffFile(files, 2)).toBeUndefined();
    });
  });

  describe('getReversePosition', () => {
    it('should return correct line position name', () => {
      expect(utils.getReversePosition(LINE_POSITION_RIGHT)).toEqual(LINE_POSITION_LEFT);
      expect(utils.getReversePosition(LINE_POSITION_LEFT)).toEqual(LINE_POSITION_RIGHT);
    });
  });

  describe('findIndexInInlineLines and findIndexInParallelLines', () => {
    const expectSet = (method, lines, invalidLines) => {
      expect(method(lines, { oldLineNumber: 3, newLineNumber: 5 })).toEqual(4);
      expect(method(invalidLines || lines, { oldLineNumber: 32, newLineNumber: 53 })).toEqual(-1);
    };

    describe('findIndexInInlineLines', () => {
      it('should return correct index for given line numbers', () => {
44
        expectSet(utils.findIndexInInlineLines, getDiffFileMock().highlighted_diff_lines);
Felipe Artur's avatar
Felipe Artur committed
45 46 47 48 49
      });
    });

    describe('findIndexInParallelLines', () => {
      it('should return correct index for given line numbers', () => {
50
        expectSet(utils.findIndexInParallelLines, getDiffFileMock().parallel_diff_lines, {});
Felipe Artur's avatar
Felipe Artur committed
51 52 53 54 55 56 57 58
      });
    });
  });

  describe('removeMatchLine', () => {
    it('should remove match line properly by regarding the bottom parameter', () => {
      const diffFile = getDiffFileMock();
      const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 };
59 60 61 62 63 64 65 66 67 68
      const inlineIndex = utils.findIndexInInlineLines(
        diffFile.highlighted_diff_lines,
        lineNumbers,
      );
      const parallelIndex = utils.findIndexInParallelLines(
        diffFile.parallel_diff_lines,
        lineNumbers,
      );
      const atInlineIndex = diffFile.highlighted_diff_lines[inlineIndex];
      const atParallelIndex = diffFile.parallel_diff_lines[parallelIndex];
Felipe Artur's avatar
Felipe Artur committed
69 70

      utils.removeMatchLine(diffFile, lineNumbers, false);
71

72 73
      expect(diffFile.highlighted_diff_lines[inlineIndex]).not.toEqual(atInlineIndex);
      expect(diffFile.parallel_diff_lines[parallelIndex]).not.toEqual(atParallelIndex);
Felipe Artur's avatar
Felipe Artur committed
74 75

      utils.removeMatchLine(diffFile, lineNumbers, true);
76

77 78
      expect(diffFile.highlighted_diff_lines[inlineIndex + 1]).not.toEqual(atInlineIndex);
      expect(diffFile.parallel_diff_lines[parallelIndex + 1]).not.toEqual(atParallelIndex);
Felipe Artur's avatar
Felipe Artur committed
79 80 81 82 83 84
    });
  });

  describe('addContextLines', () => {
    it('should add context lines properly with bottom parameter', () => {
      const diffFile = getDiffFileMock();
85 86
      const inlineLines = diffFile.highlighted_diff_lines;
      const parallelLines = diffFile.parallel_diff_lines;
Felipe Artur's avatar
Felipe Artur committed
87 88 89
      const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 };
      const contextLines = [{ lineNumber: 42 }];
      const options = { inlineLines, parallelLines, contextLines, lineNumbers, bottom: true };
90 91
      const inlineIndex = utils.findIndexInInlineLines(inlineLines, lineNumbers);
      const parallelIndex = utils.findIndexInParallelLines(parallelLines, lineNumbers);
Felipe Artur's avatar
Felipe Artur committed
92 93 94 95 96 97
      const normalizedParallelLine = {
        left: options.contextLines[0],
        right: options.contextLines[0],
      };

      utils.addContextLines(options);
98

Felipe Artur's avatar
Felipe Artur committed
99 100 101 102 103
      expect(inlineLines[inlineLines.length - 1]).toEqual(contextLines[0]);
      expect(parallelLines[parallelLines.length - 1]).toEqual(normalizedParallelLine);

      delete options.bottom;
      utils.addContextLines(options);
104

Felipe Artur's avatar
Felipe Artur committed
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
      expect(inlineLines[inlineIndex]).toEqual(contextLines[0]);
      expect(parallelLines[parallelIndex]).toEqual(normalizedParallelLine);
    });
  });

  describe('getNoteFormData', () => {
    it('should properly create note form data', () => {
      const diffFile = getDiffFileMock();
      noteableDataMock.targetType = MERGE_REQUEST_NOTEABLE_TYPE;

      const options = {
        note: 'Hello world!',
        noteableData: noteableDataMock,
        noteableType: MERGE_REQUEST_NOTEABLE_TYPE,
        diffFile,
        noteTargetLine: {
121 122 123 124
          line_code: '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_3',
          meta_data: null,
          new_line: 3,
          old_line: 1,
Felipe Artur's avatar
Felipe Artur committed
125 126 127 128 129 130
        },
        diffViewType: PARALLEL_DIFF_VIEW_TYPE,
        linePosition: LINE_POSITION_LEFT,
      };

      const position = JSON.stringify({
131 132 133 134 135
        base_sha: diffFile.diff_refs.base_sha,
        start_sha: diffFile.diff_refs.start_sha,
        head_sha: diffFile.diff_refs.head_sha,
        old_path: diffFile.old_path,
        new_path: diffFile.new_path,
Felipe Artur's avatar
Felipe Artur committed
136
        position_type: TEXT_DIFF_POSITION_TYPE,
137 138
        old_line: options.noteTargetLine.old_line,
        new_line: options.noteTargetLine.new_line,
Felipe Artur's avatar
Felipe Artur committed
139 140 141 142 143
      });

      const postData = {
        view: options.diffViewType,
        line_type: options.linePosition === LINE_POSITION_RIGHT ? NEW_LINE_TYPE : OLD_LINE_TYPE,
144
        merge_request_diff_head_sha: diffFile.diff_refs.head_sha,
Felipe Artur's avatar
Felipe Artur committed
145 146 147 148
        in_reply_to_discussion_id: '',
        note_project_id: '',
        target_type: options.noteableType,
        target_id: options.noteableData.id,
Phil Hughes's avatar
Phil Hughes committed
149
        return_discussion: true,
Felipe Artur's avatar
Felipe Artur committed
150 151 152
        note: {
          noteable_type: options.noteableType,
          noteable_id: options.noteableData.id,
153
          commit_id: undefined,
Felipe Artur's avatar
Felipe Artur committed
154
          type: DIFF_NOTE_TYPE,
155
          line_code: options.noteTargetLine.line_code,
Felipe Artur's avatar
Felipe Artur committed
156 157 158 159 160 161 162 163 164 165
          note: options.note,
          position,
        },
      };

      expect(utils.getNoteFormData(options)).toEqual({
        endpoint: options.noteableData.create_note_path,
        data: postData,
      });
    });
166 167 168

    it('should create legacy note form data', () => {
      const diffFile = getDiffFileMock();
169 170
      delete diffFile.diff_refs.start_sha;
      delete diffFile.diff_refs.head_sha;
171 172 173 174 175 176 177 178 179

      noteableDataMock.targetType = MERGE_REQUEST_NOTEABLE_TYPE;

      const options = {
        note: 'Hello world!',
        noteableData: noteableDataMock,
        noteableType: MERGE_REQUEST_NOTEABLE_TYPE,
        diffFile,
        noteTargetLine: {
180 181 182 183
          line_code: '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_3',
          meta_data: null,
          new_line: 3,
          old_line: 1,
184 185 186 187 188 189
        },
        diffViewType: PARALLEL_DIFF_VIEW_TYPE,
        linePosition: LINE_POSITION_LEFT,
      };

      const position = JSON.stringify({
190
        base_sha: diffFile.diff_refs.base_sha,
191
        start_sha: undefined,
Phil Hughes's avatar
Phil Hughes committed
192
        head_sha: undefined,
193 194
        old_path: diffFile.old_path,
        new_path: diffFile.new_path,
195
        position_type: TEXT_DIFF_POSITION_TYPE,
196 197
        old_line: options.noteTargetLine.old_line,
        new_line: options.noteTargetLine.new_line,
198 199 200 201 202
      });

      const postData = {
        view: options.diffViewType,
        line_type: options.linePosition === LINE_POSITION_RIGHT ? NEW_LINE_TYPE : OLD_LINE_TYPE,
Phil Hughes's avatar
Phil Hughes committed
203
        merge_request_diff_head_sha: undefined,
204 205 206 207
        in_reply_to_discussion_id: '',
        note_project_id: '',
        target_type: options.noteableType,
        target_id: options.noteableData.id,
Phil Hughes's avatar
Phil Hughes committed
208
        return_discussion: true,
209 210 211
        note: {
          noteable_type: options.noteableType,
          noteable_id: options.noteableData.id,
212
          commit_id: undefined,
213
          type: LEGACY_DIFF_NOTE_TYPE,
214
          line_code: options.noteTargetLine.line_code,
215 216 217 218 219 220 221 222 223 224
          note: options.note,
          position,
        },
      };

      expect(utils.getNoteFormData(options)).toEqual({
        endpoint: options.noteableData.create_note_path,
        data: postData,
      });
    });
Felipe Artur's avatar
Felipe Artur committed
225 226 227 228 229 230 231 232 233
  });

  describe('addLineReferences', () => {
    const lineNumbers = { oldLineNumber: 3, newLineNumber: 4 };

    it('should add correct line references when bottom set to true', () => {
      const lines = [{ type: null }, { type: MATCH_LINE_TYPE }];
      const linesWithReferences = utils.addLineReferences(lines, lineNumbers, true);

234 235 236 237
      expect(linesWithReferences[0].old_line).toEqual(lineNumbers.oldLineNumber + 1);
      expect(linesWithReferences[0].new_line).toEqual(lineNumbers.newLineNumber + 1);
      expect(linesWithReferences[1].meta_data.old_pos).toEqual(4);
      expect(linesWithReferences[1].meta_data.new_pos).toEqual(5);
Felipe Artur's avatar
Felipe Artur committed
238 239 240 241 242 243
    });

    it('should add correct line references when bottom falsy', () => {
      const lines = [{ type: null }, { type: MATCH_LINE_TYPE }, { type: null }];
      const linesWithReferences = utils.addLineReferences(lines, lineNumbers);

244 245 246 247
      expect(linesWithReferences[0].old_line).toEqual(0);
      expect(linesWithReferences[0].new_line).toEqual(1);
      expect(linesWithReferences[1].meta_data.old_pos).toEqual(2);
      expect(linesWithReferences[1].meta_data.new_pos).toEqual(3);
Felipe Artur's avatar
Felipe Artur committed
248 249
    });
  });
250 251 252

  describe('trimFirstCharOfLineContent', () => {
    it('trims the line when it starts with a space', () => {
253 254
      expect(utils.trimFirstCharOfLineContent({ rich_text: ' diff' })).toEqual({
        rich_text: 'diff',
Tim Zallmann's avatar
Tim Zallmann committed
255
      });
256 257 258
    });

    it('trims the line when it starts with a +', () => {
259 260
      expect(utils.trimFirstCharOfLineContent({ rich_text: '+diff' })).toEqual({
        rich_text: 'diff',
Tim Zallmann's avatar
Tim Zallmann committed
261
      });
262 263 264
    });

    it('trims the line when it starts with a -', () => {
265 266
      expect(utils.trimFirstCharOfLineContent({ rich_text: '-diff' })).toEqual({
        rich_text: 'diff',
Tim Zallmann's avatar
Tim Zallmann committed
267
      });
268 269 270
    });

    it('does not trims the line when it starts with a letter', () => {
271 272
      expect(utils.trimFirstCharOfLineContent({ rich_text: 'diff' })).toEqual({
        rich_text: 'diff',
Tim Zallmann's avatar
Tim Zallmann committed
273
      });
274 275 276 277
    });

    it('does not modify the provided object', () => {
      const lineObj = {
278
        rich_text: ' diff',
279 280 281
      };

      utils.trimFirstCharOfLineContent(lineObj);
282

283
      expect(lineObj).toEqual({ rich_text: ' diff' });
284 285 286
    });

    it('handles a undefined or null parameter', () => {
287
      expect(utils.trimFirstCharOfLineContent()).toEqual({});
288 289
    });
  });
290 291

  describe('prepareDiffData', () => {
292 293 294 295
    let preparedDiff;

    beforeEach(() => {
      preparedDiff = { diff_files: [getDiffFileMock()] };
296
      utils.prepareDiffData(preparedDiff);
297
    });
298

299
    it('sets the renderIt and collapsed attribute on files', () => {
300
      const firstParallelDiffLine = preparedDiff.diff_files[0].parallel_diff_lines[2];
301

302 303 304 305
      expect(firstParallelDiffLine.left.discussions.length).toBe(0);
      expect(firstParallelDiffLine.left).not.toHaveAttr('text');
      expect(firstParallelDiffLine.right.discussions.length).toBe(0);
      expect(firstParallelDiffLine.right).not.toHaveAttr('text');
306
      const firstParallelChar = firstParallelDiffLine.right.rich_text.charAt(0);
307

308 309 310 311
      expect(firstParallelChar).not.toBe(' ');
      expect(firstParallelChar).not.toBe('+');
      expect(firstParallelChar).not.toBe('-');

312
      const checkLine = preparedDiff.diff_files[0].highlighted_diff_lines[0];
313

314 315
      expect(checkLine.discussions.length).toBe(0);
      expect(checkLine).not.toHaveAttr('text');
316
      const firstChar = checkLine.rich_text.charAt(0);
317

318 319 320
      expect(firstChar).not.toBe(' ');
      expect(firstChar).not.toBe('+');
      expect(firstChar).not.toBe('-');
321

322 323
      expect(preparedDiff.diff_files[0].renderIt).toBeTruthy();
      expect(preparedDiff.diff_files[0].collapsed).toBeFalsy();
324
    });
325 326 327 328 329 330 331 332 333 334 335 336

    it('adds line_code to all lines', () => {
      expect(
        preparedDiff.diff_files[0].parallel_diff_lines.filter(line => !line.line_code),
      ).toHaveLength(0);
    });

    it('uses right line code if left has none', () => {
      const firstLine = preparedDiff.diff_files[0].parallel_diff_lines[0];

      expect(firstLine.line_code).toEqual(firstLine.right.line_code);
    });
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('isDiscussionApplicableToLine', () => {
    const diffPosition = {
      baseSha: 'ed13df29948c41ba367caa757ab3ec4892509910',
      headSha: 'b921914f9a834ac47e6fd9420f78db0f83559130',
      newLine: null,
      newPath: '500-lines-4.txt',
      oldLine: 5,
      oldPath: '500-lines-4.txt',
      startSha: 'ed13df29948c41ba367caa757ab3ec4892509910',
    };

    const wrongDiffPosition = {
      baseSha: 'wrong',
      headSha: 'wrong',
      newLine: null,
      newPath: '500-lines-4.txt',
      oldLine: 5,
      oldPath: '500-lines-4.txt',
      startSha: 'wrong',
    };

    const discussions = {
      upToDateDiscussion1: {
362 363
        original_position: diffPosition,
        position: wrongDiffPosition,
364 365
      },
      outDatedDiscussion1: {
366 367
        original_position: wrongDiffPosition,
        position: wrongDiffPosition,
368 369 370 371 372
      },
    };

    it('returns true when the discussion is up to date', () => {
      expect(
373 374 375 376 377
        utils.isDiscussionApplicableToLine({
          discussion: discussions.upToDateDiscussion1,
          diffPosition,
          latestDiff: true,
        }),
378 379 380 381 382
      ).toBe(true);
    });

    it('returns false when the discussion is not up to date', () => {
      expect(
383 384 385 386 387
        utils.isDiscussionApplicableToLine({
          discussion: discussions.outDatedDiscussion1,
          diffPosition,
          latestDiff: true,
        }),
388 389
      ).toBe(false);
    });
390

Phil Hughes's avatar
Phil Hughes committed
391 392 393 394 395 396
    it('returns true when line codes match and discussion does not contain position and is not active', () => {
      const discussion = { ...discussions.outDatedDiscussion1, line_code: 'ABC_1', active: false };
      delete discussion.original_position;
      delete discussion.position;

      expect(
397
        utils.isDiscussionApplicableToLine({
398
          discussion,
399
          diffPosition: {
400 401 402
            ...diffPosition,
            lineCode: 'ABC_1',
          },
403 404
          latestDiff: true,
        }),
Phil Hughes's avatar
Phil Hughes committed
405 406 407 408 409
      ).toBe(false);
    });

    it('returns true when line codes match and discussion does not contain position and is active', () => {
      const discussion = { ...discussions.outDatedDiscussion1, line_code: 'ABC_1', active: true };
410 411 412 413
      delete discussion.original_position;
      delete discussion.position;

      expect(
414
        utils.isDiscussionApplicableToLine({
415
          discussion,
416
          diffPosition: {
417
            ...diffPosition,
418
            line_code: 'ABC_1',
419
          },
420 421
          latestDiff: true,
        }),
422 423
      ).toBe(true);
    });
424 425 426 427 428 429 430

    it('returns false when not latest diff', () => {
      const discussion = { ...discussions.outDatedDiscussion1, line_code: 'ABC_1', active: true };
      delete discussion.original_position;
      delete discussion.position;

      expect(
431
        utils.isDiscussionApplicableToLine({
432
          discussion,
433
          diffPosition: {
434 435 436
            ...diffPosition,
            lineCode: 'ABC_1',
          },
437 438
          latestDiff: false,
        }),
439 440
      ).toBe(false);
    });
441
  });
442 443 444 445 446 447 448

  describe('generateTreeList', () => {
    let files;

    beforeAll(() => {
      files = [
        {
449 450 451 452 453 454
          new_path: 'app/index.js',
          deleted_file: false,
          new_file: false,
          removed_lines: 10,
          added_lines: 0,
          file_hash: 'test',
455 456
        },
        {
457 458 459 460 461 462
          new_path: 'app/test/index.js',
          deleted_file: false,
          new_file: true,
          removed_lines: 0,
          added_lines: 0,
          file_hash: 'test',
463
        },
464
        {
465 466 467 468 469 470
          new_path: 'app/test/filepathneedstruncating.js',
          deleted_file: false,
          new_file: true,
          removed_lines: 0,
          added_lines: 0,
          file_hash: 'test',
471
        },
472
        {
473 474 475 476 477 478
          new_path: 'package.json',
          deleted_file: true,
          new_file: false,
          removed_lines: 0,
          added_lines: 0,
          file_hash: 'test',
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
        },
      ];
    });

    it('creates a tree of files', () => {
      const { tree } = utils.generateTreeList(files);

      expect(tree).toEqual([
        {
          key: 'app',
          path: 'app',
          name: 'app',
          type: 'tree',
          tree: [
            {
              addedLines: 0,
              changed: true,
              deleted: false,
              fileHash: 'test',
              key: 'app/index.js',
              name: 'index.js',
500
              parentPath: 'app/',
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520
              path: 'app/index.js',
              removedLines: 10,
              tempFile: false,
              type: 'blob',
              tree: [],
            },
            {
              key: 'app/test',
              path: 'app/test',
              name: 'test',
              type: 'tree',
              opened: true,
              tree: [
                {
                  addedLines: 0,
                  changed: true,
                  deleted: false,
                  fileHash: 'test',
                  key: 'app/test/index.js',
                  name: 'index.js',
521
                  parentPath: 'app/test/',
522
                  path: 'app/test/index.js',
523 524 525 526 527 528 529 530 531 532 533 534
                  removedLines: 0,
                  tempFile: true,
                  type: 'blob',
                  tree: [],
                },
                {
                  addedLines: 0,
                  changed: true,
                  deleted: false,
                  fileHash: 'test',
                  key: 'app/test/filepathneedstruncating.js',
                  name: 'filepathneedstruncating.js',
535
                  parentPath: 'app/test/',
536
                  path: 'app/test/filepathneedstruncating.js',
537 538 539 540 541 542 543 544 545 546 547 548
                  removedLines: 0,
                  tempFile: true,
                  type: 'blob',
                  tree: [],
                },
              ],
            },
          ],
          opened: true,
        },
        {
          key: 'package.json',
549
          parentPath: '/',
550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
          path: 'package.json',
          name: 'package.json',
          type: 'blob',
          changed: true,
          tempFile: false,
          deleted: true,
          fileHash: 'test',
          addedLines: 0,
          removedLines: 0,
          tree: [],
        },
      ]);
    });

    it('creates flat list of blobs & folders', () => {
      const { treeEntries } = utils.generateTreeList(files);

      expect(Object.keys(treeEntries)).toEqual([
        'app',
        'app/index.js',
        'app/test',
        'app/test/index.js',
572
        'app/test/filepathneedstruncating.js',
573 574 575 576
        'package.json',
      ]);
    });
  });
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598

  describe('getDiffMode', () => {
    it('returns mode when matched in file', () => {
      expect(
        utils.getDiffMode({
          renamed_file: true,
        }),
      ).toBe('renamed');
    });

    it('returns mode_changed if key has no match', () => {
      expect(
        utils.getDiffMode({
          mode_changed: true,
        }),
      ).toBe('mode_changed');
    });

    it('defaults to replaced', () => {
      expect(utils.getDiffMode({})).toBe('replaced');
    });
  });
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717

  describe('getLowestSingleFolder', () => {
    it('returns path and tree of lowest single folder tree', () => {
      const folder = {
        name: 'app',
        type: 'tree',
        tree: [
          {
            name: 'javascripts',
            type: 'tree',
            tree: [
              {
                type: 'blob',
                name: 'index.js',
              },
            ],
          },
        ],
      };
      const { path, treeAcc } = utils.getLowestSingleFolder(folder);

      expect(path).toEqual('app/javascripts');
      expect(treeAcc).toEqual([
        {
          type: 'blob',
          name: 'index.js',
        },
      ]);
    });

    it('returns passed in folders path & tree when more than tree exists', () => {
      const folder = {
        name: 'app',
        type: 'tree',
        tree: [
          {
            name: 'spec',
            type: 'blob',
            tree: [],
          },
        ],
      };
      const { path, treeAcc } = utils.getLowestSingleFolder(folder);

      expect(path).toEqual('app');
      expect(treeAcc).toBeNull();
    });
  });

  describe('flattenTree', () => {
    it('returns flattened directory structure', () => {
      const tree = [
        {
          type: 'tree',
          name: 'app',
          tree: [
            {
              type: 'tree',
              name: 'javascripts',
              tree: [
                {
                  type: 'blob',
                  name: 'index.js',
                  tree: [],
                },
              ],
            },
          ],
        },
        {
          type: 'tree',
          name: 'spec',
          tree: [
            {
              type: 'tree',
              name: 'javascripts',
              tree: [],
            },
            {
              type: 'blob',
              name: 'index_spec.js',
              tree: [],
            },
          ],
        },
      ];
      const flattened = utils.flattenTree(tree);

      expect(flattened).toEqual([
        {
          type: 'tree',
          name: 'app/javascripts',
          tree: [
            {
              type: 'blob',
              name: 'index.js',
              tree: [],
            },
          ],
        },
        {
          type: 'tree',
          name: 'spec',
          tree: [
            {
              type: 'tree',
              name: 'javascripts',
              tree: [],
            },
            {
              type: 'blob',
              name: 'index_spec.js',
              tree: [],
            },
          ],
        },
      ]);
    });
  });
Felipe Artur's avatar
Felipe Artur committed
718
});