Skip to content

Commit c0ac70b

Browse files
fix: container enumeration (#1352)
* fix: make container non-enumerable * chore: fix lint, typecheck * chore: code comment
1 parent 211907a commit c0ac70b

File tree

4 files changed

+17
-8
lines changed

4 files changed

+17
-8
lines changed

src/__tests__/render.test.tsx

+9-2
Original file line numberDiff line numberDiff line change
@@ -222,12 +222,12 @@ test('returns composite UNSAFE_root', () => {
222222
test('container displays deprecation', () => {
223223
const view = render(<View testID="inner" />);
224224

225-
expect(() => view.container).toThrowErrorMatchingInlineSnapshot(`
225+
expect(() => (view as any).container).toThrowErrorMatchingInlineSnapshot(`
226226
"'container' property has been renamed to 'UNSAFE_root'.
227227
228228
Consider using 'root' property which returns root host element."
229229
`);
230-
expect(() => screen.container).toThrowErrorMatchingInlineSnapshot(`
230+
expect(() => (screen as any).container).toThrowErrorMatchingInlineSnapshot(`
231231
"'container' property has been renamed to 'UNSAFE_root'.
232232
233233
Consider using 'root' property which returns root host element."
@@ -238,3 +238,10 @@ test('RenderAPI type', () => {
238238
render(<Banana />) as RenderAPI;
239239
expect(true).toBeTruthy();
240240
});
241+
242+
test('returned output can be spread using rest operator', () => {
243+
// Next line should not throw
244+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
245+
const { rerender, ...rest } = render(<View testID="inner" />);
246+
expect(rest).toBeTruthy();
247+
});

src/__tests__/screen.test.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ test('screen throws without render', () => {
5656
expect(() => screen.UNSAFE_root).toThrow(
5757
'`render` method has not been called'
5858
);
59-
expect(() => screen.container).toThrow('`render` method has not been called');
6059
expect(() => screen.debug()).toThrow('`render` method has not been called');
6160
expect(() => screen.debug.shallow()).toThrow(
6261
'`render` method has not been called'

src/render.tsx

+8-2
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,19 @@ function buildRenderResult(
111111
return getHostChildren(instance)[0];
112112
},
113113
UNSAFE_root: instance,
114-
get container(): ReactTestInstance {
114+
};
115+
116+
// Add as non-enumerable property, so that it's safe to enumerate
117+
// `render` result, e.g. using destructuring rest syntax.
118+
Object.defineProperty(result, 'container', {
119+
enumerable: false,
120+
get() {
115121
throw new Error(
116122
"'container' property has been renamed to 'UNSAFE_root'.\n\n" +
117123
"Consider using 'root' property which returns root host element."
118124
);
119125
},
120-
};
126+
});
121127

122128
setRenderResult(result);
123129
return result;

src/screen.ts

-3
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@ const notImplementedDebug = () => {
1313
notImplementedDebug.shallow = notImplemented;
1414

1515
const defaultScreen: RenderResult = {
16-
get container(): ReactTestInstance {
17-
throw new Error(SCREEN_ERROR);
18-
},
1916
get root(): ReactTestInstance {
2017
throw new Error(SCREEN_ERROR);
2118
},

0 commit comments

Comments
 (0)