Skip to content

Commit eee6202

Browse files
MattAgnthymikee
MattAgn
authored andcommitted
feat: use makeQueries for byDisplayValue
1 parent 8fa8f39 commit eee6202

File tree

5 files changed

+66
-107
lines changed

5 files changed

+66
-107
lines changed

src/__tests__/render.test.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -202,12 +202,14 @@ test('getByDisplayValue, queryByDisplayValue', () => {
202202

203203
expect(sameInput.props.value).toBe(INPUT_FRESHNESS);
204204
expect(() => getByDisplayValue('no value')).toThrow(
205-
'No instances found with display value "no value"'
205+
'Unable to find an element with displayValue: no value'
206206
);
207207

208208
expect(queryByDisplayValue(/custom/i)).toBe(input);
209209
expect(queryByDisplayValue('no value')).toBeNull();
210-
expect(() => queryByDisplayValue(/fresh/i)).toThrow('Expected 1 but found 2');
210+
expect(() => queryByDisplayValue(/fresh/i)).toThrow(
211+
'Found multiple elements with display value: /fresh/i'
212+
);
211213
});
212214

213215
test('getByDisplayValue, queryByDisplayValue get element by default value only when value is undefined', () => {
@@ -227,7 +229,9 @@ test('getAllByDisplayValue, queryAllByDisplayValue', () => {
227229
const inputs = getAllByDisplayValue(/fresh/i);
228230

229231
expect(inputs).toHaveLength(2);
230-
expect(() => getAllByDisplayValue('no value')).toThrow('No instances found');
232+
expect(() => getAllByDisplayValue('no value')).toThrow(
233+
'Unable to find an element with displayValue: no value'
234+
);
231235

232236
expect(queryAllByDisplayValue(/fresh/i)).toEqual(inputs);
233237
expect(queryAllByDisplayValue('no value')).toHaveLength(0);

src/helpers/byDisplayValue.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// @flow
2+
import { makeQueries } from './makeQueries';
3+
import type { Queries } from './makeQueries';
4+
import { filterNodeByType } from './filterNodeByType';
5+
import { createLibraryNotSupportedError } from './errors';
6+
7+
const getTextInputNodeByDisplayValue = (node, value) => {
8+
try {
9+
const { TextInput } = require('react-native');
10+
const nodeValue =
11+
node.props.value !== undefined
12+
? node.props.value
13+
: node.props.defaultValue;
14+
return (
15+
filterNodeByType(node, TextInput) &&
16+
(typeof value === 'string' ? value === nodeValue : value.test(nodeValue))
17+
);
18+
} catch (error) {
19+
throw createLibraryNotSupportedError(error);
20+
}
21+
};
22+
23+
const queryAllByDisplayValue = (
24+
instance: ReactTestInstance
25+
): ((displayValue: string | RegExp) => Array<ReactTestInstance>) =>
26+
function queryAllByDisplayValueFn(displayValue) {
27+
return instance.findAll((node) =>
28+
getTextInputNodeByDisplayValue(node, displayValue)
29+
);
30+
};
31+
32+
const getMultipleError = (displayValue: string | RegExp) =>
33+
`Found multiple elements with display value: ${String(displayValue)} `;
34+
const getMissingError = (displayValue: string | RegExp) =>
35+
`Unable to find an element with displayValue: ${String(displayValue)}`;
36+
37+
const {
38+
getBy: getByDisplayValue,
39+
getAllBy: getAllByDisplayValue,
40+
queryBy: queryByDisplayValue,
41+
findBy: findByDisplayValue,
42+
findAllBy: findAllByDisplayValue,
43+
}: Queries<string | RegExp> = makeQueries(
44+
queryAllByDisplayValue,
45+
getMissingError,
46+
getMultipleError
47+
);
48+
49+
export {
50+
getByDisplayValue,
51+
getAllByDisplayValue,
52+
queryByDisplayValue,
53+
findByDisplayValue,
54+
findAllByDisplayValue,
55+
queryAllByDisplayValue,
56+
};

src/helpers/findByAPI.js

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
// @flow
2-
import waitFor from '../waitFor';
32
import type { WaitForOptions } from '../waitFor';
4-
import { getByDisplayValue, getAllByDisplayValue } from './getByAPI';
53
import { findAllByTestId, findByTestId } from './byTestId';
64
import { findAllByText, findByText } from './byText';
75
import {
86
findAllByPlaceholderText,
97
findByPlaceholderText,
108
} from './byPlaceholderText';
9+
import { findAllByDisplayValue, findByDisplayValue } from './byDisplayValue';
1110
import { throwRenamedFunctionError } from './errors';
1211

1312
export type FindByAPI = {|
@@ -47,33 +46,6 @@ export type FindByAPI = {|
4746
) => Promise<ReactTestInstance>,
4847
|};
4948

50-
const makeFindQuery = <Text, Result>(
51-
instance: ReactTestInstance,
52-
getQuery: (instance: ReactTestInstance) => (text: Text) => Result,
53-
text: Text,
54-
waitForOptions: WaitForOptions
55-
): Promise<Result> => waitFor(() => getQuery(instance)(text), waitForOptions);
56-
57-
export const findByDisplayValue = (
58-
instance: ReactTestInstance
59-
): ((
60-
value: string | RegExp,
61-
waitForOptions?: WaitForOptions
62-
) => Promise<ReactTestInstance>) => (
63-
value: string | RegExp,
64-
waitForOptions: WaitForOptions = {}
65-
) => makeFindQuery(instance, getByDisplayValue, value, waitForOptions);
66-
67-
export const findAllByDisplayValue = (
68-
instance: ReactTestInstance
69-
): ((
70-
value: string | RegExp,
71-
waitForOptions?: WaitForOptions
72-
) => Promise<Array<ReactTestInstance>>) => (
73-
value: string | RegExp,
74-
waitForOptions: WaitForOptions = {}
75-
) => makeFindQuery(instance, getAllByDisplayValue, value, waitForOptions);
76-
7749
export const findByAPI = (instance: ReactTestInstance): FindByAPI => ({
7850
findByTestId: findByTestId(instance),
7951
findByText: findByText(instance),

src/helpers/getByAPI.js

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// @flow
22
import * as React from 'react';
33
import prettyFormat from 'pretty-format';
4-
import { filterNodeByType } from './filterNodeByType';
54
import {
65
ErrorWithStack,
7-
createLibraryNotSupportedError,
86
prepareErrorMessage,
97
throwRemovedFunctionError,
108
throwRenamedFunctionError,
@@ -15,6 +13,7 @@ import {
1513
getAllByPlaceholderText,
1614
getByPlaceholderText,
1715
} from './byPlaceholderText';
16+
import { getAllByDisplayValue, getByDisplayValue } from './byDisplayValue';
1817

1918
export type GetByAPI = {|
2019
getByText: (text: string | RegExp) => ReactTestInstance,
@@ -47,54 +46,6 @@ export type GetByAPI = {|
4746
getAllByPlaceholder: () => void,
4847
|};
4948

50-
const getTextInputNodeByDisplayValue = (node, value) => {
51-
try {
52-
const { TextInput } = require('react-native');
53-
const nodeValue =
54-
node.props.value !== undefined
55-
? node.props.value
56-
: node.props.defaultValue;
57-
return (
58-
filterNodeByType(node, TextInput) &&
59-
(typeof value === 'string' ? value === nodeValue : value.test(nodeValue))
60-
);
61-
} catch (error) {
62-
throw createLibraryNotSupportedError(error);
63-
}
64-
};
65-
66-
export const getByDisplayValue = (
67-
instance: ReactTestInstance
68-
): ((displayValue: string | RegExp) => ReactTestInstance) =>
69-
function getByDisplayValueFn(displayValue: string | RegExp) {
70-
try {
71-
return instance.find((node) =>
72-
getTextInputNodeByDisplayValue(node, displayValue)
73-
);
74-
} catch (error) {
75-
throw new ErrorWithStack(
76-
prepareErrorMessage(error, 'display value', displayValue),
77-
getByDisplayValueFn
78-
);
79-
}
80-
};
81-
82-
export const getAllByDisplayValue = (
83-
instance: ReactTestInstance
84-
): ((value: string | RegExp) => Array<ReactTestInstance>) =>
85-
function getAllByDisplayValueFn(value: string | RegExp) {
86-
const results = instance.findAll((node) =>
87-
getTextInputNodeByDisplayValue(node, value)
88-
);
89-
if (results.length === 0) {
90-
throw new ErrorWithStack(
91-
`No instances found with display value: ${String(value)}`,
92-
getAllByDisplayValueFn
93-
);
94-
}
95-
return results;
96-
};
97-
9849
export const UNSAFE_getByType = (
9950
instance: ReactTestInstance
10051
): ((type: React.ComponentType<any>) => ReactTestInstance) =>

src/helpers/queryByAPI.js

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
// @flow
22
import * as React from 'react';
33
import {
4-
getByDisplayValue,
5-
getAllByDisplayValue,
64
UNSAFE_getByType,
75
UNSAFE_getByProps,
86
UNSAFE_getAllByType,
@@ -14,6 +12,7 @@ import {
1412
queryByPlaceholderText,
1513
queryAllByPlaceholderText,
1614
} from './byPlaceholderText';
15+
import { queryByDisplayValue, queryAllByDisplayValue } from './byDisplayValue';
1716
import {
1817
createQueryByError,
1918
throwRemovedFunctionError,
@@ -56,29 +55,6 @@ export type QueryByAPI = {|
5655
queryAllByPlaceholder: () => void,
5756
|};
5857

59-
export const queryByDisplayValue = (
60-
instance: ReactTestInstance
61-
): ((value: string | RegExp) => ReactTestInstance | null) =>
62-
function queryByDisplayValueFn(value: string | RegExp) {
63-
try {
64-
return getByDisplayValue(instance)(value);
65-
} catch (error) {
66-
return createQueryByError(error, queryByDisplayValueFn);
67-
}
68-
};
69-
70-
export const queryAllByDisplayValue = (
71-
instance: ReactTestInstance
72-
): ((value: string | RegExp) => Array<ReactTestInstance>) => (
73-
value: string | RegExp
74-
) => {
75-
try {
76-
return getAllByDisplayValue(instance)(value);
77-
} catch (error) {
78-
return [];
79-
}
80-
};
81-
8258
export const UNSAFE_queryByType = (
8359
instance: ReactTestInstance
8460
): ((type: React.ComponentType<any>) => ReactTestInstance | null) =>

0 commit comments

Comments
 (0)