Skip to content

Commit d2f9f33

Browse files
committed
Make waitForOptions param change non breaking change
1 parent 3bafdff commit d2f9f33

File tree

4 files changed

+80
-21
lines changed

4 files changed

+80
-21
lines changed

src/__tests__/byTestId.test.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,8 @@ test('getAllByTestId, queryAllByTestId', () => {
115115
test('findByTestId and findAllByTestId work asynchronously', async () => {
116116
const options = { timeout: 10 }; // Short timeout so that this test runs quickly
117117
const { rerender, findByTestId, findAllByTestId } = render(<View />);
118-
await expect(findByTestId('aTestId', options)).rejects.toBeTruthy();
119-
await expect(findAllByTestId('aTestId', options)).rejects.toBeTruthy();
118+
await expect(findByTestId('aTestId', {}, options)).rejects.toBeTruthy();
119+
await expect(findAllByTestId('aTestId', {}, options)).rejects.toBeTruthy();
120120

121121
setTimeout(
122122
() =>

src/__tests__/byText.test.js

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ test('getAllByText, queryAllByText', () => {
8888
test('findByText queries work asynchronously', async () => {
8989
const options = { timeout: 10 }; // Short timeout so that this test runs quickly
9090
const { rerender, findByText, findAllByText } = render(<View />);
91-
await expect(findByText('Some Text', options)).rejects.toBeTruthy();
92-
await expect(findAllByText('Some Text', options)).rejects.toBeTruthy();
91+
await expect(findByText('Some Text', {}, options)).rejects.toBeTruthy();
92+
await expect(findAllByText('Some Text', {}, options)).rejects.toBeTruthy();
9393

9494
setTimeout(
9595
() =>
@@ -105,6 +105,31 @@ test('findByText queries work asynchronously', async () => {
105105
await expect(findAllByText('Some Text')).resolves.toHaveLength(1);
106106
}, 20000);
107107

108+
test('findByText queries warn on deprecated use of WaitForOptions', async () => {
109+
const options = { timeout: 10 };
110+
// mock implementation to avoid warning in the test suite
111+
const warnSpy = jest.spyOn(console, 'warn').mockImplementation(() => {});
112+
const { rerender, findByText } = render(<View />);
113+
await expect(findByText('Some Text', options)).rejects.toBeTruthy();
114+
115+
setTimeout(
116+
() =>
117+
rerender(
118+
<View>
119+
<Text>Some Text</Text>
120+
</View>
121+
),
122+
20
123+
);
124+
125+
await expect(findByText('Some Text')).resolves.toBeTruthy();
126+
127+
expect(warnSpy).toHaveBeenCalledWith(
128+
expect.stringContaining('Use of option timeout')
129+
);
130+
// TODO remove mock
131+
}, 20000);
132+
108133
test.skip('getByText works properly with custom text component', () => {
109134
function BoldText({ children }) {
110135
return <Text>{children}</Text>;

src/helpers/findByAPI.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,44 +13,44 @@ import { throwRenamedFunctionError } from './errors';
1313
export type FindByAPI = {|
1414
findAllByDisplayValue: (
1515
value: string | RegExp,
16-
queryOptions?: TextMatchOptions,
16+
queryOptions?: TextMatchOptions & WaitForOptions,
1717
waitForOptions?: WaitForOptions
1818
) => Promise<Array<ReactTestInstance>>,
1919
findAllByPlaceholder: () => void,
2020
findAllByPlaceholderText: (
2121
placeholder: string | RegExp,
22-
queryOptions?: TextMatchOptions,
22+
queryOptions?: TextMatchOptions & WaitForOptions,
2323
waitForOptions?: WaitForOptions
2424
) => Promise<Array<ReactTestInstance>>,
2525
findAllByTestId: (
2626
testId: string | RegExp,
27-
queryOptions?: TextMatchOptions,
27+
queryOptions?: TextMatchOptions & WaitForOptions,
2828
waitForOptions?: WaitForOptions
2929
) => Promise<Array<ReactTestInstance>>,
3030
findAllByText: (
3131
text: string | RegExp,
32-
queryOptions?: TextMatchOptions,
32+
queryOptions?: TextMatchOptions & WaitForOptions,
3333
waitForOptions?: WaitForOptions
3434
) => Promise<Array<ReactTestInstance>>,
3535
findByDisplayValue: (
3636
value: string | RegExp,
37-
queryOptions?: TextMatchOptions,
37+
queryOptions?: TextMatchOptions & WaitForOptions,
3838
waitForOptions?: WaitForOptions
3939
) => Promise<ReactTestInstance>,
4040
findByPlaceholder: () => void,
4141
findByPlaceholderText: (
4242
placeholder: string | RegExp,
43-
queryOptions?: TextMatchOptions,
43+
queryOptions?: TextMatchOptions & WaitForOptions,
4444
waitForOptions?: WaitForOptions
4545
) => Promise<ReactTestInstance>,
4646
findByTestId: (
4747
testId: string | RegExp,
48-
queryOptions?: TextMatchOptions,
48+
queryOptions?: TextMatchOptions & WaitForOptions,
4949
waitForOptions?: WaitForOptions
5050
) => Promise<ReactTestInstance>,
5151
findByText: (
5252
text: string | RegExp,
53-
queryOptions?: TextMatchOptions,
53+
queryOptions?: TextMatchOptions & WaitForOptions,
5454
waitForOptions?: WaitForOptions
5555
) => Promise<ReactTestInstance>,
5656
|};

src/helpers/makeQueries.js

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ type FindQueryFunction<ArgType, ReturnType> = (
1212
instance: ReactTestInstance
1313
) => (
1414
args: ArgType,
15-
queryOptions?: TextMatchOptions,
15+
queryOptions?: TextMatchOptions & WaitForOptions,
1616
waitForOptions?: WaitForOptions
1717
) => Promise<ReturnType>;
1818

@@ -42,6 +42,34 @@ export type Queries<QueryArg> = {
4242
findAllBy: FindAllByQuery<QueryArg>,
4343
};
4444

45+
// The WaitForOptions as been moved to the second option param of findBy* methods with the adding of TextMatchOptions
46+
// To make the migration easier and avoid a breaking change, keep reading this options from the first param but warn
47+
const deprecatedKeys: $Keys<WaitForOptions>[] = [
48+
'timeout',
49+
'interval',
50+
'stackTraceError',
51+
];
52+
const extractDeprecatedWaitForOptionUsage = (queryOptions?: WaitForOptions) => {
53+
if (queryOptions) {
54+
const waitForOptions: WaitForOptions = {
55+
timeout: queryOptions.timeout,
56+
interval: queryOptions.interval,
57+
stackTraceError: queryOptions.stackTraceError,
58+
};
59+
deprecatedKeys.forEach((key) => {
60+
if (queryOptions[key]) {
61+
// eslint-disable-next-line no-console
62+
console.warn(
63+
`Use of option ${key} in a findBy* query's first option parameter, TextMatchOptions, is deprecated. Please pass this option in the WaitForOptions parameter. For instance, "findByText(instance, {}, {${key}: ${queryOptions[
64+
key
65+
].toString()}})"`
66+
);
67+
}
68+
});
69+
return waitForOptions;
70+
}
71+
};
72+
4573
export function makeQueries<QueryArg>(
4674
queryAllByQuery: QueryAllByQuery<QueryArg>,
4775
getMissingError: (args: QueryArg) => string,
@@ -97,26 +125,32 @@ export function makeQueries<QueryArg>(
97125
function findAllByQuery(instance: ReactTestInstance) {
98126
return function findAllFn(
99127
args: QueryArg,
100-
queryOptions?: TextMatchOptions,
128+
queryOptions?: TextMatchOptions & WaitForOptions,
101129
waitForOptions?: WaitForOptions = {}
102130
) {
103-
return waitFor(
104-
() => getAllByQuery(instance)(args, queryOptions),
105-
waitForOptions
131+
const deprecatedWaitForOptions = extractDeprecatedWaitForOptionUsage(
132+
queryOptions
106133
);
134+
return waitFor(() => getAllByQuery(instance)(args, queryOptions), {
135+
...deprecatedWaitForOptions,
136+
...waitForOptions,
137+
});
107138
};
108139
}
109140

110141
function findByQuery(instance: ReactTestInstance) {
111142
return function findFn(
112143
args: QueryArg,
113-
queryOptions?: TextMatchOptions,
144+
queryOptions?: TextMatchOptions & WaitForOptions,
114145
waitForOptions?: WaitForOptions = {}
115146
) {
116-
return waitFor(
117-
() => getByQuery(instance)(args, queryOptions),
118-
waitForOptions
147+
const deprecatedWaitForOptions = extractDeprecatedWaitForOptionUsage(
148+
queryOptions
119149
);
150+
return waitFor(() => getByQuery(instance)(args, queryOptions), {
151+
...deprecatedWaitForOptions,
152+
...waitForOptions,
153+
});
120154
};
121155
}
122156

0 commit comments

Comments
 (0)