Skip to content

Commit

Permalink
[enzyme-adapter-react-*] [fix] call ref for a root element
Browse files Browse the repository at this point in the history
  • Loading branch information
koba04 authored and ljharb committed Feb 24, 2018
1 parent b05147c commit dcafde7
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 25 deletions.
13 changes: 8 additions & 5 deletions packages/enzyme-adapter-react-13/src/ReactThirteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,15 @@ class ReactThirteenAdapter extends EnzymeAdapter {
return {
render(el, context, callback) {
if (instance === null) {
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, {
Component: el.type,
props: el.props,
const { ref, type, props } = el;
const wrapperProps = {
Component: type,
props,
context,
});
...(ref && { ref }),
};
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
instance = React.render(wrappedEl, domNode);
if (typeof callback === 'function') {
callback();
Expand Down
13 changes: 8 additions & 5 deletions packages/enzyme-adapter-react-14/src/ReactFourteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,15 @@ class ReactFifteenAdapter extends EnzymeAdapter {
return {
render(el, context, callback) {
if (instance === null) {
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, {
Component: el.type,
props: el.props,
const { type, props, ref } = el;
const wrapperProps = {
Component: type,
props,
context,
});
...(ref && { ref }),
};
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
instance = ReactDOM.render(wrappedEl, domNode);
if (typeof callback === 'function') {
callback();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,15 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
return {
render(el, context, callback) {
if (instance === null) {
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, {
Component: el.type,
props: el.props,
const { type, props, ref } = el;
const wrapperProps = {
Component: type,
props,
context,
});
...(ref && { ref }),
};
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
instance = ReactDOM.render(wrappedEl, domNode);
if (typeof callback === 'function') {
callback();
Expand Down
13 changes: 8 additions & 5 deletions packages/enzyme-adapter-react-15/src/ReactFifteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,15 @@ class ReactFifteenAdapter extends EnzymeAdapter {
return {
render(el, context, callback) {
if (instance === null) {
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, {
Component: el.type,
props: el.props,
const { type, props, ref } = el;
const wrapperProps = {
Component: type,
props,
context,
});
...(ref && { ref }),
};
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
instance = ReactDOM.render(wrappedEl, domNode);
if (typeof callback === 'function') {
callback();
Expand Down
13 changes: 8 additions & 5 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,12 +171,15 @@ class ReactSixteenAdapter extends EnzymeAdapter {
return {
render(el, context, callback) {
if (instance === null) {
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, {
Component: el.type,
props: el.props,
const { type, props, ref } = el;
const wrapperProps = {
Component: type,
props,
context,
});
...(ref && { ref }),
};
const ReactWrapperComponent = createMountWrapper(el, options);
const wrappedEl = React.createElement(ReactWrapperComponent, wrapperProps);
instance = ReactDOM.render(wrappedEl, domNode);
if (typeof callback === 'function') {
callback();
Expand Down
6 changes: 6 additions & 0 deletions packages/enzyme-test-suite/test/ReactWrapper-spec.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ describeWithDOM('mount', () => {
expect(wrapper.children().instance()).to.be.instanceOf(Box);
expect(wrapper.children().props().bam).to.equal(true);
});

it('should call ref', () => {
const spy = sinon.spy();
mount(<div ref={spy} />);
expect(spy).to.have.property('callCount', 1);
});
});

describe('context', () => {
Expand Down

0 comments on commit dcafde7

Please sign in to comment.