CommunitySolidServer/test/unit/util/templates/TemplateEngine.test.ts
Jasper Vaneessen fe39f97ee0
refactor: Use fs-extra instead of fs to simplify file access
* refactor: use fs-extra instead of fs

* tests: manual mocks for fs-extra base + ensureDir

* refactor: mockFileSystem + mockFs and mockFsExtra

* add remove mock and some further test tweaks

* test: FileDataAccessor tests passing

* refactor: remove try-catch due to fs-extra handlin

* refactor: fs-extra in atomicFileDataAccessor

* refactor: AtomicFileDataAccessor fs-extra

* test: fix coverage

* refactor: use read/writeJson from fs-extra

* refactor: less duplicate mocking code

* refactor: re-use opendir mocking code
2022-04-12 11:02:30 +02:00

78 lines
2.7 KiB
TypeScript

import { resolveAssetPath } from '../../../../src/util/PathUtil';
import { getTemplateFilePath, readTemplate } from '../../../../src/util/templates/TemplateEngine';
import { mockFileSystem } from '../../../util/Util';
jest.mock('fs');
describe('TemplateEngine', (): void => {
describe('#getTemplateFilePath', (): void => {
const templateFile = 'template.xyz';
const templatePath = 'other';
beforeEach(async(): Promise<void> => {
const { data } = mockFileSystem(resolveAssetPath(''));
Object.assign(data, {
'template.xyz': '{{template}}',
other: {
'template.xyz': '{{other}}',
},
});
});
it('returns the undefined when no template is provided.', async(): Promise<void> => {
expect(getTemplateFilePath()).toBeUndefined();
});
it('returns the input if it was a filename.', async(): Promise<void> => {
expect(getTemplateFilePath(templateFile)).toBe(resolveAssetPath(templateFile));
});
it('returns undefined for options with a string template.', async(): Promise<void> => {
expect(getTemplateFilePath({ templateString: 'abc' })).toBeUndefined();
});
it('accepts options with a filename.', async(): Promise<void> => {
expect(getTemplateFilePath({ templateFile })).toBe(resolveAssetPath(templateFile));
});
it('accepts options with a filename and a path.', async(): Promise<void> => {
expect(getTemplateFilePath({ templateFile, templatePath })).toBe(resolveAssetPath('other/template.xyz'));
});
});
describe('#readTemplate', (): void => {
const templateFile = 'template.xyz';
const templatePath = 'other';
beforeEach(async(): Promise<void> => {
const { data } = mockFileSystem(resolveAssetPath(''));
Object.assign(data, {
'template.xyz': '{{template}}',
other: {
'template.xyz': '{{other}}',
},
});
});
it('returns the empty string when no template is provided.', async(): Promise<void> => {
await expect(readTemplate()).resolves.toBe('');
});
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 options with a filename.', async(): Promise<void> => {
await expect(readTemplate({ templateFile })).resolves.toBe('{{template}}');
});
it('accepts options with a filename and a path.', async(): Promise<void> => {
await expect(readTemplate({ templateFile, templatePath })).resolves.toBe('{{other}}');
});
});
});