Skip to content

Commit ec927df

Browse files
38elementseddyerburgh
authored andcommitted
fix(exist): update VueWrapper.exists() (#225)
* Fix Wrapper.exists() * Fix exists() * Move exists() * Add test * Use Array.every()
1 parent 217a78d commit ec927df

File tree

5 files changed

+47
-5
lines changed

5 files changed

+47
-5
lines changed

src/wrappers/wrapper-array.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,9 @@ export default class WrapperArray implements BaseWrapper {
3737

3838
return this.wrappers.every(wrapper => wrapper.contains(selector))
3939
}
40+
4041
exists (): boolean {
41-
return this.wrappers.length > 0
42+
return this.length > 0 && this.wrappers.every(wrapper => wrapper.exists())
4243
}
4344

4445
emitted (): void {

src/wrappers/wrapper.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ export default class Wrapper implements BaseWrapper {
119119
* Utility to check wrapper exists. Returns true as Wrapper always exists
120120
*/
121121
exists (): boolean {
122+
if (this.isVueComponent) {
123+
return !!this.vm && !this.vm._isDestroyed
124+
}
122125
return true
123126
}
124127

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,13 @@ describe('exists', () => {
88
expect(wrapper.exists()).to.equal(true)
99
})
1010

11+
it('returns false if Wrapper is destroyed', () => {
12+
const compiled = compileToFunctions('<div />')
13+
const wrapper = mount(compiled)
14+
wrapper.destroy()
15+
expect(wrapper.exists()).to.equal(false)
16+
})
17+
1118
it('returns false if called on an ErrorWrapper', () => {
1219
const compiled = compileToFunctions('<div />')
1320
const wrapper = mount(compiled)

test/unit/specs/mount/options/listeners.spec.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ describe('mount.listeners', () => {
1111
aListener
1212
}
1313
})
14-
console.info(123)
1514

1615
expect(wrapper.vm.$listeners.aListener).to.equal(aListener)
1716
wrapper.update()

test/unit/specs/wrappers/wrapper-array.spec.js

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,16 +96,48 @@ describe('WrapperArray', () => {
9696
expect(wrapperArray.contains()).to.equal(false)
9797
})
9898

99-
it('exists returns true if length is greater then 0', () => {
100-
const wrapperArray = new WrapperArray([{}])
99+
it('exists returns true if it has every existing wrappers', () => {
100+
const wrapper = {
101+
exists () {
102+
return true
103+
}
104+
}
105+
const wrapperArray = new WrapperArray([wrapper])
101106
expect(wrapperArray.exists()).to.equal(true)
102107
})
103108

104-
it('exists returns false if length is 0', () => {
109+
it('exists returns false if it does not have existing wrappers', () => {
105110
const wrapperArray = new WrapperArray([])
106111
expect(wrapperArray.exists()).to.equal(false)
107112
})
108113

114+
it('exists returns false if it has not existing wrappers', () => {
115+
const wrapper1 = {
116+
exists () {
117+
return true
118+
}
119+
}
120+
const wrapper2 = {
121+
exists () {
122+
return false
123+
}
124+
}
125+
const wrapperArray = new WrapperArray([wrapper1, wrapper2])
126+
expect(wrapperArray.exists()).to.equal(false)
127+
})
128+
129+
it('exists returns false if it does not have existing wrappers', () => {
130+
const wrapperArray1 = new WrapperArray([])
131+
expect(wrapperArray1.exists()).to.equal(false)
132+
const wrapper = {
133+
exists () {
134+
return false
135+
}
136+
}
137+
const wrapperArray2 = new WrapperArray([wrapper])
138+
expect(wrapperArray2.exists()).to.equal(false)
139+
})
140+
109141
it('hasAttribute returns true if every wrapper.hasAttribute() returns true', () => {
110142
const attribute = 'attribute'
111143
const value = 'value'

0 commit comments

Comments
 (0)