diff --git a/src/fireEvent.ts b/src/fireEvent.ts index bdc6a5276..68746e300 100644 --- a/src/fireEvent.ts +++ b/src/fireEvent.ts @@ -7,9 +7,9 @@ import { ScrollViewProps, } from 'react-native'; import act from './act'; -import { isPointerEventEnabled } from './helpers/pointer-events'; import { isHostElement } from './helpers/component-tree'; import { isHostTextInput } from './helpers/host-component-names'; +import { isPointerEventEnabled } from './helpers/pointer-events'; type EventHandler = (...args: unknown[]) => unknown; diff --git a/src/helpers/filterNodeByType.ts b/src/helpers/filterNodeByType.ts deleted file mode 100644 index 1330d41c5..000000000 --- a/src/helpers/filterNodeByType.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { ReactTestInstance } from 'react-test-renderer'; -import * as React from 'react'; - -export const filterNodeByType = ( - node: ReactTestInstance | React.ReactElement, - type: React.ElementType | string -) => node.type === type; diff --git a/src/helpers/host-component-names.tsx b/src/helpers/host-component-names.tsx index 3fd339b0d..eb1c57223 100644 --- a/src/helpers/host-component-names.tsx +++ b/src/helpers/host-component-names.tsx @@ -3,6 +3,7 @@ import { ReactTestInstance } from 'react-test-renderer'; import { Switch, Text, TextInput, View } from 'react-native'; import { configureInternal, getConfig, HostComponentNames } from '../config'; import { renderWithAct } from '../render-act'; +import { HostTestInstance } from './component-tree'; const userConfigErrorMessage = `There seems to be an issue with your configuration that prevents React Native Testing Library from working correctly. 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) { return nodes[0]; } -export function isHostText(element?: ReactTestInstance) { +/** + * Checks if the given element is a host Text. + * @param element The element to check. + */ +export function isHostText( + element?: ReactTestInstance | null +): element is HostTestInstance { return element?.type === getHostComponentNames().text; } -export function isHostTextInput(element?: ReactTestInstance) { +/** + * Checks if the given element is a host TextInput. + * @param element The element to check. + */ +export function isHostTextInput( + element?: ReactTestInstance | null +): element is HostTestInstance { return element?.type === getHostComponentNames().textInput; } + +/** + * Checks if the given element is a host Switch. + * @param element The element to check. + */ +export function isHostSwitch( + element?: ReactTestInstance | null +): element is HostTestInstance { + return element?.type === getHostComponentNames().switch; +} diff --git a/src/queries/displayValue.ts b/src/queries/displayValue.ts index 44db16f93..d521dbf36 100644 --- a/src/queries/displayValue.ts +++ b/src/queries/displayValue.ts @@ -1,8 +1,7 @@ import type { ReactTestInstance } from 'react-test-renderer'; -import { filterNodeByType } from '../helpers/filterNodeByType'; import { findAll } from '../helpers/findAll'; +import { isHostTextInput } from '../helpers/host-component-names'; import { matches, TextMatch, TextMatchOptions } from '../matches'; -import { getHostComponentNames } from '../helpers/host-component-names'; import { makeQueries } from './makeQueries'; import type { FindAllByQuery, @@ -16,19 +15,15 @@ import type { CommonQueryOptions } from './options'; type ByDisplayValueOptions = CommonQueryOptions & TextMatchOptions; -const getTextInputNodeByDisplayValue = ( +const matchDisplayValue = ( node: ReactTestInstance, value: TextMatch, options: TextMatchOptions = {} ) => { const { exact, normalizer } = options; - const nodeValue = - node.props.value !== undefined ? node.props.value : node.props.defaultValue; + const nodeValue = node.props.value ?? node.props.defaultValue; - return ( - filterNodeByType(node, getHostComponentNames().textInput) && - matches(value, nodeValue, normalizer, exact) - ); + return matches(value, nodeValue, normalizer, exact); }; const queryAllByDisplayValue = ( @@ -38,7 +33,8 @@ const queryAllByDisplayValue = ( return findAll( instance, (node) => - getTextInputNodeByDisplayValue(node, displayValue, queryOptions), + isHostTextInput(node) && + matchDisplayValue(node, displayValue, queryOptions), queryOptions ); }; diff --git a/src/queries/placeholderText.ts b/src/queries/placeholderText.ts index 43dfc5bed..5967f5ccd 100644 --- a/src/queries/placeholderText.ts +++ b/src/queries/placeholderText.ts @@ -1,8 +1,7 @@ import type { ReactTestInstance } from 'react-test-renderer'; import { findAll } from '../helpers/findAll'; import { matches, TextMatch, TextMatchOptions } from '../matches'; -import { filterNodeByType } from '../helpers/filterNodeByType'; -import { getHostComponentNames } from '../helpers/host-component-names'; +import { isHostTextInput } from '../helpers/host-component-names'; import { makeQueries } from './makeQueries'; import type { FindAllByQuery, @@ -16,17 +15,13 @@ import type { CommonQueryOptions } from './options'; type ByPlaceholderTextOptions = CommonQueryOptions & TextMatchOptions; -const getTextInputNodeByPlaceholderText = ( +const matchPlaceholderText = ( node: ReactTestInstance, placeholder: TextMatch, options: TextMatchOptions = {} ) => { const { exact, normalizer } = options; - - return ( - filterNodeByType(node, getHostComponentNames().textInput) && - matches(placeholder, node.props.placeholder, normalizer, exact) - ); + return matches(placeholder, node.props.placeholder, normalizer, exact); }; const queryAllByPlaceholderText = ( @@ -36,7 +31,8 @@ const queryAllByPlaceholderText = ( return findAll( instance, (node) => - getTextInputNodeByPlaceholderText(node, placeholder, queryOptions), + isHostTextInput(node) && + matchPlaceholderText(node, placeholder, queryOptions), queryOptions ); }; diff --git a/src/queries/text.ts b/src/queries/text.ts index cf563ea33..0cb9ede3a 100644 --- a/src/queries/text.ts +++ b/src/queries/text.ts @@ -1,7 +1,6 @@ import type { ReactTestInstance } from 'react-test-renderer'; -import { filterNodeByType } from '../helpers/filterNodeByType'; import { findAll } from '../helpers/findAll'; -import { getHostComponentNames } from '../helpers/host-component-names'; +import { isHostText } from '../helpers/host-component-names'; import { matchTextContent } from '../helpers/matchers/matchTextContent'; import { TextMatch, TextMatchOptions } from '../matches'; import { makeQueries } from './makeQueries'; @@ -23,9 +22,7 @@ const queryAllByText = ( function queryAllByTextFn(text, options = {}) { return findAll( instance, - (node) => - filterNodeByType(node, getHostComponentNames().text) && - matchTextContent(node, text, options), + (node) => isHostText(node) && matchTextContent(node, text, options), { ...options, matchDeepestOnly: true,