Skip to content

Commit b14afae

Browse files
38elementseddyerburgh
authored andcommitted
feat: set wrapper.vm if the element binds Vue instance (#724)
1 parent 97b505d commit b14afae

File tree

9 files changed

+76
-27
lines changed

9 files changed

+76
-27
lines changed

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import VueWrapper from './vue-wrapper'
77
export default function createWrapper (
88
node: VNode | Component,
99
options: WrapperOptions
10-
) {
10+
): VueWrapper | Wrapper {
11+
const componentInstance = node.componentInstance || node.child
12+
if (componentInstance) {
13+
return new VueWrapper(componentInstance, options)
14+
}
1115
return node instanceof Vue
1216
? new VueWrapper(node, options)
1317
: new Wrapper(node, options)

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

+12-1
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,11 @@ export default class Wrapper implements BaseWrapper {
323323
typeof selector === 'string' ? selector : 'Component'
324324
)
325325
}
326+
// Using CSS Selector, returns a VueWrapper instance if the root element
327+
// binds a Vue instance.
328+
if (nodes[0].elm === this.element) {
329+
return this
330+
}
326331
return createWrapper(nodes[0], this.options)
327332
}
328333

@@ -333,7 +338,13 @@ export default class Wrapper implements BaseWrapper {
333338
findAll (selector: Selector): WrapperArray {
334339
getSelectorTypeOrThrow(selector, 'findAll')
335340
const nodes = findAll(this.vm, this.vnode, this.element, selector)
336-
const wrappers = nodes.map(node => createWrapper(node, this.options))
341+
const wrappers = nodes.map(node => {
342+
// Using CSS Selector, returns a VueWrapper instance if the root element
343+
// binds a Vue instance.
344+
return node.elm === this.element
345+
? this
346+
: createWrapper(node, this.options)
347+
})
337348
return new WrapperArray(wrappers)
338349
}
339350

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

+18
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ describeWithShallowAndMount('find', mountingMethod => {
2020
const compiled = compileToFunctions('<div><p></p><p></p></div>')
2121
const wrapper = mountingMethod(compiled)
2222
expect(wrapper.find('p').vnode).to.be.an('object')
23+
expect(wrapper.find('p').vm).to.equal(undefined)
2324
})
2425

2526
it('returns Wrapper matching class selector passed', () => {
2627
const compiled = compileToFunctions('<div><div class="foo" /></div>')
2728
const wrapper = mountingMethod(compiled)
2829
expect(wrapper.find('.foo').vnode).to.be.an('object')
30+
expect(wrapper.find('.foo').vm).to.equal(undefined)
2931
})
3032

3133
it('returns Wrapper matching class selector passed if nested in a transition', () => {
@@ -395,4 +397,20 @@ describeWithShallowAndMount('find', mountingMethod => {
395397
.with.property('message', message)
396398
})
397399
})
400+
401+
itDoNotRunIf(
402+
mountingMethod.name === 'shallowMount',
403+
'returns a VueWrapper instance by CSS selector if the element binds a Vue instance', () => {
404+
const childComponent = {
405+
name: 'bar',
406+
template: '<p/>'
407+
}
408+
const wrapper = mountingMethod({
409+
name: 'foo',
410+
template: '<div><child-component /></div>',
411+
components: { childComponent }
412+
})
413+
expect(wrapper.find('div').vm.$options.name).to.equal('foo')
414+
expect(wrapper.find('p').vm.$options.name).to.equal('bar')
415+
})
398416
})

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

+18
Original file line numberDiff line numberDiff line change
@@ -323,4 +323,22 @@ describeWithShallowAndMount('findAll', mountingMethod => {
323323
.with.property('message', message)
324324
})
325325
})
326+
327+
itDoNotRunIf(
328+
mountingMethod.name === 'shallowMount',
329+
'returns a WrapperArray which includes VueWrapper if the elements binds a Vue instance', () => {
330+
const childComponent = {
331+
name: 'bar',
332+
template: '<p class="foo" />'
333+
}
334+
const wrapper = mountingMethod({
335+
name: 'foo',
336+
template: '<div class="foo"><p class="foo" /><child-component /></div>',
337+
components: { childComponent }
338+
})
339+
const wrappers = wrapper.findAll('.foo')
340+
expect(wrappers.at(0).vm.$options.name).to.equal('foo')
341+
expect(wrappers.at(1).vm).to.equal(undefined)
342+
expect(wrappers.at(2).vm.$options.name).to.equal('bar')
343+
})
326344
})

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ describeWithShallowAndMount('setChecked', mountingMethod => {
8484
})
8585

