Skip to content

Commit a8bd444

Browse files
committed
refactor: remove flierNodeByType
1 parent 18f8997 commit a8bd444

File tree

6 files changed

+39
-34
lines changed

6 files changed

+39
-34
lines changed

src/fireEvent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ import {
77
ScrollViewProps,
88
} from 'react-native';
99
import act from './act';
10-
import { isPointerEventEnabled } from './helpers/pointer-events';
1110
import { isHostElement } from './helpers/component-tree';
1211
import { isHostTextInput } from './helpers/host-component-names';
12+
import { isPointerEventEnabled } from './helpers/pointer-events';
1313

1414
type EventHandler = (...args: unknown[]) => unknown;
1515

src/helpers/filterNodeByType.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/helpers/host-component-names.tsx

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { ReactTestInstance } from 'react-test-renderer';
33
import { Switch, Text, TextInput, View } from 'react-native';
44
import { configureInternal, getConfig, HostComponentNames } from '../config';
55
import { renderWithAct } from '../render-act';
6+
import { HostTestInstance } from './component-tree';
67

78
const userConfigErrorMessage = `There seems to be an issue with your configuration that prevents React Native Testing Library from working correctly.
89
Please check if you are using compatible versions of React Native and React Native Testing Library.`;
@@ -66,10 +67,32 @@ function getByTestId(instance: ReactTestInstance, testID: string) {
6667
return nodes[0];
6768
}
6869

69-
export function isHostText(element?: ReactTestInstance) {
70+
/**
71+
* Checks if the given element is a host Text.
72+
* @param element The element to check.
73+
*/
74+
export function isHostText(
75+
element?: ReactTestInstance | null
76+
): element is HostTestInstance {
7077
return element?.type === getHostComponentNames().text;
7178
}
7279

73-
export function isHostTextInput(element?: ReactTestInstance) {
80+
/**
81+
* Checks if the given element is a host TextInput.
82+
* @param element The element to check.
83+
*/
84+
export function isHostTextInput(
85+
element?: ReactTestInstance | null
86+
): element is HostTestInstance {
7487
return element?.type === getHostComponentNames().textInput;
7588
}
89+
90+
/**
91+
* Checks if the given element is a host Switch.
92+
* @param element The element to check.
93+
*/
94+
export function isHostSwitch(
95+
element?: ReactTestInstance | null
96+
): element is HostTestInstance {
97+
return element?.type === getHostComponentNames().switch;
98+
}

src/queries/displayValue.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
2-
import { filterNodeByType } from '../helpers/filterNodeByType';
32
import { findAll } from '../helpers/findAll';
3+
import { isHostTextInput } from '../helpers/host-component-names';
44
import { matches, TextMatch, TextMatchOptions } from '../matches';
5-
import { getHostComponentNames } from '../helpers/host-component-names';
65
import { makeQueries } from './makeQueries';
76
import type {
87
FindAllByQuery,
@@ -16,19 +15,15 @@ import type { CommonQueryOptions } from './options';
1615

1716
type ByDisplayValueOptions = CommonQueryOptions & TextMatchOptions;
1817

19-
const getTextInputNodeByDisplayValue = (
18+
const matchDisplayValue = (
2019
node: ReactTestInstance,
2120
value: TextMatch,
2221
options: TextMatchOptions = {}
2322
) => {
2423
const { exact, normalizer } = options;
25-
const nodeValue =
26-
node.props.value !== undefined ? node.props.value : node.props.defaultValue;
24+
const nodeValue = node.props.value ?? node.props.defaultValue;
2725

28-
return (
29-
filterNodeByType(node, getHostComponentNames().textInput) &&
30-
matches(value, nodeValue, normalizer, exact)
31-
);
26+
return matches(value, nodeValue, normalizer, exact);
3227
};
3328

3429
const queryAllByDisplayValue = (
@@ -38,7 +33,8 @@ const queryAllByDisplayValue = (
3833
return findAll(
3934
instance,
4035
(node) =>
41-
getTextInputNodeByDisplayValue(node, displayValue, queryOptions),
36+
isHostTextInput(node) &&
37+
matchDisplayValue(node, displayValue, queryOptions),
4238
queryOptions
4339
);
4440
};

src/queries/placeholderText.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
22
import { findAll } from '../helpers/findAll';
33
import { matches, TextMatch, TextMatchOptions } from '../matches';
4-
import { filterNodeByType } from '../helpers/filterNodeByType';
5-
import { getHostComponentNames } from '../helpers/host-component-names';
4+
import { isHostTextInput } from '../helpers/host-component-names';
65
import { makeQueries } from './makeQueries';
76
import type {
87
FindAllByQuery,
@@ -16,17 +15,13 @@ import type { CommonQueryOptions } from './options';
1615

1716
type ByPlaceholderTextOptions = CommonQueryOptions & TextMatchOptions;
1817

19-
const getTextInputNodeByPlaceholderText = (
18+
const matchPlaceholderText = (
2019
node: ReactTestInstance,
2120
placeholder: TextMatch,
2221
options: TextMatchOptions = {}
2322
) => {
2423
const { exact, normalizer } = options;
25-
26-
return (
27-
filterNodeByType(node, getHostComponentNames().textInput) &&
28-
matches(placeholder, node.props.placeholder, normalizer, exact)
29-
);
24+
return matches(placeholder, node.props.placeholder, normalizer, exact);
3025
};
3126

3227
const queryAllByPlaceholderText = (
@@ -36,7 +31,8 @@ const queryAllByPlaceholderText = (
3631
return findAll(
3732
instance,
3833
(node) =>
39-
getTextInputNodeByPlaceholderText(node, placeholder, queryOptions),
34+
isHostTextInput(node) &&
35+
matchPlaceholderText(node, placeholder, queryOptions),
4036
queryOptions
4137
);
4238
};

src/queries/text.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
2-
import { filterNodeByType } from '../helpers/filterNodeByType';
32
import { findAll } from '../helpers/findAll';
4-
import { getHostComponentNames } from '../helpers/host-component-names';
3+
import { isHostText } from '../helpers/host-component-names';
54
import { matchTextContent } from '../helpers/matchers/matchTextContent';
65
import { TextMatch, TextMatchOptions } from '../matches';
76
import { makeQueries } from './makeQueries';
@@ -23,9 +22,7 @@ const queryAllByText = (
2322
function queryAllByTextFn(text, options = {}) {
2423
return findAll(
2524
instance,
26-
(node) =>
27-
filterNodeByType(node, getHostComponentNames().text) &&
28-
matchTextContent(node, text, options),
25+
(node) => isHostText(node) && matchTextContent(node, text, options),
2926
{
3027
...options,
3128
matchDeepestOnly: true,

0 commit comments

Comments
 (0)