Skip to content

Commit 3a2a2b1

Browse files
committed
make API compatible with TL
1 parent f01e6a3 commit 3a2a2b1

File tree

3 files changed

+26
-20
lines changed

3 files changed

+26
-20
lines changed

src/__tests__/render.test.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -384,16 +384,14 @@ test('renders options.wrapper around updated node', () => {
384384
});
385385

386386
test('returns custom queries added', () => {
387-
const _getByCustom = (instance: ReactTestInstance) =>
388-
function (someArg: string) {
389-
console.log({ instance });
390-
return `You sent: ${someArg}`;
391-
};
387+
const _getByCustom = (instance: ReactTestInstance, someArg: string) => {
388+
return `You sent: ${someArg}`;
389+
};
392390

393391
const { getByCustom } = render(<View />, {
394-
queries: (instance) => ({
395-
getByCustom: _getByCustom(instance),
396-
}),
392+
queries: {
393+
getByCustom: _getByCustom,
394+
},
397395
});
398396

399397
expect(getByCustom('yass!!')).toBe('You sent: yass!!');

src/render.js

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,16 @@ import * as React from 'react';
33
import TestRenderer, { type ReactTestRenderer } from 'react-test-renderer'; // eslint-disable-line import/no-extraneous-dependencies
44
import act from './act';
55
import { addToCleanupQueue } from './cleanup';
6-
import { getByAPI } from './helpers/getByAPI';
7-
import { queryByAPI } from './helpers/queryByAPI';
8-
import { findByAPI } from './helpers/findByAPI';
9-
import a11yAPI from './helpers/a11yAPI';
106
import debugShallow from './helpers/debugShallow';
117
import debugDeep from './helpers/debugDeep';
8+
import { getQueriesForElement } from './within';
129

1310
type Options = {
1411
wrapper?: React.ComponentType<any>,
1512
createNodeMock?: (element: React.Element<any>) => any,
16-
queries?: (instance: ReactTestInstance) => any,
13+
queries?: {
14+
[key: string]: (instance: ReactTestInstance, ...rest: Array<any>) => any,
15+
},
1716
};
1817
type TestRendererOptions = {
1918
createNodeMock: (element: React.Element<any>) => any,
@@ -25,7 +24,7 @@ type TestRendererOptions = {
2524
*/
2625
export default function render<T>(
2726
component: React.Element<T>,
28-
{ wrapper: Wrapper, createNodeMock, queries = () => ({}) }: Options = {}
27+
{ wrapper: Wrapper, createNodeMock, queries = {} }: Options = {}
2928
) {
3029
const wrap = (innerElement: React.Element<any>) =>
3130
Wrapper ? <Wrapper>{innerElement}</Wrapper> : innerElement;
@@ -37,14 +36,17 @@ export default function render<T>(
3736
const update = updateWithAct(renderer, wrap);
3837
const instance = renderer.root;
3938

39+
for (let query in queries) {
40+
queries[query] = queries[query].bind(null, instance);
41+
}
42+
4043
addToCleanupQueue(renderer.unmount);
4144

4245
return {
43-
...getByAPI(instance),
44-
...queryByAPI(instance),
45-
...findByAPI(instance),
46-
...a11yAPI(instance),
47-
...queries(instance),
46+
...(queries: {
47+
[key: $Keys<typeof queries>]: (...rest: Array<any>) => any,
48+
}),
49+
...getQueriesForElement(instance),
4850
update,
4951
rerender: update, // alias for `update`
5052
unmount: renderer.unmount,

typings/index.d.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,10 @@ interface FindByAPI {
137137
value: string | RegExp,
138138
waitForOptions?: WaitForOptions
139139
) => FindReturn;
140-
findByTestId: (testID: string | RegExp, waitForOptions?: WaitForOptions) => FindReturn;
140+
findByTestId: (
141+
testID: string | RegExp,
142+
waitForOptions?: WaitForOptions
143+
) => FindReturn;
141144
findAllByText: (
142145
text: string | RegExp,
143146
waitForOptions?: WaitForOptions
@@ -293,6 +296,9 @@ export interface Thenable {
293296
export interface RenderOptions {
294297
wrapper?: React.ComponentType<any>;
295298
createNodeMock?: (element: React.ReactElement<any>) => any;
299+
queries?: {
300+
[key: string]: (instance: ReactTestInstance, ...rest: Array<any>) => any;
301+
};
296302
}
297303

298304
type Queries = GetByAPI & QueryByAPI & FindByAPI & A11yAPI;

0 commit comments

Comments
 (0)