diff --git a/src/__tests__/render.test.js b/src/__tests__/render.test.js
index ebf846967..7f29a081f 100644
--- a/src/__tests__/render.test.js
+++ b/src/__tests__/render.test.js
@@ -382,3 +382,17 @@ test('renders options.wrapper around updated node', () => {
`);
});
+
+test('uses custom queries', () => {
+ const _getByCustom = (instance: ReactTestInstance, someArg: string) => {
+ return `You sent: ${someArg}`;
+ };
+
+ const { getByCustom } = render(, {
+ queries: {
+ getByCustom: _getByCustom,
+ },
+ });
+
+ expect(getByCustom('yass!!')).toBe('You sent: yass!!');
+});
diff --git a/src/render.js b/src/render.js
index 3adfcc8d7..a427b3ae5 100644
--- a/src/render.js
+++ b/src/render.js
@@ -3,16 +3,16 @@ 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 debugShallow from './helpers/debugShallow';
import debugDeep from './helpers/debugDeep';
+import { getQueriesForElement } from './within';
type Options = {
wrapper?: React.ComponentType,
createNodeMock?: (element: React.Element) => any,
+ queries?: {
+ [key: string]: (instance: ReactTestInstance, ...rest: Array) => any,
+ },
};
type TestRendererOptions = {
createNodeMock: (element: React.Element) => any,
@@ -24,7 +24,7 @@ type TestRendererOptions = {
*/
export default function render(
component: React.Element,
- { wrapper: Wrapper, createNodeMock }: Options = {}
+ { wrapper: Wrapper, createNodeMock, queries = {} }: Options = {}
) {
const wrap = (innerElement: React.Element) =>
Wrapper ? {innerElement} : innerElement;
@@ -36,13 +36,17 @@ export default function render(
const update = updateWithAct(renderer, wrap);
const instance = renderer.root;
+ for (let query in queries) {
+ queries[query] = queries[query].bind(null, instance);
+ }
+
addToCleanupQueue(renderer.unmount);
return {
- ...getByAPI(instance),
- ...queryByAPI(instance),
- ...findByAPI(instance),
- ...a11yAPI(instance),
+ ...(queries: {
+ [key: $Keys]: (...rest: Array) => any,
+ }),
+ ...getQueriesForElement(instance),
update,
rerender: update, // alias for `update`
unmount: renderer.unmount,
diff --git a/typings/index.d.ts b/typings/index.d.ts
index 898e504da..0143b3d22 100644
--- a/typings/index.d.ts
+++ b/typings/index.d.ts
@@ -137,7 +137,10 @@ interface FindByAPI {
value: string | RegExp,
waitForOptions?: WaitForOptions
) => FindReturn;
- findByTestId: (testID: string | RegExp, waitForOptions?: WaitForOptions) => FindReturn;
+ findByTestId: (
+ testID: string | RegExp,
+ waitForOptions?: WaitForOptions
+ ) => FindReturn;
findAllByText: (
text: string | RegExp,
waitForOptions?: WaitForOptions
@@ -293,6 +296,9 @@ export interface Thenable {
export interface RenderOptions {
wrapper?: React.ComponentType;
createNodeMock?: (element: React.ReactElement) => any;
+ queries?: {
+ [key: string]: (instance: ReactTestInstance, ...rest: Array) => any;
+ };
}
type Queries = GetByAPI & QueryByAPI & FindByAPI & A11yAPI;