// @vitest-environment jsdom
import { describe, expect, it } from 'vitest';
import { isValidXML, parseXml } from './xml-diff.parser';
describe('xml-diff parser', () => {
describe('parseXml', () => {
it('returns undefined for an empty string', () => {
expect(parseXml('')).toBeUndefined();
expect(parseXml(' ')).toBeUndefined();
});
it('returns undefined for invalid xml', () => {
expect(parseXml('')).toBeUndefined();
});
it('parses a leaf element to its text content', () => {
expect(parseXml('bar')).toEqual('bar');
});
it('parses an empty element to an empty string', () => {
expect(parseXml('')).toEqual('');
});
it('parses attributes with an "@" prefix', () => {
expect(parseXml('')).toEqual({ '@a': '1', '@b': '2' });
});
it('parses a single child element as a plain value', () => {
expect(parseXml('- A
')).toEqual({ item: 'A' });
});
it('parses repeated child elements as an array', () => {
expect(parseXml('- A
- B
')).toEqual({ item: ['A', 'B'] });
});
it('combines attributes, text and children', () => {
expect(parseXml('helloworld')).toEqual({
'@id': '1',
'#text': 'hello',
child: 'world',
});
});
it('ignores insignificant whitespace between tags', () => {
const withoutWhitespace = parseXml('baz');
const withWhitespace = parseXml('\n baz\n');
expect(withWhitespace).toEqual(withoutWhitespace);
});
});
describe('isValidXML', () => {
it('accepts an empty string', () => {
expect(isValidXML('')).toBe(true);
});
it('accepts valid xml', () => {
expect(isValidXML('bar')).toBe(true);
});
it('rejects invalid xml', () => {
expect(isValidXML('')).toBe(false);
});
});
});