Skip to content
Prev Previous commit
Next Next commit
refactor: remove flierNodeByType
  • Loading branch information
mdjastrzebski committed Aug 11, 2023
commit a8bd44496dd7ac62a714b68627e4ae809e6660dd
2 changes: 1 addition & 1 deletion src/fireEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 0 additions & 7 deletions src/helpers/filterNodeByType.ts

This file was deleted.

27 changes: 25 additions & 2 deletions src/helpers/host-component-names.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.`;
Expand Down Expand Up @@ -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;
}
16 changes: 6 additions & 10 deletions src/queries/displayValue.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = (
Expand All @@ -38,7 +33,8 @@ const queryAllByDisplayValue = (
return findAll(
instance,
(node) =>
getTextInputNodeByDisplayValue(node, displayValue, queryOptions),
isHostTextInput(node) &&
matchDisplayValue(node, displayValue, queryOptions),
queryOptions
);
};
Expand Down
14 changes: 5 additions & 9 deletions src/queries/placeholderText.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = (
Expand All @@ -36,7 +31,8 @@ const queryAllByPlaceholderText = (
return findAll(
instance,
(node) =>
getTextInputNodeByPlaceholderText(node, placeholder, queryOptions),
isHostTextInput(node) &&
matchPlaceholderText(node, placeholder, queryOptions),
queryOptions
);
};
Expand Down
7 changes: 2 additions & 5 deletions src/queries/text.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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,
Expand Down