Skip to content

Commit c8826a7

Browse files
committed
Add additional type checks to adapters
1 parent d490104 commit c8826a7

File tree

1 file changed

+13
-12
lines changed

1 file changed

+13
-12
lines changed

packages/enzyme/src/selectors.js

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1818
const parser = createParser();
1919

@@ -239,8 +239,17 @@ function isComplexSelector(tokens) {
239239
* @param {Function|Object|String} selector
240240
*/
241241
export 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

Comments
 (0)