Skip to content

Commit 9252947

Browse files
committed
chore: bring back waitForOptions compatibility
1 parent ad5749e commit 9252947

File tree

2 files changed

+85
-15
lines changed

2 files changed

+85
-15
lines changed

src/queries/__tests__/labelText.test.tsx

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as React from 'react';
2-
import { TouchableOpacity, Text } from 'react-native';
2+
import { View, Text, TouchableOpacity } from 'react-native';
33
import { render } from '../..';
44

55
const BUTTON_LABEL = 'cool button';
@@ -114,3 +114,32 @@ test('getAllByLabelText, queryAllByLabelText, findAllByLabelText with exact as f
114114
findAllByLabelText(NO_MATCHES_TEXT, { exact: false })
115115
).rejects.toThrow(getNoInstancesFoundMessage(NO_MATCHES_TEXT));
116116
});
117+
118+
describe('findBy options deprecations', () => {
119+
let warnSpy: jest.SpyInstance;
120+
beforeEach(() => {
121+
warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
122+
});
123+
afterEach(() => {
124+
warnSpy.mockRestore();
125+
});
126+
127+
test('findByText queries warn on deprecated use of WaitForOptions', async () => {
128+
const options = { timeout: 10 };
129+
// mock implementation to avoid warning in the test suite
130+
const view = render(<View />);
131+
await expect(
132+
view.findByLabelText('Some Text', options)
133+
).rejects.toBeTruthy();
134+
135+
setTimeout(
136+
() => view.rerender(<View accessibilityLabel="Some Text" />),
137+
20
138+
);
139+
await expect(view.findByLabelText('Some Text')).resolves.toBeTruthy();
140+
141+
expect(warnSpy).toHaveBeenCalledWith(
142+
expect.stringContaining('Use of option "timeout"')
143+
);
144+
}, 20000);
145+
});

src/queries/makeQueries.ts

Lines changed: 55 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ export type QueryAllByQuery<Predicate, Options = void> = (
2525

2626
export type FindByQuery<Predicate, Options = void> = (
2727
predicate: Predicate,
28-
options?: Options,
28+
// Remove `& WaitForOptions` when all queries have been migrated to support 2nd arg query options.
29+
options?: Options & WaitForOptions,
2930
waitForOptions?: WaitForOptions
3031
) => Promise<ReactTestInstance>;
3132

3233
export type FindAllByQuery<Predicate, Options = void> = (
3334
predicate: Predicate,
34-
options?: Options,
35+
// Remove `& WaitForOptions` when all queries have been migrated to support 2nd arg query options.
36+
options?: Options & WaitForOptions,
3537
waitForOptions?: WaitForOptions
3638
) => Promise<ReactTestInstance[]>;
3739

@@ -46,6 +48,41 @@ export type UnboundQueries<Predicate, Options> = {
4648
findAllBy: UnboundQuery<FindAllByQuery<Predicate, Options>>;
4749
};
4850

51+
const deprecatedKeys: (keyof WaitForOptions)[] = [
52+
'timeout',
53+
'interval',
54+
'stackTraceError',
55+
];
56+
57+
// The WaitForOptions has been moved to the second option param of findBy* methods with the adding of TextMatchOptions
58+
// To make the migration easier and avoid a breaking change, keep reading this options from the first param but warn
59+
function extractDeprecatedWaitForOptions(options?: WaitForOptions) {
60+
if (!options) {
61+
return undefined;
62+
}
63+
64+
const waitForOptions: WaitForOptions = {
65+
timeout: options.timeout,
66+
interval: options.interval,
67+
stackTraceError: options.stackTraceError,
68+
};
69+
70+
deprecatedKeys.forEach((key) => {
71+
const option = options[key];
72+
if (option) {
73+
// eslint-disable-next-line no-console
74+
console.warn(
75+
`Use of option "${key}" in a findBy* query options (2nd parameter) is deprecated. Please pass this option in the waitForOptions (3rd parameter).
76+
Example:
77+
78+
findByText(text, {}, { ${key}: ${option.toString()} })`
79+
);
80+
}
81+
});
82+
83+
return waitForOptions;
84+
}
85+
4986
export function makeQueries<Predicate, Options>(
5087
queryAllByQuery: UnboundQuery<QueryAllByQuery<Predicate, Options>>,
5188
getMissingError: (predicate: Predicate, options?: Options) => string,
@@ -101,26 +138,30 @@ export function makeQueries<Predicate, Options>(
101138
function findAllByQuery(instance: ReactTestInstance) {
102139
return function findAllFn(
103140
predicate: Predicate,
104-
queryOptions?: Options,
105-
waitForOptions?: WaitForOptions
141+
queryOptions?: Options & WaitForOptions,
142+
waitForOptions: WaitForOptions = {}
106143
) {
107-
return waitFor(
108-
() => getAllByQuery(instance)(predicate, queryOptions),
109-
waitForOptions
110-
);
144+
const deprecatedWaitForOptions =
145+
extractDeprecatedWaitForOptions(queryOptions);
146+
return waitFor(() => getAllByQuery(instance)(predicate, queryOptions), {
147+
...deprecatedWaitForOptions,
148+
...waitForOptions,
149+
});
111150
};
112151
}
113152

114153
function findByQuery(instance: ReactTestInstance) {
115154
return function findFn(
116155
predicate: Predicate,
117-
queryOptions?: Options,
118-
waitForOptions?: WaitForOptions
156+
queryOptions?: Options & WaitForOptions,
157+
waitForOptions: WaitForOptions = {}
119158
) {
120-
return waitFor(
121-
() => getByQuery(instance)(predicate, queryOptions),
122-
waitForOptions
123-
);
159+
const deprecatedWaitForOptions =
160+
extractDeprecatedWaitForOptions(queryOptions);
161+
return waitFor(() => getByQuery(instance)(predicate, queryOptions), {
162+
...deprecatedWaitForOptions,
163+
...waitForOptions,
164+
});
124165
};
125166
}
126167

0 commit comments

Comments
 (0)