@@ -2,7 +2,6 @@ import type { HostElement } from 'universal-test-renderer';
22
33import { getConfig } from '../config' ;
44import { isHiddenFromAccessibility } from './accessibility' ;
5- import { isHostElement } from './component-tree' ;
65
76interface FindAllOptions {
87 /** Match elements hidden from accessibility */
@@ -18,9 +17,10 @@ interface FindAllOptions {
1817export function findAll (
1918 root : HostElement ,
2019 predicate : ( element : HostElement ) => boolean ,
21- options ? : FindAllOptions ,
20+ options : FindAllOptions = { } ,
2221) : HostElement [ ] {
23- const results = findAllInternal ( root , predicate , options ) ;
22+ const { matchDeepestOnly } = options ;
23+ const results = root . findAll ( predicate , { matchDeepestOnly } ) ;
2424
2525 const includeHiddenElements =
2626 options ?. includeHiddenElements ?? options ?. hidden ?? getConfig ( ) ?. defaultIncludeHiddenElements ;
@@ -32,36 +32,3 @@ export function findAll(
3232 const cache = new WeakMap < HostElement > ( ) ;
3333 return results . filter ( ( element ) => ! isHiddenFromAccessibility ( element , { cache } ) ) ;
3434}
35-
36- // Extracted from React Test Renderer
37- // src: https://github.com/facebook/react/blob/8e2bde6f2751aa6335f3cef488c05c3ea08e074a/packages/universal-test-renderer/src/Root.js#L402
38- function findAllInternal (
39- root : HostElement ,
40- predicate : ( element : HostElement ) => boolean ,
41- options ?: FindAllOptions ,
42- ) : HostElement [ ] {
43- const results : HostElement [ ] = [ ] ;
44-
45- // Match descendants first but do not add them to results yet.
46- const matchingDescendants : HostElement [ ] = [ ] ;
47- root . children . forEach ( ( child ) => {
48- if ( typeof child === 'string' ) {
49- return ;
50- }
51- matchingDescendants . push ( ...findAllInternal ( child , predicate , options ) ) ;
52- } ) ;
53-
54- if (
55- // When matchDeepestOnly = true: add current element only if no descendants match
56- ( ! options ?. matchDeepestOnly || matchingDescendants . length === 0 ) &&
57- isHostElement ( root ) &&
58- predicate ( root )
59- ) {
60- results . push ( root ) ;
61- }
62-
63- // Add matching descendants after element to preserve original tree walk order.
64- results . push ( ...matchingDescendants ) ;
65-
66- return results ;
67- }
0 commit comments