utils_spec.js 13.7 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 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 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 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 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
  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', () => {
    const files = [{ fileHash: 1, name: 'one' }];

    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', () => {
        expectSet(utils.findIndexInInlineLines, getDiffFileMock().highlightedDiffLines);
      });
    });

    describe('findIndexInParallelLines', () => {
      it('should return correct index for given line numbers', () => {
        expectSet(utils.findIndexInParallelLines, getDiffFileMock().parallelDiffLines, {});
      });
    });
  });

  describe('removeMatchLine', () => {
    it('should remove match line properly by regarding the bottom parameter', () => {
      const diffFile = getDiffFileMock();
      const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 };
      const inlineIndex = utils.findIndexInInlineLines(diffFile.highlightedDiffLines, lineNumbers);
      const parallelIndex = utils.findIndexInParallelLines(diffFile.parallelDiffLines, lineNumbers);
      const atInlineIndex = diffFile.highlightedDiffLines[inlineIndex];
      const atParallelIndex = diffFile.parallelDiffLines[parallelIndex];

      utils.removeMatchLine(diffFile, lineNumbers, false);
      expect(diffFile.highlightedDiffLines[inlineIndex]).not.toEqual(atInlineIndex);
      expect(diffFile.parallelDiffLines[parallelIndex]).not.toEqual(atParallelIndex);

      utils.removeMatchLine(diffFile, lineNumbers, true);
      expect(diffFile.highlightedDiffLines[inlineIndex + 1]).not.toEqual(atInlineIndex);
      expect(diffFile.parallelDiffLines[parallelIndex + 1]).not.toEqual(atParallelIndex);
    });
  });

  describe('addContextLines', () => {
    it('should add context lines properly with bottom parameter', () => {
      const diffFile = getDiffFileMock();
      const inlineLines = diffFile.highlightedDiffLines;
      const parallelLines = diffFile.parallelDiffLines;
      const lineNumbers = { oldLineNumber: 3, newLineNumber: 5 };
      const contextLines = [{ lineNumber: 42 }];
      const options = { inlineLines, parallelLines, contextLines, lineNumbers, bottom: true };
      const inlineIndex = utils.findIndexInInlineLines(diffFile.highlightedDiffLines, lineNumbers);
      const parallelIndex = utils.findIndexInParallelLines(diffFile.parallelDiffLines, lineNumbers);
      const normalizedParallelLine = {
        left: options.contextLines[0],
        right: options.contextLines[0],
      };

      utils.addContextLines(options);
      expect(inlineLines[inlineLines.length - 1]).toEqual(contextLines[0]);
      expect(parallelLines[parallelLines.length - 1]).toEqual(normalizedParallelLine);

      delete options.bottom;
      utils.addContextLines(options);
      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: {
          lineCode: '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_3',
          metaData: null,
          newLine: 3,
          oldLine: 1,
        },
        diffViewType: PARALLEL_DIFF_VIEW_TYPE,
        linePosition: LINE_POSITION_LEFT,
      };

      const position = JSON.stringify({
        base_sha: diffFile.diffRefs.baseSha,
        start_sha: diffFile.diffRefs.startSha,
        head_sha: diffFile.diffRefs.headSha,
        old_path: diffFile.oldPath,
        new_path: diffFile.newPath,
        position_type: TEXT_DIFF_POSITION_TYPE,
        old_line: options.noteTargetLine.oldLine,
        new_line: options.noteTargetLine.newLine,
      });

      const postData = {
        view: options.diffViewType,
        line_type: options.linePosition === LINE_POSITION_RIGHT ? NEW_LINE_TYPE : OLD_LINE_TYPE,
        merge_request_diff_head_sha: diffFile.diffRefs.headSha,
        in_reply_to_discussion_id: '',
        note_project_id: '',
        target_type: options.noteableType,
        target_id: options.noteableData.id,
        note: {
          noteable_type: options.noteableType,
          noteable_id: options.noteableData.id,
          commit_id: '',
          type: DIFF_NOTE_TYPE,
          line_code: options.noteTargetLine.lineCode,
          note: options.note,
          position,
        },
      };

      expect(utils.getNoteFormData(options)).toEqual({
        endpoint: options.noteableData.create_note_path,
        data: postData,
      });
    });
155 156 157 158

    it('should create legacy note form data', () => {
      const diffFile = getDiffFileMock();
      delete diffFile.diffRefs.startSha;
Phil Hughes's avatar
Phil Hughes committed
159
      delete diffFile.diffRefs.headSha;
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

      noteableDataMock.targetType = MERGE_REQUEST_NOTEABLE_TYPE;

      const options = {
        note: 'Hello world!',
        noteableData: noteableDataMock,
        noteableType: MERGE_REQUEST_NOTEABLE_TYPE,
        diffFile,
        noteTargetLine: {
          lineCode: '1c497fbb3a46b78edf04cc2a2fa33f67e3ffbe2a_1_3',
          metaData: null,
          newLine: 3,
          oldLine: 1,
        },
        diffViewType: PARALLEL_DIFF_VIEW_TYPE,
        linePosition: LINE_POSITION_LEFT,
      };

      const position = JSON.stringify({
        base_sha: diffFile.diffRefs.baseSha,
        start_sha: undefined,
Phil Hughes's avatar
Phil Hughes committed
181
        head_sha: undefined,
182 183 184 185 186 187 188 189 190 191
        old_path: diffFile.oldPath,
        new_path: diffFile.newPath,
        position_type: TEXT_DIFF_POSITION_TYPE,
        old_line: options.noteTargetLine.oldLine,
        new_line: options.noteTargetLine.newLine,
      });

      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
192
        merge_request_diff_head_sha: undefined,
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
        in_reply_to_discussion_id: '',
        note_project_id: '',
        target_type: options.noteableType,
        target_id: options.noteableData.id,
        note: {
          noteable_type: options.noteableType,
          noteable_id: options.noteableData.id,
          commit_id: '',
          type: LEGACY_DIFF_NOTE_TYPE,
          line_code: options.noteTargetLine.lineCode,
          note: options.note,
          position,
        },
      };

      expect(utils.getNoteFormData(options)).toEqual({
        endpoint: options.noteableData.create_note_path,
        data: postData,
      });
    });
