eturns 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('
'); const elems = $div.parseHTML(''); $div('div').append(elems); expect(elems).toHaveLength(2); }); it('(html, context) : should ignore context argument', () => { const $div = cheerio.load('
'); const elems = $div.parseHTML('
', { foo: 123 }); $div('div').append(elems); expect(elems).toHaveLength(1); }); it('(html, context, keepScripts) : should ignore context argument', () => { const $div = cheerio.load('
'); const elems = $div.parseHTML( '
', { foo: 123 }, true, ); $div('div').append(elems); expect(elems).toHaveLength(2); }); }); describe('.merge', () => { const $ = cheerio.load(''); it('should be a function', () => { expect(typeof $.merge).toBe('function'); }); it('(arraylike, arraylike) : should modify the first array, but not the second', () => { const arr1 = [1, 2, 3]; const arr2 = [4, 5, 6]; const ret = $.merge(arr1, arr2); expect(typeof ret).toBe('object'); expect(Array.isArray(ret)).toBe(true); expect(ret).toBe(arr1); expect(arr1).toHaveLength(6); expect(arr2).toHaveLength(3); }); it('(arraylike, arraylike) : should handle objects that arent arrays, but are arraylike', () => { const arr1: ArrayLike = { length: 3, 0: 'a', 1: 'b', 2: 'c', }; const arr2 = { length: 3, 0: 'd', 1: 'e', 2: 'f', }; $.merge(arr1, arr2); expect(arr1).toHaveLength(6); expect(arr1[3]).toBe('d'); expect(arr1[4]).toBe('e'); expect(arr1[5]).toBe('f'); expect(arr2).toHaveLength(3); }); it('(?, ?) : should gracefully reject invalid inputs', () => { expect($.merge([4], 3 as never)).toBeFalsy(); expect($.merge({} as never, {} as never)).toBeFalsy(); expect($.merge([], {} as never)).toBeFalsy(); expect($.merge({} as never, [])).toBeFalsy(); const fakeArray1 = { length: 3, 0: 'a', 1: 'b', 3: 'd' }; expect($.merge(fakeArray1, [])).toBeFalsy(); expect($.merge([], fakeArray1)).toBeFalsy(); expect($.merge({ length: '7' } as never, [])).toBeFalsy(); expect($.merge({ length: -1 }, [])).toBeFalsy(); }); it('(?, ?) : should no-op on invalid inputs', () => { const fakeArray1 = { length: 3, 0: 'a', 1: 'b', 3: 'd' }; $.merge(fakeArray1, []); expect(fakeArray1).toHaveLength(3); expect(fakeArray1[0]).toBe('a'); expect(fakeArray1[1]).toBe('b'); expect(fakeArray1[3]).toBe('d'); $.merge([], fakeArray1); expect(fakeArray1).toHaveLength(3); expect(fakeArray1[0]).toBe('a'); expect(fakeArray1[1]).toBe('b'); expect(fakeArray1[3]).toBe('d'); }); }); describe('.contains', () => { let $: CheerioAPI; beforeEach(() => { $ = cheerio.load(food); }); it('(container, contained) : should correctly detect the provided element', () => { const $food = $('#food'); const $fruits = $('#fruits'); const $apple = $('.apple'); expect($.contains($food[0], $fruits[0])).toBe(true); expect($.contains($food[0], $apple[0])).toBe(true); }); it('(container, other) : should not detect elements that are not contained', () => { const $fruits = $('#fruits'); const $vegetables = $('#vegetables'); const $apple = $('.apple'); expect($.contains($vegetables[0], $apple[0])).toBe(false); expect($.contains($fruits[0], $vegetables[0])).toBe(false); expect($.contains($vegetables[0], $fruits[0])).toBe(false); expect($.contains($fruits[0], $fruits[0])).toBe(false); expect($.contains($vegetables[0], $vegetables[0])).toBe(false); }); }); describe('.root', () => { it('() : should return a cheerio-wrapped root object', () => { const $ = cheerio.load('
foo