Skip to content

Commit

Permalink
feat(contains): refactor contains to include root element
Browse files Browse the repository at this point in the history
breaking change: this changes the contains behavior to include the root element.
closes #24
  • Loading branch information
eddyerburgh committed Dec 17, 2017
1 parent c8d082a commit 4543c4f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/wrappers/wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ export default class Wrapper implements BaseWrapper {

if (selectorType === selectorTypes.VUE_COMPONENT) {
const vm = this.vm || this.vnode.context.$root
return findVueComponents(vm, selector.name).length > 0
return findVueComponents(vm, selector.name).length > 0 || this.is(selector)
}

if (selectorType === selectorTypes.OPTIONS_OBJECT) {
Expand All @@ -87,7 +87,7 @@ export default class Wrapper implements BaseWrapper {
}

if (selectorType === selectorTypes.DOM_SELECTOR && this.element instanceof HTMLElement) {
return this.element.querySelectorAll(selector).length > 0
return this.element.querySelectorAll(selector).length > 0 || this.is(selector)
}

return false
Expand Down
19 changes: 18 additions & 1 deletion test/unit/specs/mount/Wrapper/contains.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,29 @@ describe('contains', () => {
expect(fn).to.throw().with.property('message', message)
})

it('returns false if wrapper does not contain element', () => {
it('returns true when wrapper contains root element', () => {
const compiled = compileToFunctions('<div><input /></div>')
const wrapper = mount(compiled)
expect(wrapper.contains('doesntexist')).to.equal(false)
})

it('returns true if wrapper root element matches contains', () => {
const compiled = compileToFunctions('<div><input /></div>')
const wrapper = mount(compiled)
expect(wrapper.contains('doesntexist')).to.equal(false)
})

it('returns true if wrapper root Component matches selector', () => {
const wrapper = mount(Component)
expect(wrapper.contains(Component)).to.equal(true)
})

it('returns false if wrapper does not contain element', () => {
const compiled = compileToFunctions('<div></div>')
const wrapper = mount(compiled)
expect(wrapper.contains('div')).to.equal(true)
})

it('returns false if wrapper does not contain element specified by ref selector', () => {
const compiled = compileToFunctions('<div><input ref="bar" /></div>')
const wrapper = mount(compiled)
Expand Down

0 comments on commit 4543c4f

Please sign in to comment.