Skip to content

Commit

Permalink
fix: find ref childComponent (#268)
Browse files Browse the repository at this point in the history
  • Loading branch information
aiankile authored and eddyerburgh committed Dec 15, 2017
1 parent 1897300 commit 256086b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
6 changes: 6 additions & 0 deletions src/wrappers/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ export default class Wrapper implements BaseWrapper {
if (!this.isVueComponent) {
throwError('$ref selectors can only be used on Vue component wrappers')
}
if (this.vm && this.vm.$refs && selector.ref in this.vm.$refs && this.vm.$refs[selector.ref] instanceof Vue) {
return new VueWrapper(this.vm.$refs[selector.ref], this.options)
}
const nodes = findVNodesByRef(this.vnode, selector.ref)
if (nodes.length === 0) {
return new ErrorWrapper(`ref="${selector.ref}"`)
Expand Down Expand Up @@ -278,6 +281,9 @@ export default class Wrapper implements BaseWrapper {
if (!this.isVueComponent) {
throwError('$ref selectors can only be used on Vue component wrappers')
}
if (this.vm && this.vm.$refs && selector.ref in this.vm.$refs && this.vm.$refs[selector.ref] instanceof Vue) {
return new WrapperArray([new VueWrapper(this.vm.$refs[selector.ref], this.options)])
}
const nodes = findVNodesByRef(this.vnode, selector.ref)
return new WrapperArray(nodes.map(node => new Wrapper(node, this.update, this.options)))
}
Expand Down
3 changes: 2 additions & 1 deletion test/unit/specs/mount/Wrapper/find.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import ComponentWithoutName from '~resources/components/component-without-name.v
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
import ComponentWithVFor from '~resources/components/component-with-v-for.vue'
import Component from '~resources/components/component.vue'
import VueWrapper from '~src/wrappers/vue-wrapper'
import Wrapper from '~src/wrappers/wrapper'
import ErrorWrapper from '~src/wrappers/error-wrapper'

Expand Down Expand Up @@ -172,7 +173,7 @@ describe('find', () => {

it('returns Wrapper of Vue Components matching the ref in options object', () => {
const wrapper = mount(ComponentWithChild)
expect(wrapper.find({ ref: 'child' })).to.be.instanceOf(Wrapper)
expect(wrapper.find({ ref: 'child' })).to.be.instanceOf(VueWrapper)
})

it('throws an error when ref selector is called on a wrapper that is not a Vue component', () => {
Expand Down

0 comments on commit 256086b

Please sign in to comment.