File tree 6 files changed +25
-13
lines changed
test/unit/specs/mount/Wrapper 6 files changed +25
-13
lines changed Original file line number Diff line number Diff line change 3
3
import type Wrapper from '~src/Wrapper'
4
4
import type WrapperArray from '~src/WrapperArray'
5
5
6
- // declare type FindOptions = {
6
+ // declare type RefSelector = {
7
7
// ref: string,
8
8
// }
9
9
10
- // declare type Selector = string | Component | FindOptions
10
+ // declare type Selector = string | Component | RefSelector
11
11
// Component is not getting imported
12
12
declare type Selector = any
13
13
Original file line number Diff line number Diff line change 1
1
// @flow
2
2
3
- import { isDomSelector , isVueComponent , isFindOption } from './validators.js'
3
+ import { isDomSelector , isVueComponent , isRefSelector } from './validators.js'
4
4
import { throwError } from '../lib/util'
5
5
6
6
export const selectorTypes = {
@@ -18,7 +18,7 @@ function getSelectorType (selector: Selector): string | void {
18
18
return selectorTypes . VUE_COMPONENT
19
19
}
20
20
21
- if ( isFindOption ( selector ) ) {
21
+ if ( isRefSelector ( selector ) ) {
22
22
return selectorTypes . OPTIONS_OBJECT
23
23
}
24
24
}
Original file line number Diff line number Diff line change @@ -47,20 +47,20 @@ export function isValidSelector (selector: any): boolean {
47
47
return true
48
48
}
49
49
50
- return isFindOption ( selector )
50
+ return isRefSelector ( selector )
51
51
}
52
52
53
- export function isFindOption ( findOptions : any ) {
54
- if ( typeof findOptions !== 'object' ) {
53
+ export function isRefSelector ( refOptionsObject : any ) {
54
+ if ( typeof refOptionsObject !== 'object' ) {
55
55
return false
56
56
}
57
57
58
- if ( findOptions === null ) {
58
+ if ( refOptionsObject === null ) {
59
59
return false
60
60
}
61
61
62
62
const validFindKeys = [ 'ref' ]
63
- const entries = Object . entries ( findOptions )
63
+ const entries = Object . entries ( refOptionsObject )
64
64
65
65
if ( ! entries . length ) {
66
66
return false
Original file line number Diff line number Diff line change @@ -45,6 +45,9 @@ export default class Wrapper implements BaseWrapper {
45
45
}
46
46
47
47
if ( selectorType === selectorTypes . OPTIONS_OBJECT ) {
48
+ if ( ! this . isVueComponent ) {
49
+ throwError ( '$ref selectors can only be used on Vue component wrappers' )
50
+ }
48
51
const nodes = findVNodesByRef ( this . vnode , selector . ref )
49
52
return nodes . length > 0
50
53
}
Original file line number Diff line number Diff line change @@ -21,6 +21,15 @@ describe('contains', () => {
21
21
expect ( wrapper . contains ( { ref : 'foo' } ) ) . to . equal ( true )
22
22
} )
23
23
24
+ it ( 'throws an error when ref selector is called on a wrapper that is not a Vue component' , ( ) => {
25
+ const compiled = compileToFunctions ( '<div><a href="/"></a></div>' )
26
+ const wrapper = mount ( compiled )
27
+ const a = wrapper . find ( 'a' )
28
+ const message = '[vue-test-utils]: $ref selectors can only be used on Vue component wrappers'
29
+ const fn = ( ) => a . contains ( { ref : 'foo' } )
30
+ expect ( fn ) . to . throw ( ) . with . property ( 'message' , message )
31
+ } )
32
+
24
33
it ( 'returns false if wrapper does not contain element' , ( ) => {
25
34
const compiled = compileToFunctions ( '<div><input /></div>' )
26
35
const wrapper = mount ( compiled )
Original file line number Diff line number Diff line change @@ -26,9 +26,9 @@ type Stubs = {
26
26
} | string [ ]
27
27
28
28
/**
29
- * Utility type for options object that can be used as a Selector
29
+ * Utility type for ref options object that can be used as a Selector
30
30
*/
31
- type FindOptions = {
31
+ type RefSelector = {
32
32
ref : string
33
33
}
34
34
@@ -67,13 +67,13 @@ interface Wrapper<V extends Vue> extends BaseWrapper {
67
67
find < R extends Vue > ( selector : ComponentOptions < R > ) : Wrapper < R >
68
68
find ( selector : FunctionalComponentOptions ) : Wrapper < Vue >
69
69
find ( selector : string ) : Wrapper < Vue >
70
- find ( selector : FindOptions ) : Wrapper < Vue >
70
+ find ( selector : RefSelector ) : Wrapper < Vue >
71
71
72
72
findAll < R extends Vue , Ctor extends VueClass < R > = VueClass < R > > ( selector : Ctor ) : WrapperArray < R >
73
73
findAll < R extends Vue > ( selector : ComponentOptions < R > ) : WrapperArray < R >
74
74
findAll ( selector : FunctionalComponentOptions ) : WrapperArray < Vue >
75
75
findAll ( selector : string ) : WrapperArray < Vue >
76
- findAll ( selector : FindOptions ) : WrapperArray < Vue >
76
+ findAll ( selector : RefSelector ) : WrapperArray < Vue >
77
77
78
78
html ( ) : string
79
79
text ( ) : string
You can’t perform that action at this time.
0 commit comments