import LineByLineRenderer from '../line-by-line-renderer'; import HoganJsUtils from '../hoganjs-utils'; import { LineType, DiffFile, LineMatchingType } from '../types'; import { CSSLineClass } from '../render-utils'; describe('LineByLineRenderer', () => { describe('_generateEmptyDiff', () => { it('should return an empty diff', () => { const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const fileHtml = lineByLineRenderer.generateEmptyDiff(); expect(fileHtml).toMatchInlineSnapshot(` "
File without changes
" `); }); }); describe('makeLineHtml', () => { it('should work for insertions', () => { const file = { addedLines: 12, deletedLines: 41, language: 'js', oldName: 'my/file/name.js', newName: 'my/file/name.js', isCombined: false, isGitDiff: false, blocks: [], }; const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, { type: CSSLineClass.INSERTS, prefix: '+', content: 'test', oldNumber: undefined, newNumber: 30, }); expect(fileHtml).toMatchInlineSnapshot(` "
30
+ test
" `); }); it('should work for deletions', () => { const file = { addedLines: 12, deletedLines: 41, language: 'js', oldName: 'my/file/name.js', newName: 'my/file/name.js', isCombined: false, isGitDiff: false, blocks: [], }; const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, { type: CSSLineClass.DELETES, prefix: '-', content: 'test', oldNumber: 30, newNumber: undefined, }); expect(fileHtml).toMatchInlineSnapshot(` "
30
- test
" `); }); it('should convert indents into non breakin spaces (2 white spaces)', () => { const file = { addedLines: 12, deletedLines: 41, language: 'js', oldName: 'my/file/name.js', newName: 'my/file/name.js', isCombined: false, isGitDiff: false, blocks: [], }; const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, { type: CSSLineClass.INSERTS, prefix: '+', content: ' test', oldNumber: undefined, newNumber: 30, }); expect(fileHtml).toMatchInlineSnapshot(` "
30
+ test
" `); }); it('should convert indents into non breakin spaces (4 white spaces)', () => { const file = { addedLines: 12, deletedLines: 41, language: 'js', oldName: 'my/file/name.js', newName: 'my/file/name.js', isCombined: false, isGitDiff: false, blocks: [], }; const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, { type: CSSLineClass.INSERTS, prefix: '+', content: ' test', oldNumber: undefined, newNumber: 30, }); expect(fileHtml).toMatchInlineSnapshot(` "
30
+ test
" `); }); it('should preserve tabs', () => { const file = { addedLines: 12, deletedLines: 41, language: 'js', oldName: 'my/file/name.js', newName: 'my/file/name.js', isCombined: false, isGitDiff: false, blocks: [], }; const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, { type: CSSLineClass.INSERTS, prefix: '+', content: '\ttest', oldNumber: undefined, newNumber: 30, }); expect(fileHtml).toMatchInlineSnapshot(` "
30
+ test
" `); }); }); describe('makeFileDiffHtml', () => { it('should work for simple file', () => { const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const file = { addedLines: 12, deletedLines: 41, language: 'js', oldName: 'my/file/name.js', newName: 'my/file/name.js', isCombined: false, isGitDiff: false, blocks: [], }; const diffs = 'Random Html'; const fileHtml = lineByLineRenderer.makeFileDiffHtml(file, diffs); expect(fileHtml).toMatchInlineSnapshot(` "
my/file/name.js CHANGED
Random Html
" `); }); it('should work for simple added file', () => { const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const file = { addedLines: 12, deletedLines: 0, language: 'js', oldName: 'dev/null', newName: 'my/file/name.js', isNew: true, isCombined: false, isGitDiff: false, blocks: [], }; const diffs = 'Random Html'; const fileHtml = lineByLineRenderer.makeFileDiffHtml(file, diffs); expect(fileHtml).toMatchInlineSnapshot(` "
my/file/name.js ADDED
Random Html
" `); }); it('should work for simple deleted file', () => { const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const file = { addedLines: 0, deletedLines: 41, language: 'js', oldName: 'my/file/name.js', newName: 'dev/null', isDeleted: true, isCombined: false, isGitDiff: false, blocks: [], }; const diffs = 'Random Html'; const fileHtml = lineByLineRenderer.makeFileDiffHtml(file, diffs); expect(fileHtml).toMatchInlineSnapshot(` "
my/file/name.js DELETED
Random Html
" `); }); it('should work for simple renamed file', () => { const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const file = { addedLines: 12, deletedLines: 41, language: 'js', oldName: 'my/file/name1.js', newName: 'my/file/name2.js', isRename: true, isCombined: false, isGitDiff: false, blocks: [], }; const diffs = 'Random Html'; const fileHtml = lineByLineRenderer.makeFileDiffHtml(file, diffs); expect(fileHtml).toMatchInlineSnapshot(` "
my/file/{name1.js → name2.js} RENAMED
Random Html
" `); }); it('should return empty when option renderNothingWhenEmpty is true and file blocks not present', () => { const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, { renderNothingWhenEmpty: true, }); const file = { addedLines: 0, deletedLines: 0, language: 'js', oldName: 'my/file/name1.js', newName: 'my/file/name2.js', isRename: true, isCombined: false, isGitDiff: false, blocks: [], }; const diffs = 'Random Html'; const fileHtml = lineByLineRenderer.makeFileDiffHtml(file, diffs); expect(fileHtml).toMatchInlineSnapshot(`""`); }); }); describe('generateLineByLineJsonHtml', () => { it('should work for list of files', () => { const exampleJson: DiffFile[] = [ { blocks: [ { lines: [ { content: '-test', type: LineType.DELETE, oldNumber: 1, newNumber: undefined, }, { content: '+test1r', type: LineType.INSERT, oldNumber: undefined, newNumber: 1, }, ], oldStartLine: 1, oldStartLine2: undefined, newStartLine: 1, header: '@@ -1 +1 @@', }, ], deletedLines: 1, addedLines: 1, checksumBefore: '0000001', checksumAfter: '0ddf2ba', oldName: 'sample', newName: 'sample', language: 'txt', isCombined: false, isGitDiff: true, }, ]; const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, { matching: LineMatchingType.LINES, }); const html = lineByLineRenderer.render(exampleJson); expect(html).toMatchInlineSnapshot(` "
sample CHANGED
@@ -1 +1 @@
1
- test
1
+ test1r
" `); }); it('should work for empty blocks', () => { const exampleJson = [ { blocks: [], deletedLines: 0, addedLines: 0, oldName: 'sample', language: 'js', newName: 'sample', isCombined: false, isGitDiff: false, }, ]; const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, { renderNothingWhenEmpty: false, }); const html = lineByLineRenderer.render(exampleJson); expect(html).toMatchInlineSnapshot(` "
sample CHANGED
File without changes
" `); }); it('should work for too big file diff', () => { const exampleJson = [ { blocks: [ { header: 'Custom link to render', lines: [], newStartLine: 0, oldStartLine: 0, oldStartLine2: undefined, }, ], deletedLines: 0, addedLines: 0, oldName: 'sample', language: 'js', newName: 'sample', isCombined: false, isGitDiff: false, isTooBig: true, }, ]; const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils); const html = lineByLineRenderer.render(exampleJson); expect(html).toMatchInlineSnapshot(` "
sample CHANGED
" `); }); }); describe('_generateFileHtml', () => { it('should work for simple file', () => { const hoganUtils = new HoganJsUtils({}); const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {}); const file: DiffFile = { blocks: [ { lines: [ { content: ' one context line', type: LineType.CONTEXT, oldNumber: 1, newNumber: 1, }, { content: '-test', type: LineType.DELETE, oldNumber: 2, newNumber: undefined, }, { content: '+test1r', type: LineType.INSERT, oldNumber: undefined, newNumber: 2, }, { content: '+test2r', type: LineType.INSERT, oldNumber: undefined, newNumber: 3, }, ], oldStartLine: 1, oldStartLine2: undefined, newStartLine: 1, header: '@@ -1 +1 @@', }, ], deletedLines: 1, addedLines: 1, checksumBefore: '0000001', checksumAfter: '0ddf2ba', oldName: 'sample', language: 'txt', newName: 'sample', isCombined: false, isGitDiff: true, }; const html = lineByLineRenderer.generateFileHtml(file); expect(html).toMatchInlineSnapshot(` "
@@ -1 +1 @@
1
1
  one context line
2
- test
2
+ test1r
3
+ test2r
" `); }); }); });