Skip to content

Commit

Permalink
Address some PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
lelandrichardson committed Jul 16, 2017
1 parent f78a8a4 commit 538550f
Show file tree
Hide file tree
Showing 10 changed files with 44 additions and 22 deletions.
11 changes: 1 addition & 10 deletions src/ReactWrapper.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
ITERATOR_SYMBOL,
nodeEqual,
nodeMatches,
getAdapter,
} from './Utils';
import {
debugNodes,
Expand All @@ -25,7 +26,6 @@ import {
treeFilter,
buildPredicate,
} from './RSTTraversal';
import configuration from './configuration';

const noop = () => {};

Expand Down Expand Up @@ -62,15 +62,6 @@ function getFromRenderer(renderer) {
};
}

function getAdapter(options) {
if (options.adapter) {
return options.adapter;
}
const adapter = configuration.get().adapter;
// TODO(lmr): warn about no adapter being configured
return adapter;
}

/**
* @class ReactWrapper
*/
Expand Down
11 changes: 1 addition & 10 deletions src/ShallowWrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
isFunctionalComponent,
isCustomComponentElement,
ITERATOR_SYMBOL,
getAdapter,
} from './Utils';
import {
debugNodes,
Expand All @@ -29,7 +30,6 @@ import {
treeFilter,
buildPredicate,
} from './RSTTraversal';
import configuration from './configuration';

/**
* Finds all nodes in the current wrapper nodes' render trees that match the provided predicate
Expand Down Expand Up @@ -98,15 +98,6 @@ function getRootNode(node) {
return node.rendered;
}

function getAdapter(options) {
if (options.adapter) {
return options.adapter;
}
const adapter = configuration.get().adapter;
// TODO(lmr): warn about no adapter being configured
return adapter;
}

/**
* @class ShallowWrapper
*/
Expand Down
22 changes: 22 additions & 0 deletions src/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,31 @@ import is from 'object-is';
import uuidv4 from 'uuid/v4';
import entries from 'object.entries';
import functionName from 'function.prototype.name';
import configuration from './configuration';

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

export function getAdapter(options = {}) {
if (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
`);
}
return adapter;
}

// TODO(lmr): we shouldn't need this
export function isFunctionalComponent(inst) {
return !!inst && !!inst.constructor && typeof inst.constructor === 'function' &&
functionName(inst.constructor) === 'StatelessComponent';
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/ReactFifteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
mapNativeEventNames,
propFromEvent,
withSetStateAllowed,
assertDomAvailable,
} from './Utils';

function compositeTypeToNodeType(type) {
Expand Down Expand Up @@ -73,6 +74,7 @@ SimpleWrapper.propTypes = { node: PropTypes.node.isRequired };

class ReactFifteenAdapter extends EnzymeAdapter {
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
let instance = null;
return {
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/ReactFifteenFourAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
mapNativeEventNames,
propFromEvent,
withSetStateAllowed,
assertDomAvailable,
} from './Utils';

function compositeTypeToNodeType(type) {
Expand Down Expand Up @@ -73,6 +74,7 @@ SimpleWrapper.propTypes = { node: PropTypes.node.isRequired };

class ReactFifteenFourAdapter extends EnzymeAdapter {
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
let instance = null;
return {
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/ReactFourteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
mapNativeEventNames,
propFromEvent,
withSetStateAllowed,
assertDomAvailable,
} from './Utils';

function typeToNodeType(type) {
Expand Down Expand Up @@ -68,6 +69,7 @@ SimpleWrapper.propTypes = { node: PropTypes.node.isRequired };

class ReactFifteenAdapter extends EnzymeAdapter {
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
let instance = null;
return {
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/ReactSixteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import elementToTree from './elementToTree';
import {
mapNativeEventNames,
propFromEvent,
assertDomAvailable,
} from './Utils';

const HostRoot = 3;
Expand Down Expand Up @@ -142,6 +143,7 @@ SimpleWrapper.propTypes = { node: PropTypes.node.isRequired };

class ReactSixteenAdapter extends EnzymeAdapter {
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
let instance = null;
return {
Expand Down
2 changes: 2 additions & 0 deletions src/adapters/ReactThirteenAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
mapNativeEventNames,
propFromEvent,
withSetStateAllowed,
assertDomAvailable,
} from './Utils';

// this fixes some issues in React 0.13 with setState and jsdom...
Expand Down Expand Up @@ -94,6 +95,7 @@ SimpleWrapper.propTypes = { node: PropTypes.node.isRequired };

class ReactThirteenAdapter extends EnzymeAdapter {
createMountRenderer(options) {
assertDomAvailable('mount');
const domNode = options.attachTo || global.document.createElement('div');
let instance = null;
return {
Expand Down
8 changes: 8 additions & 0 deletions src/adapters/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,11 @@ export function withSetStateAllowed(fn) {
}
return result;
}

export function assertDomAvailable(feature) {
if (!global || !global.document || !global.document.createElement) {
throw new Error(
`Enzyme's ${feature} expects a DOM environment to be loaded, but found none`,
);
}
}
4 changes: 2 additions & 2 deletions test/_helpers/react-compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
import/prefer-default-export: 0,
*/

import { REACT155, REACT16 } from '../../src/version';
import { is } from '../../src/version';

let createClass;

if (REACT155 || REACT16) {
if (is('>=15.5')) {
// eslint-disable-next-line import/no-extraneous-dependencies
createClass = require('create-react-class');
} else {
Expand Down

0 comments on commit 538550f

Please sign in to comment.