refactor(xml-formatter): improved unit tests

This commit is contained in:
Corentin Thomasset 2023-06-18 12:13:48 +02:00
parent 18e2010b21
commit ddd191e35d
No known key found for this signature in database
GPG key ID: DBD997E935996158
3 changed files with 61 additions and 33 deletions

View file

@ -1,18 +1,27 @@
import { describe, expect, it } from 'vitest'; import { describe, expect, it } from 'vitest';
import { formatXml } from './xml-formatter.service'; import { formatXml } from './xml-formatter.service';
const options = { describe('xml-formatter service', () => {
indentation: ' ', describe('formatXml', () => {
collapseContent: true, it('converts XML into a human readable format', () => {
lineSeparator: '\n',
};
const initString = '<hello><world>foo</world><world>bar</world></hello>'; const initString = '<hello><world>foo</world><world>bar</world></hello>';
const endString = `<hello>
<world>foo</world> expect(formatXml(initString)).toMatchInlineSnapshot(`
<world>bar</world> "<hello>
</hello>`; <world>
describe('xml-formatter', () => { foo
it('Should generate same string', () => { </world>
expect(formatXml(initString, options)).toEqual(endString); <world>
bar
</world>
</hello>"
`);
});
it('returns an empty string if the input is not valid XML', () => {
const initString = 'hello world';
expect(formatXml(initString)).toEqual('');
});
}); });
}); });

View file

@ -1,5 +1,28 @@
import xmlFormat, { type XMLFormatterOptions } from 'xml-formatter'; import xmlFormat, { type XMLFormatterOptions } from 'xml-formatter';
import { withDefaultOnError } from '@/utils/defaults';
export function formatXml(value: string, options?: XMLFormatterOptions): string { export { formatXml, isValidXML };
return xmlFormat(value, options) || '';
function cleanRawXml(rawXml: string): string {
return rawXml.trim();
}
function formatXml(rawXml: string, options?: XMLFormatterOptions): string {
return withDefaultOnError(() => xmlFormat(cleanRawXml(rawXml), options) ?? '', '');
}
function isValidXML(rawXml: string): boolean {
const cleanedRawXml = cleanRawXml(rawXml);
if (cleanedRawXml === '') {
return true;
}
try {
xmlFormat(cleanedRawXml);
return true;
}
catch (e) {
return false;
}
} }

View file

@ -1,41 +1,37 @@
<script setup lang="ts"> <script setup lang="ts">
import { formatXml } from './xml-formatter.service'; import { formatXml, isValidXML } from './xml-formatter.service';
import type { UseValidationRule } from '@/composable/validation'; import type { UseValidationRule } from '@/composable/validation';
import { isNotThrowing } from '@/utils/boolean';
import { withDefaultOnError } from '@/utils/defaults';
const defaultValue = '<hello><world>foo</world><world>bar</world></hello>'; const defaultValue = '<hello><world>foo</world><world>bar</world></hello>';
const indentSize = useStorage('xml-formatter:indent-size', 2); const indentSize = useStorage('xml-formatter:indent-size', 2);
const collapseContent = useStorage('xml-formatter:collapse-content', true); const collapseContent = useStorage('xml-formatter:collapse-content', true);
function transformer(value: string) { function transformer(value: string) {
return withDefaultOnError(() => { return formatXml(value, {
const def = {
indentation: ' '.repeat(indentSize.value), indentation: ' '.repeat(indentSize.value),
collapseContent: collapseContent.value, collapseContent: collapseContent.value,
lineSeparator: '\n\r', lineSeparator: '\n',
}; });
return formatXml(value, def);
}, '');
} }
const rules: UseValidationRule<string>[] = [ const rules: UseValidationRule<string>[] = [
{ {
validator: (value: string) => isNotThrowing(() => formatXml(value)), validator: isValidXML,
message: 'Provided XML is not valid.', message: 'Provided XML is not valid.',
}, },
]; ];
</script> </script>
<template> <template>
<div style="flex: 0 0 100%"> <div important:flex-full important:flex-shrink-0 important:flex-grow-0>
<n-space style="margin: 0 auto; max-width: 600px" justify="center"> <div flex justify-center>
<n-form-item label="Collapse Content :" label-placement="left" label-width="100"> <n-form-item label="Collapse content:" label-placement="left">
<n-switch v-model:value="collapseContent" /> <n-switch v-model:value="collapseContent" />
</n-form-item> </n-form-item>
<n-form-item label="Indent size:" label-placement="left" label-width="100" :show-feedback="false"> <n-form-item label="Indent size:" label-placement="left" label-width="100" :show-feedback="false">
<n-input-number v-model:value="indentSize" min="0" max="10" style="width: 100px" /> <n-input-number v-model:value="indentSize" min="0" max="10" w-100px />
</n-form-item> </n-form-item>
</n-space> </div>
</div> </div>
<format-transformer <format-transformer