Skip to content

Adding get method to Wrapper #1304

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Vue Test Utils is the official unit testing utility library for Vue.js.
- [destroy](api/wrapper/destroy.md)
- [find](api/wrapper/find.md)
- [findAll](api/wrapper/findAll.md)
- [get](api/wrapper/get.md)
- [html](api/wrapper/html.md)
- [is](api/wrapper/is.md)
- [isEmpty](api/wrapper/isEmpty.md)
Expand Down
1 change: 1 addition & 0 deletions docs/api/wrapper/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ A `Wrapper` is an object that contains a mounted component or vnode and methods
!!!include(docs/api/wrapper/find.md)!!!
!!!include(docs/api/wrapper/findAll.md)!!!
!!!include(docs/api/wrapper/html.md)!!!
!!!include(docs/api/wrapper/get.md)!!!
!!!include(docs/api/wrapper/is.md)!!!
!!!include(docs/api/wrapper/isEmpty.md)!!!
!!!include(docs/api/wrapper/isVisible.md)!!!
Expand Down
19 changes: 19 additions & 0 deletions docs/api/wrapper/get.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
## get

Works just like [find](../find.md) but will throw an error if nothing matching
the given selector is found. You should use `find` when searching for an element
that may not exist. You should use this method when getting an element that should
exist and it will provide a nice error message if that is not the case.

```js
import { mount } from '@vue/test-utils'

const wrapper = mount(Foo)

expect(() => wrapper.get('.does-not-exist'))
.to.throw()
.with.property(
'message',
'Unable to find .does-not-exist within: <div>the actual DOM here...</div>'
)
```
12 changes: 12 additions & 0 deletions packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,18 @@ export default class Wrapper implements BaseWrapper {
throwError('filter() must be called on a WrapperArray')
}

/**
* Gets first node in tree of the current wrapper that
* matches the provided selector.
*/
get(rawSelector: Selector): Wrapper {
const found = this.find(rawSelector)
if (found instanceof ErrorWrapper) {
throw new Error(`Unable to find ${rawSelector} within: ${this.html()}`)
}
return found
}

/**
* Finds first node in tree of the current wrapper that
* matches the provided selector.
Expand Down
7 changes: 7 additions & 0 deletions packages/test-utils/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ export interface Wrapper<V extends Vue | null> extends BaseWrapper {
readonly element: HTMLElement
readonly options: WrapperOptions

get<R extends Vue> (selector: VueClass<R>): Wrapper<R>
get<R extends Vue> (selector: ComponentOptions<R>): Wrapper<R>
get (selector: FunctionalComponentOptions): Wrapper<Vue>
get (selector: string): Wrapper<Vue>
get (selector: RefSelector): Wrapper<Vue>
get (selector: NameSelector): Wrapper<Vue>

find<R extends Vue> (selector: VueClass<R>): Wrapper<R>
find<R extends Vue> (selector: ComponentOptions<R>): Wrapper<R>
find (selector: FunctionalComponentOptions): Wrapper<Vue>
Expand Down
7 changes: 7 additions & 0 deletions packages/test-utils/types/test/wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,13 @@ array = wrapper.findAll(ClassComponent)
array = wrapper.findAll({ ref: 'myButton' })
array = wrapper.findAll({ name: 'my-button' })

let gotten = wrapper.get('.foo')
gotten = wrapper.get(normalOptions)
gotten = wrapper.get(functionalOptions)
gotten = wrapper.get(ClassComponent)
gotten = wrapper.get({ ref: 'myButton' })
gotten = wrapper.get({ name: 'my-button' })

wrapper.setChecked()
wrapper.setChecked(true)
wrapper.setValue('some string')
Expand Down
20 changes: 20 additions & 0 deletions test/specs/wrapper/get.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { compileToFunctions } from 'vue-template-compiler'
import { describeWithShallowAndMount } from '~resources/utils'

describeWithShallowAndMount('get', mountingMethod => {
it('throws describing error when element not found', () => {
const compiled = compileToFunctions('<div/>')
const wrapper = mountingMethod(compiled)
expect(() => wrapper.get('.does-not-exist'))
.to.throw()
.with.property(
'message',
'Unable to find .does-not-exist within: <div></div>'
)
})
it('gets the element when element is found', () => {
const compiled = compileToFunctions('<div class="does-exist"><div>')
const wrapper = mountingMethod(compiled)
expect(wrapper.get('.does-exist')).to.be.an('object')
})
})