diff2html/src/__tests__/printer-utils-tests.ts

153 lines
4.9 KiB
TypeScript
Raw Normal View History

2019-12-29 22:31:32 +00:00
import { escapeForHtml, getHtmlId, filenameDiff, diffHighlight } from '../render-utils';
import { DiffStyleType, LineMatchingType } from '../types';
2019-12-29 22:31:32 +00:00
describe('Utils', () => {
describe('escapeForHtml', () => {
it('should escape & with &', () => {
const result = escapeForHtml('&');
expect(result).toEqual('&');
2019-11-26 09:44:09 +00:00
});
2019-12-29 22:31:32 +00:00
it('should escape < with &lt;', () => {
const result = escapeForHtml('<');
expect(result).toEqual('&lt;');
2019-11-26 09:44:09 +00:00
});
2019-12-29 22:31:32 +00:00
it('should escape > with &gt;', () => {
const result = escapeForHtml('>');
expect(result).toEqual('&gt;');
2019-11-26 09:44:09 +00:00
});
it('should escape " with &quot;', () => {
const result = escapeForHtml('"');
2019-12-29 22:31:32 +00:00
expect(result).toEqual('&quot;');
2019-11-26 09:44:09 +00:00
});
it("should escape ' with &#x27;", () => {
const result = escapeForHtml("'");
2019-12-29 22:31:32 +00:00
expect(result).toEqual('&#x27;');
2019-11-26 09:44:09 +00:00
});
2019-12-29 22:31:32 +00:00
it('should escape / with &#x2F;', () => {
const result = escapeForHtml('/');
expect(result).toEqual('&#x2F;');
2019-11-26 09:44:09 +00:00
});
2019-12-29 22:31:32 +00:00
it('should escape a string containing HTML code', () => {
2019-11-26 09:44:09 +00:00
const result = escapeForHtml(`<a href="/search?q=diff2html">Search 'Diff2Html'</a>`);
expect(result).toEqual(
2019-12-29 22:31:32 +00:00
'&lt;a href=&quot;&#x2F;search?q=diff2html&quot;&gt;Search &#x27;Diff2Html&#x27;&lt;&#x2F;a&gt;',
2019-11-26 09:44:09 +00:00
);
});
});
2019-12-29 22:31:32 +00:00
describe('getHtmlId', () => {
it('should generate file unique id', () => {
2019-11-26 09:44:09 +00:00
const result = getHtmlId({
2019-12-29 22:31:32 +00:00
oldName: 'sample.js',
newName: 'sample.js',
});
2019-12-29 22:31:32 +00:00
expect(result).toEqual('d2h-960013');
});
});
2019-12-29 22:31:32 +00:00
describe('getDiffName', () => {
it('should generate the file name for a changed file', () => {
2019-11-26 09:44:09 +00:00
const result = filenameDiff({
2019-12-29 22:31:32 +00:00
oldName: 'sample.js',
newName: 'sample.js',
});
2019-12-29 22:31:32 +00:00
expect(result).toEqual('sample.js');
});
2019-12-29 22:31:32 +00:00
it('should generate the file name for a changed file and full rename', () => {
2019-11-26 09:44:09 +00:00
const result = filenameDiff({
2019-12-29 22:31:32 +00:00
oldName: 'sample1.js',
newName: 'sample2.js',
});
2019-12-29 22:31:32 +00:00
expect(result).toEqual('sample1.js → sample2.js');
});
2019-12-29 22:31:32 +00:00
it('should generate the file name for a changed file and prefix rename', () => {
2019-11-26 09:44:09 +00:00
const result = filenameDiff({
2019-12-29 22:31:32 +00:00
oldName: 'src/path/sample.js',
newName: 'source/path/sample.js',
});
2019-12-29 22:31:32 +00:00
expect(result).toEqual('{src → source}/path/sample.js');
});
2019-12-29 22:31:32 +00:00
it('should generate the file name for a changed file and suffix rename', () => {
2019-11-26 09:44:09 +00:00
const result = filenameDiff({
2019-12-29 22:31:32 +00:00
oldName: 'src/path/sample1.js',
newName: 'src/path/sample2.js',
});
2019-12-29 22:31:32 +00:00
expect(result).toEqual('src/path/{sample1.js → sample2.js}');
});
2019-12-29 22:31:32 +00:00
it('should generate the file name for a changed file and middle rename', () => {
2019-11-26 09:44:09 +00:00
const result = filenameDiff({
2019-12-29 22:31:32 +00:00
oldName: 'src/really/big/path/sample.js',
newName: 'src/small/path/sample.js',
});
2019-12-29 22:31:32 +00:00
expect(result).toEqual('src/{really/big → small}/path/sample.js');
});
2019-12-29 22:31:32 +00:00
it('should generate the file name for a deleted file', () => {
2019-11-26 09:44:09 +00:00
const result = filenameDiff({
2019-12-29 22:31:32 +00:00
oldName: 'src/my/file.js',
newName: '/dev/null',
});
2019-12-29 22:31:32 +00:00
expect(result).toEqual('src/my/file.js');
});
2019-12-29 22:31:32 +00:00
it('should generate the file name for a new file', () => {
2019-11-26 09:44:09 +00:00
const result = filenameDiff({
2019-12-29 22:31:32 +00:00
oldName: '/dev/null',
newName: 'src/my/file.js',
});
2019-12-29 22:31:32 +00:00
expect(result).toEqual('src/my/file.js');
});
});
2019-12-29 22:31:32 +00:00
describe('diffHighlight', () => {
it('should highlight two lines', () => {
const result = diffHighlight('-var myVar = 2;', '+var myVariable = 3;', false, {
matching: LineMatchingType.WORDS,
2019-10-12 21:45:49 +00:00
});
2019-10-12 21:45:49 +00:00
expect(result).toEqual({
oldLine: {
2019-12-29 22:31:32 +00:00
prefix: '-',
content: 'var <del>myVar</del> = <del>2</del>;',
},
2019-10-12 21:45:49 +00:00
newLine: {
2019-12-29 22:31:32 +00:00
prefix: '+',
content: 'var <ins>myVariable</ins> = <ins>3</ins>;',
},
2019-10-12 21:45:49 +00:00
});
});
2019-12-29 22:31:32 +00:00
it('should highlight two lines char by char', () => {
const result = diffHighlight('-var myVar = 2;', '+var myVariable = 3;', false, {
diffStyle: DiffStyleType.CHAR,
2019-10-21 22:37:42 +00:00
});
2019-12-22 19:52:51 +00:00
expect(result).toEqual({
2019-10-12 21:45:49 +00:00
oldLine: {
2019-12-29 22:31:32 +00:00
prefix: '-',
content: 'var myVar = <del>2</del>;',
},
2019-10-12 21:45:49 +00:00
newLine: {
2019-12-29 22:31:32 +00:00
prefix: '+',
content: 'var myVar<ins>iable</ins> = <ins>3</ins>;',
},
2019-12-22 19:52:51 +00:00
});
});
2019-12-29 22:31:32 +00:00
it('should highlight combined diff lines', () => {
const result = diffHighlight(' -var myVar = 2;', ' +var myVariable = 3;', true, {
2019-10-21 22:37:42 +00:00
diffStyle: DiffStyleType.WORD,
matching: LineMatchingType.WORDS,
2019-12-29 22:31:32 +00:00
matchWordsThreshold: 1.0,
});
2019-12-22 19:52:51 +00:00
expect(result).toEqual({
2019-10-12 21:45:49 +00:00
oldLine: {
2019-12-29 22:31:32 +00:00
prefix: ' -',
content: 'var <del class="d2h-change">myVar</del> = <del class="d2h-change">2</del>;',
},
2019-10-12 21:45:49 +00:00
newLine: {
2019-12-29 22:31:32 +00:00
prefix: ' +',
content: 'var <ins class="d2h-change">myVariable</ins> = <ins class="d2h-change">3</ins>;',
},
2019-12-22 19:52:51 +00:00
});
});
});
});