import type { CheerioRoot } from '@crawlee/utils';
import { htmlToText } from '@crawlee/utils';
import * as cheerio from 'cheerio';
import * as htmlToTextData from '../shared/data/html_to_text_test_data';
const checkHtmlToText = (html: string | CheerioRoot, expectedText: string, hasBody = false) => {
const text1 = htmlToText(html);
expect(text1).toEqual(expectedText);
// Test embedding into
gives the same result
if (typeof html === 'string' && !hasBody) {
const html2 = `
Title should be ignored
${html}
`;
const text2 = htmlToText(html2);
expect(text2).toEqual(expectedText);
}
};
describe('htmlToText()', () => {
test('handles invalid args', () => {
// @ts-expect-error invalid input type
checkHtmlToText(null, '');
checkHtmlToText('', '');
// @ts-expect-error invalid input type
checkHtmlToText(0, '');
// @ts-expect-error invalid input type
checkHtmlToText(undefined, '');
});
test('handles basic HTML elements correctly', () => {
checkHtmlToText('Plain text node', 'Plain text node');
checkHtmlToText(' Plain text node ', 'Plain text node');
checkHtmlToText(' \nPlain text node \n ', 'Plain text node');
checkHtmlToText('