From 722e703c6bc4df27f23a985b16f68e6c89e048c1 Mon Sep 17 00:00:00 2001 From: wxiaoguang Date: Tue, 26 Nov 2024 23:10:45 +0800 Subject: [PATCH] Bypass vitest bug (#32647) --- web_src/js/utils/dom.test.ts | 8 +++++++- web_src/js/utils/dom.ts | 5 +++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/web_src/js/utils/dom.test.ts b/web_src/js/utils/dom.test.ts index cb99a85511..6e71596850 100644 --- a/web_src/js/utils/dom.test.ts +++ b/web_src/js/utils/dom.test.ts @@ -1,4 +1,4 @@ -import {createElementFromAttrs, createElementFromHTML, querySingleVisibleElem} from './dom.ts'; +import {createElementFromAttrs, createElementFromHTML, queryElemChildren, querySingleVisibleElem} from './dom.ts'; test('createElementFromHTML', () => { expect(createElementFromHTML('foobar').outerHTML).toEqual('foobar'); @@ -26,3 +26,9 @@ test('querySingleVisibleElem', () => { el = createElementFromHTML('
foobar
'); expect(() => querySingleVisibleElem(el, 'span')).toThrowError('Expected exactly one visible element'); }); + +test('queryElemChildren', () => { + const el = createElementFromHTML('
ab
'); + const children = queryElemChildren(el, '.a'); + expect(children.length).toEqual(1); +}); diff --git a/web_src/js/utils/dom.ts b/web_src/js/utils/dom.ts index a4c7c0e4c6..da9ce71644 100644 --- a/web_src/js/utils/dom.ts +++ b/web_src/js/utils/dom.ts @@ -76,6 +76,11 @@ export function queryElemSiblings(el: Element, selector = '*' // it works like jQuery.children: only the direct children are selected export function queryElemChildren(parent: Element | ParentNode, selector = '*', fn?: ElementsCallback): ArrayLikeIterable { + if (window.vitest) { + // bypass the vitest bug: it doesn't support ":scope >" + const selected = Array.from(parent.children as any).filter((child) => child.matches(selector)); + return applyElemsCallback(selected, fn); + } return applyElemsCallback(parent.querySelectorAll(`:scope > ${selector}`), fn); }