Skip to content

Commit fa7870e

Browse files
koba04ljharb
authored andcommitted
[enzyme-adapter-react-*] [fix] call ref for a root element
1 parent 8a9fe1f commit fa7870e

File tree

6 files changed

+46
-25
lines changed

6 files changed

+46
-25
lines changed

packages/enzyme-adapter-react-13/src/ReactThirteenAdapter.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,15 @@ class ReactThirteenAdapter extends EnzymeAdapter {
111111
return {
112112
render(el, context, callback) {
113113
if (instance === null) {
114-
const ReactWrapperComponent = createMountWrapper(el, options);
115-
const wrappedEl = React.createElement(ReactWrapperComponent, {
116-
Component: el.type,
117-
props: el.props,
114+
const { ref, type, props } = el;
115+
const wrapperProps = {
116+
Component: type,
117+
props,
118118
context,
119-
});
119+
...(ref && { ref }),
120+
};
121+
const ReactWrapperComponent = createMountWrapper(el, options);
122+
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
120123
instance = React.render(wrappedEl, domNode);
121124
if (typeof callback === 'function') {
122125
callback();

packages/enzyme-adapter-react-14/src/ReactFourteenAdapter.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,15 @@ class ReactFifteenAdapter extends EnzymeAdapter {
8383
return {
8484
render(el, context, callback) {
8585
if (instance === null) {
86-
const ReactWrapperComponent = createMountWrapper(el, options);
87-
const wrappedEl = React.createElement(ReactWrapperComponent, {
88-
Component: el.type,
89-
props: el.props,
86+
const { type, props, ref } = el;
87+
const wrapperProps = {
88+
Component: type,
89+
props,
9090
context,
91-
});
91+
...(ref && { ref }),
92+
};
93+
const ReactWrapperComponent = createMountWrapper(el, options);
94+
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
9295
instance = ReactDOM.render(wrappedEl, domNode);
9396
if (typeof callback === 'function') {
9497
callback();

packages/enzyme-adapter-react-15.4/src/ReactFifteenFourAdapter.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,15 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
114114
return {
115115
render(el, context, callback) {
116116
if (instance === null) {
117-
const ReactWrapperComponent = createMountWrapper(el, options);
118-
const wrappedEl = React.createElement(ReactWrapperComponent, {
119-
Component: el.type,
120-
props: el.props,
117+
const { type, props, ref } = el;
118+
const wrapperProps = {
119+
Component: type,
120+
props,
121121
context,
122-
});
122+
...(ref && { ref }),
123+
};
124+
const ReactWrapperComponent = createMountWrapper(el, options);
125+
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
123126
instance = ReactDOM.render(wrappedEl, domNode);
124127
if (typeof callback === 'function') {
125128
callback();

packages/enzyme-adapter-react-15/src/ReactFifteenAdapter.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,15 @@ class ReactFifteenAdapter extends EnzymeAdapter {
114114
return {
115115
render(el, context, callback) {
116116
if (instance === null) {
117-
const ReactWrapperComponent = createMountWrapper(el, options);
118-
const wrappedEl = React.createElement(ReactWrapperComponent, {
119-
Component: el.type,
120-
props: el.props,
117+
const { type, props, ref } = el;
118+
const wrapperProps = {
119+
Component: type,
120+
props,
121121
context,
122-
});
122+
...(ref && { ref }),
123+
};
124+
const ReactWrapperComponent = createMountWrapper(el, options);
125+
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
123126
instance = ReactDOM.render(wrappedEl, domNode);
124127
if (typeof callback === 'function') {
125128
callback();

packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,12 +171,15 @@ class ReactSixteenAdapter extends EnzymeAdapter {
171171
return {
172172
render(el, context, callback) {
173173
if (instance === null) {
174-
const ReactWrapperComponent = createMountWrapper(el, options);
175-
const wrappedEl = React.createElement(ReactWrapperComponent, {
176-
Component: el.type,
177-
props: el.props,
174+
const { type, props, ref } = el;
175+
const wrapperProps = {
176+
Component: type,
177+
props,
178178
context,
179-
});
179+
...(ref && { ref }),
180+
};
181+
const ReactWrapperComponent = createMountWrapper(el, options);
182+
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
180183
instance = ReactDOM.render(wrappedEl, domNode);
181184
if (typeof callback === 'function') {
182185
callback();

packages/enzyme-test-suite/test/ReactWrapper-spec.jsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,12 @@ describeWithDOM('mount', () => {
5656
expect(wrapper.children().instance()).to.be.instanceOf(Box);
5757
expect(wrapper.children().props().bam).to.equal(true);
5858
});
59+
60+
it('should call ref', () => {
61+
const spy = sinon.spy();
62+
mount(<div ref={spy} />);
63+
expect(spy).to.have.property('callCount', 1);
64+
});
5965
});
6066

6167
describe('context', () => {

0 commit comments

Comments
 (0)