Skip to content

Commit

Permalink
some bug fixes and PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
lelandrichardson committed Aug 15, 2017
1 parent 706b91b commit f014e34
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 68 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "enzyme",
"version": "2.9.1",
"version": "3.0.0-alpha.2",
"description": "JavaScript Testing utilities for React",
"homepage": "http://airbnb.io/enzyme/",
"main": "build",
Expand Down
4 changes: 2 additions & 2 deletions src/ReactWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@ class ReactWrapper {
simulate(event, mock = {}) {
this.single('simulate', (n) => {
this.renderer.simulateEvent(n, event, mock);
this.update();
this.root.update();
});
return this;
}
Expand Down Expand Up @@ -689,7 +689,7 @@ class ReactWrapper {
* @returns {String}
*/
key() {
return this.single('key', n => n.key); // TODO(lmr): RSTNode might need to understand key?
return this.single('key', n => n.key);
}

/**
Expand Down
15 changes: 3 additions & 12 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,17 @@ import uuidv4 from 'uuid/v4';
import entries from 'object.entries';
import functionName from 'function.prototype.name';
import configuration from './configuration';
import validateAdapter from './validateAdapter';

export const ITERATOR_SYMBOL = typeof Symbol === 'function' && Symbol.iterator;

export function getAdapter(options = {}) {
if (options.adapter) {
validateAdapter(options.adapter);
return options.adapter;
}
const adapter = configuration.get().adapter;
if (!adapter) {
throw new Error(`
Enzyme expects an adapter to be configured, but found none. To configure an adapter,
you should call \`Enzyme.configure({ adapter: new Adapter() })\` before using any of
Enzyme's top level APIs, where \`Adapter\` is the adapter corresponding to the library
currently being tested. For example:
import Adapter from 'enzyme-adapter-react-15';
To find out more about this, see http://airbnb.io/enzyme/docs/installation/index.html
`);
}
validateAdapter(adapter);
return adapter;
}

Expand Down
6 changes: 6 additions & 0 deletions src/adapters/EnzymeAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ class EnzymeAdapter {
}
}

EnzymeAdapter.MODES = {
STRING: 'string',
MOUNT: 'mount',
SHALLOW: 'shallow',
};

module.exports = EnzymeAdapter;
23 changes: 16 additions & 7 deletions src/adapters/ReactFifteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import {

function compositeTypeToNodeType(type) {
switch (type) {
case 0: return 'class';
case 0:
case 1: return 'class';
case 2: return 'function';
default:
throw new Error(`Enzyme Internal Error: unknown composite type ${type}`);
}
}

function instanceToTree(inst) {
if (typeof inst !== 'object') {
if (!inst || typeof inst !== 'object') {
return inst;
}
const el = inst._currentElement;
Expand All @@ -35,6 +36,8 @@ function instanceToTree(inst) {
nodeType: inst._hostNode ? 'host' : compositeTypeToNodeType(inst._compositeType),
type: el.type,
props: el.props,
key: el.key,
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: values(inst._renderedChildren).map(instanceToTree),
};
Expand All @@ -48,6 +51,8 @@ function instanceToTree(inst) {
nodeType: 'host',
type: el.type,
props: el.props,
key: el.key,
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: values(children).map(instanceToTree),
};
Expand All @@ -57,6 +62,8 @@ function instanceToTree(inst) {
nodeType: compositeTypeToNodeType(inst._compositeType),
type: el.type,
props: el.props,
key: el.key,
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: instanceToTree(inst._renderedComponent),
};
Expand Down Expand Up @@ -117,7 +124,7 @@ class ReactFifteenAdapter extends EnzymeAdapter {
isDOM = true;
} else {
isDOM = false;
return renderer.render(el, context); // TODO: context
return withSetStateAllowed(() => renderer.render(el, context));
}
},
unmount() {
Expand All @@ -132,6 +139,8 @@ class ReactFifteenAdapter extends EnzymeAdapter {
nodeType: 'class',
type: cachedNode.type,
props: cachedNode.props,
key: cachedNode.key,
ref: cachedNode.ref,
instance: renderer._instance._instance,
rendered: elementToTree(output),
};
Expand Down Expand Up @@ -167,11 +176,11 @@ class ReactFifteenAdapter extends EnzymeAdapter {
// eslint-disable-next-line class-methods-use-this, no-unused-vars
createRenderer(options) {
switch (options.mode) {
case 'mount': return this.createMountRenderer(options);
case 'shallow': return this.createShallowRenderer(options);
case 'string': return this.createStringRenderer(options);
case EnzymeAdapter.MODES.MOUNT: return this.createMountRenderer(options);
case EnzymeAdapter.MODES.SHALLOW: return this.createShallowRenderer(options);
case EnzymeAdapter.MODES.STRING: return this.createStringRenderer(options);
default:
throw new Error('Unrecognized mode');
throw new Error(`Enzyme Internal Error: Unrecognized mode: ${options.mode}`);
}
}

Expand Down
23 changes: 16 additions & 7 deletions src/adapters/ReactFifteenFourAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ import {

function compositeTypeToNodeType(type) {
switch (type) {
case 0: return 'class';
case 0:
case 1: return 'class';
case 2: return 'function';
default:
throw new Error(`Enzyme Internal Error: unknown composite type ${type}`);
}
}

function instanceToTree(inst) {
if (typeof inst !== 'object') {
if (!inst || typeof inst !== 'object') {
return inst;
}
const el = inst._currentElement;
Expand All @@ -35,6 +36,8 @@ function instanceToTree(inst) {
nodeType: inst._hostNode ? 'host' : compositeTypeToNodeType(inst._compositeType),
type: el.type,
props: el.props,
key: el.key,
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: values(inst._renderedChildren).map(instanceToTree),
};
Expand All @@ -48,6 +51,8 @@ function instanceToTree(inst) {
nodeType: 'host',
type: el.type,
props: el.props,
key: el.key,
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: values(children).map(instanceToTree),
};
Expand All @@ -57,6 +62,8 @@ function instanceToTree(inst) {
nodeType: compositeTypeToNodeType(inst._compositeType),
type: el.type,
props: el.props,
key: el.key,
ref: el.ref,
instance: inst._instance || inst._hostNode || null,
rendered: instanceToTree(inst._renderedComponent),
};
Expand Down Expand Up @@ -117,7 +124,7 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
isDOM = true;
} else {
isDOM = false;
return renderer.render(el, context); // TODO: context
return withSetStateAllowed(() => renderer.render(el, context));
}
},
unmount() {
Expand All @@ -132,6 +139,8 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
nodeType: 'class',
type: cachedNode.type,
props: cachedNode.props,
key: cachedNode.key,
ref: cachedNode.ref,
instance: renderer._instance._instance,
rendered: elementToTree(output),
};
Expand Down Expand Up @@ -167,11 +176,11 @@ class ReactFifteenFourAdapter extends EnzymeAdapter {
// eslint-disable-next-line class-methods-use-this, no-unused-vars
createRenderer(options) {
switch (options.mode) {
case 'mount': return this.createMountRenderer(options);
case 'shallow': return this.createShallowRenderer(options);
case 'string': return this.createStringRenderer(options);
case EnzymeAdapter.MODES.MOUNT: return this.createMountRenderer(options);
case EnzymeAdapter.MODES.SHALLOW: return this.createShallowRenderer(options);
case EnzymeAdapter.MODES.STRING: return this.createStringRenderer(options);
default:
throw new Error('Unrecognized mode');
throw new Error(`Enzyme Internal Error: Unrecognized mode: ${options.mode}`);
}
}

Expand Down
18 changes: 12 additions & 6 deletions src/adapters/ReactFourteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function typeToNodeType(type) {
}

function instanceToTree(inst) {
if (typeof inst !== 'object') {
if (!inst || typeof inst !== 'object') {
return inst;
}
const el = inst._currentElement;
Expand All @@ -43,6 +43,8 @@ function instanceToTree(inst) {
nodeType: 'host',
type: el.type,
props: el.props,
key: el.key,
ref: el.ref,
instance: ReactDOM.findDOMNode(inst.getPublicInstance()) || null,
rendered: values(children).map(instanceToTree),
};
Expand All @@ -52,6 +54,8 @@ function instanceToTree(inst) {
nodeType: typeToNodeType(el.type),
type: el.type,
props: el.props,
key: el.key,
ref: el.ref,
instance: inst._instance || null,
rendered: instanceToTree(inst._renderedComponent),
};
Expand Down Expand Up @@ -112,7 +116,7 @@ class ReactFifteenAdapter extends EnzymeAdapter {
isDOM = true;
} else {
isDOM = false;
return renderer.render(el, context); // TODO: context
return withSetStateAllowed(() => renderer.render(el, context));
}
},
unmount() {
Expand All @@ -127,6 +131,8 @@ class ReactFifteenAdapter extends EnzymeAdapter {
nodeType: 'class',
type: cachedNode.type,
props: cachedNode.props,
key: cachedNode.key,
ref: cachedNode.ref,
instance: renderer._instance._instance,
rendered: elementToTree(output),
};
Expand Down Expand Up @@ -162,11 +168,11 @@ class ReactFifteenAdapter extends EnzymeAdapter {
// eslint-disable-next-line class-methods-use-this, no-unused-vars
createRenderer(options) {
switch (options.mode) {
case 'mount': return this.createMountRenderer(options);
case 'shallow': return this.createShallowRenderer(options);
case 'string': return this.createStringRenderer(options);
case EnzymeAdapter.MODES.MOUNT: return this.createMountRenderer(options);
case EnzymeAdapter.MODES.SHALLOW: return this.createShallowRenderer(options);
case EnzymeAdapter.MODES.STRING: return this.createStringRenderer(options);
default:
throw new Error('Unrecognized mode');
throw new Error(`Enzyme Internal Error: Unrecognized mode: ${options.mode}`);
}
}

Expand Down
20 changes: 14 additions & 6 deletions src/adapters/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
import React from 'react';
import ReactDOM from 'react-dom';
import ReactDOMServer from 'react-dom/server';
// import TestRenderer from 'react-test-renderer';
import ShallowRenderer from 'react-test-renderer/shallow';
import TestUtils from 'react-dom/test-utils';
import PropTypes from 'prop-types';
Expand All @@ -12,6 +11,7 @@ import {
mapNativeEventNames,
propFromEvent,
assertDomAvailable,
withSetStateAllowed,
} from './Utils';

const HostRoot = 3;
Expand Down Expand Up @@ -66,6 +66,8 @@ function toTree(vnode) {
nodeType: 'class',
type: node.type,
props: { ...node.memoizedProps },
key: node.key,
ref: node.ref,
instance: node.stateNode,
rendered: childrenToTree(node.child),
};
Expand All @@ -76,6 +78,8 @@ function toTree(vnode) {
nodeType: 'function',
type: node.type,
props: { ...node.memoizedProps },
key: node.key,
ref: node.ref,
instance: null,
rendered: childrenToTree(node.child),
};
Expand All @@ -88,6 +92,8 @@ function toTree(vnode) {
nodeType: 'host',
type: node.type,
props: { ...node.memoizedProps },
key: node.key,
ref: node.ref,
instance: node.stateNode,
rendered: renderedNodes,
};
Expand Down Expand Up @@ -187,7 +193,7 @@ class ReactSixteenAdapter extends EnzymeAdapter {
isDOM = true;
} else {
isDOM = false;
return renderer.render(el, context);
return withSetStateAllowed(() => renderer.render(el, context));
}
},
unmount() {
Expand All @@ -202,6 +208,8 @@ class ReactSixteenAdapter extends EnzymeAdapter {
nodeType: 'class',
type: cachedNode.type,
props: cachedNode.props,
key: cachedNode.key,
ref: cachedNode.ref,
instance: renderer._instance,
rendered: elementToTree(output),
};
Expand Down Expand Up @@ -238,11 +246,11 @@ class ReactSixteenAdapter extends EnzymeAdapter {
// eslint-disable-next-line class-methods-use-this, no-unused-vars
createRenderer(options) {
switch (options.mode) {
case 'mount': return this.createMountRenderer(options);
case 'shallow': return this.createShallowRenderer(options);
case 'string': return this.createStringRenderer(options);
case EnzymeAdapter.MODES.MOUNT: return this.createMountRenderer(options);
case EnzymeAdapter.MODES.SHALLOW: return this.createShallowRenderer(options);
case EnzymeAdapter.MODES.STRING: return this.createStringRenderer(options);
default:
throw new Error('Unrecognized mode');
throw new Error(`Enzyme Internal Error: Unrecognized mode: ${options.mode}`);
}
}

Expand Down

0 comments on commit f014e34

Please sign in to comment.