Skip to content

Commit 9fc83fe

Browse files
Feat(breaking): remove deprecated functions (#334)
* WIP * More changes * WIP * Added throwRemovedFunctionError docsRef * Fixed tests * Typescript types cleanup * Cleanup * Moving some changes from `prepare-2` branch * Moving some changes from `prepare-2` branch * Update src/waitFor.js Co-authored-by: Michał Pierzchała <thymikee@gmail.com> * Update website/docs/Migration20.md Co-authored-by: Michał Pierzchała <thymikee@gmail.com> * Code review changes * More code review changes * Code review changes * More code review changes * Added @deprecated messages for TS type defs * Spelling mistake * Reformatted index.d.ts * Removed remaining refs to warnFn Co-authored-by: Michał Pierzchała <thymikee@gmail.com>
1 parent 163cc85 commit 9fc83fe

File tree

16 files changed

+429
-438
lines changed

16 files changed

+429
-438
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ The [public API](https://callstack.github.io/react-native-testing-library/docs/a
124124

125125
## Migration Guides
126126

127-
- [Migration to 2.0](https://callstack.github.io/react-native-testing-library/docs/migration20)
127+
- [Migration to 2.0](https://callstack.github.io/react-native-testing-library/docs/migration-v2)
128128

129129
## Made with ❤️ at Callstack
130130

src/__tests__/__snapshots__/render.test.js.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,12 +150,12 @@ exports[`debug: shallow 1`] = `
150150
underlineColorAndroid=\\"transparent\\"
151151
value=\\"I inspected freshie\\"
152152
/>
153-
<Button
153+
<MyButton
154154
onPress={[Function anonymous]}
155155
type=\\"primary\\"
156156
>
157157
Change freshness!
158-
</Button>
158+
</MyButton>
159159
<Text
160160
testID=\\"duplicateText\\"
161161
>
@@ -200,12 +200,12 @@ exports[`debug: shallow with message 1`] = `
200200
underlineColorAndroid=\\"transparent\\"
201201
value=\\"I inspected freshie\\"
202202
/>
203-
<Button
203+
<MyButton
204204
onPress={[Function anonymous]}
205205
type=\\"primary\\"
206206
>
207207
Change freshness!
208-
</Button>
208+
</MyButton>
209209
<Text
210210
testID=\\"duplicateText\\"
211211
>

src/__tests__/__snapshots__/shallow.test.js.snap

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/__tests__/render.test.js

Lines changed: 26 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const PLACEHOLDER_CHEF = 'Who inspected freshness?';
1717
const INPUT_FRESHNESS = 'Custom Freshie';
1818
const INPUT_CHEF = 'I inspected freshie';
1919

20-
class Button extends React.Component<any> {
20+
class MyButton extends React.Component<any> {
2121
render() {
2222
return (
2323
<TouchableOpacity onPress={this.props.onPress}>
@@ -68,9 +68,9 @@ class Banana extends React.Component<any, any> {
6868
placeholder={PLACEHOLDER_CHEF}
6969
value={INPUT_CHEF}
7070
/>
71-
<Button onPress={this.changeFresh} type="primary">
71+
<MyButton onPress={this.changeFresh} type="primary">
7272
Change freshness!
73-
</Button>
73+
</MyButton>
7474
<Text testID="duplicateText">First Text</Text>
7575
<Text testID="duplicateText">Second Text</Text>
7676
<Text>{test}</Text>
@@ -109,51 +109,18 @@ test('getAllByTestId, queryAllByTestId', () => {
109109
expect(queryAllByTestId('nonExistentTestId')).toHaveLength(0);
110110
});
111111

112-
test('getByName, queryByName', () => {
113-
const { getByText, getByName, queryByName } = render(<Banana />);
114-
const bananaFresh = getByText('not fresh');
115-
const button = getByName('Button');
116-
117-
button.props.onPress();
118-
119-
expect(bananaFresh.props.children).toBe('fresh');
120-
121-
const sameButton = getByName(Button);
122-
sameButton.props.onPress();
123-
124-
expect(bananaFresh.props.children).toBe('not fresh');
125-
expect(() => getByName('InExistent')).toThrow('No instances found');
126-
expect(() => getByName(Text)).toThrow('Expected 1 but found 6');
127-
128-
expect(queryByName('Button')).toBe(button);
129-
expect(queryByName('InExistent')).toBeNull();
130-
});
131-
132-
test('getAllByName, queryAllByName', () => {
133-
const { getAllByName, queryAllByName } = render(<Banana />);
134-
const [text, status, button] = getAllByName('Text');
135-
136-
expect(text.props.children).toBe('Is the banana fresh?');
137-
expect(status.props.children).toBe('not fresh');
138-
expect(button.props.children).toBe('Change freshness!');
139-
expect(() => getAllByName('InExistent')).toThrow('No instances found');
140-
141-
expect(queryAllByName('Text')[1]).toBe(status);
142-
expect(queryAllByName('InExistent')).toHaveLength(0);
143-
});
144-
145-
test('getAllByType, queryAllByType', () => {
146-
const { getAllByType, queryAllByType } = render(<Banana />);
147-
const [text, status, button] = getAllByType(Text);
112+
test('UNSAFE_getAllByType, UNSAFE_queryAllByType', () => {
113+
const { UNSAFE_getAllByType, UNSAFE_queryAllByType } = render(<Banana />);
114+
const [text, status, button] = UNSAFE_getAllByType(Text);
148115
const InExistent = () => null;
149116

150117
expect(text.props.children).toBe('Is the banana fresh?');
151118
expect(status.props.children).toBe('not fresh');
152119
expect(button.props.children).toBe('Change freshness!');
153-
expect(() => getAllByType(InExistent)).toThrow('No instances found');
120+
expect(() => UNSAFE_getAllByType(InExistent)).toThrow('No instances found');
154121

155-
expect(queryAllByType(Text)[1]).toBe(status);
156-
expect(queryAllByType(InExistent)).toHaveLength(0);
122+
expect(UNSAFE_queryAllByType(Text)[1]).toBe(status);
123+
expect(UNSAFE_queryAllByType(InExistent)).toHaveLength(0);
157124
});
158125

159126
test('getByText, queryByText', () => {
@@ -267,38 +234,37 @@ test('getAllByDisplayValue, queryAllByDisplayValue', () => {
267234
expect(queryAllByDisplayValue('no value')).toHaveLength(0);
268235
});
269236

270-
test('getByProps, queryByProps', () => {
271-
const { getByProps, queryByProps } = render(<Banana />);
272-
const primaryType = getByProps({ type: 'primary' });
237+
test('UNSAFE_getByProps, UNSAFE_queryByProps', () => {
238+
const { UNSAFE_getByProps, UNSAFE_queryByProps } = render(<Banana />);
239+
const primaryType = UNSAFE_getByProps({ type: 'primary' });
273240

274241
expect(primaryType.props.children).toBe('Change freshness!');
275-
expect(() => getByProps({ type: 'inexistent' })).toThrow(
242+
expect(() => UNSAFE_getByProps({ type: 'inexistent' })).toThrow(
276243
'No instances found'
277244
);
278245

279-
expect(queryByProps({ type: 'primary' })).toBe(primaryType);
280-
expect(queryByProps({ type: 'inexistent' })).toBeNull();
246+
expect(UNSAFE_queryByProps({ type: 'primary' })).toBe(primaryType);
247+
expect(UNSAFE_queryByProps({ type: 'inexistent' })).toBeNull();
281248
});
282249

283-
test('getAllByProp, queryAllByProps', () => {
284-
const { getAllByProps, queryAllByProps } = render(<Banana />);
285-
const primaryTypes = getAllByProps({ type: 'primary' });
250+
test('UNSAFE_getAllByProp, UNSAFE_queryAllByProps', () => {
251+
const { UNSAFE_getAllByProps, UNSAFE_queryAllByProps } = render(<Banana />);
252+
const primaryTypes = UNSAFE_getAllByProps({ type: 'primary' });
286253

287254
expect(primaryTypes).toHaveLength(1);
288-
expect(() => getAllByProps({ type: 'inexistent' })).toThrow(
255+
expect(() => UNSAFE_getAllByProps({ type: 'inexistent' })).toThrow(
289256
'No instances found'
290257
);
291258

292-
expect(queryAllByProps({ type: 'primary' })).toEqual(primaryTypes);
293-
expect(queryAllByProps({ type: 'inexistent' })).toHaveLength(0);
259+
expect(UNSAFE_queryAllByProps({ type: 'primary' })).toEqual(primaryTypes);
260+
expect(UNSAFE_queryAllByProps({ type: 'inexistent' })).toHaveLength(0);
294261
});
295262

296263
test('update', () => {
297264
const fn = jest.fn();
298-
const { getByName, update, rerender } = render(<Banana onUpdate={fn} />);
299-
const button = getByName('Button');
265+
const { getByText, update, rerender } = render(<Banana onUpdate={fn} />);
300266

301-
button.props.onPress();
267+
fireEvent.press(getByText('Change freshness!'));
302268

303269
update(<Banana onUpdate={fn} />);
304270
rerender(<Banana onUpdate={fn} />);
@@ -314,7 +280,7 @@ test('unmount', () => {
314280
});
315281

316282
test('toJSON', () => {
317-
const { toJSON } = render(<Button>press me</Button>);
283+
const { toJSON } = render(<MyButton>press me</MyButton>);
318284

319285
expect(toJSON()).toMatchSnapshot();
320286
});
@@ -345,9 +311,9 @@ test('debug', () => {
345311
test('debug changing component', () => {
346312
jest.spyOn(console, 'log').mockImplementation((x) => x);
347313

348-
const { getByProps, debug } = render(<Banana />);
314+
const { UNSAFE_getByProps, debug } = render(<Banana />);
349315

350-
fireEvent.press(getByProps({ type: 'primary' }));
316+
fireEvent.press(UNSAFE_getByProps({ type: 'primary' }));
351317

352318
debug();
353319

src/__tests__/shallow.test.js

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/__tests__/waitFor.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ class BananaContainer extends React.Component<{}, any> {
3636
}
3737

3838
test('waits for element until it stops throwing', async () => {
39-
const { getByText, getByName, queryByText } = render(<BananaContainer />);
39+
const { getByText, queryByText } = render(<BananaContainer />);
4040

41-
fireEvent.press(getByName('TouchableOpacity'));
41+
fireEvent.press(getByText('Change freshness!'));
4242

4343
expect(queryByText('Fresh')).toBeNull();
4444

@@ -48,9 +48,9 @@ test('waits for element until it stops throwing', async () => {
4848
});
4949

5050
test('waits for element until timeout is met', async () => {
51-
const { getByText, getByName } = render(<BananaContainer />);
51+
const { getByText } = render(<BananaContainer />);
5252

53-
fireEvent.press(getByName('TouchableOpacity'));
53+
fireEvent.press(getByText('Change freshness!'));
5454

5555
await expect(
5656
waitFor(() => getByText('Fresh'), { timeout: 100 })

src/helpers/errors.js

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,7 @@ export const createQueryByError = (error: Error, callsite: Function) => {
2424
throw new ErrorWithStack(error.message, callsite);
2525
};
2626

27-
const warned = {
28-
getByName: false,
29-
getAllByName: false,
30-
queryByName: false,
31-
queryAllByName: false,
32-
33-
getByProps: false,
34-
getAllByProps: false,
35-
queryByProps: false,
36-
queryAllByProps: false,
37-
38-
getByType: false,
39-
getAllByType: false,
40-
queryByType: false,
41-
queryAllByType: false,
42-
};
27+
const warned = {};
4328

4429
export function printDeprecationWarning(functionName: string) {
4530
if (warned[functionName]) {
@@ -67,3 +52,12 @@ export function printUnsafeWarning(functionName: string) {
6752

6853
warned[functionName] = true;
6954
}
55+
56+
export function throwRemovedFunctionError(
57+
functionName: string,
58+
docsRef: string
59+
) {
60+
throw new Error(
61+
`${functionName} has been removed in version 2.0.\n\nPlease consult: https://callstack.github.io/react-native-testing-library/docs/${docsRef}`
62+
);
63+
}

0 commit comments

Comments
 (0)