mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00

The previous package was outdated, preventing us from updating TS. This one also lints YAML and JSON, and applies many more rules to the test files, explaining all the changes in this PR.
48 lines
2.3 KiB
TypeScript
48 lines
2.3 KiB
TypeScript
import { NotFoundHttpError, StaticTemplateEngine } from '../../../../src';
|
|
import type { AsyncHandler, TemplateEngineInput } from '../../../../src';
|
|
import Dict = NodeJS.Dict;
|
|
|
|
describe('A StaticTemplateEngine', (): void => {
|
|
let templateEngine: jest.Mocked<AsyncHandler<TemplateEngineInput<Dict<any>>, string>>;
|
|
|
|
it('forwards calls to the handle method of the provided templateEngine, adding the template as an argument.',
|
|
async(): Promise<void> => {
|
|
templateEngine = {
|
|
canHandle: jest.fn(),
|
|
handle: jest.fn().mockResolvedValue(''),
|
|
} as any;
|
|
const input = { contents: {}};
|
|
const engine = new StaticTemplateEngine(templateEngine, 'template');
|
|
await expect(engine.handleSafe(input)).resolves.toBe('');
|
|
expect(templateEngine.canHandle).toHaveBeenCalledTimes(1);
|
|
expect(templateEngine.canHandle).toHaveBeenLastCalledWith({ contents: {}, template: 'template' });
|
|
expect(templateEngine.handle).toHaveBeenCalledTimes(1);
|
|
expect(templateEngine.handle).toHaveBeenLastCalledWith({ contents: {}, template: 'template' });
|
|
});
|
|
|
|
it('propagates errors that occur in the handle method of the provided handler.', async(): Promise<void> => {
|
|
templateEngine = {
|
|
canHandle: jest.fn(),
|
|
handle: jest.fn().mockRejectedValue(new NotFoundHttpError()),
|
|
} as any;
|
|
const input = { contents: {}};
|
|
const engine = new StaticTemplateEngine(templateEngine, 'template');
|
|
await expect(engine.handleSafe(input)).rejects.toThrow(NotFoundHttpError);
|
|
expect(templateEngine.canHandle).toHaveBeenCalledTimes(1);
|
|
expect(templateEngine.canHandle).toHaveBeenLastCalledWith({ contents: input.contents, template: 'template' });
|
|
expect(templateEngine.handle).toHaveBeenCalledTimes(1);
|
|
expect(templateEngine.handle).toHaveBeenLastCalledWith({ contents: input.contents, template: 'template' });
|
|
});
|
|
|
|
it('results in an error when calling handle with template defined.', async(): Promise<void> => {
|
|
templateEngine = {
|
|
canHandle: jest.fn(),
|
|
handle: jest.fn().mockResolvedValue(''),
|
|
} as any;
|
|
const input = { contents: {}, template: 'template2' };
|
|
const engine = new StaticTemplateEngine(templateEngine, 'template1');
|
|
await expect(engine.handleSafe(input)).rejects
|
|
.toThrow('StaticTemplateEngine does not support template as handle input');
|
|
});
|
|
});
|