diff2html/src/__tests__/line-by-line-tests.ts

726 lines
26 KiB
TypeScript
Raw Normal View History

2019-12-29 22:31:32 +00:00
import LineByLineRenderer from '../line-by-line-renderer';
import HoganJsUtils from '../hoganjs-utils';
import { LineType, DiffFile, LineMatchingType } from '../types';
import { CSSLineClass } from '../render-utils';
2019-10-12 21:45:49 +00:00
2019-12-29 22:31:32 +00:00
describe('LineByLineRenderer', () => {
describe('_generateEmptyDiff', () => {
it('should return an empty diff', () => {
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const fileHtml = lineByLineRenderer.generateEmptyDiff();
2019-12-22 19:52:51 +00:00
expect(fileHtml).toMatchInlineSnapshot(`
"<tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-info">
<div class="d2h-code-line">
2019-12-22 19:52:51 +00:00
File without changes
</div>
</td>
</tr>"
`);
});
});
2019-12-29 22:31:32 +00:00
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: [],
};
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
type: CSSLineClass.INSERTS,
2019-12-29 22:31:32 +00:00
prefix: '+',
content: 'test',
oldNumber: undefined,
2019-12-29 22:31:32 +00:00
newNumber: 30,
});
2019-12-22 19:52:51 +00:00
expect(fileHtml).toMatchInlineSnapshot(`
"<tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-ins">
<div class="line-num1"></div>
<div class="line-num2">30</div>
2019-12-22 19:52:51 +00:00
</td>
2022-10-15 23:01:01 +00:00
<td class="d2h-ins">
<div class="d2h-code-line">
<span class="d2h-code-line-prefix">+</span>
<span class="d2h-code-line-ctn">test</span>
2019-12-22 19:52:51 +00:00
</div>
</td>
</tr>"
`);
});
2019-12-29 22:31:32 +00:00
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: [],
};
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
type: CSSLineClass.DELETES,
2019-12-29 22:31:32 +00:00
prefix: '-',
content: 'test',
oldNumber: 30,
2019-12-29 22:31:32 +00:00
newNumber: undefined,
});
2019-12-22 19:52:51 +00:00
expect(fileHtml).toMatchInlineSnapshot(`
"<tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-del">
<div class="line-num1">30</div>
<div class="line-num2"></div>
2019-12-22 19:52:51 +00:00
</td>
2022-10-15 23:01:01 +00:00
<td class="d2h-del">
<div class="d2h-code-line">
<span class="d2h-code-line-prefix">-</span>
<span class="d2h-code-line-ctn">test</span>
2019-12-22 19:52:51 +00:00
</div>
</td>
</tr>"
`);
});
2019-12-29 22:31:32 +00:00
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: [],
};
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
type: CSSLineClass.INSERTS,
2019-12-29 22:31:32 +00:00
prefix: '+',
content: ' test',
oldNumber: undefined,
2019-12-29 22:31:32 +00:00
newNumber: 30,
});
2019-12-22 19:52:51 +00:00
expect(fileHtml).toMatchInlineSnapshot(`
"<tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-ins">
<div class="line-num1"></div>
<div class="line-num2">30</div>
2019-12-22 19:52:51 +00:00
</td>
2022-10-15 23:01:01 +00:00
<td class="d2h-ins">
<div class="d2h-code-line">
<span class="d2h-code-line-prefix">+</span>
<span class="d2h-code-line-ctn"> test</span>
2019-12-22 19:52:51 +00:00
</div>
</td>
</tr>"
`);
});
2019-12-29 22:31:32 +00:00
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: [],
};
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
type: CSSLineClass.INSERTS,
2019-12-29 22:31:32 +00:00
prefix: '+',
content: ' test',
oldNumber: undefined,
2019-12-29 22:31:32 +00:00
newNumber: 30,
});
2019-12-22 19:52:51 +00:00
expect(fileHtml).toMatchInlineSnapshot(`
"<tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-ins">
<div class="line-num1"></div>
<div class="line-num2">30</div>
2019-12-22 19:52:51 +00:00
</td>
2022-10-15 23:01:01 +00:00
<td class="d2h-ins">
<div class="d2h-code-line">
<span class="d2h-code-line-prefix">+</span>
<span class="d2h-code-line-ctn"> test</span>
2019-12-22 19:52:51 +00:00
</div>
</td>
</tr>"
`);
});
2019-12-29 22:31:32 +00:00
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: [],
};
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const fileHtml = lineByLineRenderer.generateSingleLineHtml(file, {
type: CSSLineClass.INSERTS,
2019-12-29 22:31:32 +00:00
prefix: '+',
content: '\ttest',
oldNumber: undefined,
2019-12-29 22:31:32 +00:00
newNumber: 30,
});
2019-12-22 19:52:51 +00:00
expect(fileHtml).toMatchInlineSnapshot(`
"<tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-ins">
<div class="line-num1"></div>
<div class="line-num2">30</div>
2019-12-22 19:52:51 +00:00
</td>
2022-10-15 23:01:01 +00:00
<td class="d2h-ins">
<div class="d2h-code-line">
<span class="d2h-code-line-prefix">+</span>
<span class="d2h-code-line-ctn"> test</span>
2019-12-22 19:52:51 +00:00
</div>
</td>
</tr>"
`);
});
});
2019-12-29 22:31:32 +00:00
describe('makeFileDiffHtml', () => {
it('should work for simple file', () => {
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const file = {
addedLines: 12,
deletedLines: 41,
2019-12-29 22:31:32 +00:00
language: 'js',
oldName: 'my/file/name.js',
newName: 'my/file/name.js',
2019-10-12 21:45:49 +00:00
isCombined: false,
isGitDiff: false,
2019-12-29 22:31:32 +00:00
blocks: [],
};
2019-12-29 22:31:32 +00:00
const diffs = '<span>Random Html</span>';
2019-10-12 21:45:49 +00:00
const fileHtml = lineByLineRenderer.makeFileDiffHtml(file, diffs);
2019-12-22 19:52:51 +00:00
expect(fileHtml).toMatchInlineSnapshot(`
2022-10-15 23:01:01 +00:00
"<div id="d2h-781444" class="d2h-file-wrapper" data-lang="js">
<div class="d2h-file-header">
<span class="d2h-file-name-wrapper">
<svg aria-hidden="true" class="d2h-icon" height="16" version="1.1" viewBox="0 0 12 16" width="12">
<path d="M6 5H2v-1h4v1zM2 8h7v-1H2v1z m0 2h7v-1H2v1z m0 2h7v-1H2v1z m10-7.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V2c0-0.55 0.45-1 1-1h7.5l3.5 3.5z m-1 0.5L8 2H1v12h10V5z"></path>
</svg> <span class="d2h-file-name">my/file/name.js</span>
<span class="d2h-tag d2h-changed d2h-changed-tag">CHANGED</span></span>
<label class="d2h-file-collapse">
<input class="d2h-file-collapse-input" type="checkbox" name="viewed" value="viewed">
2021-01-23 15:07:14 +00:00
Viewed
</label>
2019-12-22 19:52:51 +00:00
</div>
2022-10-15 23:01:01 +00:00
<div class="d2h-file-diff">
<div class="d2h-code-wrapper">
<table class="d2h-diff-table">
<tbody class="d2h-diff-tbody">
2019-12-22 19:52:51 +00:00
<span>Random Html</span>
</tbody>
</table>
</div>
</div>
</div>"
`);
});
2019-12-29 22:31:32 +00:00
it('should work for simple added file', () => {
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const file = {
addedLines: 12,
deletedLines: 0,
2019-12-29 22:31:32 +00:00
language: 'js',
oldName: 'dev/null',
newName: 'my/file/name.js',
2019-10-12 21:45:49 +00:00
isNew: true,
isCombined: false,
isGitDiff: false,
2019-12-29 22:31:32 +00:00
blocks: [],
};
2019-12-29 22:31:32 +00:00
const diffs = '<span>Random Html</span>';
2019-10-12 21:45:49 +00:00
const fileHtml = lineByLineRenderer.makeFileDiffHtml(file, diffs);
2019-12-22 19:52:51 +00:00
expect(fileHtml).toMatchInlineSnapshot(`
2022-10-15 23:01:01 +00:00
"<div id="d2h-781444" class="d2h-file-wrapper" data-lang="js">
<div class="d2h-file-header">
<span class="d2h-file-name-wrapper">
<svg aria-hidden="true" class="d2h-icon" height="16" version="1.1" viewBox="0 0 12 16" width="12">
<path d="M6 5H2v-1h4v1zM2 8h7v-1H2v1z m0 2h7v-1H2v1z m0 2h7v-1H2v1z m10-7.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V2c0-0.55 0.45-1 1-1h7.5l3.5 3.5z m-1 0.5L8 2H1v12h10V5z"></path>
</svg> <span class="d2h-file-name">my/file/name.js</span>
<span class="d2h-tag d2h-added d2h-added-tag">ADDED</span></span>
<label class="d2h-file-collapse">
<input class="d2h-file-collapse-input" type="checkbox" name="viewed" value="viewed">
2021-01-23 15:07:14 +00:00
Viewed
</label>
2019-12-22 19:52:51 +00:00
</div>
2022-10-15 23:01:01 +00:00
<div class="d2h-file-diff">
<div class="d2h-code-wrapper">
<table class="d2h-diff-table">
<tbody class="d2h-diff-tbody">
2019-12-22 19:52:51 +00:00
<span>Random Html</span>
</tbody>
</table>
</div>
</div>
</div>"
`);
});
2019-12-29 22:31:32 +00:00
it('should work for simple deleted file', () => {
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const file = {
addedLines: 0,
deletedLines: 41,
2019-12-29 22:31:32 +00:00
language: 'js',
oldName: 'my/file/name.js',
newName: 'dev/null',
2019-10-12 21:45:49 +00:00
isDeleted: true,
isCombined: false,
isGitDiff: false,
2019-12-29 22:31:32 +00:00
blocks: [],
};
2019-12-29 22:31:32 +00:00
const diffs = '<span>Random Html</span>';
2019-10-12 21:45:49 +00:00
const fileHtml = lineByLineRenderer.makeFileDiffHtml(file, diffs);
2019-12-22 19:52:51 +00:00
expect(fileHtml).toMatchInlineSnapshot(`
2022-10-15 23:01:01 +00:00
"<div id="d2h-781444" class="d2h-file-wrapper" data-lang="js">
<div class="d2h-file-header">
<span class="d2h-file-name-wrapper">
<svg aria-hidden="true" class="d2h-icon" height="16" version="1.1" viewBox="0 0 12 16" width="12">
<path d="M6 5H2v-1h4v1zM2 8h7v-1H2v1z m0 2h7v-1H2v1z m0 2h7v-1H2v1z m10-7.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V2c0-0.55 0.45-1 1-1h7.5l3.5 3.5z m-1 0.5L8 2H1v12h10V5z"></path>
</svg> <span class="d2h-file-name">my/file/name.js</span>
<span class="d2h-tag d2h-deleted d2h-deleted-tag">DELETED</span></span>
<label class="d2h-file-collapse">
<input class="d2h-file-collapse-input" type="checkbox" name="viewed" value="viewed">
2021-01-23 15:07:14 +00:00
Viewed
</label>
2019-12-22 19:52:51 +00:00
</div>
2022-10-15 23:01:01 +00:00
<div class="d2h-file-diff">
<div class="d2h-code-wrapper">
<table class="d2h-diff-table">
<tbody class="d2h-diff-tbody">
2019-12-22 19:52:51 +00:00
<span>Random Html</span>
</tbody>
</table>
</div>
</div>
</div>"
`);
});
2019-12-29 22:31:32 +00:00
it('should work for simple renamed file', () => {
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const file = {
addedLines: 12,
deletedLines: 41,
2019-12-29 22:31:32 +00:00
language: 'js',
oldName: 'my/file/name1.js',
newName: 'my/file/name2.js',
2019-10-12 21:45:49 +00:00
isRename: true,
isCombined: false,
isGitDiff: false,
2019-12-29 22:31:32 +00:00
blocks: [],
};
2019-12-29 22:31:32 +00:00
const diffs = '<span>Random Html</span>';
2019-10-12 21:45:49 +00:00
const fileHtml = lineByLineRenderer.makeFileDiffHtml(file, diffs);
2019-12-22 19:52:51 +00:00
expect(fileHtml).toMatchInlineSnapshot(`
2022-10-15 23:01:01 +00:00
"<div id="d2h-662683" class="d2h-file-wrapper" data-lang="js">
<div class="d2h-file-header">
<span class="d2h-file-name-wrapper">
<svg aria-hidden="true" class="d2h-icon" height="16" version="1.1" viewBox="0 0 12 16" width="12">
<path d="M6 5H2v-1h4v1zM2 8h7v-1H2v1z m0 2h7v-1H2v1z m0 2h7v-1H2v1z m10-7.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V2c0-0.55 0.45-1 1-1h7.5l3.5 3.5z m-1 0.5L8 2H1v12h10V5z"></path>
</svg> <span class="d2h-file-name">my/file/{name1.js name2.js}</span>
<span class="d2h-tag d2h-moved d2h-moved-tag">RENAMED</span></span>
<label class="d2h-file-collapse">
<input class="d2h-file-collapse-input" type="checkbox" name="viewed" value="viewed">
2021-01-23 15:07:14 +00:00
Viewed
</label>
2019-12-22 19:52:51 +00:00
</div>
2022-10-15 23:01:01 +00:00
<div class="d2h-file-diff">
<div class="d2h-code-wrapper">
<table class="d2h-diff-table">
<tbody class="d2h-diff-tbody">
2019-12-22 19:52:51 +00:00
<span>Random Html</span>
</tbody>
</table>
</div>
</div>
</div>"
`);
});
2019-12-29 22:31:32 +00:00
it('should return empty when option renderNothingWhenEmpty is true and file blocks not present', () => {
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {
2019-12-29 22:31:32 +00:00
renderNothingWhenEmpty: true,
});
const file = {
2019-10-12 21:45:49 +00:00
addedLines: 0,
deletedLines: 0,
2019-12-29 22:31:32 +00:00
language: 'js',
oldName: 'my/file/name1.js',
newName: 'my/file/name2.js',
2019-10-12 21:45:49 +00:00
isRename: true,
isCombined: false,
isGitDiff: false,
2019-12-29 22:31:32 +00:00
blocks: [],
};
2019-12-29 22:31:32 +00:00
const diffs = '<span>Random Html</span>';
2019-10-12 21:45:49 +00:00
const fileHtml = lineByLineRenderer.makeFileDiffHtml(file, diffs);
2019-12-22 19:52:51 +00:00
expect(fileHtml).toMatchInlineSnapshot(`""`);
});
});
2019-12-29 22:31:32 +00:00
describe('generateLineByLineJsonHtml', () => {
it('should work for list of files', () => {
2019-10-12 21:45:49 +00:00
const exampleJson: DiffFile[] = [
2016-12-17 23:39:21 +00:00
{
blocks: [
{
2016-12-17 23:39:21 +00:00
lines: [
{
2019-12-29 22:31:32 +00:00
content: '-test',
2019-10-12 21:45:49 +00:00
type: LineType.DELETE,
2016-12-17 23:39:21 +00:00
oldNumber: 1,
2019-12-29 22:31:32 +00:00
newNumber: undefined,
2016-12-17 23:39:21 +00:00
},
{
2019-12-29 22:31:32 +00:00
content: '+test1r',
2019-10-12 21:45:49 +00:00
type: LineType.INSERT,
oldNumber: undefined,
2019-12-29 22:31:32 +00:00
newNumber: 1,
},
2016-12-17 23:39:21 +00:00
],
2019-10-12 21:45:49 +00:00
oldStartLine: 1,
oldStartLine2: undefined,
newStartLine: 1,
2019-12-29 22:31:32 +00:00
header: '@@ -1 +1 @@',
},
2016-12-17 23:39:21 +00:00
],
deletedLines: 1,
addedLines: 1,
2019-12-29 22:31:32 +00:00
checksumBefore: '0000001',
checksumAfter: '0ddf2ba',
oldName: 'sample',
newName: 'sample',
language: 'txt',
2019-10-12 21:45:49 +00:00
isCombined: false,
2019-12-29 22:31:32 +00:00
isGitDiff: true,
},
2016-12-17 23:39:21 +00:00
];
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {
2019-12-29 22:31:32 +00:00
matching: LineMatchingType.LINES,
2019-10-12 21:45:49 +00:00
});
const html = lineByLineRenderer.render(exampleJson);
2019-12-22 19:52:51 +00:00
expect(html).toMatchInlineSnapshot(`
2023-09-26 23:06:06 +00:00
"<div class="d2h-wrapper d2h-light-color-scheme">
2022-10-15 23:01:01 +00:00
<div id="d2h-675094" class="d2h-file-wrapper" data-lang="txt">
<div class="d2h-file-header">
<span class="d2h-file-name-wrapper">
<svg aria-hidden="true" class="d2h-icon" height="16" version="1.1" viewBox="0 0 12 16" width="12">
<path d="M6 5H2v-1h4v1zM2 8h7v-1H2v1z m0 2h7v-1H2v1z m0 2h7v-1H2v1z m10-7.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V2c0-0.55 0.45-1 1-1h7.5l3.5 3.5z m-1 0.5L8 2H1v12h10V5z"></path>
</svg> <span class="d2h-file-name">sample</span>
<span class="d2h-tag d2h-changed d2h-changed-tag">CHANGED</span></span>
<label class="d2h-file-collapse">
<input class="d2h-file-collapse-input" type="checkbox" name="viewed" value="viewed">
2021-01-23 15:07:14 +00:00
Viewed
</label>
2019-12-22 19:52:51 +00:00
</div>
2022-10-15 23:01:01 +00:00
<div class="d2h-file-diff">
<div class="d2h-code-wrapper">
<table class="d2h-diff-table">
<tbody class="d2h-diff-tbody">
2019-12-22 19:52:51 +00:00
<tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-info"></td>
<td class="d2h-info">
<div class="d2h-code-line">@@ -1 +1 @@</div>
2019-12-22 19:52:51 +00:00
</td>
</tr><tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-del d2h-change">
<div class="line-num1">1</div>
<div class="line-num2"></div>
2019-12-22 19:52:51 +00:00
</td>
2022-10-15 23:01:01 +00:00
<td class="d2h-del d2h-change">
<div class="d2h-code-line">
<span class="d2h-code-line-prefix">-</span>
<span class="d2h-code-line-ctn"><del>test</del></span>
2019-12-22 19:52:51 +00:00
</div>
</td>
</tr><tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-ins d2h-change">
<div class="line-num1"></div>
<div class="line-num2">1</div>
2019-12-22 19:52:51 +00:00
</td>
2022-10-15 23:01:01 +00:00
<td class="d2h-ins d2h-change">
<div class="d2h-code-line">
<span class="d2h-code-line-prefix">+</span>
<span class="d2h-code-line-ctn"><ins>test1r</ins></span>
2019-12-22 19:52:51 +00:00
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>"
`);
});
2019-12-29 22:31:32 +00:00
it('should work for empty blocks', () => {
const exampleJson = [
{
blocks: [],
deletedLines: 0,
addedLines: 0,
2019-12-29 22:31:32 +00:00
oldName: 'sample',
language: 'js',
newName: 'sample',
2019-10-12 21:45:49 +00:00
isCombined: false,
2019-12-29 22:31:32 +00:00
isGitDiff: false,
},
];
2016-04-25 18:24:35 +00:00
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {
2019-12-29 22:31:32 +00:00
renderNothingWhenEmpty: false,
2019-10-12 21:45:49 +00:00
});
const html = lineByLineRenderer.render(exampleJson);
2019-12-22 19:52:51 +00:00
expect(html).toMatchInlineSnapshot(`
2023-09-26 23:06:06 +00:00
"<div class="d2h-wrapper d2h-light-color-scheme">
2022-10-15 23:01:01 +00:00
<div id="d2h-675094" class="d2h-file-wrapper" data-lang="js">
<div class="d2h-file-header">
<span class="d2h-file-name-wrapper">
<svg aria-hidden="true" class="d2h-icon" height="16" version="1.1" viewBox="0 0 12 16" width="12">
<path d="M6 5H2v-1h4v1zM2 8h7v-1H2v1z m0 2h7v-1H2v1z m0 2h7v-1H2v1z m10-7.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V2c0-0.55 0.45-1 1-1h7.5l3.5 3.5z m-1 0.5L8 2H1v12h10V5z"></path>
</svg> <span class="d2h-file-name">sample</span>
<span class="d2h-tag d2h-changed d2h-changed-tag">CHANGED</span></span>
<label class="d2h-file-collapse">
<input class="d2h-file-collapse-input" type="checkbox" name="viewed" value="viewed">
2021-01-23 15:07:14 +00:00
Viewed
</label>
2019-12-22 19:52:51 +00:00
</div>
2022-10-15 23:01:01 +00:00
<div class="d2h-file-diff">
<div class="d2h-code-wrapper">
<table class="d2h-diff-table">
<tbody class="d2h-diff-tbody">
2019-12-22 19:52:51 +00:00
<tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-info">
<div class="d2h-code-line">
2019-12-22 19:52:51 +00:00
File without changes
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>"
`);
});
2021-03-01 21:05:12 +00:00
it('should work for too big file diff', () => {
const exampleJson = [
{
2021-03-01 21:05:12 +00:00
blocks: [
{
header: '<a href="http://example.com">Custom link to render</a>',
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(`
2023-09-26 23:06:06 +00:00
"<div class="d2h-wrapper d2h-light-color-scheme">
2022-10-15 23:01:01 +00:00
<div id="d2h-675094" class="d2h-file-wrapper" data-lang="js">
<div class="d2h-file-header">
<span class="d2h-file-name-wrapper">
<svg aria-hidden="true" class="d2h-icon" height="16" version="1.1" viewBox="0 0 12 16" width="12">
<path d="M6 5H2v-1h4v1zM2 8h7v-1H2v1z m0 2h7v-1H2v1z m0 2h7v-1H2v1z m10-7.5v9.5c0 0.55-0.45 1-1 1H1c-0.55 0-1-0.45-1-1V2c0-0.55 0.45-1 1-1h7.5l3.5 3.5z m-1 0.5L8 2H1v12h10V5z"></path>
</svg> <span class="d2h-file-name">sample</span>
<span class="d2h-tag d2h-changed d2h-changed-tag">CHANGED</span></span>
<label class="d2h-file-collapse">
<input class="d2h-file-collapse-input" type="checkbox" name="viewed" value="viewed">
Viewed
</label>
</div>
2022-10-15 23:01:01 +00:00
<div class="d2h-file-diff">
<div class="d2h-code-wrapper">
<table class="d2h-diff-table">
<tbody class="d2h-diff-tbody">
<tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-info"></td>
<td class="d2h-info">
<div class="d2h-code-line"><a href="http://example.com">Custom link to render</a></div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>"
`);
});
});
2019-12-29 22:31:32 +00:00
describe('_generateFileHtml', () => {
it('should work for simple file', () => {
2019-10-12 21:45:49 +00:00
const hoganUtils = new HoganJsUtils({});
const lineByLineRenderer = new LineByLineRenderer(hoganUtils, {});
const file: DiffFile = {
2016-12-17 23:39:21 +00:00
blocks: [
{
lines: [
{
2019-12-29 22:31:32 +00:00
content: ' one context line',
2019-10-12 21:45:49 +00:00
type: LineType.CONTEXT,
2016-12-17 23:39:21 +00:00
oldNumber: 1,
2019-12-29 22:31:32 +00:00
newNumber: 1,
2016-12-17 23:39:21 +00:00
},
{
2019-12-29 22:31:32 +00:00
content: '-test',
2019-10-12 21:45:49 +00:00
type: LineType.DELETE,
2016-12-17 23:39:21 +00:00
oldNumber: 2,
2019-12-29 22:31:32 +00:00
newNumber: undefined,
2016-12-17 23:39:21 +00:00
},
{
2019-12-29 22:31:32 +00:00
content: '+test1r',
2019-10-12 21:45:49 +00:00
type: LineType.INSERT,
oldNumber: undefined,
2019-12-29 22:31:32 +00:00
newNumber: 2,
2016-12-17 23:39:21 +00:00
},
{
2019-12-29 22:31:32 +00:00
content: '+test2r',
2019-10-12 21:45:49 +00:00
type: LineType.INSERT,
oldNumber: undefined,
2019-12-29 22:31:32 +00:00
newNumber: 3,
},
2016-12-17 23:39:21 +00:00
],
2019-10-12 21:45:49 +00:00
oldStartLine: 1,
oldStartLine2: undefined,
newStartLine: 1,
2019-12-29 22:31:32 +00:00
header: '@@ -1 +1 @@',
},
2016-12-17 23:39:21 +00:00
],
deletedLines: 1,
addedLines: 1,
2019-12-29 22:31:32 +00:00
checksumBefore: '0000001',
checksumAfter: '0ddf2ba',
oldName: 'sample',
language: 'txt',
newName: 'sample',
2019-10-12 21:45:49 +00:00
isCombined: false,
2019-12-29 22:31:32 +00:00
isGitDiff: true,
};
2019-10-12 21:45:49 +00:00
const html = lineByLineRenderer.generateFileHtml(file);
2019-12-22 19:52:51 +00:00
expect(html).toMatchInlineSnapshot(`
"<tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-info"></td>
<td class="d2h-info">
<div class="d2h-code-line">@@ -1 +1 @@</div>
2019-12-22 19:52:51 +00:00
</td>
</tr><tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-cntx">
<div class="line-num1">1</div>
<div class="line-num2">1</div>
2019-12-22 19:52:51 +00:00
</td>
2022-10-15 23:01:01 +00:00
<td class="d2h-cntx">
<div class="d2h-code-line">
<span class="d2h-code-line-prefix">&nbsp;</span>
<span class="d2h-code-line-ctn">one context line</span>
2019-12-22 19:52:51 +00:00
</div>
</td>
</tr><tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-del d2h-change">
<div class="line-num1">2</div>
<div class="line-num2"></div>
2019-12-22 19:52:51 +00:00
</td>
2022-10-15 23:01:01 +00:00
<td class="d2h-del d2h-change">
<div class="d2h-code-line">
<span class="d2h-code-line-prefix">-</span>
<span class="d2h-code-line-ctn"><del>test</del></span>
2019-12-22 19:52:51 +00:00
</div>
</td>
</tr><tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-ins d2h-change">
<div class="line-num1"></div>
<div class="line-num2">2</div>
2019-12-22 19:52:51 +00:00
</td>
2022-10-15 23:01:01 +00:00
<td class="d2h-ins d2h-change">
<div class="d2h-code-line">
<span class="d2h-code-line-prefix">+</span>
<span class="d2h-code-line-ctn"><ins>test1r</ins></span>
2019-12-22 19:52:51 +00:00
</div>
</td>
</tr><tr>
2022-10-15 23:01:01 +00:00
<td class="d2h-code-linenumber d2h-ins">
<div class="line-num1"></div>
<div class="line-num2">3</div>
2019-12-22 19:52:51 +00:00
</td>
2022-10-15 23:01:01 +00:00
<td class="d2h-ins">
<div class="d2h-code-line">
<span class="d2h-code-line-prefix">+</span>
<span class="d2h-code-line-ctn">test2r</span>
2019-12-22 19:52:51 +00:00
</div>
</td>
</tr>"
`);
});
});
});