2018-10-06 23:42:52 +02:00
|
|
|
// @flow
|
|
|
|
|
import * as React from 'react';
|
2018-11-17 19:13:44 +01:00
|
|
|
import prettyFormat from 'pretty-format';
|
2018-11-19 15:16:33 +01:00
|
|
|
import {
|
|
|
|
|
ErrorWithStack,
|
|
|
|
|
createLibraryNotSupportedError,
|
|
|
|
|
logDeprecationWarning,
|
|
|
|
|
} from './errors';
|
2018-10-06 23:42:52 +02:00
|
|
|
|
2018-11-17 19:13:44 +01:00
|
|
|
const filterNodeByType = (node, type) => node.type === type;
|
2018-10-06 23:42:52 +02:00
|
|
|
|
2018-11-17 19:13:44 +01:00
|
|
|
const filterNodeByName = (node, name) =>
|
|
|
|
|
typeof node.type !== 'string' &&
|
|
|
|
|
(node.type.displayName === name || node.type.name === name);
|
2018-10-06 23:42:52 +02:00
|
|
|
|
2018-11-17 19:13:44 +01:00
|
|
|
const getNodeByText = (node, text) => {
|
|
|
|
|
try {
|
|
|
|
|
// eslint-disable-next-line
|
|
|
|
|
const { Text, TextInput } = require('react-native');
|
|
|
|
|
return (
|
|
|
|
|
(filterNodeByType(node, Text) || filterNodeByType(node, TextInput)) &&
|
|
|
|
|
(typeof text === 'string'
|
|
|
|
|
? text === node.props.children
|
|
|
|
|
: text.test(node.props.children))
|
|
|
|
|
);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw createLibraryNotSupportedError(error);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const prepareErrorMessage = error =>
|
|
|
|
|
// Strip info about custom predicate
|
|
|
|
|
error.message.replace(/ matching custom predicate[^]*/gm, '');
|
|
|
|
|
|
2018-10-07 17:10:26 +02:00
|
|
|
export const getByName = (instance: ReactTestInstance) =>
|
|
|
|
|
function getByNameFn(name: string | React.ComponentType<*>) {
|
2018-11-19 15:16:33 +01:00
|
|
|
logDeprecationWarning('getByName', 'getByType');
|
2018-10-07 17:10:26 +02:00
|
|
|
try {
|
2018-11-17 19:13:44 +01:00
|
|
|
return typeof name === 'string'
|
|
|
|
|
? instance.find(node => filterNodeByName(node, name))
|
|
|
|
|
: instance.findByType(name);
|
2018-10-07 17:10:26 +02:00
|
|
|
} catch (error) {
|
2018-11-17 19:13:44 +01:00
|
|
|
throw new ErrorWithStack(prepareErrorMessage(error), getByNameFn);
|
2018-10-07 17:10:26 +02:00
|
|
|
}
|
|
|
|
|
};
|
2018-10-06 23:42:52 +02:00
|
|
|
|
2018-11-19 13:44:51 +01:00
|
|
|
export const getByType = (instance: ReactTestInstance) =>
|
|
|
|
|
function getByTypeFn(type: React.ComponentType<*>) {
|
|
|
|
|
try {
|
|
|
|
|
return instance.findByType(type);
|
|
|
|
|
} catch (error) {
|
|
|
|
|
throw new ErrorWithStack(prepareErrorMessage(error), getByTypeFn);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-07 17:10:26 +02:00
|
|
|
export const getByText = (instance: ReactTestInstance) =>
|
|
|
|
|
function getByTextFn(text: string | RegExp) {
|
|
|
|
|
try {
|
|
|
|
|
return instance.find(node => getNodeByText(node, text));
|
|
|
|
|
} catch (error) {
|
2018-11-17 19:13:44 +01:00
|
|
|
throw new ErrorWithStack(prepareErrorMessage(error), getByTextFn);
|
2018-10-07 17:10:26 +02:00
|
|
|
}
|
|
|
|
|
};
|
2018-10-06 23:42:52 +02:00
|
|
|
|
2018-10-07 17:10:26 +02:00
|
|
|
export const getByProps = (instance: ReactTestInstance) =>
|
|
|
|
|
function getByPropsFn(props: { [propName: string]: any }) {
|
|
|
|
|
try {
|
|
|
|
|
return instance.findByProps(props);
|
|
|
|
|
} catch (error) {
|
2018-11-17 19:13:44 +01:00
|
|
|
throw new ErrorWithStack(prepareErrorMessage(error), getByPropsFn);
|
2018-10-07 17:10:26 +02:00
|
|
|
}
|
|
|
|
|
};
|
2018-10-06 23:42:52 +02:00
|
|
|
|
2018-10-07 17:10:26 +02:00
|
|
|
export const getByTestId = (instance: ReactTestInstance) =>
|
|
|
|
|
function getByTestIdFn(testID: string) {
|
|
|
|
|
try {
|
|
|
|
|
return instance.findByProps({ testID });
|
|
|
|
|
} catch (error) {
|
2018-11-17 19:13:44 +01:00
|
|
|
throw new ErrorWithStack(prepareErrorMessage(error), getByTestIdFn);
|
2018-10-07 17:10:26 +02:00
|
|
|
}
|
|
|
|
|
};
|
2018-10-06 23:42:52 +02:00
|
|
|
|
2018-10-07 17:10:26 +02:00
|
|
|
export const getAllByName = (instance: ReactTestInstance) =>
|
|
|
|
|
function getAllByNameFn(name: string | React.ComponentType<*>) {
|
2018-11-19 15:16:33 +01:00
|
|
|
logDeprecationWarning('getAllByName', 'getAllByType');
|
2018-11-17 19:13:44 +01:00
|
|
|
const results =
|
|
|
|
|
typeof name === 'string'
|
|
|
|
|
? instance.findAll(node => filterNodeByName(node, name))
|
|
|
|
|
: instance.findAllByType(name);
|
2018-10-07 17:10:26 +02:00
|
|
|
if (results.length === 0) {
|
2018-11-17 19:13:44 +01:00
|
|
|
throw new ErrorWithStack('No instances found', getAllByNameFn);
|
2018-10-07 17:10:26 +02:00
|
|
|
}
|
|
|
|
|
return results;
|
|
|
|
|
};
|
2018-10-06 23:42:52 +02:00
|
|
|
|
2018-11-19 13:44:51 +01:00
|
|
|
export const getAllByType = (instance: ReactTestInstance) =>
|
|
|
|
|
function getAllByTypeFn(type: React.ComponentType<*>) {
|
|
|
|
|
const results = instance.findAllByType(type);
|
|
|
|
|
if (results.length === 0) {
|
|
|
|
|
throw new ErrorWithStack('No instances found', getAllByTypeFn);
|
|
|
|
|
}
|
|
|
|
|
return results;
|
|
|
|
|
};
|
|
|
|
|
|
2018-10-07 17:10:26 +02:00
|
|
|
export const getAllByText = (instance: ReactTestInstance) =>
|
|
|
|
|
function getAllByTextFn(text: string | RegExp) {
|
|
|
|
|
const results = instance.findAll(node => getNodeByText(node, text));
|
|
|
|
|
if (results.length === 0) {
|
2018-11-17 19:13:44 +01:00
|
|
|
throw new ErrorWithStack(
|
|
|
|
|
`No instances found with text: ${String(text)}`,
|
|
|
|
|
getAllByTextFn
|
|
|
|
|
);
|
2018-10-07 17:10:26 +02:00
|
|
|
}
|
|
|
|
|
return results;
|
|
|
|
|
};
|
2018-10-06 23:42:52 +02:00
|
|
|
|
2018-10-07 17:10:26 +02:00
|
|
|
export const getAllByProps = (instance: ReactTestInstance) =>
|
|
|
|
|
function getAllByPropsFn(props: { [propName: string]: any }) {
|
|
|
|
|
const results = instance.findAllByProps(props);
|
|
|
|
|
if (results.length === 0) {
|
2018-11-17 19:13:44 +01:00
|
|
|
throw new ErrorWithStack(
|
|
|
|
|
`No instances found with props:\n${prettyFormat(props)}`,
|
|
|
|
|
getAllByPropsFn
|
|
|
|
|
);
|
2018-10-07 17:10:26 +02:00
|
|
|
}
|
|
|
|
|
return results;
|
|
|
|
|
};
|
2018-10-07 10:59:27 +02:00
|
|
|
|
|
|
|
|
export const getByAPI = (instance: ReactTestInstance) => ({
|
|
|
|
|
getByTestId: getByTestId(instance),
|
|
|
|
|
getByName: getByName(instance),
|
2018-11-19 13:44:51 +01:00
|
|
|
getByType: getByType(instance),
|
2018-10-07 10:59:27 +02:00
|
|
|
getByText: getByText(instance),
|
|
|
|
|
getByProps: getByProps(instance),
|
|
|
|
|
getAllByName: getAllByName(instance),
|
2018-11-19 13:44:51 +01:00
|
|
|
getAllByType: getAllByType(instance),
|
2018-10-07 10:59:27 +02:00
|
|
|
getAllByText: getAllByText(instance),
|
|
|
|
|
getAllByProps: getAllByProps(instance),
|
|
|
|
|
});
|