Skip to content

Commit 6d63a4f

Browse files
committed
feat(breaking): rename ByPlaceholder to ByPlaceholderText (#455)
1 parent 7feab6c commit 6d63a4f

File tree

15 files changed

+158
-102
lines changed

15 files changed

+158
-102
lines changed

examples/redux/components/AddTodo.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ describe('Application test', () => {
1414
</Provider>
1515
);
1616

17-
const { getByPlaceholder, getByText } = render(component);
17+
const { getByPlaceholderText, getByText } = render(component);
1818

19-
const input = getByPlaceholder(/repository/i);
19+
const input = getByPlaceholderText(/repository/i);
2020
expect(input).toBeTruthy();
2121

2222
const textToEnter = 'This is a random element';

src/__tests__/findByApi.test.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ test('findBy queries work asynchronously', async () => {
1111
findAllByTestId,
1212
findByText,
1313
findAllByText,
14-
findByPlaceholder,
15-
findAllByPlaceholder,
14+
findByPlaceholderText,
15+
findAllByPlaceholderText,
1616
findByDisplayValue,
1717
findAllByDisplayValue,
1818
} = render(<View />);
@@ -21,10 +21,10 @@ test('findBy queries work asynchronously', async () => {
2121
await expect(findByText('Some Text', options)).rejects.toBeTruthy();
2222
await expect(findAllByText('Some Text', options)).rejects.toBeTruthy();
2323
await expect(
24-
findByPlaceholder('Placeholder Text', options)
24+
findByPlaceholderText('Placeholder Text', options)
2525
).rejects.toBeTruthy();
2626
await expect(
27-
findAllByPlaceholder('Placeholder Text', options)
27+
findAllByPlaceholderText('Placeholder Text', options)
2828
).rejects.toBeTruthy();
2929
await expect(
3030
findByDisplayValue('Display Value', options)
@@ -49,10 +49,10 @@ test('findBy queries work asynchronously', async () => {
4949
await expect(findAllByTestId('aTestId')).resolves.toHaveLength(1);
5050
await expect(findByText('Some Text')).resolves.toBeTruthy();
5151
await expect(findAllByText('Some Text')).resolves.toHaveLength(1);
52-
await expect(findByPlaceholder('Placeholder Text')).resolves.toBeTruthy();
53-
await expect(findAllByPlaceholder('Placeholder Text')).resolves.toHaveLength(
54-
1
55-
);
52+
await expect(findByPlaceholderText('Placeholder Text')).resolves.toBeTruthy();
53+
await expect(
54+
findAllByPlaceholderText('Placeholder Text')
55+
).resolves.toHaveLength(1);
5656
await expect(findByDisplayValue('Display Value')).resolves.toBeTruthy();
5757
await expect(findAllByDisplayValue('Display Value')).resolves.toHaveLength(1);
5858
}, 20000);

src/__tests__/fireEvent.test.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ test('fireEvent.changeText', () => {
123123
const onChangeTextMock = jest.fn();
124124
const CHANGE_TEXT = 'content';
125125

126-
const { getByPlaceholder } = render(
126+
const { getByPlaceholderText } = render(
127127
<View>
128128
<TextInput
129129
placeholder="Customer placeholder"
@@ -132,7 +132,10 @@ test('fireEvent.changeText', () => {
132132
</View>
133133
);
134134

135-
fireEvent.changeText(getByPlaceholder('Customer placeholder'), CHANGE_TEXT);
135+
fireEvent.changeText(
136+
getByPlaceholderText('Customer placeholder'),
137+
CHANGE_TEXT
138+
);
136139

137140
expect(onChangeTextMock).toHaveBeenCalledWith(CHANGE_TEXT);
138141
});

src/__tests__/render.test.js

+18-14
Original file line numberDiff line numberDiff line change
@@ -176,35 +176,39 @@ test('getAllByText, queryAllByText', () => {
176176
expect(queryAllByText('InExistent')).toHaveLength(0);
177177
});
178178

179-
test('getByPlaceholder, queryByPlaceholder', () => {
180-
const { getByPlaceholder, queryByPlaceholder } = render(<Banana />);
181-
const input = getByPlaceholder(/custom/i);
179+
test('getByPlaceholderText, queryByPlaceholderText', () => {
180+
const { getByPlaceholderText, queryByPlaceholderText } = render(<Banana />);
181+
const input = getByPlaceholderText(/custom/i);
182182

183183
expect(input.props.placeholder).toBe(PLACEHOLDER_FRESHNESS);
184184

185-
const sameInput = getByPlaceholder(PLACEHOLDER_FRESHNESS);
185+
const sameInput = getByPlaceholderText(PLACEHOLDER_FRESHNESS);
186186

187187
expect(sameInput.props.placeholder).toBe(PLACEHOLDER_FRESHNESS);
188-
expect(() => getByPlaceholder('no placeholder')).toThrow(
188+
expect(() => getByPlaceholderText('no placeholder')).toThrow(
189189
'No instances found'
190190
);
191191

192-
expect(queryByPlaceholder(/add/i)).toBe(input);
193-
expect(queryByPlaceholder('no placeholder')).toBeNull();
194-
expect(() => queryByPlaceholder(/fresh/)).toThrow('Expected 1 but found 2');
192+
expect(queryByPlaceholderText(/add/i)).toBe(input);
193+
expect(queryByPlaceholderText('no placeholder')).toBeNull();
194+
expect(() => queryByPlaceholderText(/fresh/)).toThrow(
195+
'Expected 1 but found 2'
196+
);
195197
});
196198

197-
test('getAllByPlaceholder, queryAllByPlaceholder', () => {
198-
const { getAllByPlaceholder, queryAllByPlaceholder } = render(<Banana />);
199-
const inputs = getAllByPlaceholder(/fresh/i);
199+
test('getAllByPlaceholderText, queryAllByPlaceholderText', () => {
200+
const { getAllByPlaceholderText, queryAllByPlaceholderText } = render(
201+
<Banana />
202+
);
203+
const inputs = getAllByPlaceholderText(/fresh/i);
200204

201205
expect(inputs).toHaveLength(2);
202-
expect(() => getAllByPlaceholder('no placeholder')).toThrow(
206+
expect(() => getAllByPlaceholderText('no placeholder')).toThrow(
203207
'No instances found'
204208
);
205209

206-
expect(queryAllByPlaceholder(/fresh/i)).toEqual(inputs);
207-
expect(queryAllByPlaceholder('no placeholder')).toHaveLength(0);
210+
expect(queryAllByPlaceholderText(/fresh/i)).toEqual(inputs);
211+
expect(queryAllByPlaceholderText('no placeholder')).toHaveLength(0);
208212
});
209213

210214
test('getByDisplayValue, queryByDisplayValue', () => {

src/__tests__/within.test.js

+6-4
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,22 @@ test('within() exposes basic queries', async () => {
1919

2020
expect(rootQueries.getAllByText('Same Text')).toHaveLength(2);
2121
expect(rootQueries.getAllByDisplayValue('Same Value')).toHaveLength(2);
22-
expect(rootQueries.getAllByPlaceholder('Same Placeholder')).toHaveLength(2);
22+
expect(rootQueries.getAllByPlaceholderText('Same Placeholder')).toHaveLength(
23+
2
24+
);
2325

2426
const firstQueries = within(rootQueries.getByA11yHint('first'));
2527
expect(firstQueries.getAllByText('Same Text')).toHaveLength(1);
2628
expect(firstQueries.getByText('Same Text')).toBeTruthy();
2729
expect(firstQueries.queryAllByText('Same Text')).toHaveLength(1);
2830
expect(firstQueries.queryByText('Same Text')).toBeTruthy();
2931
expect(firstQueries.getByDisplayValue('Same Value')).toBeTruthy();
30-
expect(firstQueries.getByPlaceholder('Same Placeholder')).toBeTruthy();
32+
expect(firstQueries.getByPlaceholderText('Same Placeholder')).toBeTruthy();
3133
await expect(
3234
firstQueries.findByDisplayValue('Same Value')
3335
).resolves.toBeTruthy();
3436
await expect(
35-
firstQueries.findAllByPlaceholder('Same Placeholder')
37+
firstQueries.findAllByPlaceholderText('Same Placeholder')
3638
).resolves.toHaveLength(1);
3739

3840
const secondQueries = within(rootQueries.getByA11yHint('second'));
@@ -41,7 +43,7 @@ test('within() exposes basic queries', async () => {
4143
expect(secondQueries.queryAllByText('Same Text')).toHaveLength(1);
4244
expect(secondQueries.queryByText('Same Text')).toBeTruthy();
4345
expect(secondQueries.getByDisplayValue('Same Value')).toBeTruthy();
44-
expect(secondQueries.getByPlaceholder('Same Placeholder')).toBeTruthy();
46+
expect(secondQueries.getByPlaceholderText('Same Placeholder')).toBeTruthy();
4547
});
4648

4749
test('within() exposes a11y queries', async () => {

src/helpers/errors.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,16 @@ export function throwRemovedFunctionError(
4444
docsRef: string
4545
) {
4646
throw new Error(
47-
`${functionName} has been removed in version 2.0.\n\nPlease consult: https://callstack.github.io/react-native-testing-library/docs/${docsRef}`
47+
`"${functionName}" has been removed.\n\nPlease consult: https://callstack.github.io/react-native-testing-library/docs/${docsRef}`
48+
);
49+
}
50+
51+
export function throwRenamedFunctionError(
52+
functionName: string,
53+
newFunctionName: string
54+
) {
55+
throw new ErrorWithStack(
56+
`The "${functionName}" function has been renamed to "${newFunctionName}". Please replace all occurrences.`,
57+
throwRenamedFunctionError
4858
);
4959
}

src/helpers/findByAPI.js

+19-8
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ import {
66
getAllByTestId,
77
getByText,
88
getAllByText,
9-
getByPlaceholder,
10-
getAllByPlaceholder,
9+
getByPlaceholderText,
10+
getAllByPlaceholderText,
1111
getByDisplayValue,
1212
getAllByDisplayValue,
1313
} from './getByAPI';
14+
import { throwRenamedFunctionError } from './errors';
1415

1516
const makeFindQuery = <Text, Result>(
1617
instance: ReactTestInstance,
@@ -39,15 +40,16 @@ export const findAllByText = (instance: ReactTestInstance) => (
3940
waitForOptions: WaitForOptions = {}
4041
) => makeFindQuery(instance, getAllByText, text, waitForOptions);
4142

42-
export const findByPlaceholder = (instance: ReactTestInstance) => (
43+
export const findByPlaceholderText = (instance: ReactTestInstance) => (
4344
placeholder: string | RegExp,
4445
waitForOptions: WaitForOptions = {}
45-
) => makeFindQuery(instance, getByPlaceholder, placeholder, waitForOptions);
46+
) => makeFindQuery(instance, getByPlaceholderText, placeholder, waitForOptions);
4647

47-
export const findAllByPlaceholder = (instance: ReactTestInstance) => (
48+
export const findAllByPlaceholderText = (instance: ReactTestInstance) => (
4849
placeholder: string | RegExp,
4950
waitForOptions: WaitForOptions = {}
50-
) => makeFindQuery(instance, getAllByPlaceholder, placeholder, waitForOptions);
51+
) =>
52+
makeFindQuery(instance, getAllByPlaceholderText, placeholder, waitForOptions);
5153

5254
export const findByDisplayValue = (instance: ReactTestInstance) => (
5355
value: string | RegExp,
@@ -62,10 +64,19 @@ export const findAllByDisplayValue = (instance: ReactTestInstance) => (
6264
export const findByAPI = (instance: ReactTestInstance) => ({
6365
findByTestId: findByTestId(instance),
6466
findByText: findByText(instance),
65-
findByPlaceholder: findByPlaceholder(instance),
67+
findByPlaceholderText: findByPlaceholderText(instance),
6668
findByDisplayValue: findByDisplayValue(instance),
6769
findAllByTestId: findAllByTestId(instance),
6870
findAllByText: findAllByText(instance),
69-
findAllByPlaceholder: findAllByPlaceholder(instance),
71+
findAllByPlaceholderText: findAllByPlaceholderText(instance),
7072
findAllByDisplayValue: findAllByDisplayValue(instance),
73+
74+
// Renamed
75+
findByPlaceholder: () =>
76+
throwRenamedFunctionError('findByPlaceholder', 'findByPlaceholderText'),
77+
findAllByPlaceholder: () =>
78+
throwRenamedFunctionError(
79+
'findAllByPlaceholder',
80+
'findAllByPlaceholderText'
81+
),
7182
});

src/helpers/getByAPI.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
createLibraryNotSupportedError,
77
prepareErrorMessage,
88
throwRemovedFunctionError,
9+
throwRenamedFunctionError,
910
} from './errors';
1011

1112
const filterNodeByType = (node, type) => node.type === type;
@@ -50,7 +51,7 @@ const getChildrenAsText = (children, TextComponent, textContent = []) => {
5051
return textContent;
5152
};
5253

53-
const getTextInputNodeByPlaceholder = (node, placeholder) => {
54+
const getTextInputNodeByPlaceholderText = (node, placeholder) => {
5455
try {
5556
// eslint-disable-next-line
5657
const { TextInput } = require('react-native');
@@ -89,14 +90,17 @@ export const getByText = (instance: ReactTestInstance) =>
8990
}
9091
};
9192

92-
export const getByPlaceholder = (instance: ReactTestInstance) =>
93-
function getByPlaceholderFn(placeholder: string | RegExp) {
93+
export const getByPlaceholderText = (instance: ReactTestInstance) =>
94+
function getByPlaceholderTextFn(placeholder: string | RegExp) {
9495
try {
9596
return instance.find((node) =>
96-
getTextInputNodeByPlaceholder(node, placeholder)
97+
getTextInputNodeByPlaceholderText(node, placeholder)
9798
);
9899
} catch (error) {
99-
throw new ErrorWithStack(prepareErrorMessage(error), getByPlaceholderFn);
100+
throw new ErrorWithStack(
101+
prepareErrorMessage(error),
102+
getByPlaceholderTextFn
103+
);
100104
}
101105
};
102106

@@ -142,15 +146,15 @@ export const getAllByText = (instance: ReactTestInstance) =>
142146
return results;
143147
};
144148

145-
export const getAllByPlaceholder = (instance: ReactTestInstance) =>
146-
function getAllByPlaceholderFn(placeholder: string | RegExp) {
149+
export const getAllByPlaceholderText = (instance: ReactTestInstance) =>
150+
function getAllByPlaceholderTextFn(placeholder: string | RegExp) {
147151
const results = instance.findAll((node) =>
148-
getTextInputNodeByPlaceholder(node, placeholder)
152+
getTextInputNodeByPlaceholderText(node, placeholder)
149153
);
150154
if (results.length === 0) {
151155
throw new ErrorWithStack(
152156
`No instances found with placeholder: ${String(placeholder)}`,
153-
getAllByPlaceholderFn
157+
getAllByPlaceholderTextFn
154158
);
155159
}
156160
return results;
@@ -226,11 +230,11 @@ export const UNSAFE_getAllByProps = (instance: ReactTestInstance) =>
226230

227231
export const getByAPI = (instance: ReactTestInstance) => ({
228232
getByText: getByText(instance),
229-
getByPlaceholder: getByPlaceholder(instance),
233+
getByPlaceholderText: getByPlaceholderText(instance),
230234
getByDisplayValue: getByDisplayValue(instance),
231235
getByTestId: getByTestId(instance),
232236
getAllByText: getAllByText(instance),
233-
getAllByPlaceholder: getAllByPlaceholder(instance),
237+
getAllByPlaceholderText: getAllByPlaceholderText(instance),
234238
getAllByDisplayValue: getAllByDisplayValue(instance),
235239
getAllByTestId: getAllByTestId(instance),
236240

@@ -256,4 +260,10 @@ export const getByAPI = (instance: ReactTestInstance) => ({
256260
'getAllByProps',
257261
'migration-v2#removed-functions'
258262
),
263+
264+
// Renamed
265+
getByPlaceholder: () =>
266+
throwRenamedFunctionError('getByPlaceholder', 'getByPlaceholderText'),
267+
getAllByPlaceholder: () =>
268+
throwRenamedFunctionError('getAllByPlaceholder', 'getByPlaceholderText'),
259269
});

0 commit comments

Comments
 (0)