From 5a9281bf2e52a9f8958ae6bcf058626f7eff7965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20B=C3=B6hm?= <188768+fb55@users.noreply.github.com> Date: Fri, 14 May 2021 15:28:38 +0100 Subject: [PATCH] Add test cases from #1706 Co-Authored-By: 5saviahv <49443574+5saviahv@users.noreply.github.com> --- src/api/traversing.spec.ts | 7 +++++++ src/cheerio.spec.ts | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/src/api/traversing.spec.ts b/src/api/traversing.spec.ts index 113f13df50..63b2504777 100644 --- a/src/api/traversing.spec.ts +++ b/src/api/traversing.spec.ts @@ -1238,6 +1238,13 @@ describe('$(...)', () => { expect($selection[0]).toBe($fruits[0]); expect($selection[1]).toBe($orange[0]); }); + it('is root object preserved', () => { + const $selection = $('
').add('#fruits'); + + expect($selection).toHaveLength(2); + expect($selection.eq(0).is('div')).toBe(true); + expect($selection.eq(1).is($fruits.eq(0))).toBe(true); + }); }); describe('(selector) matched elements :', () => { it('occur before the current selection', () => { diff --git a/src/cheerio.spec.ts b/src/cheerio.spec.ts index 28bc07d60a..fa7cc2d936 100644 --- a/src/cheerio.spec.ts +++ b/src/cheerio.spec.ts @@ -270,6 +270,15 @@ describe('cheerio', () => { expect(lis).toHaveLength(3); }); + it('should preserve root content', () => { + const $ = cheerio.load(fruits); + // Root should not be overwritten + const el = $('
'); + expect(Object.is(el, el._root)).toBe(false); + // Query has to have results + expect($('li', 'ul')).toHaveLength(3); + }); + it('should allow loading a pre-parsed DOM', () => { const dom = parseDOM(food); const $ = cheerio.load(dom);