import { escapeForHtml, getHtmlId, filenameDiff, diffHighlight } from '../render-utils'; import { DiffStyleType, LineMatchingType } from '../types'; describe('Utils', () => { describe('escapeForHtml', () => { it('should escape & with &', () => { const result = escapeForHtml('&'); expect(result).toEqual('&'); }); it('should escape < with <', () => { const result = escapeForHtml('<'); expect(result).toEqual('<'); }); it('should escape > with >', () => { const result = escapeForHtml('>'); expect(result).toEqual('>'); }); it('should escape " with "', () => { const result = escapeForHtml('"'); expect(result).toEqual('"'); }); it("should escape ' with '", () => { const result = escapeForHtml("'"); expect(result).toEqual('''); }); it('should escape / with /', () => { const result = escapeForHtml('/'); expect(result).toEqual('/'); }); it('should escape a string containing HTML code', () => { const result = escapeForHtml(`Search 'Diff2Html'`); expect(result).toEqual( '<a href="/search?q=diff2html">Search 'Diff2Html'</a>', ); }); }); describe('getHtmlId', () => { it('should generate file unique id', () => { const result = getHtmlId({ oldName: 'sample.js', newName: 'sample.js', }); expect(result).toEqual('d2h-960013'); }); }); describe('getDiffName', () => { it('should generate the file name for a changed file', () => { const result = filenameDiff({ oldName: 'sample.js', newName: 'sample.js', }); expect(result).toEqual('sample.js'); }); it('should generate the file name for a changed file and full rename', () => { const result = filenameDiff({ oldName: 'sample1.js', newName: 'sample2.js', }); expect(result).toEqual('sample1.js → sample2.js'); }); it('should generate the file name for a changed file and prefix rename', () => { const result = filenameDiff({ oldName: 'src/path/sample.js', newName: 'source/path/sample.js', }); expect(result).toEqual('{src → source}/path/sample.js'); }); it('should generate the file name for a changed file and suffix rename', () => { const result = filenameDiff({ oldName: 'src/path/sample1.js', newName: 'src/path/sample2.js', }); expect(result).toEqual('src/path/{sample1.js → sample2.js}'); }); it('should generate the file name for a changed file and middle rename', () => { const result = filenameDiff({ oldName: 'src/really/big/path/sample.js', newName: 'src/small/path/sample.js', }); expect(result).toEqual('src/{really/big → small}/path/sample.js'); }); it('should generate the file name for a deleted file', () => { const result = filenameDiff({ oldName: 'src/my/file.js', newName: '/dev/null', }); expect(result).toEqual('src/my/file.js'); }); it('should generate the file name for a new file', () => { const result = filenameDiff({ oldName: '/dev/null', newName: 'src/my/file.js', }); expect(result).toEqual('src/my/file.js'); }); }); describe('diffHighlight', () => { it('should highlight two lines', () => { const result = diffHighlight('-var myVar = 2;', '+var myVariable = 3;', false, { matching: LineMatchingType.WORDS, }); expect(result).toEqual({ oldLine: { prefix: '-', content: 'var myVar = 2;', }, newLine: { prefix: '+', content: 'var myVariable = 3;', }, }); }); it('should highlight two lines char by char', () => { const result = diffHighlight('-var myVar = 2;', '+var myVariable = 3;', false, { diffStyle: DiffStyleType.CHAR, }); expect(result).toEqual({ oldLine: { prefix: '-', content: 'var myVar = 2;', }, newLine: { prefix: '+', content: 'var myVariable = 3;', }, }); }); it('should highlight combined diff lines', () => { const result = diffHighlight(' -var myVar = 2;', ' +var myVariable = 3;', true, { diffStyle: DiffStyleType.WORD, matching: LineMatchingType.WORDS, matchWordsThreshold: 1.0, }); expect(result).toEqual({ oldLine: { prefix: ' -', content: 'var myVar = 2;', }, newLine: { prefix: ' +', content: 'var myVariable = 3;', }, }); }); }); });