Skip to content

Commit 8bd1952

Browse files
chore: deprecate some a11y queries (#1226)
1 parent 897b7de commit 8bd1952

File tree

9 files changed

+307
-46
lines changed

9 files changed

+307
-46
lines changed
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { getQueryPrefix } from '../query-name';
2+
3+
test('getQueryPrefix should return correct prefix', () => {
4+
expect(getQueryPrefix('getByRole')).toBe('get');
5+
expect(getQueryPrefix('getAllByText')).toEqual('getAll');
6+
expect(getQueryPrefix('queryByTestId')).toEqual('query');
7+
expect(getQueryPrefix('queryAllByPlaceholderText')).toEqual('queryAll');
8+
expect(getQueryPrefix('findByHintText')).toEqual('find');
9+
expect(getQueryPrefix('findAllByDisplayValue')).toEqual('findAll');
10+
});

src/helpers/deprecation.ts

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { getQueryPrefix } from './query-name';
2+
3+
export function deprecateQueries<Queries extends Record<string, any>>(
4+
queriesObject: Queries,
5+
recommendation: string
6+
): Queries {
7+
const result = {} as Queries;
8+
Object.keys(queriesObject).forEach((queryName) => {
9+
const queryFn = queriesObject[queryName];
10+
// @ts-expect-error: generic typing is hard
11+
result[queryName] = deprecateQuery(queryFn, queryName, recommendation);
12+
});
13+
14+
return result;
15+
}
16+
17+
function deprecateQuery<QueryFn extends (...args: any) => any>(
18+
queryFn: QueryFn,
19+
queryName: string,
20+
recommendation: string
21+
): QueryFn {
22+
const formattedRecommendation = recommendation.replace(
23+
/{queryPrefix}/g,
24+
getQueryPrefix(queryName)
25+
);
26+
27+
// @ts-expect-error: generic typing is hard
28+
const wrapper: QueryFn = (...args: any) => {
29+
const errorMessage = `${queryName}(...) is deprecated and will be removed in the future.\n\n${formattedRecommendation}`;
30+
// eslint-disable-next-line no-console
31+
console.warn(errorMessage);
32+
return queryFn(...args);
33+
};
34+
35+
return wrapper;
36+
}

src/helpers/query-name.ts

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export function getQueryPrefix(queryName: string) {
2+
const parts = queryName.split('By');
3+
return parts[0];
4+
}

src/queries/__tests__/a11yState.test.tsx

+101
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
/* eslint-disable no-console */
12
import * as React from 'react';
23
import { View, Text, Pressable, TouchableOpacity } from 'react-native';
34
import { render } from '../..';
45

6+
type ConsoleLogMock = jest.Mock<typeof console.log>;
7+
8+
beforeEach(() => {
9+
jest.spyOn(console, 'warn').mockImplementation(() => {});
10+
});
11+
512
const TEXT_LABEL = 'cool text';
613

714
const Typography = ({ children, ...rest }: any) => {
@@ -251,3 +258,97 @@ test('byA11yState queries support hidden option', () => {
251258
`"Unable to find an element with expanded state: false"`
252259
);
253260
});
261+
262+
test('*ByA11yState deprecation warnings', () => {
263+
const mockCalls = (console.warn as ConsoleLogMock).mock.calls;
264+
const view = render(<View accessibilityState={{ disabled: true }} />);
265+
266+
view.getByA11yState({ disabled: true });
267+
expect(mockCalls[0][0]).toMatchInlineSnapshot(`
268+
"getByA11yState(...) is deprecated and will be removed in the future.
269+
270+
Use getByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead."
271+
`);
272+
273+
view.getAllByA11yState({ disabled: true });
274+
expect(mockCalls[1][0]).toMatchInlineSnapshot(`
275+
"getAllByA11yState(...) is deprecated and will be removed in the future.
276+
277+
Use getAllByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead."
278+
`);
279+
280+
view.queryByA11yState({ disabled: true });
281+
expect(mockCalls[2][0]).toMatchInlineSnapshot(`
282+
"queryByA11yState(...) is deprecated and will be removed in the future.
283+
284+
Use queryByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead."
285+
`);
286+
287+
view.queryAllByA11yState({ disabled: true });
288+
expect(mockCalls[3][0]).toMatchInlineSnapshot(`
289+
"queryAllByA11yState(...) is deprecated and will be removed in the future.
290+
291+
Use queryAllByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead."
292+
`);
293+
294+
view.findByA11yState({ disabled: true });
295+
expect(mockCalls[4][0]).toMatchInlineSnapshot(`
296+
"findByA11yState(...) is deprecated and will be removed in the future.
297+
298+
Use findByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead."
299+
`);
300+
301+
view.findAllByA11yState({ disabled: true });
302+
expect(mockCalls[5][0]).toMatchInlineSnapshot(`
303+
"findAllByA11yState(...) is deprecated and will be removed in the future.
304+
305+
Use findAllByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead."
306+
`);
307+
});
308+
309+
test('*ByAccessibilityState deprecation warnings', () => {
310+
const mockCalls = (console.warn as ConsoleLogMock).mock.calls;
311+
const view = render(<View accessibilityState={{ disabled: true }} />);
312+
313+
view.getByAccessibilityState({ disabled: true });
314+
expect(mockCalls[0][0]).toMatchInlineSnapshot(`
315+
"getByAccessibilityState(...) is deprecated and will be removed in the future.
316+
317+
Use getByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead."
318+
`);
319+
320+
view.getAllByAccessibilityState({ disabled: true });
321+
expect(mockCalls[1][0]).toMatchInlineSnapshot(`
322+
"getAllByAccessibilityState(...) is deprecated and will be removed in the future.
323+
324+
Use getAllByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead."
325+
`);
326+
327+
view.queryByAccessibilityState({ disabled: true });
328+
expect(mockCalls[2][0]).toMatchInlineSnapshot(`
329+
"queryByAccessibilityState(...) is deprecated and will be removed in the future.
330+
331+
Use queryByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead."
332+
`);
333+
334+
view.queryAllByAccessibilityState({ disabled: true });
335+
expect(mockCalls[3][0]).toMatchInlineSnapshot(`
336+
"queryAllByAccessibilityState(...) is deprecated and will be removed in the future.
337+
338+
Use queryAllByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead."
339+
`);
340+
341+
view.findByAccessibilityState({ disabled: true });
342+
expect(mockCalls[4][0]).toMatchInlineSnapshot(`
343+
"findByAccessibilityState(...) is deprecated and will be removed in the future.
344+
345+
Use findByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead."
346+
`);
347+
348+
view.findAllByAccessibilityState({ disabled: true });
349+
expect(mockCalls[5][0]).toMatchInlineSnapshot(`
350+
"findAllByAccessibilityState(...) is deprecated and will be removed in the future.
351+
352+
Use findAllByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead."
353+
`);
354+
});

src/queries/__tests__/a11yValue.test.tsx

+101
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1+
/* eslint-disable no-console */
12
import * as React from 'react';
23
import { View, Text, TouchableOpacity } from 'react-native';
34
import { render } from '../..';
45

6+
type ConsoleLogMock = jest.Mock<typeof console.log>;
7+
8+
beforeEach(() => {
9+
jest.spyOn(console, 'warn').mockImplementation(() => {});
10+
});
11+
512
const TEXT_LABEL = 'cool text';
613

714
const Typography = ({ children, ...rest }: any) => {
@@ -130,3 +137,97 @@ test('byA11yValue error messages', () => {
130137
`"Unable to find an element with min value: 1, max value: 2, now value: 3, text value: /foo/i"`
131138
);
132139
});
140+
141+
test('*ByA11yValue deprecation warnings', () => {
142+
const mockCalls = (console.warn as ConsoleLogMock).mock.calls;
143+
const view = render(<View accessibilityValue={{ min: 10 }} />);
144+
145+
view.getByA11yValue({ min: 10 });
146+
expect(mockCalls[0][0]).toMatchInlineSnapshot(`
147+
"getByA11yValue(...) is deprecated and will be removed in the future.
148+
149+
Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or getByRole(role, { value: ... }) query instead."
150+
`);
151+
152+
view.getAllByA11yValue({ min: 10 });
153+
expect(mockCalls[1][0]).toMatchInlineSnapshot(`
154+
"getAllByA11yValue(...) is deprecated and will be removed in the future.
155+
156+
Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or getAllByRole(role, { value: ... }) query instead."
157+
`);
158+
159+
view.queryByA11yValue({ min: 10 });
160+
expect(mockCalls[2][0]).toMatchInlineSnapshot(`
161+
"queryByA11yValue(...) is deprecated and will be removed in the future.
162+
163+
Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or queryByRole(role, { value: ... }) query instead."
164+
`);
165+
166+
view.queryAllByA11yValue({ min: 10 });
167+
expect(mockCalls[3][0]).toMatchInlineSnapshot(`
168+
"queryAllByA11yValue(...) is deprecated and will be removed in the future.
169+
170+
Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or queryAllByRole(role, { value: ... }) query instead."
171+
`);
172+
173+
view.findByA11yValue({ min: 10 });
174+
expect(mockCalls[4][0]).toMatchInlineSnapshot(`
175+
"findByA11yValue(...) is deprecated and will be removed in the future.
176+
177+
Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or findByRole(role, { value: ... }) query instead."
178+
`);
179+
180+
view.findAllByA11yValue({ min: 10 });
181+
expect(mockCalls[5][0]).toMatchInlineSnapshot(`
182+
"findAllByA11yValue(...) is deprecated and will be removed in the future.
183+
184+
Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or findAllByRole(role, { value: ... }) query instead."
185+
`);
186+
});
187+
188+
test('*ByAccessibilityValue deprecation warnings', () => {
189+
const mockCalls = (console.warn as ConsoleLogMock).mock.calls;
190+
const view = render(<View accessibilityValue={{ min: 10 }} />);
191+
192+
view.getByAccessibilityValue({ min: 10 });
193+
expect(mockCalls[0][0]).toMatchInlineSnapshot(`
194+
"getByAccessibilityValue(...) is deprecated and will be removed in the future.
195+
196+
Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or getByRole(role, { value: ... }) query instead."
197+
`);
198+
199+
view.getAllByAccessibilityValue({ min: 10 });
200+
expect(mockCalls[1][0]).toMatchInlineSnapshot(`
201+
"getAllByAccessibilityValue(...) is deprecated and will be removed in the future.
202+
203+
Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or getAllByRole(role, { value: ... }) query instead."
204+
`);
205+
206+
view.queryByAccessibilityValue({ min: 10 });
207+
expect(mockCalls[2][0]).toMatchInlineSnapshot(`
208+
"queryByAccessibilityValue(...) is deprecated and will be removed in the future.
209+
210+
Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or queryByRole(role, { value: ... }) query instead."
211+
`);
212+
213+
view.queryAllByAccessibilityValue({ min: 10 });
214+
expect(mockCalls[3][0]).toMatchInlineSnapshot(`
215+
"queryAllByAccessibilityValue(...) is deprecated and will be removed in the future.
216+
217+
Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or queryAllByRole(role, { value: ... }) query instead."
218+
`);
219+
220+
view.findByAccessibilityValue({ min: 10 });
221+
expect(mockCalls[4][0]).toMatchInlineSnapshot(`
222+
"findByAccessibilityValue(...) is deprecated and will be removed in the future.
223+
224+
Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or findByRole(role, { value: ... }) query instead."
225+
`);
226+
227+
view.findAllByAccessibilityValue({ min: 10 });
228+
expect(mockCalls[5][0]).toMatchInlineSnapshot(`
229+
"findAllByAccessibilityValue(...) is deprecated and will be removed in the future.
230+
231+
Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or findAllByRole(role, { value: ... }) query instead."
232+
`);
233+
});

src/queries/a11yState.ts

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
22
import { AccessibilityState } from 'react-native';
33
import { accessibilityStateKeys } from '../helpers/accessiblity';
4+
import { deprecateQueries } from '../helpers/deprecation';
45
import { findAll } from '../helpers/findAll';
56
import { matchAccessibilityState } from '../helpers/matchers/accessibilityState';
67
import { makeQueries } from './makeQueries';
@@ -92,18 +93,22 @@ export const bindByA11yStateQueries = (
9293
const findAllByA11yState = findAllBy(instance);
9394

9495
return {
95-
getByA11yState,
96-
getAllByA11yState,
97-
queryByA11yState,
98-
queryAllByA11yState,
99-
findByA11yState,
100-
findAllByA11yState,
101-
102-
getByAccessibilityState: getByA11yState,
103-
getAllByAccessibilityState: getAllByA11yState,
104-
queryByAccessibilityState: queryByA11yState,
105-
queryAllByAccessibilityState: queryAllByA11yState,
106-
findByAccessibilityState: findByA11yState,
107-
findAllByAccessibilityState: findAllByA11yState,
96+
...deprecateQueries(
97+
{
98+
getByA11yState,
99+
getAllByA11yState,
100+
queryByA11yState,
101+
queryAllByA11yState,
102+
findByA11yState,
103+
findAllByA11yState,
104+
getByAccessibilityState: getByA11yState,
105+
getAllByAccessibilityState: getAllByA11yState,
106+
queryByAccessibilityState: queryByA11yState,
107+
queryAllByAccessibilityState: queryAllByA11yState,
108+
findByAccessibilityState: findByA11yState,
109+
findAllByAccessibilityState: findAllByA11yState,
110+
},
111+
'Use {queryPrefix}ByRole(role, { disabled, selected, checked, busy, expanded }) query or expect(...).toHaveAccessibilityState(...) matcher from "@testing-library/jest-native" package instead.'
112+
),
108113
};
109114
};

src/queries/a11yValue.ts

+18-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { ReactTestInstance } from 'react-test-renderer';
22
import { accessiblityValueKeys } from '../helpers/accessiblity';
3+
import { deprecateQueries } from '../helpers/deprecation';
34
import { findAll } from '../helpers/findAll';
45
import {
56
AccessibilityValueMatcher,
@@ -109,18 +110,22 @@ export const bindByA11yValueQueries = (
109110
const findAllByA11yValue = findAllBy(instance);
110111

111112
return {
112-
getByA11yValue,
113-
getAllByA11yValue,
114-
queryByA11yValue,
115-
queryAllByA11yValue,
116-
findByA11yValue,
117-
findAllByA11yValue,
118-
119-
getByAccessibilityValue: getByA11yValue,
120-
getAllByAccessibilityValue: getAllByA11yValue,
121-
queryByAccessibilityValue: queryByA11yValue,
122-
queryAllByAccessibilityValue: queryAllByA11yValue,
123-
findByAccessibilityValue: findByA11yValue,
124-
findAllByAccessibilityValue: findAllByA11yValue,
113+
...deprecateQueries(
114+
{
115+
getByA11yValue,
116+
getAllByA11yValue,
117+
queryByA11yValue,
118+
queryAllByA11yValue,
119+
findByA11yValue,
120+
findAllByA11yValue,
121+
getByAccessibilityValue: getByA11yValue,
122+
getAllByAccessibilityValue: getAllByA11yValue,
123+
queryByAccessibilityValue: queryByA11yValue,
124+
queryAllByAccessibilityValue: queryAllByA11yValue,
125+
findByAccessibilityValue: findByA11yValue,
126+
findAllByAccessibilityValue: findAllByA11yValue,
127+
},
128+
'Use expect(...).toHaveAccessibilityValue(...) matcher from "@testing-library/jest-native" package or {queryPrefix}ByRole(role, { value: ... }) query instead.'
129+
),
125130
};
126131
};

0 commit comments

Comments
 (0)