Skip to content

Commit 3cadbdc

Browse files
committed
refactor: console.warn for current users
1 parent 39685fe commit 3cadbdc

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

src/__tests__/render.breaking.test.tsx

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -229,12 +229,16 @@ test('returns composite UNSAFE_root', () => {
229229
test('container displays deprecation', () => {
230230
const view = render(<View testID="inner" />);
231231

232-
expect(() => view.container).toThrowErrorMatchingInlineSnapshot(
233-
`"'container' property has been renamed to 'UNSAFE_root'"`
234-
);
235-
expect(() => screen.container).toThrowErrorMatchingInlineSnapshot(
236-
`"'container' property has been renamed to 'UNSAFE_root'"`
237-
);
232+
expect(() => view.container).toThrowErrorMatchingInlineSnapshot(`
233+
"'container' property has been renamed to 'UNSAFE_root'.
234+
235+
Consider using 'root' property which returns root host element."
236+
`);
237+
expect(() => screen.container).toThrowErrorMatchingInlineSnapshot(`
238+
"'container' property has been renamed to 'UNSAFE_root'.
239+
240+
Consider using 'root' property which returns root host element."
241+
`);
238242
});
239243

240244
test('RenderAPI type', () => {

src/__tests__/render.test.tsx

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import * as React from 'react';
33
import { View, Text, TextInput, Pressable, SafeAreaView } from 'react-native';
44
import { render, fireEvent, RenderAPI } from '..';
55

6+
type ConsoleLogMock = jest.Mock<typeof console.log>;
7+
8+
beforeEach(() => {
9+
jest.spyOn(console, 'warn').mockImplementation(() => {});
10+
});
11+
612
const PLACEHOLDER_FRESHNESS = 'Add custom freshness';
713
const PLACEHOLDER_CHEF = 'Who inspected freshness?';
814
const INPUT_FRESHNESS = 'Custom Freshie';
@@ -223,6 +229,13 @@ test('returns composite UNSAFE_root', () => {
223229
test('returns container', () => {
224230
const { container } = render(<View testID="inner" />);
225231

232+
const mockCalls = (console.warn as any as ConsoleLogMock).mock.calls;
233+
expect(mockCalls[0][0]).toMatchInlineSnapshot(`
234+
"'container' property is deprecated and has been renamed to 'UNSAFE_root'.
235+
236+
Consider using 'root' property which returns root host element."
237+
`);
238+
226239
expect(container).toBeDefined();
227240
// `View` composite component is returned. This behavior will break if we
228241
// start returning only host components.

src/render.tsx

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,18 @@ function buildRenderResult(
113113
UNSAFE_root: instance,
114114
get container() {
115115
if (!getConfig().useBreakingChanges) {
116+
// eslint-disable-next-line no-console
117+
console.warn(
118+
"'container' property is deprecated and has been renamed to 'UNSAFE_root'.\n\n" +
119+
"Consider using 'root' property which returns root host element."
120+
);
116121
return instance;
117122
}
118123

119-
throw new Error("'container' property has been renamed to 'UNSAFE_root'");
124+
throw new Error(
125+
"'container' property has been renamed to 'UNSAFE_root'.\n\n" +
126+
"Consider using 'root' property which returns root host element."
127+
);
120128
},
121129
};
122130

website/docs/Queries.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ title: Queries
3939

4040
## Variants
4141

42-
> `getBy` queries are shown by default in the [query documentation](#queries)
42+
> `getBy*` queries are shown by default in the [query documentation](#queries)
4343
> below.
4444
4545
### getBy
@@ -60,22 +60,22 @@ title: Queries
6060

6161
### findBy
6262

63-
`findBy` queries return a promise which resolves when a matching element is found. The promise is rejected if no elements match or if more than one match is found after a default timeout of 1000 ms. If you need to find more than one element, then use `findAllBy`.
63+
`findBy*` queries return a promise which resolves when a matching element is found. The promise is rejected if no elements match or if more than one match is found after a default timeout of 1000 ms. If you need to find more than one element, then use `findAllBy*`.
6464

6565
### findAllBy
6666

67-
`findAllBy` queries return a promise which resolves to an array of matching elements. The promise is rejected if no elements match after a default timeout of 1000 ms.
67+
`findAllBy*` queries return a promise which resolves to an array of matching elements. The promise is rejected if no elements match after a default timeout of 1000 ms.
6868

6969
:::info
70-
`findBy` and `findAllBy` queries accept optional `waitForOptions` object argument which can contain `timeout`, `interval` and `onTimeout` properies which have the same meaning as respective options for [`waitFor`](api#waitfor) function.
70+
`findBy*` and `findAllBy*` queries accept optional `waitForOptions` object argument which can contain `timeout`, `interval` and `onTimeout` properies which have the same meaning as respective options for [`waitFor`](api#waitfor) function.
7171
:::
7272

7373
:::info
74-
In cases when your `findBy` and `findAllBy` queries throw when not able to find matching elements it is useful to pass `onTimeout: () => { screen.debug(); }` callback using `waitForOptions` parameter.
74+
In cases when your `findBy*` and `findAllBy*` queries throw when not able to find matching elements it is useful to pass `onTimeout: () => { screen.debug(); }` callback using `waitForOptions` parameter.
7575
:::
7676

7777
:::info
78-
In order to properly use `findBy` and `findAllBy` queries you need at least React >=16.9.0 (featuring async `act`) or React Native >=0.61 (which comes with React >=16.9.0).
78+
In order to properly use `findBy*` and `findAllBy*` queries you need at least React >=16.9.0 (featuring async `act`) or React Native >=0.61 (which comes with React >=16.9.0).
7979
:::
8080

8181
## Queries

0 commit comments

Comments
 (0)