@@ -5,16 +5,17 @@ import getSelectorTypeOrThrow from '../lib/get-selector-type'
55import {
66 REF_SELECTOR ,
77 COMPONENT_SELECTOR ,
8- NAME_SELECTOR ,
9- DOM_SELECTOR
8+ NAME_SELECTOR
109} from '../lib/consts'
11- import findVueComponents , { vmCtorMatchesName , vmCtorMatchesSelector } from '../lib/find-vue-components'
12- import findVNodesBySelector from '../lib/find-vnodes-by-selector'
13- import findVNodesByRef from '../lib/find-vnodes-by-ref'
10+ import {
11+ vmCtorMatchesName ,
12+ vmCtorMatchesSelector
13+ } from '../lib/find-vue-components'
1414import VueWrapper from './vue-wrapper'
1515import WrapperArray from './wrapper-array'
1616import ErrorWrapper from './error-wrapper'
1717import { throwError , warn } from '../lib/util'
18+ import findAll from '../lib/find'
1819
1920export default class Wrapper implements BaseWrapper {
2021 vnode : VNode ;
@@ -78,25 +79,9 @@ export default class Wrapper implements BaseWrapper {
7879 */
7980 contains ( selector : Selector ) {
8081 const selectorType = getSelectorTypeOrThrow ( selector , 'contains' )
81-
82- if ( selectorType === NAME_SELECTOR || selectorType === COMPONENT_SELECTOR ) {
83- const vm = this . vm || this . vnode . context . $root
84- return findVueComponents ( vm , selector , selector ) . length > 0 || this . is ( selector )
85- }
86-
87- if ( selectorType === REF_SELECTOR ) {
88- if ( ! this . vm ) {
89- throwError ( '$ref selectors can only be used on Vue component wrappers' )
90- }
91- const nodes = findVNodesByRef ( this . vnode , selector . ref )
92- return nodes . length > 0
93- }
94-
95- if ( selectorType === DOM_SELECTOR && this . element instanceof HTMLElement ) {
96- return this . element . querySelectorAll ( selector ) . length > 0 || this . is ( selector )
97- }
98-
99- return false
82+ const nodes = findAll ( this . vm , this . vnode , selectorType , selector )
83+ const is = selectorType === REF_SELECTOR ? false : this . is ( selector )
84+ return nodes . length > 0 || is
10085 }
10186
10287 /**
@@ -234,73 +219,29 @@ export default class Wrapper implements BaseWrapper {
234219 */
235220 find ( selector : Selector ) : Wrapper | ErrorWrapper | VueWrapper {
236221 const selectorType = getSelectorTypeOrThrow ( selector , 'find' )
237-
238- if ( selectorType === COMPONENT_SELECTOR ||
239- selectorType === NAME_SELECTOR ) {
240- const root = this . vm || this . vnode
241- // $FlowIgnore warning about selectorType being undefined
242- const components = findVueComponents ( root , selectorType , selector )
243- if ( components . length === 0 ) {
244- return new ErrorWrapper ( 'Component' )
245- }
246- return new VueWrapper ( components [ 0 ] , this . options )
247- }
248-
249- if ( selectorType === REF_SELECTOR ) {
250- if ( ! this . vm ) {
251- throwError ( '$ref selectors can only be used on Vue component wrappers' )
252- }
253- if ( this . vm && this . vm . $refs && selector . ref in this . vm . $refs && this . vm . $refs [ selector . ref ] instanceof Vue ) {
254- return new VueWrapper ( this . vm . $refs [ selector . ref ] , this . options )
255- }
256- const nodes = findVNodesByRef ( this . vnode , selector . ref )
257- if ( nodes . length === 0 ) {
222+ const nodes = findAll ( this . vm , this . vnode , selectorType , selector )
223+ if ( nodes . length === 0 ) {
224+ if ( selector . ref ) {
258225 return new ErrorWrapper ( `ref="${ selector . ref } "` )
259226 }
260- return new Wrapper ( nodes [ 0 ] , this . update , this . options )
227+ return new ErrorWrapper ( typeof selector === 'string' ? selector : 'Component' )
261228 }
262-
263- const nodes = findVNodesBySelector ( this . vnode , selector )
264-
265- if ( nodes . length === 0 ) {
266- return new ErrorWrapper ( selector )
267- }
268- return new Wrapper ( nodes [ 0 ] , this . update , this . options )
229+ return nodes [ 0 ] instanceof Vue
230+ ? new VueWrapper ( nodes [ 0 ] , this . options )
231+ : new Wrapper ( nodes [ 0 ] , this . update , this . options )
269232 }
270233
271234 /**
272235 * Finds node in tree of the current wrapper that matches the provided selector.
273236 */
274237 findAll ( selector : Selector ) : WrapperArray {
275238 const selectorType = getSelectorTypeOrThrow ( selector , 'findAll' )
239+ const nodes = findAll ( this . vm , this . vnode , selectorType , selector )
240+ const wrappers = nodes [ 0 ] && nodes [ 0 ] instanceof Vue
241+ ? nodes . map ( node => new VueWrapper ( node , this . options ) )
242+ : nodes . map ( node => new Wrapper ( node , this . update , this . options ) )
276243
277- if ( selectorType === COMPONENT_SELECTOR ||
278- selectorType === NAME_SELECTOR ) {
279- const root = this . vm || this . vnode
280- // $FlowIgnore warning about selectorType being undefined
281- const components = findVueComponents ( root , selectorType , selector )
282- return new WrapperArray ( components . map ( component => new VueWrapper ( component , this . options ) ) )
283- }
284-
285- if ( selectorType === REF_SELECTOR ) {
286- if ( ! this . vm ) {
287- throwError ( '$ref selectors can only be used on Vue component wrappers' )
288- }
289- if ( this . vm && this . vm . $refs && selector . ref in this . vm . $refs && this . vm . $refs [ selector . ref ] instanceof Vue ) {
290- return new WrapperArray ( [ new VueWrapper ( this . vm . $refs [ selector . ref ] , this . options ) ] )
291- }
292- const nodes = findVNodesByRef ( this . vnode , selector . ref )
293- return new WrapperArray ( nodes . map ( node => new Wrapper ( node , this . update , this . options ) ) )
294- }
295-
296- function nodeMatchesSelector ( node , selector ) {
297- return node . elm && node . elm . getAttribute && node . elm . matches ( selector )
298- }
299-
300- const nodes = findVNodesBySelector ( this . vnode , selector )
301- const matchingNodes = nodes . filter ( node => nodeMatchesSelector ( node , selector ) )
302-
303- return new WrapperArray ( matchingNodes . map ( node => new Wrapper ( node , this . update , this . options ) ) )
244+ return new WrapperArray ( wrappers )
304245 }
305246
306247 /**
0 commit comments