Skip to content

Commit 4543c4f

Browse files
committed
feat(contains): refactor contains to include root element
breaking change: this changes the contains behavior to include the root element. closes #24
1 parent c8d082a commit 4543c4f

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

Diff for: src/wrappers/wrapper.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export default class Wrapper implements BaseWrapper {
7575

7676
if (selectorType === selectorTypes.VUE_COMPONENT) {
7777
const vm = this.vm || this.vnode.context.$root
78-
return findVueComponents(vm, selector.name).length > 0
78+
return findVueComponents(vm, selector.name).length > 0 || this.is(selector)
7979
}
8080

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

8989
if (selectorType === selectorTypes.DOM_SELECTOR && this.element instanceof HTMLElement) {
90-
return this.element.querySelectorAll(selector).length > 0
90+
return this.element.querySelectorAll(selector).length > 0 || this.is(selector)
9191
}
9292

9393
return false

Diff for: test/unit/specs/mount/Wrapper/contains.spec.js

+18-1
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,29 @@ describe('contains', () => {
3030
expect(fn).to.throw().with.property('message', message)
3131
})
3232

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

39+
it('returns true if wrapper root element matches contains', () => {
40+
const compiled = compileToFunctions('<div><input /></div>')
41+
const wrapper = mount(compiled)
42+
expect(wrapper.contains('doesntexist')).to.equal(false)
43+
})
44+
45+
it('returns true if wrapper root Component matches selector', () => {
46+
const wrapper = mount(Component)
47+
expect(wrapper.contains(Component)).to.equal(true)
48+
})
49+
50+
it('returns false if wrapper does not contain element', () => {
51+
const compiled = compileToFunctions('<div></div>')
52+
const wrapper = mount(compiled)
53+
expect(wrapper.contains('div')).to.equal(true)
54+
})
55+
3956
it('returns false if wrapper does not contain element specified by ref selector', () => {
4057
const compiled = compileToFunctions('<div><input ref="bar" /></div>')
4158
const wrapper = mount(compiled)

0 commit comments

Comments
 (0)