debian-mirror-gitlab/spec/frontend/ide/lib/diff/diff_spec.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

86 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-05-09 12:01:36 +05:30
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', () => {
2019-07-31 22:56:46 +05:30
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];
2022-08-27 11:52:29 +05:30
expect(diff.added).toBe(true);
expect(diff.modified).toBe(true);
2019-07-31 22:56:46 +05:30
expect(diff.removed).toBeUndefined();
expect(diff.lineNumber).toBe(lineNumber);
},
);
2018-05-09 12:01:36 +05:30
});
describe('added', () => {
2019-07-31 22:56:46 +05:30
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];
2022-08-27 11:52:29 +05:30
expect(diff.added).toBe(true);
2019-07-31 22:56:46 +05:30
expect(diff.modified).toBeUndefined();
expect(diff.removed).toBeUndefined();
expect(diff.lineNumber).toBe(lineNumber);
},
);
2018-05-09 12:01:36 +05:30
});
describe('removed', () => {
2019-07-31 22:56:46 +05:30
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);
2022-08-27 11:52:29 +05:30
expect(diff.removed).toBe(true);
2019-07-31 22:56:46 +05:30
expect(diff.lineNumber).toBe(lineNumber);
},
);
2018-05-09 12:01:36 +05:30
});
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);
});
2020-06-23 00:09:42 +05:30
it('disregards changes for EOL type changes', () => {
const text1 = 'line1\nline2\nline3\n';
const text2 = 'line1\r\nline2\r\nline3\r\n';
expect(computeDiff(text1, text2)).toEqual([]);
expect(computeDiff(text2, text1)).toEqual([]);
});
2018-05-09 12:01:36 +05:30
});
});