mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
feat: RegExp support for *ByTestId queries (#535)
* getAllByTestId supports a regex matcher * update typings * docs: update * refactor types * refactor. remove unnecessary try/catch * refactor test assertions
This commit is contained in:
@@ -35,3 +35,18 @@ test('getByTestId returns only native elements', () => {
|
||||
'No instances found with testID: myComponent'
|
||||
);
|
||||
});
|
||||
|
||||
test('supports a regex matcher', () => {
|
||||
const { getByTestId, getAllByTestId } = render(
|
||||
<View>
|
||||
<Text testID="text">Text</Text>
|
||||
<TextInput testID="textInput" />
|
||||
<View testID="view" />
|
||||
<Button testID="button" title="Button" onPress={jest.fn()} />
|
||||
<MyComponent testID="myComponent" />
|
||||
</View>
|
||||
);
|
||||
|
||||
expect(getByTestId(/view/)).toBeTruthy();
|
||||
expect(getAllByTestId(/text/)).toHaveLength(2);
|
||||
});
|
||||
|
||||
@@ -21,12 +21,12 @@ const makeFindQuery = <Text, Result>(
|
||||
): Promise<Result> => waitFor(() => getQuery(instance)(text), waitForOptions);
|
||||
|
||||
export const findByTestId = (instance: ReactTestInstance) => (
|
||||
testId: string,
|
||||
testId: string | RegExp,
|
||||
waitForOptions: WaitForOptions = {}
|
||||
) => makeFindQuery(instance, getByTestId, testId, waitForOptions);
|
||||
|
||||
export const findAllByTestId = (instance: ReactTestInstance) => (
|
||||
testId: string,
|
||||
testId: string | RegExp,
|
||||
waitForOptions: WaitForOptions = {}
|
||||
) => makeFindQuery(instance, getAllByTestId, testId, waitForOptions);
|
||||
|
||||
|
||||
@@ -89,6 +89,12 @@ const getTextInputNodeByDisplayValue = (node, value) => {
|
||||
}
|
||||
};
|
||||
|
||||
const getNodeByTestId = (node, testID) => {
|
||||
return typeof testID === 'string'
|
||||
? testID === node.props.testID
|
||||
: testID.test(node.props.testID);
|
||||
};
|
||||
|
||||
export const getByText = (instance: ReactTestInstance) =>
|
||||
function getByTextFn(text: string | RegExp) {
|
||||
try {
|
||||
@@ -124,7 +130,7 @@ export const getByDisplayValue = (instance: ReactTestInstance) =>
|
||||
};
|
||||
|
||||
export const getByTestId = (instance: ReactTestInstance) =>
|
||||
function getByTestIdFn(testID: string) {
|
||||
function getByTestIdFn(testID: string | RegExp) {
|
||||
try {
|
||||
const results = getAllByTestId(instance)(testID);
|
||||
if (results.length === 1) {
|
||||
@@ -183,9 +189,9 @@ export const getAllByDisplayValue = (instance: ReactTestInstance) =>
|
||||
};
|
||||
|
||||
export const getAllByTestId = (instance: ReactTestInstance) =>
|
||||
function getAllByTestIdFn(testID: string): ReactTestInstance[] {
|
||||
function getAllByTestIdFn(testID: string | RegExp): ReactTestInstance[] {
|
||||
const results = instance
|
||||
.findAllByProps({ testID })
|
||||
.findAll((node) => getNodeByTestId(node, testID))
|
||||
.filter((element) => typeof element.type === 'string');
|
||||
|
||||
if (results.length === 0) {
|
||||
|
||||
@@ -48,7 +48,7 @@ export const queryByDisplayValue = (instance: ReactTestInstance) =>
|
||||
};
|
||||
|
||||
export const queryByTestId = (instance: ReactTestInstance) =>
|
||||
function queryByTestIdFn(testID: string) {
|
||||
function queryByTestIdFn(testID: string | RegExp) {
|
||||
try {
|
||||
return getByTestId(instance)(testID);
|
||||
} catch (error) {
|
||||
@@ -87,7 +87,7 @@ export const queryAllByDisplayValue = (instance: ReactTestInstance) => (
|
||||
};
|
||||
|
||||
export const queryAllByTestId = (instance: ReactTestInstance) => (
|
||||
testID: string
|
||||
testID: string | RegExp
|
||||
) => {
|
||||
try {
|
||||
return getAllByTestId(instance)(testID);
|
||||
|
||||
Reference in New Issue
Block a user