Skip to content

Commit f2b5d52

Browse files
Djalerxanf
andauthored
fix: check if component unmounted in Wrapper.exists() (#1629)
* fix: check if component unmounted in Wrapper.exists() * chore(exists): move exists check to VueWrapper Co-authored-by: Illya Klymov <[email protected]>
1 parent d594aca commit f2b5d52

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

src/vueWrapper.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,10 @@ export class VueWrapper<
107107
return this.vm.$
108108
}
109109

110+
exists() {
111+
return !this.getCurrentComponent().isUnmounted
112+
}
113+
110114
findAll<K extends keyof HTMLElementTagNameMap>(
111115
selector: K
112116
): DOMWrapper<HTMLElementTagNameMap[K]>[]

tests/exists.spec.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,31 @@ describe('exists', () => {
2222
const wrapper = mount(Component)
2323
expect(wrapper.find('#msg').exists()).toBe(true)
2424
})
25+
26+
it('returns false when component destroyed', async () => {
27+
const ChildComponent = defineComponent({
28+
render() {
29+
return h('div')
30+
}
31+
})
32+
const Component = defineComponent({
33+
props: {
34+
hide: {
35+
type: Boolean,
36+
default: false
37+
}
38+
},
39+
render() {
40+
if (this.hide) {
41+
return h('div')
42+
} else {
43+
return h(ChildComponent)
44+
}
45+
}
46+
})
47+
const wrapper = mount(Component)
48+
const child = wrapper.findComponent(ChildComponent)
49+
await wrapper.setProps({ hide: true })
50+
expect(child.exists()).toBe(false)
51+
})
2552
})

0 commit comments

Comments
 (0)