File tree 8 files changed +73
-0
lines changed
8 files changed +73
-0
lines changed Original file line number Diff line number Diff line change @@ -45,6 +45,7 @@ Vue Test Utils is the official unit testing utility library for Vue.js.
45
45
- [ destroy] ( api/wrapper/destroy.md )
46
46
- [ find] ( api/wrapper/find.md )
47
47
- [ findAll] ( api/wrapper/findAll.md )
48
+ - [ get] ( api/wrapper/get.md )
48
49
- [ html] ( api/wrapper/html.md )
49
50
- [ is] ( api/wrapper/is.md )
50
51
- [ isEmpty] ( api/wrapper/isEmpty.md )
Original file line number Diff line number Diff line change @@ -36,6 +36,7 @@ A `Wrapper` is an object that contains a mounted component or vnode and methods
36
36
!!!include(docs/api/wrapper/find.md)!!!
37
37
!!!include(docs/api/wrapper/findAll.md)!!!
38
38
!!!include(docs/api/wrapper/html.md)!!!
39
+ !!!include(docs/api/wrapper/get.md)!!!
39
40
!!!include(docs/api/wrapper/is.md)!!!
40
41
!!!include(docs/api/wrapper/isEmpty.md)!!!
41
42
!!!include(docs/api/wrapper/isVisible.md)!!!
Original file line number Diff line number Diff line change @@ -31,3 +31,5 @@ expect(barByName.is(Bar)).toBe(true)
31
31
const fooRef = wrapper .find ({ ref: ' foo' })
32
32
expect (fooRef .is (Foo)).toBe (true )
33
33
```
34
+
35
+ See also: [ get] ( ../get.md ) .
Original file line number Diff line number Diff line change
1
+ ## get
2
+
3
+ Works just like [ find] ( ../find.md ) but will throw an error if nothing matching
4
+ the given selector is found. You should use ` find ` when searching for an element
5
+ that may not exist. You should use this method when getting an element that should
6
+ exist and it will provide a nice error message if that is not the case.
7
+
8
+ ``` js
9
+ import { mount } from ' @vue/test-utils'
10
+
11
+ const wrapper = mount (Foo)
12
+
13
+ // similar to `wrapper.find`.
14
+ // `get` will throw an error if an element is not found. `find` will do nothing.
15
+ expect (wrapper .get (' .does-exist' ))
16
+
17
+ expect (() => wrapper .get (' .does-not-exist' ))
18
+ .to .throw ()
19
+ .with .property (
20
+ ' message' ,
21
+ ' Unable to find .does-not-exist within: <div>the actual DOM here...</div>'
22
+ )
23
+ ```
Original file line number Diff line number Diff line change @@ -185,6 +185,18 @@ export default class Wrapper implements BaseWrapper {
185
185
throwError ( 'filter() must be called on a WrapperArray' )
186
186
}
187
187
188
+ /**
189
+ * Gets first node in tree of the current wrapper that
190
+ * matches the provided selector.
191
+ */
192
+ get ( rawSelector : Selector ) : Wrapper {
193
+ const found = this . find ( rawSelector )
194
+ if ( found instanceof ErrorWrapper ) {
195
+ throw new Error ( `Unable to find ${ rawSelector } within: ${ this . html ( ) } ` )
196
+ }
197
+ return found
198
+ }
199
+
188
200
/**
189
201
* Finds first node in tree of the current wrapper that
190
202
* matches the provided selector.
Original file line number Diff line number Diff line change @@ -77,6 +77,13 @@ export interface Wrapper<V extends Vue | null> extends BaseWrapper {
77
77
readonly element : HTMLElement
78
78
readonly options : WrapperOptions
79
79
80
+ get < R extends Vue > ( selector : VueClass < R > ) : Wrapper < R >
81
+ get < R extends Vue > ( selector : ComponentOptions < R > ) : Wrapper < R >
82
+ get ( selector : FunctionalComponentOptions ) : Wrapper < Vue >
83
+ get ( selector : string ) : Wrapper < Vue >
84
+ get ( selector : RefSelector ) : Wrapper < Vue >
85
+ get ( selector : NameSelector ) : Wrapper < Vue >
86
+
80
87
find < R extends Vue > ( selector : VueClass < R > ) : Wrapper < R >
81
88
find < R extends Vue > ( selector : ComponentOptions < R > ) : Wrapper < R >
82
89
find ( selector : FunctionalComponentOptions ) : Wrapper < Vue >
Original file line number Diff line number Diff line change @@ -72,6 +72,13 @@ selector = array.selector
72
72
array = wrapper . findAll ( { name : 'my-button' } )
73
73
selector = array . selector
74
74
75
+ let gotten = wrapper . get ( '.foo' )
76
+ gotten = wrapper . get ( normalOptions )
77
+ gotten = wrapper . get ( functionalOptions )
78
+ gotten = wrapper . get ( ClassComponent )
79
+ gotten = wrapper . get ( { ref : 'myButton' } )
80
+ gotten = wrapper . get ( { name : 'my-button' } )
81
+
75
82
wrapper . setChecked ( )
76
83
wrapper . setChecked ( true )
77
84
wrapper . setValue ( 'some string' )
Original file line number Diff line number Diff line change
1
+ import { compileToFunctions } from 'vue-template-compiler'
2
+ import { describeWithShallowAndMount } from '~resources/utils'
3
+
4
+ describeWithShallowAndMount ( 'get' , mountingMethod => {
5
+ it ( 'throws describing error when element not found' , ( ) => {
6
+ const compiled = compileToFunctions ( '<div/>' )
7
+ const wrapper = mountingMethod ( compiled )
8
+ expect ( ( ) => wrapper . get ( '.does-not-exist' ) )
9
+ . to . throw ( )
10
+ . with . property (
11
+ 'message' ,
12
+ 'Unable to find .does-not-exist within: <div></div>'
13
+ )
14
+ } )
15
+ it ( 'gets the element when element is found' , ( ) => {
16
+ const compiled = compileToFunctions ( '<div class="does-exist"><div>' )
17
+ const wrapper = mountingMethod ( compiled )
18
+ expect ( wrapper . get ( '.does-exist' ) ) . to . be . an ( 'object' )
19
+ } )
20
+ } )
You can’t perform that action at this time.
0 commit comments