Skip to content

Commit

Permalink
[enzyme-adapter-react-*] [new] add wrapWithWrappingComponent
Browse files Browse the repository at this point in the history
  • Loading branch information
minznerjosh committed Feb 26, 2019
1 parent 47ea580 commit 17cb36d
Show file tree
Hide file tree
Showing 9 changed files with 284 additions and 15 deletions.
4 changes: 4 additions & 0 deletions packages/enzyme-adapter-react-13/src/ReactThirteenAdapter.js
Expand Up @@ -277,6 +277,10 @@ class ReactThirteenAdapter extends EnzymeAdapter {
return typeof object === 'string' || typeof object === 'function';
}

isCustomComponent(component) {
return typeof component === 'function';
}

createElement(...args) {
return React.createElement(...args);
}
Expand Down
34 changes: 33 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 @@ -252,9 +273,20 @@ class ReactFourteenAdapter extends EnzymeAdapter {
return isValidElementType(object);
}

isCustomComponent(component) {
return typeof component === 'function';
}

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 @@ -287,6 +308,10 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
return isValidElementType(object);
}

isCustomComponent(component) {
return typeof component === 'function';
}

createElement(...args) {
return React.createElement(...args);
}
Expand All @@ -300,6 +325,13 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
);
invoke();
}

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

module.exports = ReactFifteenFourAdapter;
34 changes: 33 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 @@ -287,6 +308,10 @@ class ReactFifteenAdapter extends EnzymeAdapter {
return isValidElementType(object);
}

isCustomComponent(component) {
return typeof component === 'function';
}

createElement(...args) {
return React.createElement(...args);
}
Expand All @@ -295,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;
36 changes: 34 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 @@ -505,9 +526,20 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
return isValidElementType(object);
}

isCustomComponent(component) {
return typeof component === 'function';
}

createElement(...args) {
return React.createElement(...args);
}

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

module.exports = ReactSixteenOneAdapter;

0 comments on commit 17cb36d

Please sign in to comment.