diff2html/src/__tests__/hogan-cache-tests.ts

57 lines
2.3 KiB
TypeScript
Raw Normal View History

2019-12-29 22:31:32 +00:00
import HoganJsUtils from '../hoganjs-utils';
import { CSSLineClass } from '../render-utils';
2019-10-12 21:45:49 +00:00
2019-12-29 22:31:32 +00:00
describe('HoganJsUtils', () => {
describe('render', () => {
it('should render view', () => {
2019-10-12 21:45:49 +00:00
const hoganJsUtils = new HoganJsUtils({});
2019-12-29 22:31:32 +00:00
const result = hoganJsUtils.render('generic', 'empty-diff', {
contentClass: 'd2h-code-line',
CSSLineClass: CSSLineClass,
2019-10-12 21:45:49 +00:00
});
2019-12-22 19:52:51 +00:00
expect(result).toMatchInlineSnapshot(`
"<tr>
<td class=\\"d2h-info\\">
<div class=\\"d2h-code-line d2h-info\\">
File without changes
</div>
</td>
</tr>"
`);
2019-10-12 21:45:49 +00:00
});
2019-12-29 22:31:32 +00:00
it('should throw exception if template is missing', () => {
2019-10-12 21:45:49 +00:00
const hoganJsUtils = new HoganJsUtils({});
2019-12-29 22:31:32 +00:00
expect(() => hoganJsUtils.render('generic', 'missing-template', {})).toThrow(Error);
2019-10-12 21:45:49 +00:00
});
2019-12-29 22:31:32 +00:00
it('should allow templates to be overridden with compiled templates', () => {
const emptyDiffTemplate = HoganJsUtils.compile('<p>{{myName}}</p>');
const hoganJsUtils = new HoganJsUtils({ compiledTemplates: { 'generic-empty-diff': emptyDiffTemplate } });
2019-10-12 21:45:49 +00:00
2019-12-29 22:31:32 +00:00
const result = hoganJsUtils.render('generic', 'empty-diff', { myName: 'Rodrigo Fernandes' });
2019-12-22 19:52:51 +00:00
expect(result).toMatchInlineSnapshot(`"<p>Rodrigo Fernandes</p>"`);
2019-10-12 21:45:49 +00:00
});
2019-12-29 22:31:32 +00:00
it('should allow templates to be overridden with uncompiled templates', () => {
const emptyDiffTemplate = '<p>{{myName}}</p>';
const hoganJsUtils = new HoganJsUtils({ rawTemplates: { 'generic-empty-diff': emptyDiffTemplate } });
2019-10-12 21:45:49 +00:00
2019-12-29 22:31:32 +00:00
const result = hoganJsUtils.render('generic', 'empty-diff', { myName: 'Rodrigo Fernandes' });
2019-12-22 19:52:51 +00:00
expect(result).toMatchInlineSnapshot(`"<p>Rodrigo Fernandes</p>"`);
2019-10-12 21:45:49 +00:00
});
2019-12-29 22:31:32 +00:00
it('should allow templates to be overridden giving priority to raw templates', () => {
const emptyDiffTemplate = HoganJsUtils.compile('<p>Not used!</p>');
const emptyDiffTemplateUncompiled = '<p>{{myName}}</p>';
2019-10-12 21:45:49 +00:00
const hoganJsUtils = new HoganJsUtils({
2019-12-29 22:31:32 +00:00
compiledTemplates: { 'generic-empty-diff': emptyDiffTemplate },
rawTemplates: { 'generic-empty-diff': emptyDiffTemplateUncompiled },
2019-10-12 21:45:49 +00:00
});
2019-12-29 22:31:32 +00:00
const result = hoganJsUtils.render('generic', 'empty-diff', { myName: 'Rodrigo Fernandes' });
2019-12-22 19:52:51 +00:00
expect(result).toMatchInlineSnapshot(`"<p>Rodrigo Fernandes</p>"`);
2019-10-12 21:45:49 +00:00
});
});
});