import {
describe,
expect,
it,
} from '@monochromatic-dev/module-test/ts';
import { $, } from './index.ts';
await describe({
name: $.name,
children: [
it({
name: 'creates a simple element with text content',
fn: async () => {
expect($({ tag: 'title', text: 'My Feed', },),).toBe('
My Feed',);
},
},),
it({
name: 'self-closes elements with no content',
fn: async () => {
expect($({ tag: 'link', },),).toBe('',);
},
},),
it({
name: 'self-closes elements with attrs but no content',
fn: async () => {
expect($({ tag: 'link', attrs: { href: 'https://example.com', }, },),).toBe(
'',
);
},
},),
it({
name: 'does not self-close when text is provided',
fn: async () => {
expect($({ tag: 'name', text: '', },),).toBe('',);
},
},),
it({
name: 'does not self-close when raw is provided',
fn: async () => {
expect($({ tag: 'content', raw: '', },),).toBe('',);
},
},),
it({
name: 'does not self-close when children array is non-empty',
fn: async () => {
expect($({ tag: 'items', children: [$({ tag: 'item', text: 'a', },),], },),).toBe(
'- a
',
);
},
},),
it({
name: 'self-closes when children array is empty',
fn: async () => {
expect($({ tag: 'items', children: [], },),).toBe('',);
},
},),
it({
name: 'escapes text content',
fn: async () => {
expect($({ tag: 'content', text: 'x < y & z > w', },),).toBe(
'x < y & z > w',
);
},
},),
it({
name: 'escapes double quotes in text',
fn: async () => {
expect($({ tag: 'title', text: 'say "hello"', },),).toBe(
'say "hello"',
);
},
},),
it({
name: 'escapes single quotes (apostrophes) in text',
fn: async () => {
expect($({ tag: 'title', text: "it's", },),).toBe(
'it's',
);
},
},),
it({
name: 'escapes attribute values',
fn: async () => {
expect($({ tag: 'link', attrs: { href: 'a&b=c',
);
},
},),
it({
name: 'does not escape raw content',
fn: async () => {
const inner = 'raw & unescaped';
expect($({ tag: 'wrapper', raw: inner, },),).toBe(
`${inner}`,
);
},
},),
it({
name: 'renders multiple attributes in order',
fn: async () => {
expect($({
tag: 'entry',
attrs: { id: '1', type: 'post', },
},),)
.toBe('',);
},
},),
it({
name: 'renders namespaced tag names',
fn: async () => {
expect($({ tag: 'atom:title', text: 'Feed', },),).toBe(
'Feed',
);
},
},),
it({
name: 'renders namespaced attributes',
fn: async () => {
expect($({
tag: 'feed',
attrs: { 'xmlns:atom': 'http://www.w3.org/2005/Atom', },
children: [$({ tag: 'atom:title', text: 'Feed', },),],
},),)
.toBe(
'Feed',
);
},
},),
it({
name: 'concatenates text, raw, and children in order',
fn: async () => {
expect($({
tag: 'mixed',
text: 'hello',
raw: 'world',
children: ['!',],
},),)
.toBe('helloworld!',);
},
},),
it({
name: 'concatenates multiple children',
fn: async () => {
expect($({
tag: 'list',
children: [
$({ tag: 'item', text: 'a', },),
$({ tag: 'item', text: 'b', },),
$({ tag: 'item', text: 'c', },),
],
},),)
.toBe('- a
- b
- c
',);
},
},),
it({
name: 'handles deeply nested elements',
fn: async () => {
const result = $({
tag: 'root',
children: [
$({
tag: 'parent',
children: [
$({ tag: 'child', text: 'leaf', },),
],
},),
],
},);
expect(result,).toBe('leaf',);
},
},),
it({
name: 'handles RSS-like structure',
fn: async () => {
const result = $({
tag: 'rss',
attrs: { version: '2.0', },
children: [
$({
tag: 'channel',
children: [
$({ tag: 'title', text: 'My Blog', },),
$({ tag: 'link', attrs: { href: 'https://example.com', }, },),
$({
tag: 'item',
children: [
$({ tag: 'title', text: 'First Post', },),
$({ tag: 'description', text: 'Hello ', },),
],
},),
],
},),
],
},);
expect(result,).toBe(
'My Blog- First PostHello <world>
',
);
},
},),
],
},);