import { describe, expect, it } from 'vitest';
import type { ReactElement } from 'react';
import {
concatChildren,
createElement,
RawHTML,
renderToString,
switchChildrenNodeName,
} from '../';
describe( 'element', () => {
describe( 'renderToString', () => {
it( 'should return an empty string for booleans/null/undefined values', () => {
expect(
// @ts-expect-error renderToString requires an element argument; missing input serializes to an empty string.
renderToString()
).toBe( '' );
expect( renderToString( false ) ).toBe( '' );
expect( renderToString( true ) ).toBe( '' );
expect( renderToString( null ) ).toBe( '' );
} );
it( 'should return a string 0', () => {
expect( renderToString( 0 ) ).toBe( '0' );
} );
it( 'should return a string 12345', () => {
expect( renderToString( 12345 ) ).toBe( '12345' );
} );
it( 'should return a string verbatim', () => {
expect( renderToString( 'Zucchini' ) ).toBe( 'Zucchini' );
} );
it( 'should return a string from an array', () => {
expect(
renderToString( [
'Zucchini ',
createElement( 'em', null, 'is a' ),
' summer squash',
] )
).toBe( 'Zucchini is a summer squash' );
} );
it( 'should return a string from an element', () => {
expect(
renderToString( createElement( 'strong', null, 'Courgette' ) )
).toBe( 'Courgette' );
} );
it( 'should escape attributes and html', () => {
const result = renderToString(
createElement(
'a',
{
href: '/index.php?foo=bar&qux=<"scary">',
style: {
backgroundColor: 'red',
},
},
'<"WordPress" & Friends>'
)
);
expect( result ).toBe(
'' +
'<"WordPress" & Friends>' +
''
);
} );
it( 'strips raw html wrapper', () => {
const html = '
So scary!
';
expect( renderToString( { html } ) ).toBe(
html
);
} );
} );
describe( 'concatChildren', () => {
it( 'should return an empty array for undefined children', () => {
expect( concatChildren() ).toEqual( [] );
} );
it( 'should concat the string arrays', () => {
expect(
concatChildren(
[ 'a' ],
// @ts-expect-error concatChildren expects an array; a scalar string is accepted at runtime.
'b'
)
).toEqual( [ 'a', 'b' ] );
} );
it( 'should concat the object arrays and rewrite keys', () => {
const concat = concatChildren(
[ createElement( 'strong', {}, 'Courgette' ) ],
// @ts-expect-error concatChildren expects an array; a single element is accepted at runtime.
createElement( 'strong', {}, 'Concombre' )
) as ReactElement[];
expect( concat ).toHaveLength( 2 );
expect( concat[ 0 ].key ).toBe( '0,0' );
expect( concat[ 1 ].key ).toBe( '1,0' );
} );
} );
describe( 'switchChildrenNodeName', () => {
it( 'should return undefined for undefined children', () => {
expect(
// @ts-expect-error switchChildrenNodeName requires children and nodeName arguments; missing input returns undefined.
switchChildrenNodeName()
).toBeUndefined();
} );
it( 'should switch strings', () => {
const children = switchChildrenNodeName(
[ 'a', 'b' ],
'strong'
) as ReactElement< { children: string } >[];
expect( children ).toHaveLength( 2 );
expect( children[ 0 ].type ).toBe( 'strong' );
expect( children[ 0 ].props.children ).toBe( 'a' );
expect( children[ 1 ].type ).toBe( 'strong' );
expect( children[ 1 ].props.children ).toBe( 'b' );
} );
it( 'should switch elements', () => {
const children = switchChildrenNodeName(
[
createElement( 'strong', { align: 'left' }, 'Courgette' ),
createElement( 'strong', {}, 'Concombre' ),
],
'em'
) as ReactElement< { children: string; align?: string } >[];
expect( children ).toHaveLength( 2 );
expect( children[ 0 ].type ).toBe( 'em' );
expect( children[ 0 ].props.children ).toBe( 'Courgette' );
expect( children[ 0 ].props.align ).toBe( 'left' );
expect( children[ 1 ].type ).toBe( 'em' );
expect( children[ 1 ].props.children ).toBe( 'Concombre' );
} );
} );
} );