File tree Expand file tree Collapse file tree 8 files changed +73
-0
lines changed Expand file tree Collapse file tree 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.
4545 - [ destroy] ( api/wrapper/destroy.md )
4646 - [ find] ( api/wrapper/find.md )
4747 - [ findAll] ( api/wrapper/findAll.md )
48+ - [ get] ( api/wrapper/get.md )
4849 - [ html] ( api/wrapper/html.md )
4950 - [ is] ( api/wrapper/is.md )
5051 - [ 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
3636!!!include(docs/api/wrapper/find.md)!!!
3737!!!include(docs/api/wrapper/findAll.md)!!!
3838!!!include(docs/api/wrapper/html.md)!!!
39+ !!!include(docs/api/wrapper/get.md)!!!
3940!!!include(docs/api/wrapper/is.md)!!!
4041!!!include(docs/api/wrapper/isEmpty.md)!!!
4142!!!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)
3131const fooRef = wrapper .find ({ ref: ' foo' })
3232expect (fooRef .is (Foo)).toBe (true )
3333```
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 {
185185 throwError ( 'filter() must be called on a WrapperArray' )
186186 }
187187
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+
188200 /**
189201 * Finds first node in tree of the current wrapper that
190202 * 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 {
7777 readonly element : HTMLElement
7878 readonly options : WrapperOptions
7979
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+
8087 find < R extends Vue > ( selector : VueClass < R > ) : Wrapper < R >
8188 find < R extends Vue > ( selector : ComponentOptions < R > ) : Wrapper < R >
8289 find ( selector : FunctionalComponentOptions ) : Wrapper < Vue >
Original file line number Diff line number Diff line change @@ -72,6 +72,13 @@ selector = array.selector
7272array = wrapper . findAll ( { name : 'my-button' } )
7373selector = array . selector
7474
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+
7582wrapper . setChecked ( )
7683wrapper . setChecked ( true )
7784wrapper . 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