Skip to content

Commit

Permalink
[enzyme-adapter-react-{14,15.4,15,16.1,16.2,16.3,16] [new] add `wrapW…
Browse files Browse the repository at this point in the history
…ithWrappingComponent`
  • Loading branch information
minznerjosh authored and ljharb committed Jan 2, 2019
1 parent 2e11497 commit fc61100
Show file tree
Hide file tree
Showing 8 changed files with 225 additions and 13 deletions.
30 changes: 29 additions & 1 deletion packages/enzyme-adapter-react-14/src/ReactFourteenAdapter.js
Expand Up @@ -19,6 +19,10 @@ import {
propsWithKeysAndRef,
ensureKeyOrUndefined,
wrap,
RootFinder,
getNodeFromRootFinder,
wrapWithWrappingComponent,
getWrappingComponentMountRenderer,
} from 'enzyme-adapter-utils';

function typeToNodeType(type) {
Expand Down Expand Up @@ -103,6 +107,7 @@ class ReactFourteenAdapter extends EnzymeAdapter {
const { type, props, ref } = el;
const wrapperProps = {
Component: type,
wrappingComponentProps: options.wrappingComponentProps,
props,
context,
...(ref && { ref }),
Expand All @@ -122,7 +127,14 @@ class ReactFourteenAdapter extends EnzymeAdapter {
instance = null;
},
getNode() {
return instance ? instanceToTree(instance._reactInternalInstance).rendered : null;
if (!instance) {
return null;
}
return getNodeFromRootFinder(
adapter.isCustomComponent,
instanceToTree(instance._reactInternalInstance),
options,
);
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event);
Expand All @@ -136,6 +148,15 @@ class ReactFourteenAdapter extends EnzymeAdapter {
batchedUpdates(fn) {
return ReactDOM.unstable_batchedUpdates(fn);
},
getWrappingComponentRenderer() {
return {
...this,
...getWrappingComponentMountRenderer({
toTree: inst => instanceToTree(inst._reactInternalInstance),
getMountWrapperInstance: () => instance,
}),
};
},
};
}

Expand Down Expand Up @@ -259,6 +280,13 @@ class ReactFourteenAdapter extends EnzymeAdapter {
createElement(...args) {
return React.createElement(...args);
}

wrapWithWrappingComponent(node, options) {
return {
RootFinder,
node: wrapWithWrappingComponent(React.createElement, node, options),
};
}
}

module.exports = ReactFourteenAdapter;
Expand Up @@ -19,6 +19,10 @@ import {
propsWithKeysAndRef,
ensureKeyOrUndefined,
wrap,
RootFinder,
getNodeFromRootFinder,
wrapWithWrappingComponent,
getWrappingComponentMountRenderer,
} from 'enzyme-adapter-utils';
import ifReact from 'enzyme-adapter-react-helper/build/ifReact';

Expand Down Expand Up @@ -138,6 +142,7 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
const { type, props, ref } = el;
const wrapperProps = {
Component: type,
wrappingComponentProps: options.wrappingComponentProps,
props,
context,
...(ref && { ref }),
Expand All @@ -157,7 +162,14 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
instance = null;
},
getNode() {
return instance ? instanceToTree(instance._reactInternalInstance).rendered : null;
if (!instance) {
return null;
}
return getNodeFromRootFinder(
adapter.isCustomComponent,
instanceToTree(instance._reactInternalInstance),
options,
);
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, eventOptions);
Expand All @@ -171,6 +183,15 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
batchedUpdates(fn) {
return ReactDOM.unstable_batchedUpdates(fn);
},
getWrappingComponentRenderer() {
return {
...this,
...getWrappingComponentMountRenderer({
toTree: inst => instanceToTree(inst._reactInternalInstance),
getMountWrapperInstance: () => instance,
}),
};
},
};
}

Expand Down Expand Up @@ -304,6 +325,13 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
);
invoke();
}

wrapWithWrappingComponent(node, options) {
return {
RootFinder,
node: wrapWithWrappingComponent(React.createElement, node, options),
};
}
}

module.exports = ReactFifteenFourAdapter;
30 changes: 29 additions & 1 deletion packages/enzyme-adapter-react-15/src/ReactFifteenAdapter.js
Expand Up @@ -21,6 +21,10 @@ import {
propsWithKeysAndRef,
ensureKeyOrUndefined,
wrap,
RootFinder,
getNodeFromRootFinder,
wrapWithWrappingComponent,
getWrappingComponentMountRenderer,
} from 'enzyme-adapter-utils';

