Skip to content

Commit e0ca51a

Browse files
authored
Make React.forwardRef() components discoverable by TestRenderer traversal (#12725)
1 parent 7dd4ca2 commit e0ca51a

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

packages/react-test-renderer/src/ReactTestRenderer.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ const validWrapperTypes = new Set([
411411
FunctionalComponent,
412412
ClassComponent,
413413
HostComponent,
414+
ForwardRef,
414415
]);
415416

416417
class ReactTestInstance {
@@ -475,6 +476,7 @@ class ReactTestInstance {
475476
case FunctionalComponent:
476477
case ClassComponent:
477478
case HostComponent:
479+
case ForwardRef:
478480
children.push(wrapFiber(node));
479481
break;
480482
case HostText:
@@ -484,7 +486,6 @@ class ReactTestInstance {
484486
case ContextProvider:
485487
case ContextConsumer:
486488
case Mode:
487-
case ForwardRef:
488489
descend = true;
489490
break;
490491
default:

packages/react-test-renderer/src/__tests__/ReactTestRendererTraversal-test.js

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ describe('ReactTestRendererTraversal', () => {
3737
<View void="void" />
3838
<View void="void" />
3939
</ExampleNull>
40+
<ExampleForwardRef qux="qux" />
4041
</View>
4142
</View>
4243
);
@@ -48,13 +49,17 @@ describe('ReactTestRendererTraversal', () => {
4849
const ExampleFn = props => <View baz="baz" />;
4950
const ExampleNull = props => null;
5051

52+
const ExampleForwardRef = React.forwardRef((props, ref) => (
53+
<View {...props} ref={ref} />
54+
));
55+
5156
it('initializes', () => {
5257
const render = ReactTestRenderer.create(<Example />);
5358
const hasFooProp = node => node.props.hasOwnProperty('foo');
5459

5560
// assert .props, .type and .parent attributes
5661
const foo = render.root.find(hasFooProp);
57-
expect(foo.props.children).toHaveLength(7);
62+
expect(foo.props.children).toHaveLength(8);
5863
expect(foo.type).toBe(View);
5964
expect(render.root.parent).toBe(null);
6065
expect(foo.children[0].parent).toBe(foo);
@@ -116,14 +121,16 @@ describe('ReactTestRendererTraversal', () => {
116121

117122
expect(() => render.root.findByType(ExampleFn)).not.toThrow(); // 1 match
118123
expect(() => render.root.findByType(View)).not.toThrow(); // 1 match
124+
expect(() => render.root.findByType(ExampleForwardRef)).not.toThrow(); // 1 match
119125
// note: there are clearly multiple <View /> in general, but there
120126
// is only one being rendered at root node level
121127
expect(() => render.root.findByType(ExampleNull)).toThrow(); // 2 matches
122128

123129
expect(render.root.findAllByType(ExampleFn)).toHaveLength(1);
124130
expect(render.root.findAllByType(View, {deep: false})).toHaveLength(1);
125-
expect(render.root.findAllByType(View)).toHaveLength(7);
131+
expect(render.root.findAllByType(View)).toHaveLength(8);
126132
expect(render.root.findAllByType(ExampleNull)).toHaveLength(2);
133+
expect(render.root.findAllByType(ExampleForwardRef)).toHaveLength(1);
127134

128135
const nulls = render.root.findAllByType(ExampleNull);
129136
expect(nulls[0].findAllByType(View)).toHaveLength(0);
@@ -138,17 +145,21 @@ describe('ReactTestRendererTraversal', () => {
138145
const foo = 'foo';
139146
const bar = 'bar';
140147
const baz = 'baz';
148+
const qux = 'qux';
141149

142150
expect(() => render.root.findByProps({foo})).not.toThrow(); // 1 match
143151
expect(() => render.root.findByProps({bar})).toThrow(); // >1 matches
144152
expect(() => render.root.findByProps({baz})).toThrow(); // >1 matches
153+
expect(() => render.root.findByProps({qux})).not.toThrow(); // 1 match
145154

146155
expect(render.root.findAllByProps({foo}, {deep: false})).toHaveLength(1);
147156
expect(render.root.findAllByProps({bar}, {deep: false})).toHaveLength(5);
148157
expect(render.root.findAllByProps({baz}, {deep: false})).toHaveLength(2);
158+
expect(render.root.findAllByProps({qux}, {deep: false})).toHaveLength(1);
149159

150160
expect(render.root.findAllByProps({foo})).toHaveLength(2);
151161
expect(render.root.findAllByProps({bar})).toHaveLength(9);
152162
expect(render.root.findAllByProps({baz})).toHaveLength(4);
163+
expect(render.root.findAllByProps({qux})).toHaveLength(3);
153164
});
154165
});

0 commit comments

Comments
 (0)