Felipe Artur's avatar
Felipe Artur committed
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
  });

  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);

      expect(linesWithReferences[0].oldLine).toEqual(lineNumbers.oldLineNumber + 1);
      expect(linesWithReferences[0].newLine).toEqual(lineNumbers.newLineNumber + 1);
      expect(linesWithReferences[1].metaData.oldPos).toEqual(4);
      expect(linesWithReferences[1].metaData.newPos).toEqual(5);
    });

    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);

      expect(linesWithReferences[0].oldLine).toEqual(0);
      expect(linesWithReferences[0].newLine).toEqual(1);
      expect(linesWithReferences[1].metaData.oldPos).toEqual(2);
      expect(linesWithReferences[1].metaData.newPos).toEqual(3);
    });
  });
238 239 240

  describe('trimFirstCharOfLineContent', () => {
    it('trims the line when it starts with a space', () => {
Tim Zallmann's avatar
Tim Zallmann committed
241 242 243 244
      expect(utils.trimFirstCharOfLineContent({ richText: ' diff' })).toEqual({
        discussions: [],
        richText: 'diff',
      });
245 246 247
    });

    it('trims the line when it starts with a +', () => {
Tim Zallmann's avatar
Tim Zallmann committed
248 249 250 251
      expect(utils.trimFirstCharOfLineContent({ richText: '+diff' })).toEqual({
        discussions: [],
        richText: 'diff',
      });
252 253 254
    });

    it('trims the line when it starts with a -', () => {
Tim Zallmann's avatar
Tim Zallmann committed
255 256 257 258
      expect(utils.trimFirstCharOfLineContent({ richText: '-diff' })).toEqual({
        discussions: [],
        richText: 'diff',
      });
259 260 261
    });

    it('does not trims the line when it starts with a letter', () => {
Tim Zallmann's avatar
Tim Zallmann committed
262 263 264 265
      expect(utils.trimFirstCharOfLineContent({ richText: 'diff' })).toEqual({
        discussions: [],
        richText: 'diff',
      });
266 267 268 269
    });

    it('does not modify the provided object', () => {
      const lineObj = {
Tim Zallmann's avatar
Tim Zallmann committed
270
        discussions: [],
271 272 273 274
        richText: ' diff',
      };

      utils.trimFirstCharOfLineContent(lineObj);
Tim Zallmann's avatar
Tim Zallmann committed
275
      expect(lineObj).toEqual({ discussions: [], richText: ' diff' });
276 277 278
    });

    it('handles a undefined or null parameter', () => {
Tim Zallmann's avatar
Tim Zallmann committed
279
      expect(utils.trimFirstCharOfLineContent()).toEqual({ discussions: [] });
280 281
    });
  });
282 283 284 285 286 287 288 289 290 291 292

  describe('prepareDiffData', () => {
    it('sets the renderIt and collapsed attribute on files', () => {
      const preparedDiff = { diffFiles: [getDiffFileMock()] };
      utils.prepareDiffData(preparedDiff);

      const firstParallelDiffLine = preparedDiff.diffFiles[0].parallelDiffLines[2];
      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');
293 294 295 296 297 298 299 300 301 302 303 304
      const firstParallelChar = firstParallelDiffLine.right.richText.charAt(0);
      expect(firstParallelChar).not.toBe(' ');
      expect(firstParallelChar).not.toBe('+');
      expect(firstParallelChar).not.toBe('-');

      const checkLine = preparedDiff.diffFiles[0].highlightedDiffLines[0];
      expect(checkLine.discussions.length).toBe(0);
      expect(checkLine).not.toHaveAttr('text');
      const firstChar = checkLine.richText.charAt(0);
      expect(firstChar).not.toBe(' ');
      expect(firstChar).not.toBe('+');
      expect(firstChar).not.toBe('-');
305 306 307 308 309

      expect(preparedDiff.diffFiles[0].renderIt).toBeTruthy();
      expect(preparedDiff.diffFiles[0].collapsed).toBeFalsy();
    });
  });
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('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: {
        original_position: {
          formatter: diffPosition,
        },
        position: {
          formatter: wrongDiffPosition,
        },
      },
      outDatedDiscussion1: {
        original_position: {
          formatter: wrongDiffPosition,
        },
        position: {
          formatter: wrongDiffPosition,
        },
      },
    };

    it('returns true when the discussion is up to date', () => {
      expect(
        utils.isDiscussionApplicableToLine(discussions.upToDateDiscussion1, diffPosition),
      ).toBe(true);
    });

    it('returns false when the discussion is not up to date', () => {
      expect(
        utils.isDiscussionApplicableToLine(discussions.outDatedDiscussion1, diffPosition),
      ).toBe(false);
    });
362

Phil Hughes's avatar
Phil Hughes committed
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
    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(
        utils.isDiscussionApplicableToLine(discussion, {
          ...diffPosition,
          lineCode: 'ABC_1',
        }),
      ).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 };
378 379 380 381 382 383 384 385 386 387
      delete discussion.original_position;
      delete discussion.position;

      expect(
        utils.isDiscussionApplicableToLine(discussion, {
          ...diffPosition,
          lineCode: 'ABC_1',
        }),
      ).toBe(true);
    });
388
  });
Felipe Artur's avatar
Felipe Artur committed
389
});