Skip to content

fix: set wrapper.vm if the element binds Vue instance #724

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 6 commits into from
Jun 18, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion packages/test-utils/src/create-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import VueWrapper from './vue-wrapper'
export default function createWrapper (
node: VNode | Component,
options: WrapperOptions
) {
): VueWrapper | Wrapper {
const componentInstance = node.componentInstance || node.child
if (componentInstance) {
return new VueWrapper(componentInstance, options)
}
return node instanceof Vue
? new VueWrapper(node, options)
: new Wrapper(node, options)
Expand Down
13 changes: 12 additions & 1 deletion packages/test-utils/src/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,11 @@ export default class Wrapper implements BaseWrapper {
typeof selector === 'string' ? selector : 'Component'
)
}
// Using CSS Selector, returns a VueWrapper instance if the root element
// binds a Vue instance.
if (nodes[0].elm === this.element) {
return this
}
return createWrapper(nodes[0], this.options)
}

Expand All @@ -333,7 +338,13 @@ export default class Wrapper implements BaseWrapper {
findAll (selector: Selector): WrapperArray {
getSelectorTypeOrThrow(selector, 'findAll')
const nodes = findAll(this.vm, this.vnode, this.element, selector)
const wrappers = nodes.map(node => createWrapper(node, this.options))
const wrappers = nodes.map(node => {
// Using CSS Selector, returns a VueWrapper instance if the root element
// binds a Vue instance.
return node.elm === this.element
? this
: createWrapper(node, this.options)
})
return new WrapperArray(wrappers)
}

Expand Down
18 changes: 18 additions & 0 deletions test/specs/wrapper/find.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ describeWithShallowAndMount('find', mountingMethod => {
const compiled = compileToFunctions('<div><p></p><p></p></div>')
const wrapper = mountingMethod(compiled)
expect(wrapper.find('p').vnode).to.be.an('object')
expect(wrapper.find('p').vm).to.equal(undefined)
})

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

it('returns Wrapper matching class selector passed if nested in a transition', () => {
Expand Down Expand Up @@ -395,4 +397,20 @@ describeWithShallowAndMount('find', mountingMethod => {
.with.property('message', message)
})
})

itDoNotRunIf(
mountingMethod.name === 'shallowMount',
'returns a VueWrapper instance by CSS selector if the element binds a Vue instance', () => {
const childComponent = {
name: 'bar',
template: '<p/>'
}
const wrapper = mountingMethod({
name: 'foo',
template: '<div><child-component /></div>',
components: { childComponent }
})
expect(wrapper.find('div').vm.$options.name).to.equal('foo')
expect(wrapper.find('p').vm.$options.name).to.equal('bar')
})
})
18 changes: 18 additions & 0 deletions test/specs/wrapper/findAll.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -323,4 +323,22 @@ describeWithShallowAndMount('findAll', mountingMethod => {
.with.property('message', message)
})
})

itDoNotRunIf(
mountingMethod.name === 'shallowMount',
'returns a WrapperArray which includes VueWrapper if the elements binds a Vue instance', () => {
const childComponent = {
name: 'bar',
template: '<p class="foo" />'
}
const wrapper = mountingMethod({
name: 'foo',
template: '<div class="foo"><p class="foo" /><child-component /></div>',
components: { childComponent }
})
const wrappers = wrapper.findAll('.foo')
expect(wrappers.at(0).vm.$options.name).to.equal('foo')
expect(wrappers.at(1).vm).to.equal(undefined)
expect(wrappers.at(2).vm.$options.name).to.equal('bar')
})
})
8 changes: 4 additions & 4 deletions test/specs/wrapper/setChecked.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ describeWithShallowAndMount('setChecked', mountingMethod => {
})

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

const fn = () => div.setChecked()
const fn = () => p.setChecked()
const message =
'[vue-test-utils]: cannot call wrapper.setChecked() on a wrapper without an element'
expect(fn)
Expand Down
8 changes: 4 additions & 4 deletions test/specs/wrapper/setSelected.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ describeWithShallowAndMount('setSelected', mountingMethod => {
})

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

const fn = () => div.setSelected()
const fn = () => p.setSelected()
const message =
'[vue-test-utils]: cannot call wrapper.setSelected() on a wrapper without an element'
expect(fn)
Expand Down
11 changes: 5 additions & 6 deletions test/specs/wrapper/setValue.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,11 @@ describeWithShallowAndMount('setValue', mountingMethod => {
})

it('throws error if wrapper does not contain element', () => {
const wrapper = mountingMethod({ render: h => h('div') })
const div = wrapper.find('div')
div.element = null
const fn = () => div.setValue('')
const message =
'[vue-test-utils]: cannot call wrapper.setValue() on a wrapper without an element'
const wrapper = mountingMethod({ template: '<div><p/></div>' })
const p = wrapper.find('p')
p.element = null
const fn = () => p.setValue('')
const message = '[vue-test-utils]: cannot call wrapper.setValue() on a wrapper without an element'
expect(fn)
.to.throw()
.with.property('message', message)
Expand Down
13 changes: 6 additions & 7 deletions test/specs/wrapper/text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,13 @@ describeWithShallowAndMount('text', mountingMethod => {

expect(wrapper.text()).to.equal(text)
})
152

it('throws error if wrapper does not contain element', () => {
const wrapper = mountingMethod({ render: h => h('div') })
const div = wrapper.find('div')
div.element = null
const fn = () => div.text()
const message =
'[vue-test-utils]: cannot call wrapper.text() on a wrapper without an element'
const wrapper = mountingMethod({ template: '<div><p/></div>' })
const p = wrapper.find('p')
p.element = null
const fn = () => p.text()
const message = '[vue-test-utils]: cannot call wrapper.text() on a wrapper without an element'
expect(fn)
.to.throw()
.with.property('message', message)
Expand Down
8 changes: 4 additions & 4 deletions test/specs/wrapper/trigger.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,10 @@ describeWithShallowAndMount('trigger', mountingMethod => {
})

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