@@ -13,7 +13,7 @@ import {
1313 childrenOfNode ,
1414 hasClassName ,
1515} from './RSTTraversal' ;
16- import { nodeHasType , propsOfNode } from './Utils' ;
16+ import { getAdapter , nodeHasType , propsOfNode } from './Utils' ;
1717// our CSS selector parser instance
1818const parser = createParser ( ) ;
1919
@@ -239,8 +239,17 @@ function isComplexSelector(tokens) {
239239 * @param {Function|Object|String } selector
240240 */
241241export function buildPredicate ( selector ) {
242- // If the selector is a function, check if the node's constructor matches
243- if ( typeof selector === 'function' ) {
242+ // If the selector is a string, parse it as a simple CSS selector
243+ if ( typeof selector === 'string' ) {
244+ const tokens = safelyGenerateTokens ( selector ) ;
245+ if ( isComplexSelector ( tokens ) ) {
246+ throw new TypeError ( 'This method does not support complex CSS selectors' ) ;
247+ }
248+ // Simple selectors only have a single selector token
249+ return buildPredicateFromToken ( tokens [ 0 ] ) ;
250+ }
251+ // If the selector is an element type, check if the node's type matches
252+ if ( getAdapter ( ) . isValidElementType ( selector ) ) {
244253 return node => node && node . type === selector ;
245254 }
246255 // If the selector is an non-empty object, treat the keys/values as props
@@ -254,15 +263,7 @@ export function buildPredicate(selector) {
254263 }
255264 throw new TypeError ( 'Enzyme::Selector does not support an array, null, or empty object as a selector' ) ;
256265 }
257- // If the selector is a string, parse it as a simple CSS selector
258- if ( typeof selector === 'string' ) {
259- const tokens = safelyGenerateTokens ( selector ) ;
260- if ( isComplexSelector ( tokens ) ) {
261- throw new TypeError ( 'This method does not support complex CSS selectors' ) ;
262- }
263- // Simple selectors only have a single selector token
264- return buildPredicateFromToken ( tokens [ 0 ] ) ;
265- }
266+
266267 throw new TypeError ( 'Enzyme::Selector expects a string, object, or Component Constructor' ) ;
267268}
268269
0 commit comments