@@ -5,16 +5,17 @@ import getSelectorTypeOrThrow from '../lib/get-selector-type'
5
5
import {
6
6
REF_SELECTOR ,
7
7
COMPONENT_SELECTOR ,
8
- NAME_SELECTOR ,
9
- DOM_SELECTOR
8
+ NAME_SELECTOR
10
9
} 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'
14
14
import VueWrapper from './vue-wrapper'
15
15
import WrapperArray from './wrapper-array'
16
16
import ErrorWrapper from './error-wrapper'
17
17
import { throwError , warn } from '../lib/util'
18
+ import findAll from '../lib/find'
18
19
19
20
export default class Wrapper implements BaseWrapper {
20
21
vnode : VNode ;
@@ -78,25 +79,9 @@ export default class Wrapper implements BaseWrapper {
78
79
*/
79
80
contains ( selector : Selector ) {
80
81
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
100
85
}
101
86
102
87
/**
@@ -234,73 +219,29 @@ export default class Wrapper implements BaseWrapper {
234
219
*/
235
220
find ( selector : Selector ) : Wrapper | ErrorWrapper | VueWrapper {
236
221
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 ) {
258
225
return new ErrorWrapper ( `ref="${ selector . ref } "` )
259
226
}
260
- return new Wrapper ( nodes [ 0 ] , this . update , this . options )
227
+ return new ErrorWrapper ( typeof selector === 'string' ? selector : 'Component' )
261
228
}
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 )
269
232
}
270
233
271
234
/**
272
235
* Finds node in tree of the current wrapper that matches the provided selector.
273
236
*/
274
237
findAll ( selector : Selector ) : WrapperArray {
275
238
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 ) )
276
243
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 )
304
245
}
305
246
306
247
/**
0 commit comments