Welcome to mirror list, hosted at ThFree Co, Russian Federation.

diff_spec.js « diff « lib « ide « frontend « spec - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d9b088e2c123349d49fbc81e8d9b667da1c5ca36 (plain)
1
2
3
4
5
6
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
import { computeDiff } from '~/ide/lib/diff/diff';

describe('Multi-file editor library diff calculator', () => {
  describe('computeDiff', () => {
    it('returns empty array if no changes', () => {
      const diff = computeDiff('123', '123');

      expect(diff).toEqual([]);
    });

    describe('modified', () => {
      it.each`
        originalContent    | newContent          | lineNumber
        ${'123'}           | ${'1234'}           | ${1}
        ${'123\n123\n123'} | ${'123\n1234\n123'} | ${2}
      `(
        'marks line $lineNumber as added and modified but not removed',
        ({ originalContent, newContent, lineNumber }) => {
          const diff = computeDiff(originalContent, newContent)[0];

          expect(diff.added).toBeTruthy();
          expect(diff.modified).toBeTruthy();
          expect(diff.removed).toBeUndefined();
          expect(diff.lineNumber).toBe(lineNumber);
        },
      );
    });

    describe('added', () => {
      it.each`
        originalContent    | newContent               | lineNumber
        ${'123'}           | ${'123\n123'}            | ${1}
        ${'123\n123\n123'} | ${'123\n123\n1234\n123'} | ${3}
      `(
        'marks line $lineNumber as added but not modified and not removed',
        ({ originalContent, newContent, lineNumber }) => {
          const diff = computeDiff(originalContent, newContent)[0];

          expect(diff.added).toBeTruthy();
          expect(diff.modified).toBeUndefined();
          expect(diff.removed).toBeUndefined();
          expect(diff.lineNumber).toBe(lineNumber);
        },
      );
    });

    describe('removed', () => {
      it.each`
        originalContent    | newContent    | lineNumber | modified
        ${'123'}           | ${''}         | ${1}       | ${undefined}
        ${'123\n123\n123'} | ${'123\n123'} | ${2}       | ${true}
      `(
        'marks line $lineNumber as removed',
        ({ originalContent, newContent, lineNumber, modified }) => {
          const diff = computeDiff(originalContent, newContent)[0];

          expect(diff.added).toBeUndefined();
          expect(diff.modified).toBe(modified);
          expect(diff.removed).toBeTruthy();
          expect(diff.lineNumber).toBe(lineNumber);
        },
      );
    });

    it('includes line number of change', () => {
      const diff = computeDiff('123', '')[0];

      expect(diff.lineNumber).toBe(1);
    });

    it('includes end line number of change', () => {
      const diff = computeDiff('123', '')[0];

      expect(diff.endLineNumber).toBe(1);
    });
  });
});