Skip to content

Commit

Permalink
test(dom-to-react): assert replace with children
Browse files Browse the repository at this point in the history
Relates to #1126
  • Loading branch information
remarkablemark committed Oct 29, 2023
1 parent bf9e00d commit 57571aa
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions __tests__/dom-to-react.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import htmlToDOM from 'html-dom-parser';

import domToReact from '../src/dom-to-react';
import * as utilities from '../src/utilities';
import { Element } from '../src';
import { Element, type DOMNode, type HTMLReactParserOptions } from '../src';

import { render } from './helpers';
import { html, svg } from './data';
Expand Down Expand Up @@ -144,7 +144,7 @@ describe('domToReact', () => {
});
});

describe('domToReact with library option', () => {
describe('library option', () => {
const React = require('react');
const Preact = require('preact');

Expand All @@ -171,7 +171,7 @@ describe('domToReact with library option', () => {
});
});

describe('domToReact replace option', () => {
describe('replace option', () => {
it("does not set key if there's a single node", () => {
const reactElement = domToReact(htmlToDOM(html.single), {
replace: () => <div />,
Expand Down Expand Up @@ -208,9 +208,26 @@ describe('domToReact replace option', () => {
expect(reactElements[0].key).toBe('0');
expect(reactElements[1].key).toBe('myKey');
});

it('replaces with children', () => {
const options: HTMLReactParserOptions = {
replace(domNode) {
if (domNode instanceof Element) {
return <>{domToReact(domNode.children as DOMNode[], options)}</>;
}
},
};

const reactElement = domToReact(
htmlToDOM(html.single),
options,
) as JSX.Element;

expect(reactElement).toBeInstanceOf(Object);
});
});

describe('domToReact transform option', () => {
describe('transform option', () => {
it('can wrap all elements', () => {
const reactElement = domToReact(htmlToDOM(html.list), {
transform: (reactNode, domNode, index) => {
Expand Down

0 comments on commit 57571aa

Please sign in to comment.