diff --git a/packages/enzyme/src/selectors.js b/packages/enzyme/src/selectors.js index 8cb98737f..48f379e82 100644 --- a/packages/enzyme/src/selectors.js +++ b/packages/enzyme/src/selectors.js @@ -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(); @@ -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 @@ -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'); }