8686
it('throws error if wrapper does not contain element', () => {
87-
const wrapper = mountingMethod({ render: h => h('div') })
88-
const div = wrapper.find('div')
89-
div.element = null
87+
const wrapper = mountingMethod({ template: '<div><p/></div>' })
88+
const p = wrapper.find('p')
89+
p.element = null
9090

91-
const fn = () => div.setChecked()
91+
const fn = () => p.setChecked()
9292
const message =
9393
'[vue-test-utils]: cannot call wrapper.setChecked() on a wrapper without an element'
9494
expect(fn)

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ describeWithShallowAndMount('setSelected', mountingMethod => {
3434
})
3535

3636
it('throws error if wrapper does not contain element', () => {
37-
const wrapper = mountingMethod({ render: h => h('div') })
38-
const div = wrapper.find('div')
39-
div.element = null
37+
const wrapper = mountingMethod({ template: '<div><p/></div>' })
38+
const p = wrapper.find('p')
39+
p.element = null
4040

41-
const fn = () => div.setSelected()
41+
const fn = () => p.setSelected()
4242
const message =
4343
'[vue-test-utils]: cannot call wrapper.setSelected() on a wrapper without an element'
4444
expect(fn)

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

+5-6
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,11 @@ describeWithShallowAndMount('setValue', mountingMethod => {
2020
})
2121

2222
it('throws error if wrapper does not contain element', () => {
23-
const wrapper = mountingMethod({ render: h => h('div') })
24-
const div = wrapper.find('div')
25-
div.element = null
26-
const fn = () => div.setValue('')
27-
const message =
28-
'[vue-test-utils]: cannot call wrapper.setValue() on a wrapper without an element'
23+
const wrapper = mountingMethod({ template: '<div><p/></div>' })
24+
const p = wrapper.find('p')
25+
p.element = null
26+
const fn = () => p.setValue('')
27+
const message = '[vue-test-utils]: cannot call wrapper.setValue() on a wrapper without an element'
2928
expect(fn)
3029
.to.throw()
3130
.with.property('message', message)

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

+6-7
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,13 @@ describeWithShallowAndMount('text', mountingMethod => {
2020

2121
expect(wrapper.text()).to.equal(text)
2222
})
23-
152
23+
2424
it('throws error if wrapper does not contain element', () => {
25-
const wrapper = mountingMethod({ render: h => h('div') })
26-
const div = wrapper.find('div')
27-
div.element = null
28-
const fn = () => div.text()
29-
const message =
30-
'[vue-test-utils]: cannot call wrapper.text() on a wrapper without an element'
25+
const wrapper = mountingMethod({ template: '<div><p/></div>' })
26+
const p = wrapper.find('p')
27+
p.element = null
28+
const fn = () => p.text()
29+
const message = '[vue-test-utils]: cannot call wrapper.text() on a wrapper without an element'
3130
expect(fn)
3231
.to.throw()
3332
.with.property('message', message)

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,10 @@ describeWithShallowAndMount('trigger', mountingMethod => {
158158
})
159159

160160
it('throws error if wrapper does not contain element', () => {
161-
const wrapper = mountingMethod({ render: h => h('div') })
162-
const div = wrapper.find('div')
163-
div.element = null
164-
const fn = () => div.trigger('click')
161+
const wrapper = mountingMethod({ template: '<div><p/></div>' })
162+
const p = wrapper.find('p')
163+
p.element = null
164+
const fn = () => p.trigger('click')
165165
const message =
166166
'[vue-test-utils]: cannot call wrapper.trigger() on a wrapper without an element'
167167
expect(fn)

0 commit comments

Comments
 (0)