Skip to content

Commit

Permalink
Add additional type checks to adapters
Browse files Browse the repository at this point in the history
  • Loading branch information
jquense authored and ljharb committed Jul 2, 2018
1 parent 9b354d1 commit d79da18
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions packages/enzyme/src/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
childrenOfNode,
hasClassName,
} from './RSTTraversal';
import { nodeHasType, propsOfNode } from './Utils';
import { getAdapter, nodeHasType, propsOfNode } from './Utils';
// our CSS selector parser instance
const parser = createParser();

Expand Down Expand Up @@ -239,8 +239,17 @@ function isComplexSelector(tokens) {
* @param {Function|Object|String} selector
*/
export function buildPredicate(selector) {
// If the selector is a function, check if the node's constructor matches
if (typeof selector === 'function') {
// If the selector is a string, parse it as a simple CSS selector
if (typeof selector === 'string') {
const tokens = safelyGenerateTokens(selector);
if (isComplexSelector(tokens)) {
throw new TypeError('This method does not support complex CSS selectors');
}
// Simple selectors only have a single selector token
return buildPredicateFromToken(tokens[0]);
}
// If the selector is an element type, check if the node's type matches
if (getAdapter().isValidElementType(selector)) {
return node => node && node.type === selector;
}
// If the selector is an non-empty object, treat the keys/values as props
Expand All @@ -254,15 +263,7 @@ export function buildPredicate(selector) {
}
throw new TypeError('Enzyme::Selector does not support an array, null, or empty object as a selector');
}
// If the selector is a string, parse it as a simple CSS selector
if (typeof selector === 'string') {
const tokens = safelyGenerateTokens(selector);
if (isComplexSelector(tokens)) {
throw new TypeError('This method does not support complex CSS selectors');
}
// Simple selectors only have a single selector token
return buildPredicateFromToken(tokens[0]);
}

throw new TypeError('Enzyme::Selector expects a string, object, or Component Constructor');
}

Expand Down

0 comments on commit d79da18

Please sign in to comment.