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); }