Skip to content

Commit

Permalink
[New] selectors: buildPredicate: call into adapter’s `isValidElem…
Browse files Browse the repository at this point in the history
…entType`, if present
  • Loading branch information
jquense authored and ljharb committed Jul 2, 2018
1 parent 78bbbfc commit bdec3d4
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 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,23 @@ 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
const adapter = getAdapter();
const isElementType = adapter.isValidElementType
? adapter.isValidElementType(selector)
: typeof selector === 'function';
if (isElementType) {
return node => node && node.type === selector;
}
// If the selector is an non-empty object, treat the keys/values as props
Expand All @@ -254,16 +269,8 @@ 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');

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

/**
Expand Down

0 comments on commit bdec3d4

Please sign in to comment.