import { describe, it, expect, beforeEach } from 'vitest';
import { cheerio, food, eleven } from './__fixtures__/fixtures.js';
import { type CheerioAPI } from './index.js';
describe('cheerio', () => {
describe('.html', () => {
it('() : should return innerHTML; $.html(obj) should return outerHTML', () => {
const $div = cheerio(
'div',
'
foobar
',
);
const span = $div.children()[1];
expect(cheerio(span).html()).toBe('bar');
expect(cheerio.html(span)).toBe('bar');
});
it('() : should accept an object, an array, or a cheerio object', () => {
const $span = cheerio('foo');
expect(cheerio.html($span[0])).toBe('foo');
expect(cheerio.html($span)).toBe('foo');
});
it('() : should be able to set to an empty string', () => {
const $elem = cheerio('foo').html('');
expect(cheerio.html($elem)).toBe('');
});
it('() : does not render the root element', () => {
const $ = cheerio.load('');
expect(cheerio.html($.root())).toBe(
'',
);
});
it('(, , ) : does not render the root element', () => {
const $ = cheerio.load('a div
a span');
const $collection = $('div').add($.root()).add('span');
const expected =
'a div
a spana div
a span';
expect(cheerio.html($collection)).toBe(expected);
});
it('() : does not crash with `null` as `this` value', () => {
const { html } = cheerio;
expect(html.call(null as never)).toBe('');
expect(html.call(null as never, '#nothing')).toBe('');
});
});
describe('.text', () => {
it('(cheerio object) : should return the text contents of the specified elements', () => {
const $ = cheerio.load('This is content.');
expect(cheerio.text($('a'))).toBe('This is content.');
});
it('(cheerio object) : should omit comment nodes', () => {
const $ = cheerio.load(
'This is not a comment.',
);
expect(cheerio.text($('a'))).toBe('This is not a comment.');
});
it('(cheerio object) : should include text contents of children recursively', () => {
const $ = cheerio.load(
'This is a child with another child and not a comment followed by one last child and some final
text.',
);
expect(cheerio.text($('a'))).toBe(
'This is a child with another child and not a comment followed by one last child and some final text.',
);
});
it('() : should return the rendered text content of the root', () => {
const $ = cheerio.load(
'This is a child with another child and not a comment followed by one last child and some final
text.',
);
expect(cheerio.text($.root())).toBe(
'This is a child with another child and not a comment followed by one last child and some final text.',
);
});
it('(cheerio object) : should not omit script tags', () => {
const $ = cheerio.load('');
expect(cheerio.text($.root())).toBe('console.log("test")');
});
it('(cheerio object) : should omit style tags', () => {
const $ = cheerio.load(
'',
);
expect($.text()).toBe('.cf-hidden { display: none; }');
});
it('() : does not crash with `null` as `this` value', () => {
const { text } = cheerio;
expect(text.call(null as never)).toBe('');
});
});
describe('.parseHTML', () => {
const $ = cheerio.load('');
it('() : returns null', () => {
expect($.parseHTML()).toBe(null);
});
it('(null) : returns null', () => {
expect($.parseHTML(null)).toBe(null);
});
it('("") : returns null', () => {
expect($.parseHTML('')).toBe(null);
});
it('(largeHtmlString) : parses large HTML strings', () => {
const html = ''.repeat(10);
const nodes = $.parseHTML(html);
expect(nodes.length).toBe(10);
expect(nodes).toBeInstanceOf(Array);
});
it('("';
expect($.parseHTML(html)).toHaveLength(0);
});
it('("';
expect($.parseHTML(html, true)[0]).toHaveProperty('tagName', 'script');
});
it('("scriptAndNonScript) : preserves non-script nodes', () => {
const html = '';
expect($.parseHTML(html)[0]).toHaveProperty('tagName', 'div');
});
it('(scriptAndNonScript, true) : Preserves script position', () => {
const html = '';
expect($.parseHTML(html, true)[0]).toHaveProperty('tagName', 'script');
});
it('(text) : returns a text node', () => {
expect($.parseHTML('text')[0].type).toBe('text');
});
it('(>text) : preserves leading whitespace', () => {
expect($.parseHTML('\t')[0]).toHaveProperty('data', '\t');
});
it('( text) : Leading spaces are treated as text nodes', () => {
expect($.parseHTML(' ')[0].type).toBe('text');
});
it('(html) : should preserve content', () => {
const html = 'test div
';
expect(cheerio($.parseHTML(html)[0]).html()).toBe('test div');
});
it('(malformedHtml) : should not break', () => {
expect($.parseHTML('')).toHaveLength(1);
});
it('(garbageInput) : should not cause an error', () => {
expect(
$.parseHTML('<#if>This is a test.
<#/if>'),
).toBeTruthy();
});
it('(text) : should return an array that is not effected by DOM manipulation methods', () => {
const $div = cheerio.load('