refactor: Align EJS engine with Handlebars.

This commit is contained in:
Ruben Verborgh
2021-07-20 19:39:28 +02:00
committed by Joachim Van Herwegen
parent 19624dc729
commit 9628fe98b8
40 changed files with 215 additions and 266 deletions

View File

@@ -0,0 +1,23 @@
import { EjsTemplateEngine } from '../../../../src/util/templates/EjsTemplateEngine';
jest.mock('../../../../src/util/templates/TemplateEngine', (): any => ({
readTemplate: jest.fn(async({ templateString }): Promise<string> => `${templateString}: <%= detail %>`),
}));
describe('A EjsTemplateEngine', (): void => {
const defaultTemplate = { templateString: 'xyz' };
const contents = { detail: 'a&b' };
let templateEngine: EjsTemplateEngine;
beforeEach((): void => {
templateEngine = new EjsTemplateEngine(defaultTemplate);
});
it('uses the default template when no template was passed.', async(): Promise<void> => {
await expect(templateEngine.render(contents)).resolves.toBe('xyz: a&amp;b');
});
it('uses the passed template.', async(): Promise<void> => {
await expect(templateEngine.render(contents, { templateString: 'my' })).resolves.toBe('my: a&amp;b');
});
});

View File

@@ -22,15 +22,19 @@ describe('readTemplate', (): void => {
await expect(readTemplate()).resolves.toBe('');
});
it('accepts string templates.', async(): Promise<void> => {
it('accepts a filename.', async(): Promise<void> => {
await expect(readTemplate(templateFile)).resolves.toBe('{{template}}');
});
it('accepts options with a string template.', async(): Promise<void> => {
await expect(readTemplate({ templateString: 'abc' })).resolves.toBe('abc');
});
it('accepts a filename.', async(): Promise<void> => {
it('accepts options with a filename.', async(): Promise<void> => {
await expect(readTemplate({ templateFile })).resolves.toBe('{{template}}');
});
it('accepts a filename and path.', async(): Promise<void> => {
it('accepts options with a filename and a path.', async(): Promise<void> => {
await expect(readTemplate({ templateFile, templatePath })).resolves.toBe('{{other}}');
});
});