Skip to content

Commit 1fb7552

Browse files
committed
chore: simplify find-all
1 parent a9b0b44 commit 1fb7552

File tree

3 files changed

+6
-38
lines changed

3 files changed

+6
-38
lines changed

src/helpers/host-component-names.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import * as React from 'react';
22
import { Image, Modal, ScrollView, Switch, Text, TextInput, View } from 'react-native';
33
import { configureInternal, getConfig, HostComponentNames } from '../config';
44
import { HostElement } from '../renderer/host-element';
5-
import { renderWithAct } from '../render-act';
65
import { createRenderer } from '../renderer/renderer';
76
import act from '../act';
7+
import { findAll } from './find-all';
88

99
const userConfigErrorMessage = `There seems to be an issue with your configuration that prevents React Native Testing Library from working correctly.
1010
Please check if you are using compatible versions of React Native and React Native Testing Library.`;
@@ -62,8 +62,9 @@ function detectHostComponentNames(): HostComponentNames {
6262
}
6363
}
6464

65-
function getByTestId(instance: HostElement, testID: string) {
66-
const nodes = instance.findAll(
65+
function getByTestId(element: HostElement, testID: string) {
66+
const nodes = findAll(
67+
element,
6768
(node) => typeof node.type === 'string' && node.props.testID === testID,
6869
);
6970

src/matchers/to-contain-element.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { matcherHint, RECEIVED_COLOR } from 'jest-matcher-utils';
22
import { HostElement } from '../renderer/host-element';
3+
import { findAll } from '../helpers/find-all';
34
import { checkHostElement, formatElement } from './utils';
45

56
export function toContainElement(
@@ -15,7 +16,7 @@ export function toContainElement(
1516

1617
let matches: HostElement[] = [];
1718
if (element) {
18-
matches = container.findAll((node) => node === element);
19+
matches = findAll(container, (node) => node === element);
1920
}
2021

2122
return {

src/renderer/host-element.ts

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,6 @@ import { Container, Instance, TextInstance } from './reconciler';
33
export type HostNode = HostElement | string;
44
export type HostElementProps = Record<string, any>;
55

6-
type FindOptions = {
7-
deep?: boolean;
8-
};
9-
106
const instanceToHostElementMap = new WeakMap<Container | Instance, HostElement>();
117

128
export class HostElement {
@@ -89,34 +85,4 @@ export class HostElement {
8985
throw new Error(`Unexpected node type in HostElement.fromInstance: ${instance.tag}`);
9086
}
9187
}
92-
93-
findAll(predicate: (element: HostElement) => boolean, options?: FindOptions): HostElement[] {
94-
return findAll(this, predicate, options);
95-
}
96-
}
97-
98-
function findAll(
99-
root: HostElement,
100-
predicate: (element: HostElement) => boolean,
101-
options?: FindOptions,
102-
): HostElement[] {
103-
const deep = options?.deep ?? true;
104-
const results = [];
105-
106-
if (predicate(root)) {
107-
results.push(root);
108-
if (!deep) {
109-
return results;
110-
}
111-
}
112-
113-
root.children.forEach((child) => {
114-
if (typeof child === 'string') {
115-
return;
116-
}
117-
118-
results.push(...findAll(child, predicate, options));
119-
});
120-
121-
return results;
12288
}

0 commit comments

Comments
 (0)