Skip to content

Commit

Permalink
[enzyme-adapter-react-{16,16.3,16.2,16.1}] [new] add simulateError
Browse files Browse the repository at this point in the history
…to `shallow` and `mount` renderers
  • Loading branch information
ljharb committed Aug 27, 2018
1 parent 6803727 commit 66f27e9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
25 changes: 25 additions & 0 deletions packages/enzyme-adapter-react-16.1/src/ReactSixteenOneAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import {
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
simulateError,
} from 'enzyme-adapter-utils';
import { findCurrentFiberUsingSlowPath } from 'react-reconciler/reflection';

Expand Down Expand Up @@ -262,6 +263,19 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
getNode() {
return instance ? toTree(instance._reactInternalFiber).rendered : null;
},
simulateError(nodeHierarchy, rootNode, error) {
const { instance: catchingInstance } = nodeHierarchy
.find(x => x.instance && x.instance.componentDidCatch) || {};

simulateError(
error,
catchingInstance,
rootNode,
nodeHierarchy,
nodeTypeFromType,
adapter.displayNameOfNode,
);
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, eventOptions);
const eventFn = TestUtils.Simulate[mappedEvent];
Expand All @@ -279,6 +293,7 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
}

createShallowRenderer(/* options */) {
const adapter = this;
const renderer = new ShallowRenderer();
let isDOM = false;
let cachedNode = null;
Expand Down Expand Up @@ -327,6 +342,16 @@ class ReactSixteenOneAdapter extends EnzymeAdapter {
: elementToTree(output),
};
},
simulateError(nodeHierarchy, rootNode, error) {
simulateError(
error,
renderer._instance,
cachedNode,
nodeHierarchy.concat(cachedNode),
nodeTypeFromType,
adapter.displayNameOfNode,
);
},
simulateEvent(node, event, ...args) {
const handler = node.props[propFromEvent(event, eventOptions)];
if (handler) {
Expand Down
25 changes: 25 additions & 0 deletions packages/enzyme-adapter-react-16.2/src/ReactSixteenTwoAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
simulateError,
} from 'enzyme-adapter-utils';
import { findCurrentFiberUsingSlowPath } from 'react-reconciler/reflection';

Expand Down Expand Up @@ -264,6 +265,19 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
getNode() {
return instance ? toTree(instance._reactInternalFiber).rendered : null;
},
simulateError(nodeHierarchy, rootNode, error) {
const { instance: catchingInstance } = nodeHierarchy
.find(x => x.instance && x.instance.componentDidCatch) || {};

simulateError(
error,
catchingInstance,
rootNode,
nodeHierarchy,
nodeTypeFromType,
adapter.displayNameOfNode,
);
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, eventOptions);
const eventFn = TestUtils.Simulate[mappedEvent];
Expand All @@ -281,6 +295,7 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
}

createShallowRenderer(/* options */) {
const adapter = this;
const renderer = new ShallowRenderer();
let isDOM = false;
let cachedNode = null;
Expand Down Expand Up @@ -329,6 +344,16 @@ class ReactSixteenTwoAdapter extends EnzymeAdapter {
: elementToTree(output),
};
},
simulateError(nodeHierarchy, rootNode, error) {
simulateError(
error,
renderer._instance,
cachedNode,
nodeHierarchy.concat(cachedNode),
nodeTypeFromType,
adapter.displayNameOfNode,
);
},
simulateEvent(node, event, ...args) {
const handler = node.props[propFromEvent(event, eventOptions)];
if (handler) {
Expand Down
25 changes: 25 additions & 0 deletions packages/enzyme-adapter-react-16.3/src/ReactSixteenThreeAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
simulateError,
} from 'enzyme-adapter-utils';
import { findCurrentFiberUsingSlowPath } from 'react-reconciler/reflection';

Expand Down Expand Up @@ -283,6 +284,19 @@ class ReactSixteenThreeAdapter extends EnzymeAdapter {
getNode() {
return instance ? toTree(instance._reactInternalFiber).rendered : null;
},
simulateError(nodeHierarchy, rootNode, error) {
const { instance: catchingInstance } = nodeHierarchy
.find(x => x.instance && x.instance.componentDidCatch) || {};

simulateError(
error,
catchingInstance,
rootNode,
nodeHierarchy,
nodeTypeFromType,
adapter.displayNameOfNode,
);
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, eventOptions);
const eventFn = TestUtils.Simulate[mappedEvent];
Expand All @@ -300,6 +314,7 @@ class ReactSixteenThreeAdapter extends EnzymeAdapter {
}

createShallowRenderer(/* options */) {
const adapter = this;
const renderer = new ShallowRenderer();
let isDOM = false;
let cachedNode = null;
Expand Down Expand Up @@ -348,6 +363,16 @@ class ReactSixteenThreeAdapter extends EnzymeAdapter {
: elementToTree(output),
};
},
simulateError(nodeHierarchy, rootNode, error) {
simulateError(
error,
renderer._instance,
cachedNode,
nodeHierarchy.concat(cachedNode),
nodeTypeFromType,
adapter.displayNameOfNode,
);
},
simulateEvent(node, event, ...args) {
const handler = node.props[propFromEvent(event, eventOptions)];
if (handler) {
Expand Down
25 changes: 25 additions & 0 deletions packages/enzyme-adapter-react-16/src/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import {
createMountWrapper,
propsWithKeysAndRef,
ensureKeyOrUndefined,
simulateError,
} from 'enzyme-adapter-utils';
import findCurrentFiberUsingSlowPath from './findCurrentFiberUsingSlowPath';
import detectFiberTags from './detectFiberTags';
Expand Down Expand Up @@ -284,6 +285,19 @@ class ReactSixteenAdapter extends EnzymeAdapter {
getNode() {
return instance ? toTree(instance._reactInternalFiber).rendered : null;
},
simulateError(nodeHierarchy, rootNode, error) {
const { instance: catchingInstance } = nodeHierarchy
.find(x => x.instance && x.instance.componentDidCatch) || {};

simulateError(
error,
catchingInstance,
rootNode,
nodeHierarchy,
nodeTypeFromType,
adapter.displayNameOfNode,
);
},
simulateEvent(node, event, mock) {
const mappedEvent = mapNativeEventNames(event, eventOptions);
const eventFn = TestUtils.Simulate[mappedEvent];
Expand All @@ -300,6 +314,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
}

createShallowRenderer(/* options */) {
const adapter = this;
const renderer = new ShallowRenderer();
let isDOM = false;
let cachedNode = null;
Expand Down Expand Up @@ -348,6 +363,16 @@ class ReactSixteenAdapter extends EnzymeAdapter {
: elementToTree(output),
};
},
simulateError(nodeHierarchy, rootNode, error) {
simulateError(
error,
renderer._instance,
cachedNode,
nodeHierarchy.concat(cachedNode),
nodeTypeFromType,
adapter.displayNameOfNode,
);
},
simulateEvent(node, event, ...args) {
const handler = node.props[propFromEvent(event, eventOptions)];
if (handler) {
Expand Down

0 comments on commit 66f27e9

Please sign in to comment.