mirror of
https://github.com/callstack/react-native-testing-library.git
synced 2026-09-18 23:09:04 +08:00
chore: bump React Native to 0.64 RC (#626)
* chore: bump React Native to 0.64 RC * Snapshot update * Bumped node version to 12 on CI * Removed removed supress_comment syntax from config * codemod types-first * Prettied files after codemod * Added flow types to API returns * don't return null from fireEvent handlers * return ReactTestInstance instead of any * fix render returning any * less anys * less anys * simplify call type * simplify * add Flow to missing files, type debug better Co-authored-by: Michał Parkitny <michal.parkitny@callstack.com> Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
This commit is contained in:
@@ -134,7 +134,7 @@ exports[`debug: shallow 1`] = `
|
||||
>
|
||||
not fresh
|
||||
</Text>
|
||||
<Component
|
||||
<TextInput
|
||||
allowFontScaling={true}
|
||||
placeholder=\\"Add custom freshness\\"
|
||||
rejectResponderTermination={true}
|
||||
@@ -142,7 +142,7 @@ exports[`debug: shallow 1`] = `
|
||||
underlineColorAndroid=\\"transparent\\"
|
||||
value=\\"Custom Freshie\\"
|
||||
/>
|
||||
<Component
|
||||
<TextInput
|
||||
allowFontScaling={true}
|
||||
placeholder=\\"Who inspected freshness?\\"
|
||||
rejectResponderTermination={true}
|
||||
@@ -184,7 +184,7 @@ exports[`debug: shallow with message 1`] = `
|
||||
>
|
||||
not fresh
|
||||
</Text>
|
||||
<Component
|
||||
<TextInput
|
||||
allowFontScaling={true}
|
||||
placeholder=\\"Add custom freshness\\"
|
||||
rejectResponderTermination={true}
|
||||
@@ -192,7 +192,7 @@ exports[`debug: shallow with message 1`] = `
|
||||
underlineColorAndroid=\\"transparent\\"
|
||||
value=\\"Custom Freshie\\"
|
||||
/>
|
||||
<Component
|
||||
<TextInput
|
||||
allowFontScaling={true}
|
||||
placeholder=\\"Who inspected freshness?\\"
|
||||
rejectResponderTermination={true}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// @flow
|
||||
import React from 'react';
|
||||
import { View } from 'react-native';
|
||||
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
// @flow
|
||||
import React from 'react';
|
||||
import { View } from 'react-native';
|
||||
import { render } from '..';
|
||||
|
||||
+2
-1
@@ -1,8 +1,9 @@
|
||||
// @flow
|
||||
import { act } from 'react-test-renderer';
|
||||
import type { Thenable } from './types.flow';
|
||||
|
||||
const actMock = (callback: () => void) => {
|
||||
callback();
|
||||
};
|
||||
|
||||
export default act || actMock;
|
||||
export default (act || actMock: (callback: () => void) => Thenable);
|
||||
|
||||
+8
-6
@@ -89,7 +89,7 @@ const invokeEvent = (
|
||||
const handler = findEventHandler(element, eventName, callsite);
|
||||
|
||||
if (!handler) {
|
||||
return null;
|
||||
return;
|
||||
}
|
||||
|
||||
let returnValue;
|
||||
@@ -104,18 +104,20 @@ const invokeEvent = (
|
||||
const toEventHandlerName = (eventName: string) =>
|
||||
`on${eventName.charAt(0).toUpperCase()}${eventName.slice(1)}`;
|
||||
|
||||
const pressHandler = (element: ReactTestInstance) =>
|
||||
const pressHandler = (element: ReactTestInstance): void =>
|
||||
invokeEvent(element, 'press', pressHandler);
|
||||
const changeTextHandler = (element: ReactTestInstance, ...data: Array<any>) =>
|
||||
invokeEvent(element, 'changeText', changeTextHandler, ...data);
|
||||
const scrollHandler = (element: ReactTestInstance, ...data: Array<any>) =>
|
||||
const changeTextHandler = (
|
||||
element: ReactTestInstance,
|
||||
...data: Array<any>
|
||||
): void => invokeEvent(element, 'changeText', changeTextHandler, ...data);
|
||||
const scrollHandler = (element: ReactTestInstance, ...data: Array<any>): void =>
|
||||
invokeEvent(element, 'scroll', scrollHandler, ...data);
|
||||
|
||||
const fireEvent = (
|
||||
element: ReactTestInstance,
|
||||
eventName: string,
|
||||
...data: Array<any>
|
||||
) => invokeEvent(element, eventName, fireEvent, ...data);
|
||||
): void => invokeEvent(element, eventName, fireEvent, ...data);
|
||||
|
||||
fireEvent.press = pressHandler;
|
||||
fireEvent.changeText = changeTextHandler;
|
||||
|
||||
@@ -6,11 +6,11 @@ import makeQuery from './makeQuery';
|
||||
type GetReturn = ReactTestInstance;
|
||||
type GetAllReturn = Array<ReactTestInstance>;
|
||||
type QueryReturn = ReactTestInstance | null;
|
||||
type QueryAllReturn = Array<ReactTestInstance> | [];
|
||||
type QueryAllReturn = Array<ReactTestInstance>;
|
||||
type FindReturn = Promise<GetReturn>;
|
||||
type FindAllReturn = Promise<GetAllReturn>;
|
||||
|
||||
type A11yAPI = {|
|
||||
export type A11yAPI = {|
|
||||
// Label
|
||||
getByA11yLabel: (string | RegExp) => GetReturn,
|
||||
getByLabelText: (string | RegExp) => GetReturn,
|
||||
@@ -116,7 +116,7 @@ export function matchObject<T: {}>(prop?: T, matcher: T): boolean {
|
||||
: false;
|
||||
}
|
||||
|
||||
const a11yAPI = (instance: ReactTestInstance): A11yAPI =>
|
||||
export const a11yAPI = (instance: ReactTestInstance): A11yAPI =>
|
||||
({
|
||||
...makeQuery(
|
||||
'accessibilityLabel',
|
||||
@@ -239,5 +239,3 @@ const a11yAPI = (instance: ReactTestInstance): A11yAPI =>
|
||||
matchObject
|
||||
)(instance),
|
||||
}: any);
|
||||
|
||||
export default a11yAPI;
|
||||
|
||||
@@ -6,7 +6,7 @@ import format from './format';
|
||||
*/
|
||||
export default function debugDeep(
|
||||
instance: ?ReactTestRendererJSON,
|
||||
message?: any = ''
|
||||
message?: string
|
||||
) {
|
||||
if (message) {
|
||||
console.log(`${message}\n\n`, format(instance));
|
||||
|
||||
@@ -8,7 +8,7 @@ import format from './format';
|
||||
*/
|
||||
export default function debugShallow(
|
||||
instance: ReactTestInstance | React.Element<any>,
|
||||
message?: any
|
||||
message?: string
|
||||
) {
|
||||
const { output } = shallowInternal(instance);
|
||||
|
||||
|
||||
@@ -10,7 +10,7 @@ export class ErrorWithStack extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
export const createLibraryNotSupportedError = (error: Error) =>
|
||||
export const createLibraryNotSupportedError = (error: Error): Error =>
|
||||
new Error(
|
||||
`Currently the only supported library to search by text is "react-native".\n\n${error.message}`
|
||||
);
|
||||
@@ -19,7 +19,7 @@ export const prepareErrorMessage = (
|
||||
error: Error,
|
||||
name: ?string,
|
||||
value: ?mixed
|
||||
) => {
|
||||
): string => {
|
||||
// Strip info about custom predicate
|
||||
let errorMessage = error.message.replace(
|
||||
/ matching custom predicate[^]*/gm,
|
||||
@@ -32,7 +32,7 @@ export const prepareErrorMessage = (
|
||||
return errorMessage;
|
||||
};
|
||||
|
||||
export const createQueryByError = (error: Error, callsite: Function) => {
|
||||
export const createQueryByError = (error: Error, callsite: Function): null => {
|
||||
if (error.message.includes('No instances found')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -13,6 +13,43 @@ import {
|
||||
} from './getByAPI';
|
||||
import { throwRenamedFunctionError } from './errors';
|
||||
|
||||
export type FindByAPI = {|
|
||||
findAllByDisplayValue: (
|
||||
value: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<Array<ReactTestInstance>>,
|
||||
findAllByPlaceholder: () => void,
|
||||
findAllByPlaceholderText: (
|
||||
placeholder: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<Array<ReactTestInstance>>,
|
||||
findAllByTestId: (
|
||||
testId: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<Array<ReactTestInstance>>,
|
||||
findAllByText: (
|
||||
text: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<Array<ReactTestInstance>>,
|
||||
findByDisplayValue: (
|
||||
value: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<ReactTestInstance>,
|
||||
findByPlaceholder: () => void,
|
||||
findByPlaceholderText: (
|
||||
placeholder: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<ReactTestInstance>,
|
||||
findByTestId: (
|
||||
testId: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<ReactTestInstance>,
|
||||
findByText: (
|
||||
text: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<ReactTestInstance>,
|
||||
|};
|
||||
|
||||
const makeFindQuery = <Text, Result>(
|
||||
instance: ReactTestInstance,
|
||||
getQuery: (instance: ReactTestInstance) => (text: Text) => Result,
|
||||
@@ -20,48 +57,88 @@ const makeFindQuery = <Text, Result>(
|
||||
waitForOptions: WaitForOptions
|
||||
): Promise<Result> => waitFor(() => getQuery(instance)(text), waitForOptions);
|
||||
|
||||
export const findByTestId = (instance: ReactTestInstance) => (
|
||||
export const findByTestId = (
|
||||
instance: ReactTestInstance
|
||||
): ((
|
||||
testId: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<ReactTestInstance>) => (
|
||||
testId: string | RegExp,
|
||||
waitForOptions: WaitForOptions = {}
|
||||
) => makeFindQuery(instance, getByTestId, testId, waitForOptions);
|
||||
|
||||
export const findAllByTestId = (instance: ReactTestInstance) => (
|
||||
export const findAllByTestId = (
|
||||
instance: ReactTestInstance
|
||||
): ((
|
||||
testId: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<Array<ReactTestInstance>>) => (
|
||||
testId: string | RegExp,
|
||||
waitForOptions: WaitForOptions = {}
|
||||
) => makeFindQuery(instance, getAllByTestId, testId, waitForOptions);
|
||||
|
||||
export const findByText = (instance: ReactTestInstance) => (
|
||||
export const findByText = (
|
||||
instance: ReactTestInstance
|
||||
): ((
|
||||
text: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<ReactTestInstance>) => (
|
||||
text: string | RegExp,
|
||||
waitForOptions: WaitForOptions = {}
|
||||
) => makeFindQuery(instance, getByText, text, waitForOptions);
|
||||
|
||||
export const findAllByText = (instance: ReactTestInstance) => (
|
||||
export const findAllByText = (
|
||||
instance: ReactTestInstance
|
||||
): ((
|
||||
text: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<Array<ReactTestInstance>>) => (
|
||||
text: string | RegExp,
|
||||
waitForOptions: WaitForOptions = {}
|
||||
) => makeFindQuery(instance, getAllByText, text, waitForOptions);
|
||||
|
||||
export const findByPlaceholderText = (instance: ReactTestInstance) => (
|
||||
export const findByPlaceholderText = (
|
||||
instance: ReactTestInstance
|
||||
): ((
|
||||
placeholder: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<ReactTestInstance>) => (
|
||||
placeholder: string | RegExp,
|
||||
waitForOptions: WaitForOptions = {}
|
||||
) => makeFindQuery(instance, getByPlaceholderText, placeholder, waitForOptions);
|
||||
|
||||
export const findAllByPlaceholderText = (instance: ReactTestInstance) => (
|
||||
export const findAllByPlaceholderText = (
|
||||
instance: ReactTestInstance
|
||||
): ((
|
||||
placeholder: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<Array<ReactTestInstance>>) => (
|
||||
placeholder: string | RegExp,
|
||||
waitForOptions: WaitForOptions = {}
|
||||
) =>
|
||||
makeFindQuery(instance, getAllByPlaceholderText, placeholder, waitForOptions);
|
||||
|
||||
export const findByDisplayValue = (instance: ReactTestInstance) => (
|
||||
export const findByDisplayValue = (
|
||||
instance: ReactTestInstance
|
||||
): ((
|
||||
value: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<ReactTestInstance>) => (
|
||||
value: string | RegExp,
|
||||
waitForOptions: WaitForOptions = {}
|
||||
) => makeFindQuery(instance, getByDisplayValue, value, waitForOptions);
|
||||
|
||||
export const findAllByDisplayValue = (instance: ReactTestInstance) => (
|
||||
export const findAllByDisplayValue = (
|
||||
instance: ReactTestInstance
|
||||
): ((
|
||||
value: string | RegExp,
|
||||
waitForOptions?: WaitForOptions
|
||||
) => Promise<Array<ReactTestInstance>>) => (
|
||||
value: string | RegExp,
|
||||
waitForOptions: WaitForOptions = {}
|
||||
) => makeFindQuery(instance, getAllByDisplayValue, value, waitForOptions);
|
||||
|
||||
export const findByAPI = (instance: ReactTestInstance) => ({
|
||||
export const findByAPI = (instance: ReactTestInstance): FindByAPI => ({
|
||||
findByTestId: findByTestId(instance),
|
||||
findByText: findByText(instance),
|
||||
findByPlaceholderText: findByPlaceholderText(instance),
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// @flow
|
||||
import prettyFormat, { plugins } from 'pretty-format';
|
||||
|
||||
const format = (input: ?ReactTestRendererJSON) =>
|
||||
const format = (input: ?ReactTestRendererJSON): typeof prettyFormat =>
|
||||
prettyFormat(input, {
|
||||
plugins: [plugins.ReactTestComponent, plugins.ReactElement],
|
||||
highlight: true,
|
||||
|
||||
+68
-13
@@ -9,6 +9,37 @@ import {
|
||||
throwRenamedFunctionError,
|
||||
} from './errors';
|
||||
|
||||
export type GetByAPI = {|
|
||||
getByText: (text: string | RegExp) => ReactTestInstance,
|
||||
getByPlaceholderText: (placeholder: string | RegExp) => ReactTestInstance,
|
||||
getByDisplayValue: (value: string | RegExp) => ReactTestInstance,
|
||||
getByTestId: (testID: string | RegExp) => ReactTestInstance,
|
||||
getAllByTestId: (testID: string | RegExp) => Array<ReactTestInstance>,
|
||||
getAllByText: (text: string | RegExp) => Array<ReactTestInstance>,
|
||||
getAllByPlaceholderText: (
|
||||
placeholder: string | RegExp
|
||||
) => Array<ReactTestInstance>,
|
||||
getAllByDisplayValue: (value: string | RegExp) => Array<ReactTestInstance>,
|
||||
|
||||
// Unsafe aliases
|
||||
UNSAFE_getByType: <P>(type: React.ComponentType<P>) => ReactTestInstance,
|
||||
UNSAFE_getAllByType: <P>(
|
||||
type: React.ComponentType<P>
|
||||
) => Array<ReactTestInstance>,
|
||||
UNSAFE_getByProps: (props: { [string]: any }) => ReactTestInstance,
|
||||
UNSAFE_getAllByProps: (props: { [string]: any }) => Array<ReactTestInstance>,
|
||||
|
||||
getByName: () => void,
|
||||
getByType: () => void,
|
||||
getByProps: () => void,
|
||||
getAllByName: () => void,
|
||||
getAllByType: () => void,
|
||||
getAllByProps: () => void,
|
||||
|
||||
getByPlaceholder: () => void,
|
||||
getAllByPlaceholder: () => void,
|
||||
|};
|
||||
|
||||
const filterNodeByType = (node, type) => node.type === type;
|
||||
|
||||
const getNodeByText = (node, text) => {
|
||||
@@ -92,7 +123,9 @@ const getNodeByTestId = (node, testID) => {
|
||||
: testID.test(node.props.testID);
|
||||
};
|
||||
|
||||
export const getByText = (instance: ReactTestInstance) =>
|
||||
export const getByText = (
|
||||
instance: ReactTestInstance
|
||||
): ((text: string | RegExp) => ReactTestInstance) =>
|
||||
function getByTextFn(text: string | RegExp) {
|
||||
try {
|
||||
return instance.find((node) => getNodeByText(node, text));
|
||||
@@ -104,7 +137,9 @@ export const getByText = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const getByPlaceholderText = (instance: ReactTestInstance) =>
|
||||
export const getByPlaceholderText = (
|
||||
instance: ReactTestInstance
|
||||
): ((placeholder: string | RegExp) => ReactTestInstance) =>
|
||||
function getByPlaceholderTextFn(placeholder: string | RegExp) {
|
||||
try {
|
||||
return instance.find((node) =>
|
||||
@@ -118,7 +153,9 @@ export const getByPlaceholderText = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const getByDisplayValue = (instance: ReactTestInstance) =>
|
||||
export const getByDisplayValue = (
|
||||
instance: ReactTestInstance
|
||||
): ((displayValue: string | RegExp) => ReactTestInstance) =>
|
||||
function getByDisplayValueFn(displayValue: string | RegExp) {
|
||||
try {
|
||||
return instance.find((node) =>
|
||||
@@ -132,7 +169,9 @@ export const getByDisplayValue = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const getByTestId = (instance: ReactTestInstance) =>
|
||||
export const getByTestId = (
|
||||
instance: ReactTestInstance
|
||||
): ((testID: string | RegExp) => ReactTestInstance) =>
|
||||
function getByTestIdFn(testID: string | RegExp) {
|
||||
try {
|
||||
const results = getAllByTestId(instance)(testID);
|
||||
@@ -151,7 +190,9 @@ export const getByTestId = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const getAllByText = (instance: ReactTestInstance) =>
|
||||
export const getAllByText = (
|
||||
instance: ReactTestInstance
|
||||
): ((text: string | RegExp) => Array<ReactTestInstance>) =>
|
||||
function getAllByTextFn(text: string | RegExp) {
|
||||
const results = instance.findAll((node) => getNodeByText(node, text));
|
||||
if (results.length === 0) {
|
||||
@@ -163,7 +204,9 @@ export const getAllByText = (instance: ReactTestInstance) =>
|
||||
return results;
|
||||
};
|
||||
|
||||
export const getAllByPlaceholderText = (instance: ReactTestInstance) =>
|
||||
export const getAllByPlaceholderText = (
|
||||
instance: ReactTestInstance
|
||||
): ((placeholder: string | RegExp) => Array<ReactTestInstance>) =>
|
||||
function getAllByPlaceholderTextFn(placeholder: string | RegExp) {
|
||||
const results = instance.findAll((node) =>
|
||||
getTextInputNodeByPlaceholderText(node, placeholder)
|
||||
@@ -177,7 +220,9 @@ export const getAllByPlaceholderText = (instance: ReactTestInstance) =>
|
||||
return results;
|
||||
};
|
||||
|
||||
export const getAllByDisplayValue = (instance: ReactTestInstance) =>
|
||||
export const getAllByDisplayValue = (
|
||||
instance: ReactTestInstance
|
||||
): ((value: string | RegExp) => Array<ReactTestInstance>) =>
|
||||
function getAllByDisplayValueFn(value: string | RegExp) {
|
||||
const results = instance.findAll((node) =>
|
||||
getTextInputNodeByDisplayValue(node, value)
|
||||
@@ -191,7 +236,9 @@ export const getAllByDisplayValue = (instance: ReactTestInstance) =>
|
||||
return results;
|
||||
};
|
||||
|
||||
export const getAllByTestId = (instance: ReactTestInstance) =>
|
||||
export const getAllByTestId = (
|
||||
instance: ReactTestInstance
|
||||
): ((testID: string | RegExp) => Array<ReactTestInstance>) =>
|
||||
function getAllByTestIdFn(testID: string | RegExp): ReactTestInstance[] {
|
||||
const results = instance
|
||||
.findAll((node) => getNodeByTestId(node, testID))
|
||||
@@ -206,7 +253,9 @@ export const getAllByTestId = (instance: ReactTestInstance) =>
|
||||
return results;
|
||||
};
|
||||
|
||||
export const UNSAFE_getByType = (instance: ReactTestInstance) =>
|
||||
export const UNSAFE_getByType = (
|
||||
instance: ReactTestInstance
|
||||
): ((type: React.ComponentType<any>) => ReactTestInstance) =>
|
||||
function getByTypeFn(type: React.ComponentType<any>) {
|
||||
try {
|
||||
return instance.findByType(type);
|
||||
@@ -215,7 +264,9 @@ export const UNSAFE_getByType = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const UNSAFE_getByProps = (instance: ReactTestInstance) =>
|
||||
export const UNSAFE_getByProps = (
|
||||
instance: ReactTestInstance
|
||||
): ((props: { [propName: string]: any }) => ReactTestInstance) =>
|
||||
function getByPropsFn(props: { [propName: string]: any }) {
|
||||
try {
|
||||
return instance.findByProps(props);
|
||||
@@ -224,7 +275,9 @@ export const UNSAFE_getByProps = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const UNSAFE_getAllByType = (instance: ReactTestInstance) =>
|
||||
export const UNSAFE_getAllByType = (
|
||||
instance: ReactTestInstance
|
||||
): ((type: React.ComponentType<any>) => Array<ReactTestInstance>) =>
|
||||
function getAllByTypeFn(type: React.ComponentType<any>) {
|
||||
const results = instance.findAllByType(type);
|
||||
if (results.length === 0) {
|
||||
@@ -233,7 +286,9 @@ export const UNSAFE_getAllByType = (instance: ReactTestInstance) =>
|
||||
return results;
|
||||
};
|
||||
|
||||
export const UNSAFE_getAllByProps = (instance: ReactTestInstance) =>
|
||||
export const UNSAFE_getAllByProps = (
|
||||
instance: ReactTestInstance
|
||||
): ((props: { [propName: string]: any }) => Array<ReactTestInstance>) =>
|
||||
function getAllByPropsFn(props: { [propName: string]: any }) {
|
||||
const results = instance.findAllByProps(props);
|
||||
if (results.length === 0) {
|
||||
@@ -245,7 +300,7 @@ export const UNSAFE_getAllByProps = (instance: ReactTestInstance) =>
|
||||
return results;
|
||||
};
|
||||
|
||||
export const getByAPI = (instance: ReactTestInstance) => ({
|
||||
export const getByAPI = (instance: ReactTestInstance): GetByAPI => ({
|
||||
getByText: getByText(instance),
|
||||
getByPlaceholderText: getByPlaceholderText(instance),
|
||||
getByDisplayValue: getByDisplayValue(instance),
|
||||
|
||||
@@ -30,7 +30,9 @@ const makeQuery = <P: mixed, M: mixed>(
|
||||
name: string,
|
||||
queryNames: QueryNames,
|
||||
matcherFn: (prop: P, value: M) => boolean
|
||||
) => (instance: ReactTestInstance) => {
|
||||
): ((instance: ReactTestInstance) => { ... }) => (
|
||||
instance: ReactTestInstance
|
||||
) => {
|
||||
const getBy = (matcher: M) => {
|
||||
try {
|
||||
return instance.find(
|
||||
|
||||
+74
-14
@@ -20,7 +20,45 @@ import {
|
||||
throwRenamedFunctionError,
|
||||
} from './errors';
|
||||
|
||||
export const queryByText = (instance: ReactTestInstance) =>
|
||||
export type QueryByAPI = {|
|
||||
queryByText: (name: string | RegExp) => ReactTestInstance | null,
|
||||
queryByPlaceholderText: (
|
||||
placeholder: string | RegExp
|
||||
) => ReactTestInstance | null,
|
||||
queryByDisplayValue: (value: string | RegExp) => ReactTestInstance | null,
|
||||
queryByTestId: (testID: string | RegExp) => ReactTestInstance | null,
|
||||
queryAllByTestId: (testID: string | RegExp) => Array<ReactTestInstance>,
|
||||
queryAllByText: (text: string | RegExp) => Array<ReactTestInstance>,
|
||||
queryAllByPlaceholderText: (
|
||||
placeholder: string | RegExp
|
||||
) => Array<ReactTestInstance>,
|
||||
queryAllByDisplayValue: (value: string | RegExp) => Array<ReactTestInstance>,
|
||||
|
||||
// Unsafe aliases
|
||||
UNSAFE_queryByType: <P>(
|
||||
type: React.ComponentType<P>
|
||||
) => ReactTestInstance | null,
|
||||
UNSAFE_queryAllByType: <P>(
|
||||
type: React.ComponentType<P>
|
||||
) => Array<ReactTestInstance>,
|
||||
UNSAFE_queryByProps: (props: { [string]: any }) => ReactTestInstance | null,
|
||||
UNSAFE_queryAllByProps: (props: {
|
||||
[string]: any,
|
||||
}) => Array<ReactTestInstance>,
|
||||
queryByName: () => void,
|
||||
queryByType: () => void,
|
||||
queryByProps: () => void,
|
||||
queryAllByName: () => void,
|
||||
queryAllByType: () => void,
|
||||
queryAllByProps: () => void,
|
||||
|
||||
queryByPlaceholder: () => void,
|
||||
queryAllByPlaceholder: () => void,
|
||||
|};
|
||||
|
||||
export const queryByText = (
|
||||
instance: ReactTestInstance
|
||||
): ((text: string | RegExp) => ReactTestInstance | null) =>
|
||||
function queryByTextFn(text: string | RegExp) {
|
||||
try {
|
||||
return getByText(instance)(text);
|
||||
@@ -29,7 +67,9 @@ export const queryByText = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const queryByPlaceholderText = (instance: ReactTestInstance) =>
|
||||
export const queryByPlaceholderText = (
|
||||
instance: ReactTestInstance
|
||||
): ((placeholder: string | RegExp) => ReactTestInstance | null) =>
|
||||
function queryByPlaceholderTextFn(placeholder: string | RegExp) {
|
||||
try {
|
||||
return getByPlaceholderText(instance)(placeholder);
|
||||
@@ -38,7 +78,9 @@ export const queryByPlaceholderText = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const queryByDisplayValue = (instance: ReactTestInstance) =>
|
||||
export const queryByDisplayValue = (
|
||||
instance: ReactTestInstance
|
||||
): ((value: string | RegExp) => ReactTestInstance | null) =>
|
||||
function queryByDisplayValueFn(value: string | RegExp) {
|
||||
try {
|
||||
return getByDisplayValue(instance)(value);
|
||||
@@ -47,7 +89,9 @@ export const queryByDisplayValue = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const queryByTestId = (instance: ReactTestInstance) =>
|
||||
export const queryByTestId = (
|
||||
instance: ReactTestInstance
|
||||
): ((testID: string | RegExp) => ReactTestInstance | null) =>
|
||||
function queryByTestIdFn(testID: string | RegExp) {
|
||||
try {
|
||||
return getByTestId(instance)(testID);
|
||||
@@ -56,7 +100,9 @@ export const queryByTestId = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const queryAllByText = (instance: ReactTestInstance) => (
|
||||
export const queryAllByText = (
|
||||
instance: ReactTestInstance
|
||||
): ((text: string | RegExp) => Array<ReactTestInstance>) => (
|
||||
text: string | RegExp
|
||||
) => {
|
||||
try {
|
||||
@@ -66,7 +112,9 @@ export const queryAllByText = (instance: ReactTestInstance) => (
|
||||
}
|
||||
};
|
||||
|
||||
export const queryAllByPlaceholderText = (instance: ReactTestInstance) => (
|
||||
export const queryAllByPlaceholderText = (
|
||||
instance: ReactTestInstance
|
||||
): ((placeholder: string | RegExp) => Array<ReactTestInstance>) => (
|
||||
placeholder: string | RegExp
|
||||
) => {
|
||||
try {
|
||||
@@ -76,7 +124,9 @@ export const queryAllByPlaceholderText = (instance: ReactTestInstance) => (
|
||||
}
|
||||
};
|
||||
|
||||
export const queryAllByDisplayValue = (instance: ReactTestInstance) => (
|
||||
export const queryAllByDisplayValue = (
|
||||
instance: ReactTestInstance
|
||||
): ((value: string | RegExp) => Array<ReactTestInstance>) => (
|
||||
value: string | RegExp
|
||||
) => {
|
||||
try {
|
||||
@@ -86,7 +136,9 @@ export const queryAllByDisplayValue = (instance: ReactTestInstance) => (
|
||||
}
|
||||
};
|
||||
|
||||
export const queryAllByTestId = (instance: ReactTestInstance) => (
|
||||
export const queryAllByTestId = (
|
||||
instance: ReactTestInstance
|
||||
): ((testID: string | RegExp) => Array<ReactTestInstance>) => (
|
||||
testID: string | RegExp
|
||||
) => {
|
||||
try {
|
||||
@@ -96,7 +148,9 @@ export const queryAllByTestId = (instance: ReactTestInstance) => (
|
||||
}
|
||||
};
|
||||
|
||||
export const UNSAFE_queryByType = (instance: ReactTestInstance) =>
|
||||
export const UNSAFE_queryByType = (
|
||||
instance: ReactTestInstance
|
||||
): ((type: React.ComponentType<any>) => ReactTestInstance | null) =>
|
||||
function queryByTypeFn(type: React.ComponentType<any>) {
|
||||
try {
|
||||
return UNSAFE_getByType(instance)(type);
|
||||
@@ -105,7 +159,9 @@ export const UNSAFE_queryByType = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const UNSAFE_queryByProps = (instance: ReactTestInstance) =>
|
||||
export const UNSAFE_queryByProps = (
|
||||
instance: ReactTestInstance
|
||||
): ((props: { [propName: string]: any }) => ReactTestInstance | null) =>
|
||||
function queryByPropsFn(props: { [propName: string]: any }) {
|
||||
try {
|
||||
return UNSAFE_getByProps(instance)(props);
|
||||
@@ -114,7 +170,9 @@ export const UNSAFE_queryByProps = (instance: ReactTestInstance) =>
|
||||
}
|
||||
};
|
||||
|
||||
export const UNSAFE_queryAllByType = (instance: ReactTestInstance) => (
|
||||
export const UNSAFE_queryAllByType = (
|
||||
instance: ReactTestInstance
|
||||
): ((type: React.ComponentType<any>) => Array<ReactTestInstance>) => (
|
||||
type: React.ComponentType<any>
|
||||
) => {
|
||||
try {
|
||||
@@ -124,9 +182,11 @@ export const UNSAFE_queryAllByType = (instance: ReactTestInstance) => (
|
||||
}
|
||||
};
|
||||
|
||||
export const UNSAFE_queryAllByProps = (instance: ReactTestInstance) => (props: {
|
||||
export const UNSAFE_queryAllByProps = (
|
||||
instance: ReactTestInstance
|
||||
): ((props: {
|
||||
[propName: string]: any,
|
||||
}) => {
|
||||
}) => Array<ReactTestInstance>) => (props: { [propName: string]: any }) => {
|
||||
try {
|
||||
return UNSAFE_getAllByProps(instance)(props);
|
||||
} catch (error) {
|
||||
@@ -134,7 +194,7 @@ export const UNSAFE_queryAllByProps = (instance: ReactTestInstance) => (props: {
|
||||
}
|
||||
};
|
||||
|
||||
export const queryByAPI = (instance: ReactTestInstance) => ({
|
||||
export const queryByAPI = (instance: ReactTestInstance): QueryByAPI => ({
|
||||
queryByTestId: queryByTestId(instance),
|
||||
queryByText: queryByText(instance),
|
||||
queryByPlaceholderText: queryByPlaceholderText(instance),
|
||||
|
||||
+25
-6
@@ -3,10 +3,10 @@ import * as React from 'react';
|
||||
import TestRenderer, { type ReactTestRenderer } from 'react-test-renderer'; // eslint-disable-line import/no-extraneous-dependencies
|
||||
import act from './act';
|
||||
import { addToCleanupQueue } from './cleanup';
|
||||
import { getByAPI } from './helpers/getByAPI';
|
||||
import { queryByAPI } from './helpers/queryByAPI';
|
||||
import { findByAPI } from './helpers/findByAPI';
|
||||
import a11yAPI from './helpers/a11yAPI';
|
||||
import { getByAPI, type GetByAPI } from './helpers/getByAPI';
|
||||
import { queryByAPI, type QueryByAPI } from './helpers/queryByAPI';
|
||||
import { findByAPI, type FindByAPI } from './helpers/findByAPI';
|
||||
import { a11yAPI, type A11yAPI } from './helpers/a11yAPI';
|
||||
import debugShallow from './helpers/debugShallow';
|
||||
import debugDeep from './helpers/debugDeep';
|
||||
|
||||
@@ -25,7 +25,18 @@ type TestRendererOptions = {
|
||||
export default function render<T>(
|
||||
component: React.Element<T>,
|
||||
{ wrapper: Wrapper, createNodeMock }: Options = {}
|
||||
) {
|
||||
): {
|
||||
...FindByAPI,
|
||||
...QueryByAPI,
|
||||
...GetByAPI,
|
||||
...A11yAPI,
|
||||
update: (component: React.Element<any>) => void,
|
||||
container: ReactTestInstance,
|
||||
rerender: (component: React.Element<any>) => void,
|
||||
unmount: (nextElement?: React.Element<any>) => void,
|
||||
toJSON: () => null | ReactTestRendererJSON,
|
||||
debug: DebugFunction,
|
||||
} {
|
||||
const wrap = (innerElement: React.Element<any>) =>
|
||||
Wrapper ? <Wrapper>{innerElement}</Wrapper> : innerElement;
|
||||
|
||||
@@ -76,7 +87,15 @@ function updateWithAct(
|
||||
};
|
||||
}
|
||||
|
||||
function debug(instance: ReactTestInstance, renderer) {
|
||||
interface DebugFunction {
|
||||
(message?: string): void;
|
||||
shallow: (message?: string) => void;
|
||||
}
|
||||
|
||||
function debug(
|
||||
instance: ReactTestInstance,
|
||||
renderer: ReactTestRenderer
|
||||
): DebugFunction {
|
||||
function debugImpl(message?: string) {
|
||||
return debugDeep(renderer.toJSON(), message);
|
||||
}
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ import { throwRemovedFunctionError } from './helpers/errors';
|
||||
*/
|
||||
export function shallowInternal(
|
||||
instance: ReactTestInstance | React.Element<any>
|
||||
) {
|
||||
): {| output: any |} {
|
||||
const renderer = new ShallowRenderer();
|
||||
|
||||
renderer.render(React.createElement(instance.type, instance.props));
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
// @flow
|
||||
|
||||
export type Thenable = {
|
||||
then(resolve: () => mixed, reject?: () => mixed): mixed,
|
||||
};
|
||||
|
||||
export type A11yRole =
|
||||
| 'none'
|
||||
| 'button'
|
||||
|
||||
@@ -2,8 +2,9 @@
|
||||
import waitFor, { type WaitForOptions } from './waitFor';
|
||||
import { ErrorWithStack } from './helpers/errors';
|
||||
|
||||
const isRemoved = (result) =>
|
||||
!result || (Array.isArray(result) && !result.length);
|
||||
function isRemoved<T>(result: T): boolean {
|
||||
return !result || (Array.isArray(result) && !result.length);
|
||||
}
|
||||
|
||||
export default async function waitForElementToBeRemoved<T>(
|
||||
expectation: () => T,
|
||||
|
||||
+7
-5
@@ -1,10 +1,12 @@
|
||||
// @flow
|
||||
import { getByAPI } from './helpers/getByAPI';
|
||||
import { queryByAPI } from './helpers/queryByAPI';
|
||||
import { findByAPI } from './helpers/findByAPI';
|
||||
import a11yAPI from './helpers/a11yAPI';
|
||||
import { getByAPI, type GetByAPI } from './helpers/getByAPI';
|
||||
import { queryByAPI, type QueryByAPI } from './helpers/queryByAPI';
|
||||
import { findByAPI, type FindByAPI } from './helpers/findByAPI';
|
||||
import { a11yAPI, type A11yAPI } from './helpers/a11yAPI';
|
||||
|
||||
export function within(instance: ReactTestInstance) {
|
||||
export function within(
|
||||
instance: ReactTestInstance
|
||||
): { ...FindByAPI, ...QueryByAPI, ...GetByAPI, ...A11yAPI } {
|
||||
return {
|
||||
...getByAPI(instance),
|
||||
...queryByAPI(instance),
|
||||
|
||||
Reference in New Issue
Block a user