Skip to content

Commit dd7542d

Browse files
authored
Merge pull request #1304 from tomasbjerre/feature/issue-1298-get-method-on-wrapper
Adding get method to Wrapper
2 parents 4378963 + 5ee369c commit dd7542d

File tree

8 files changed

+73
-0
lines changed

8 files changed

+73
-0
lines changed

Diff for: docs/README.md

+1
Original file line numberDiff line numberDiff 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)

Diff for: docs/api/wrapper/README.md

+1
Original file line numberDiff line numberDiff 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)!!!

Diff for: docs/api/wrapper/find.md

+2
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,5 @@ expect(barByName.is(Bar)).toBe(true)
3131
const fooRef = wrapper.find({ ref: 'foo' })
3232
expect(fooRef.is(Foo)).toBe(true)
3333
```
34+
35+
See also: [get](../get.md).

Diff for: docs/api/wrapper/get.md

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
```

Diff for: packages/test-utils/src/wrapper.js

+12
Original file line numberDiff line numberDiff 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.

Diff for: packages/test-utils/types/index.d.ts

+7
Original file line numberDiff line numberDiff 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>

Diff for: packages/test-utils/types/test/wrapper.ts

+7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,13 @@ selector = array.selector
7272
array = wrapper.findAll({ name: 'my-button' })
7373
selector = 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+
7582
wrapper.setChecked()
7683
wrapper.setChecked(true)
7784
wrapper.setValue('some string')

Diff for: test/specs/wrapper/get.spec.js

+20
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)