Skip to content
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

Adding get method to Wrapper #1304

Merged
merged 7 commits into from
Jan 16, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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')
})
})