import { Element } from 'domhandler';
import render from 'dom-serializer';
import parseDocument from '../parseDocument';
function expectRendersHtml(doc: any, target: string) {
expect(render(doc)).toBe(target);
}
describe('DOMParser', () => {
it('should parse', () => {
const html = '
';
expectRendersHtml(parseDocument(html), html);
});
describe('ignoreTags option', () => {
it('should support ignoredTags', () => {
const doc = parseDocument('
', {
ignoredTags: ['span']
});
expectRendersHtml(doc, '');
});
it('should ignore children of ignoredTags', () => {
const doc = parseDocument(
'Text node!
',
{
ignoredTags: ['span']
}
);
expectRendersHtml(doc, '');
});
it('should include siblings of ignoredTags', () => {
const doc = parseDocument(
'Text
',
{
ignoredTags: ['span']
}
);
expectRendersHtml(doc, 'Text
');
});
});
describe('ignoreNode option', () => {
it('should ignore element nodes', () => {
const doc = parseDocument('
', {
ignoreNode: (node) => (node as Element).name === 'span'
});
expectRendersHtml(doc, '');
});
it('should ignore text nodes', () => {
const doc = parseDocument('Text!
', {
ignoreNode: (node) => node.type === 'text'
});
expectRendersHtml(doc, '');
});
it('should ignore children elements of ignored nodes', () => {
const doc = parseDocument(
'Text node!
',
{
ignoreNode: (node) => (node as Element).name === 'span'
}
);
expectRendersHtml(doc, '');
});
it('should provide parent in ignoreNode', () => {
const html = '
';
const ignoreNode = jest.fn((node, parent) => {
expect(parent).not.toBeNull();
return false;
});
parseDocument(html, {
ignoreNode
});
});
it('should retain sibling text nodes of ignored nodes', () => {
const doc = parseDocument(
'',
{
ignoreNode: (node) => (node as Element).name === 'a'
}
);
expectRendersHtml(
doc,
'Can you see the anchor? It has been ignored!
'
);
});
it('should retain next siblings elements of ignored nodes', () => {
const doc = parseDocument(
'Text
',
{
ignoreNode: (node) => (node as Element).name === 'span'
}
);
expectRendersHtml(doc, 'Text
');
});
it('should retain previous siblings elements of ignored nodes', () => {
const html = '';
const doc = parseDocument(html, {
ignoreNode: (node) => (node as Element).name === 'a'
});
expectRendersHtml(doc, ' B
');
});
});
describe('visitors option', () => {
it('should support visitors.onElement', () => {
const onElement = jest.fn();
parseDocument('Text
', {
visitors: {
onElement
}
});
expect(onElement).toHaveBeenCalledTimes(3);
});
it('should support visitors.onText', () => {
const onText = jest.fn();
parseDocument('Text
', {
visitors: { onText }
});
expect(onText).toHaveBeenCalledTimes(1);
});
it('should support visitors.onDocument', () => {
const onDocument = jest.fn();
parseDocument('', {
visitors: { onDocument }
});
expect(onDocument).toHaveBeenCalledTimes(1);
});
it('should call visitor.onElement when the element children have been parsed', () => {
parseDocument('Text
', {
visitors: {
onElement(element) {
if (element.tagName === 'div') {
expect(element.children).toHaveLength(3);
}
}
}
});
});
it('should call visitor.onDocument when the document children have been parsed', () => {
parseDocument('Text', {
visitors: {
onDocument(document) {
expect(document.children).toHaveLength(3);
}
}
});
});
});
});