Skip to content

Commit ea39a3f

Browse files
committed
Adding get method to Wrapper #1298
To have it fail with a clear error message when selector does not match an existing element.
1 parent ad602fc commit ea39a3f

File tree

4 files changed

+46
-0
lines changed

4 files changed

+46
-0
lines changed

packages/test-utils/src/wrapper.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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.

packages/test-utils/types/index.d.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff 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>

packages/test-utils/types/test/wrapper.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ array = wrapper.findAll(ClassComponent)
5858
array = wrapper.findAll({ ref: 'myButton' })
5959
array = 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+
6168
wrapper.setChecked()
6269
wrapper.setChecked(true)
6370
wrapper.setValue('some string')

test/specs/wrapper/get.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
})

0 commit comments

Comments
 (0)