farmcontrol-api/src/templates/__tests__/templateformatter.test.js
Tom Butcher f467b1be33
All checks were successful
farmcontrol/farmcontrol-api/pipeline/head This commit looks good
Add formatting functionality for document templates
This update introduces a new route for formatting document templates, utilizing the `formatTemplateContent` function to beautify HTML and JavaScript within templates. The `formatDocumentTemplateRouteHandler` is added to handle formatting requests, and the content is processed to maintain EJS blocks. Additionally, a new `templateformatter.js` file is created, containing the logic for formatting templates, along with corresponding tests to ensure functionality. This enhancement improves the usability and presentation of document templates in the application.
2026-08-19 20:02:39 +01:00

53 lines
1.8 KiB
JavaScript

import { formatTemplateContent } from '../templateformatter.js';
describe('formatTemplateContent', () => {
it('should return an error when content is missing', () => {
expect(formatTemplateContent()).toEqual({
error: 'Content is required and must be a string.',
code: 400,
});
});
it('should return an error when content is not a string', () => {
expect(formatTemplateContent({ foo: 'bar' })).toEqual({
error: 'Content is required and must be a string.',
code: 400,
});
});
it('should return empty content unchanged', () => {
expect(formatTemplateContent('')).toEqual({ content: '' });
expect(formatTemplateContent(' ')).toEqual({ content: '' });
});
it('should format custom XML tags', () => {
const input = '<Container><Flex vertical="true"><Title1>Hello</Title1></Flex></Container>';
const result = formatTemplateContent(input);
expect(result.content).toBe(`<Container>
<Flex vertical="true">
<Title1>Hello</Title1>
</Flex>
</Container>`);
});
it('should format XML while preserving EJS blocks', () => {
const input =
'<Container><Flex vertical="true"><Title1>Hello</Title1><% if (name) { %><Text><%= name %></Text><% } %></Flex></Container>';
const result = formatTemplateContent(input);
expect(result.content).toContain('<Container>');
expect(result.content).toContain('<% if (name) { %>');
expect(result.content).toContain('<%= name %>');
expect(result.content).toContain('<% } %>');
expect(result.content).toContain('<Text>');
});
it('should leave EJS comments unchanged', () => {
const input = '<Container><%# comment %></Container>';
const result = formatTemplateContent(input);
expect(result.content).toContain('<%# comment %>');
});
});