File tree Expand file tree Collapse file tree 4 files changed +46
-0
lines changed Expand file tree Collapse file tree 4 files changed +46
-0
lines changed Original file line number Diff line number Diff line change @@ -184,6 +184,18 @@ export default class Wrapper implements BaseWrapper {
184184 throwError ( 'filter() must be called on a WrapperArray' )
185185 }
186186
187+ /**
188+ * Gets first node in tree of the current wrapper that
189+ * matches the provided selector.
190+ */
191+ get ( rawSelector : Selector ) : Wrapper {
192+ const found = this . find ( rawSelector )
193+ if ( found instanceof ErrorWrapper ) {
194+ throw new Error ( `Unable to find ${ rawSelector } within: ${ this . html ( ) } ` )
195+ }
196+ return found
197+ }
198+
187199 /**
188200 * Finds first node in tree of the current wrapper that
189201 * matches the provided selector.
Original file line number Diff line number Diff line change @@ -76,6 +76,13 @@ export interface Wrapper<V extends Vue | null> extends BaseWrapper {
7676 readonly element : HTMLElement
7777 readonly options : WrapperOptions
7878
79+ get < R extends Vue > ( selector : VueClass < R > ) : Wrapper < R >
80+ get < R extends Vue > ( selector : ComponentOptions < R > ) : Wrapper < R >
81+ get ( selector : FunctionalComponentOptions ) : Wrapper < Vue >
82+ get ( selector : string ) : Wrapper < Vue >
83+ get ( selector : RefSelector ) : Wrapper < Vue >
84+ get ( selector : NameSelector ) : Wrapper < Vue >
85+
7986 find < R extends Vue > ( selector : VueClass < R > ) : Wrapper < R >
8087 find < R extends Vue > ( selector : ComponentOptions < R > ) : Wrapper < R >
8188 find ( selector : FunctionalComponentOptions ) : Wrapper < Vue >
Original file line number Diff line number Diff line change @@ -58,6 +58,13 @@ array = wrapper.findAll(ClassComponent)
5858array = wrapper . findAll ( { ref : 'myButton' } )
5959array = wrapper . findAll ( { name : 'my-button' } )
6060
61+ let gotten = wrapper . get ( '.foo' )
62+ gotten = wrapper . get ( normalOptions )
63+ gotten = wrapper . get ( functionalOptions )
64+ gotten = wrapper . get ( ClassComponent )
65+ gotten = wrapper . get ( { ref : 'myButton' } )
66+ gotten = wrapper . get ( { name : 'my-button' } )
67+
6168wrapper . setChecked ( )
6269wrapper . setChecked ( true )
6370wrapper . 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