function compositeTypeToNodeType(type) {
Expand Down Expand Up @@ -138,6 +142,7 @@ class ReactFifteenAdapter extends EnzymeAdapter {
const { type, props, ref } = el;
const wrapperProps = {
Component: type,
wrappingComponentProps: options.wrappingComponentProps,
props,
context,
...(ref && { ref }),
Expand All @@ -157,7 +162,14 @@ class ReactFifteenAdapter extends EnzymeAdapter {
instance = null;
},
getNode() {
return instance ? instanceToTree(instance._reactInternalInstance).rendered : null;
if (!instance) {
return null;
}
return getNodeFromRootFinder(
adapter.isCustomComponent,
instanceToTree(instance._reactInternalInstance),
options,
);
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, eventOptions);
Expand All @@ -171,6 +183,15 @@ class ReactFifteenAdapter extends EnzymeAdapter {
batchedUpdates(fn) {
return ReactDOM.unstable_batchedUpdates(fn);
},
getWrappingComponentRenderer() {
return {
...this,
...getWrappingComponentMountRenderer({
toTree: inst => instanceToTree(inst._reactInternalInstance),
getMountWrapperInstance: () => instance,
}),
};
},
};
}

Expand Down Expand Up @@ -299,6 +320,13 @@ class ReactFifteenAdapter extends EnzymeAdapter {
// React in >= 15.4, and < 16 pass undefined to a setState callback
callback.call(instance, undefined);
}

wrapWithWrappingComponent(node, options) {
return {
RootFinder,
node: wrapWithWrappingComponent(React.createElement, node, options),
};
}
}

module.exports = ReactFifteenAdapter;
32 changes: 30 additions & 2 deletions packages/enzyme-adapter-react-16.1/src/ReactSixteenOneAdapter.js
Expand Up @@ -32,6 +32,10 @@ import {
wrap,
getMaskedContext,
getComponentStack,
RootFinder,
getNodeFromRootFinder,
wrapWithWrappingComponent,
getWrappingComponentMountRenderer,
} from 'enzyme-adapter-utils';
import { findCurrentFiberUsingSlowPath } from 'react-reconciler/reflection';

Expand Down Expand Up @@ -261,7 +265,7 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {

createMountRenderer(options) {
assertDomAvailable('mount');
const { attachTo, hydrateIn } = options;
const { attachTo, hydrateIn, wrappingComponentProps } = options;
const domNode = hydrateIn || attachTo || global.document.createElement('div');
let instance = null;
const adapter = this;
Expand All @@ -272,6 +276,7 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
const wrapperProps = {
Component: type,
props,
wrappingComponentProps,
context,
...(ref && { ref }),
};
Expand All @@ -292,7 +297,14 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
instance = null;
},
getNode() {
return instance ? toTree(instance._reactInternalFiber).rendered : null;
if (!instance) {
return null;
}
return getNodeFromRootFinder(
adapter.isCustomComponent,
toTree(instance._reactInternalFiber),
options,
);
},
simulateError(nodeHierarchy, rootNode, error) {
const { instance: catchingInstance } = nodeHierarchy
Expand Down Expand Up @@ -320,6 +332,15 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
return fn();
// return ReactDOM.unstable_batchedUpdates(fn);
},
getWrappingComponentRenderer() {
return {
...this,
...getWrappingComponentMountRenderer({
toTree: inst => toTree(inst._reactInternalFiber),
getMountWrapperInstance: () => instance,
}),
};
},
};
}

Expand Down Expand Up @@ -512,6 +533,13 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
createElement(...args) {
return React.createElement(...args);
}

wrapWithWrappingComponent(node, options) {
return {
RootFinder,
node: wrapWithWrappingComponent(React.createElement, node, options),
};
}
}

module.exports = ReactSixteenOneAdapter;
32 changes: 30 additions & 2 deletions packages/enzyme-adapter-react-16.2/src/ReactSixteenTwoAdapter.js
Expand Up @@ -33,6 +33,10 @@ import {
wrap,
getMaskedContext,
getComponentStack,
RootFinder,
getNodeFromRootFinder,
wrapWithWrappingComponent,
getWrappingComponentMountRenderer,
} from 'enzyme-adapter-utils';
import { findCurrentFiberUsingSlowPath } from 'react-reconciler/reflection';

Expand Down Expand Up @@ -263,7 +267,7 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {

createMountRenderer(options) {
assertDomAvailable('mount');
const { attachTo, hydrateIn } = options;
const { attachTo, hydrateIn, wrappingComponentProps } = options;
const domNode = hydrateIn || attachTo || global.document.createElement('div');
let instance = null;
const adapter = this;
Expand All @@ -274,6 +278,7 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
const wrapperProps = {
Component: type,
props,
wrappingComponentProps,
context,
...(ref && { ref }),
};
Expand All @@ -294,7 +299,14 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
instance = null;
},
getNode() {
return instance ? toTree(instance._reactInternalFiber).rendered : null;
if (!instance) {
return null;
}
return getNodeFromRootFinder(
adapter.isCustomComponent,
toTree(instance._reactInternalFiber),
options,
);
},
simulateError(nodeHierarchy, rootNode, error) {
const { instance: catchingInstance } = nodeHierarchy
Expand Down Expand Up @@ -322,6 +334,15 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
return fn();
// return ReactDOM.unstable_batchedUpdates(fn);
},
getWrappingComponentRenderer() {
return {
...this,
...getWrappingComponentMountRenderer({
toTree: inst => toTree(inst._reactInternalFiber),
getMountWrapperInstance: () => instance,
}),
};
},
};
}

Expand Down Expand Up @@ -518,6 +539,13 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
createElement(...args) {
return React.createElement(...args);
}

wrapWithWrappingComponent(node, options) {
return {
RootFinder,
node: wrapWithWrappingComponent(React.createElement, node, options),
};
}
}

module.exports = ReactSixteenTwoAdapter;

0 comments on commit fc61100

Please sign in to comment.