Skip to content

Commit 29c2d6f

Browse files
committed
support backward compatibility for findBy*
- adds tests for deprecations - change second param on findBy queries
1 parent 0405619 commit 29c2d6f

File tree

2 files changed

+104
-24
lines changed

2 files changed

+104
-24
lines changed

src/__tests__/a11yAPI.test.js

Lines changed: 53 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// @flow
22
import * as React from 'react';
3-
import { TouchableOpacity, Text } from 'react-native';
3+
import { TouchableOpacity, Text, View } from 'react-native';
44
import { render } from '..';
55

66
const BUTTON_LABEL = 'cool button';
@@ -110,10 +110,10 @@ test('getByA11yLabel, queryByA11yLabel, findByA11yLabel', async () => {
110110
const asyncButton = await findByA11yLabel(BUTTON_LABEL);
111111
expect(asyncButton.props.accessibilityLabel).toEqual(BUTTON_LABEL);
112112
await expect(
113-
findByA11yLabel(NO_MATCHES_TEXT, waitForOptions)
113+
findByA11yLabel(NO_MATCHES_TEXT, {}, waitForOptions)
114114
).rejects.toThrow(getNoInstancesFoundMessage('accessibilityLabel'));
115115

116-
await expect(findByA11yLabel(TEXT_LABEL, waitForOptions)).rejects.toThrow(
116+
await expect(findByA11yLabel(TEXT_LABEL, {}, waitForOptions)).rejects.toThrow(
117117
FOUND_TWO_INSTANCES
118118
);
119119
});
@@ -158,10 +158,10 @@ test('getByA11yHint, queryByA11yHint, findByA11yHint', async () => {
158158

159159
const asyncButton = await findByA11yHint(BUTTON_HINT);
160160
expect(asyncButton.props.accessibilityHint).toEqual(BUTTON_HINT);
161-
await expect(findByA11yHint(NO_MATCHES_TEXT, waitForOptions)).rejects.toThrow(
162-
getNoInstancesFoundMessage('accessibilityHint')
163-
);
164-
await expect(findByA11yHint(TEXT_HINT, waitForOptions)).rejects.toThrow(
161+
await expect(
162+
findByA11yHint(NO_MATCHES_TEXT, {}, waitForOptions)
163+
).rejects.toThrow(getNoInstancesFoundMessage('accessibilityHint'));
164+
await expect(findByA11yHint(TEXT_HINT, {}, waitForOptions)).rejects.toThrow(
165165
FOUND_TWO_INSTANCES
166166
);
167167
});
@@ -229,7 +229,7 @@ test('getByA11yRole, queryByA11yRole, findByA11yRole with name', async () => {
229229
name: ONE_OCCURANCE,
230230
});
231231
await expect(
232-
findByA11yRole('button', { ...waitForOptions, name: NO_MATCHES_TEXT })
232+
findByA11yRole('button', { name: NO_MATCHES_TEXT }, waitForOptions)
233233
).rejects.toThrow(getNoInstancesFoundMessage('accessibilityRole', 'button'));
234234
await expect(
235235
findByA11yRole('button', {
@@ -257,6 +257,39 @@ test('getAllByA11yRole, queryAllByA11yRole, findAllByA11yRole', async () => {
257257
).rejects.toThrow(getNoInstancesFoundMessage('accessibilityRole'));
258258
});
259259

260+
describe('findBy options deprecations', () => {
261+
let warnSpy;
262+
beforeEach(() => {
263+
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
264+
});
265+
afterEach(() => {
266+
warnSpy.mockRestore();
267+
});
268+
269+
test('findByText queries warn on deprecated use of WaitForOptions', async () => {
270+
const options = { timeout: 10 };
271+
// mock implementation to avoid warning in the test suite
272+
const { rerender, findByText } = render(<View />);
273+
await expect(findByText('Some Text', options)).rejects.toBeTruthy();
274+
275+
setTimeout(
276+
() =>
277+
rerender(
278+
<View>
279+
<Text>Some Text</Text>
280+
</View>
281+
),
282+
20
283+
);
284+
285+
await expect(findByText('Some Text')).resolves.toBeTruthy();
286+
287+
expect(warnSpy).toHaveBeenCalledWith(
288+
expect.stringContaining('Use of option "timeout"')
289+
);
290+
}, 20000);
291+
});
292+
260293
test('getAllByA11yRole, queryAllByA11yRole, findAllByA11yRole with name', async () => {
261294
const { getAllByA11yRole, queryAllByA11yRole, findAllByA11yRole } = render(
262295
<ButtonsWithText />
@@ -358,7 +391,7 @@ test('getByA11yState, queryByA11yState, findByA11yState', async () => {
358391
expanded: false,
359392
});
360393
await expect(
361-
findByA11yState({ disabled: true }, waitForOptions)
394+
findByA11yState({ disabled: true }, {}, waitForOptions)
362395
).rejects.toThrow(
363396
getNoInstancesFoundMessage(
364397
'accessibilityState',
@@ -367,7 +400,7 @@ test('getByA11yState, queryByA11yState, findByA11yState', async () => {
367400
)
368401
);
369402
await expect(
370-
findByA11yState({ expanded: false }, waitForOptions)
403+
findByA11yState({ expanded: false }, {}, waitForOptions)
371404
).rejects.toThrow(FOUND_TWO_INSTANCES);
372405
});
373406

@@ -393,7 +426,7 @@ test('getAllByA11yState, queryAllByA11yState, findAllByA11yState', async () => {
393426

394427
await expect(findAllByA11yState({ selected: true })).resolves.toHaveLength(1);
395428
await expect(
396-
findAllByA11yState({ disabled: true }, waitForOptions)
429+
findAllByA11yState({ disabled: true }, {}, waitForOptions)
397430
).rejects.toThrow(
398431
getNoInstancesFoundMessage(
399432
'accessibilityState',
@@ -433,12 +466,14 @@ test('getByA11yValue, queryByA11yValue, findByA11yValue', async () => {
433466
min: 40,
434467
max: 60,
435468
});
436-
await expect(findByA11yValue({ min: 50 }, waitForOptions)).rejects.toThrow(
469+
await expect(
470+
findByA11yValue({ min: 50 }, {}, waitForOptions)
471+
).rejects.toThrow(
437472
getNoInstancesFoundMessage('accessibilityValue', '{"min": 50}', false)
438473
);
439-
await expect(findByA11yValue({ max: 60 }, waitForOptions)).rejects.toThrow(
440-
FOUND_TWO_INSTANCES
441-
);
474+
await expect(
475+
findByA11yValue({ max: 60 }, {}, waitForOptions)
476+
).rejects.toThrow(FOUND_TWO_INSTANCES);
442477
});
443478

444479
test('getAllByA11yValue, queryAllByA11yValue, findAllByA11yValue', async () => {
@@ -458,7 +493,9 @@ test('getAllByA11yValue, queryAllByA11yValue, findAllByA11yValue', async () => {
458493
expect(getAllByA11yValue({ max: 60 })).toHaveLength(2);
459494

460495
await expect(findAllByA11yValue({ min: 40 })).resolves.toHaveLength(1);
461-
await expect(findAllByA11yValue({ min: 50 }, waitForOptions)).rejects.toThrow(
496+
await expect(
497+
findAllByA11yValue({ min: 50 }, {}, waitForOptions)
498+
).rejects.toThrow(
462499
getNoInstancesFoundMessage('accessibilityValue', '{"min": 50}', false)
463500
);
464501
await expect(findAllByA11yValue({ max: 60 })).resolves.toHaveLength(2);

src/helpers/makeA11yQuery.js

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,6 @@ import {
88
createQueryByError,
99
} from './errors';
1010

11-
type QueryOptions = {
12-
name: string | RegExp,
13-
};
14-
1511
function isNodeValid(node: ReactTestInstance) {
1612
return typeof node.type === 'string';
1713
}
@@ -22,6 +18,39 @@ function makeAliases(aliases: Array<string>, query: Function) {
2218
.reduce((acc, query) => ({ ...acc, ...query }), {});
2319
}
2420

21+
// The WaitForOptions has been moved to the third param of findBy* methods with the addition of QueryOptions.
22+
// To make the migration easier and to avoid a breaking change, keep reading these options from second param
23+
// but warn.
24+
const deprecatedKeys: $Keys<WaitForOptions>[] = [
25+
'timeout',
26+
'interval',
27+
'stackTraceError',
28+
];
29+
const warnDeprectedWaitForOptionsUsage = (queryOptions?: WaitForOptions) => {
30+
if (queryOptions) {
31+
const waitForOptions: WaitForOptions = {
32+
timeout: queryOptions.timeout,
33+
interval: queryOptions.interval,
34+
stackTraceError: queryOptions.stackTraceError,
35+
};
36+
deprecatedKeys.forEach((key) => {
37+
if (queryOptions[key]) {
38+
// eslint-disable-next-line no-console
39+
console.warn(
40+
`Use of option "${key}" in a findBy* query's second parameter, QueryOptions, is deprecated. Please pass this option in the third, WaitForOptions, parameter.
41+
Example:
42+
findByText(text, {}, { ${key}: ${queryOptions[key].toString()} })`
43+
);
44+
}
45+
});
46+
return waitForOptions;
47+
}
48+
};
49+
50+
type QueryOptions = {
51+
name: string | RegExp,
52+
};
53+
2554
type QueryNames = {
2655
getBy: Array<string>,
2756
getAllBy: Array<string>,
@@ -112,17 +141,31 @@ const makeA11yQuery = <P: mixed, M: mixed>(
112141
const findBy = (
113142
matcher: M,
114143
queryOptions?: QueryOptions,
115-
waitForOptions?: WaitForOptions
144+
waitForOptions?: WaitForOptions = {}
116145
) => {
117-
return waitFor(() => getBy(matcher, queryOptions), waitForOptions);
146+
const deprecatedWaitForOptions = warnDeprectedWaitForOptionsUsage(
147+
queryOptions
148+
);
149+
150+
return waitFor(() => getBy(matcher, queryOptions), {
151+
...deprecatedWaitForOptions,
152+
...waitForOptions,
153+
});
118154
};
119155

120156
const findAllBy = (
121157
matcher: M,
122158
queryOptions: QueryOptions,
123-
waitForOptions?: WaitForOptions
159+
waitForOptions?: WaitForOptions = {}
124160
) => {
125-
return waitFor(() => getAllBy(matcher, queryOptions), waitForOptions);
161+
const deprecatedWaitForOptions = warnDeprectedWaitForOptionsUsage(
162+
queryOptions
163+
);
164+
165+
return waitFor(() => getAllBy(matcher, queryOptions), {
166+
...deprecatedWaitForOptions,
167+
...waitForOptions,
168+
});
126169
};
127170

128171
return {

0 commit comments

Comments
 (0)