Skip to content

Commit

Permalink
fix: check if component unmounted in Wrapper.exists() (#1629)
Browse files Browse the repository at this point in the history
* fix: check if component unmounted in Wrapper.exists()

* chore(exists): move exists check to VueWrapper

Co-authored-by: Illya Klymov <xanf@xanf.me>
  • Loading branch information
Djaler and xanf committed Jun 29, 2022
1 parent d594aca commit f2b5d52
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/vueWrapper.ts
Expand Up @@ -107,6 +107,10 @@ export class VueWrapper<
return this.vm.$
}

exists() {
return !this.getCurrentComponent().isUnmounted
}

findAll<K extends keyof HTMLElementTagNameMap>(
selector: K
): DOMWrapper<HTMLElementTagNameMap[K]>[]
Expand Down
27 changes: 27 additions & 0 deletions tests/exists.spec.ts
Expand Up @@ -22,4 +22,31 @@ describe('exists', () => {
const wrapper = mount(Component)
expect(wrapper.find('#msg').exists()).toBe(true)
})

it('returns false when component destroyed', async () => {
const ChildComponent = defineComponent({
render() {
return h('div')
}
})
const Component = defineComponent({
props: {
hide: {
type: Boolean,
default: false
}
},
render() {
if (this.hide) {
return h('div')
} else {
return h(ChildComponent)
}
}
})
const wrapper = mount(Component)
const child = wrapper.findComponent(ChildComponent)
await wrapper.setProps({ hide: true })
expect(child.exists()).toBe(false)
})
})

0 comments on commit f2b5d52

Please sign in to comment.