Skip to content

Commit 256086b

Browse files
aiankileeddyerburgh
authored andcommitted
fix: find ref childComponent (#268)
#267
1 parent 1897300 commit 256086b

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

src/wrappers/wrapper.js

+6
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,9 @@ export default class Wrapper implements BaseWrapper {
244244
if (!this.isVueComponent) {
245245
throwError('$ref selectors can only be used on Vue component wrappers')
246246
}
247+
if (this.vm && this.vm.$refs && selector.ref in this.vm.$refs && this.vm.$refs[selector.ref] instanceof Vue) {
248+
return new VueWrapper(this.vm.$refs[selector.ref], this.options)
249+
}
247250
const nodes = findVNodesByRef(this.vnode, selector.ref)
248251
if (nodes.length === 0) {
249252
return new ErrorWrapper(`ref="${selector.ref}"`)
@@ -278,6 +281,9 @@ export default class Wrapper implements BaseWrapper {
278281
if (!this.isVueComponent) {
279282
throwError('$ref selectors can only be used on Vue component wrappers')
280283
}
284+
if (this.vm && this.vm.$refs && selector.ref in this.vm.$refs && this.vm.$refs[selector.ref] instanceof Vue) {
285+
return new WrapperArray([new VueWrapper(this.vm.$refs[selector.ref], this.options)])
286+
}
281287
const nodes = findVNodesByRef(this.vnode, selector.ref)
282288
return new WrapperArray(nodes.map(node => new Wrapper(node, this.update, this.options)))
283289
}

test/unit/specs/mount/Wrapper/find.spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import ComponentWithoutName from '~resources/components/component-without-name.v
66
import ComponentWithSlots from '~resources/components/component-with-slots.vue'
77
import ComponentWithVFor from '~resources/components/component-with-v-for.vue'
88
import Component from '~resources/components/component.vue'
9+
import VueWrapper from '~src/wrappers/vue-wrapper'
910
import Wrapper from '~src/wrappers/wrapper'
1011
import ErrorWrapper from '~src/wrappers/error-wrapper'
1112

@@ -172,7 +173,7 @@ describe('find', () => {
172173

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

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

0 commit comments

Comments
 (0)