Skip to content

Commit 8fa8f39

Browse files
MattAgnthymikee
MattAgn
authored andcommitted
feat: use makeQueries for byPlaceholderText
1 parent 7a498fe commit 8fa8f39

File tree

5 files changed

+70
-101
lines changed

5 files changed

+70
-101
lines changed

src/__tests__/render.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ test('getByPlaceholderText, queryByPlaceholderText', () => {
167167

168168
expect(sameInput.props.placeholder).toBe(PLACEHOLDER_FRESHNESS);
169169
expect(() => getByPlaceholderText('no placeholder')).toThrow(
170-
'No instances found with placeholder "no placeholder"'
170+
'Unable to find an element with placeholder: no placeholder'
171171
);
172172

173173
expect(queryByPlaceholderText(/add/i)).toBe(input);
174174
expect(queryByPlaceholderText('no placeholder')).toBeNull();
175175
expect(() => queryByPlaceholderText(/fresh/)).toThrow(
176-
'Expected 1 but found 2'
176+
'Found multiple elements with placeholder: /fresh/ '
177177
);
178178
});
179179

@@ -185,7 +185,7 @@ test('getAllByPlaceholderText, queryAllByPlaceholderText', () => {
185185

186186
expect(inputs).toHaveLength(2);
187187
expect(() => getAllByPlaceholderText('no placeholder')).toThrow(
188-
'No instances found'
188+
'Unable to find an element with placeholder: no placeholder'
189189
);
190190

191191
expect(queryAllByPlaceholderText(/fresh/i)).toEqual(inputs);

src/helpers/byPlaceholderText.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 getTextInputNodeByPlaceholderText = (node, placeholder) => {
8+
try {
9+
const { TextInput } = require('react-native');
10+
return (
11+
filterNodeByType(node, TextInput) &&
12+
(typeof placeholder === 'string'
13+
? placeholder === node.props.placeholder
14+
: placeholder.test(node.props.placeholder))
15+
);
16+
} catch (error) {
17+
throw createLibraryNotSupportedError(error);
18+
}
19+
};
20+
21+
const queryAllByPlaceholderText = (
22+
instance: ReactTestInstance
23+
): ((placeholder: string | RegExp) => Array<ReactTestInstance>) =>
24+
function queryAllByPlaceholderFn(placeholder) {
25+
return instance.findAll((node) =>
26+
getTextInputNodeByPlaceholderText(node, placeholder)
27+
);
28+
};
29+
30+
const getMultipleError = (placeholder) =>
31+
`Found multiple elements with placeholder: ${String(placeholder)} `;
32+
const getMissingError = (placeholder) =>
33+
`Unable to find an element with placeholder: ${String(placeholder)}`;
34+
35+
const {
36+
getBy: getByPlaceholderText,
37+
getAllBy: getAllByPlaceholderText,
38+
queryBy: queryByPlaceholderText,
39+
findBy: findByPlaceholderText,
40+
findAllBy: findAllByPlaceholderText,
41+
}: Queries<string | RegExp> = makeQueries(
42+
queryAllByPlaceholderText,
43+
getMissingError,
44+
getMultipleError
45+
);
46+
47+
export {
48+
getByPlaceholderText,
49+
getAllByPlaceholderText,
50+
queryByPlaceholderText,
51+
findByPlaceholderText,
52+
findAllByPlaceholderText,
53+
queryAllByPlaceholderText,
54+
};

src/helpers/findByAPI.js

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
// @flow
22
import waitFor from '../waitFor';
33
import type { WaitForOptions } from '../waitFor';
4-
import {
5-
getByPlaceholderText,
6-
getAllByPlaceholderText,
7-
getByDisplayValue,
8-
getAllByDisplayValue,
9-
} from './getByAPI';
4+
import { getByDisplayValue, getAllByDisplayValue } from './getByAPI';
105
import { findAllByTestId, findByTestId } from './byTestId';
116
import { findAllByText, findByText } from './byText';
7+
import {
8+
findAllByPlaceholderText,
9+
findByPlaceholderText,
10+
} from './byPlaceholderText';
1211
import { throwRenamedFunctionError } from './errors';
1312

1413
export type FindByAPI = {|
@@ -55,27 +54,6 @@ const makeFindQuery = <Text, Result>(
5554
waitForOptions: WaitForOptions
5655
): Promise<Result> => waitFor(() => getQuery(instance)(text), waitForOptions);
5756

58-
export const findByPlaceholderText = (
59-
instance: ReactTestInstance
60-
): ((
61-
placeholder: string | RegExp,
62-
waitForOptions?: WaitForOptions
63-
) => Promise<ReactTestInstance>) => (
64-
placeholder: string | RegExp,
65-
waitForOptions: WaitForOptions = {}
66-
) => makeFindQuery(instance, getByPlaceholderText, placeholder, waitForOptions);
67-
68-
export const findAllByPlaceholderText = (
69-
instance: ReactTestInstance
70-
): ((
71-
placeholder: string | RegExp,
72-
waitForOptions?: WaitForOptions
73-
) => Promise<Array<ReactTestInstance>>) => (
74-
placeholder: string | RegExp,
75-
waitForOptions: WaitForOptions = {}
76-
) =>
77-
makeFindQuery(instance, getAllByPlaceholderText, placeholder, waitForOptions);
78-
7957
export const findByDisplayValue = (
8058
instance: ReactTestInstance
8159
): ((

src/helpers/getByAPI.js

Lines changed: 4 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ import {
1111
} from './errors';
1212
import { getAllByTestId, getByTestId } from './byTestId';
1313
import { getAllByText, getByText } from './byText';
14+
import {
15+
getAllByPlaceholderText,
16+
getByPlaceholderText,
17+
} from './byPlaceholderText';
1418

1519
export type GetByAPI = {|
1620
getByText: (text: string | RegExp) => ReactTestInstance,
@@ -43,20 +47,6 @@ export type GetByAPI = {|
4347
getAllByPlaceholder: () => void,
4448
|};
4549

46-
const getTextInputNodeByPlaceholderText = (node, placeholder) => {
47-
try {
48-
const { TextInput } = require('react-native');
49-
return (
50-
filterNodeByType(node, TextInput) &&
51-
(typeof placeholder === 'string'
52-
? placeholder === node.props.placeholder
53-
: placeholder.test(node.props.placeholder))
54-
);
55-
} catch (error) {
56-
throw createLibraryNotSupportedError(error);
57-
}
58-
};
59-
6050
const getTextInputNodeByDisplayValue = (node, value) => {
6151
try {
6252
const { TextInput } = require('react-native');
@@ -73,22 +63,6 @@ const getTextInputNodeByDisplayValue = (node, value) => {
7363
}
7464
};
7565

76-
export const getByPlaceholderText = (
77-
instance: ReactTestInstance
78-
): ((placeholder: string | RegExp) => ReactTestInstance) =>
79-
function getByPlaceholderTextFn(placeholder: string | RegExp) {
80-
try {
81-
return instance.find((node) =>
82-
getTextInputNodeByPlaceholderText(node, placeholder)
83-
);
84-
} catch (error) {
85-
throw new ErrorWithStack(
86-
prepareErrorMessage(error, 'placeholder', placeholder),
87-
getByPlaceholderTextFn
88-
);
89-
}
90-
};
91-
9266
export const getByDisplayValue = (
9367
instance: ReactTestInstance
9468
): ((displayValue: string | RegExp) => ReactTestInstance) =>
@@ -105,22 +79,6 @@ export const getByDisplayValue = (
10579
}
10680
};
10781

108-
export const getAllByPlaceholderText = (
109-
instance: ReactTestInstance
110-
): ((placeholder: string | RegExp) => Array<ReactTestInstance>) =>
111-
function getAllByPlaceholderTextFn(placeholder: string | RegExp) {
112-
const results = instance.findAll((node) =>
113-
getTextInputNodeByPlaceholderText(node, placeholder)
114-
);
115-
if (results.length === 0) {
116-
throw new ErrorWithStack(
117-
`No instances found with placeholder: ${String(placeholder)}`,
118-
getAllByPlaceholderTextFn
119-
);
120-
}
121-
return results;
122-
};
123-
12482
export const getAllByDisplayValue = (
12583
instance: ReactTestInstance
12684
): ((value: string | RegExp) => Array<ReactTestInstance>) =>

src/helpers/queryByAPI.js

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
// @flow
22
import * as React from 'react';
33
import {
4-
getByPlaceholderText,
54
getByDisplayValue,
6-
getAllByPlaceholderText,
75
getAllByDisplayValue,
86
UNSAFE_getByType,
97
UNSAFE_getByProps,
@@ -12,6 +10,10 @@ import {
1210
} from './getByAPI';
1311
import { queryByTestId, queryAllByTestId } from './byTestId';
1412
import { queryByText, queryAllByText } from './byText';
13+
import {
14+
queryByPlaceholderText,
15+
queryAllByPlaceholderText,
16+
} from './byPlaceholderText';
1517
import {
1618
createQueryByError,
1719
throwRemovedFunctionError,
@@ -54,17 +56,6 @@ export type QueryByAPI = {|
5456
queryAllByPlaceholder: () => void,
5557
|};
5658

57-
export const queryByPlaceholderText = (
58-
instance: ReactTestInstance
59-
): ((placeholder: string | RegExp) => ReactTestInstance | null) =>
60-
function queryByPlaceholderTextFn(placeholder: string | RegExp) {
61-
try {
62-
return getByPlaceholderText(instance)(placeholder);
63-
} catch (error) {
64-
return createQueryByError(error, queryByPlaceholderTextFn);
65-
}
66-
};
67-
6859
export const queryByDisplayValue = (
6960
instance: ReactTestInstance
7061
): ((value: string | RegExp) => ReactTestInstance | null) =>
@@ -76,18 +67,6 @@ export const queryByDisplayValue = (
7667
}
7768
};
7869

79-
export const queryAllByPlaceholderText = (
80-
instance: ReactTestInstance
81-
): ((placeholder: string | RegExp) => Array<ReactTestInstance>) => (
82-
placeholder: string | RegExp
83-
) => {
84-
try {
85-
return getAllByPlaceholderText(instance)(placeholder);
86-
} catch (error) {
87-
return [];
88-
}
89-
};
90-
9170
export const queryAllByDisplayValue = (
9271
instance: ReactTestInstance
9372
): ((value: string | RegExp) => Array<ReactTestInstance>) => (

0 commit comments

Comments
 (0)