From f2b5d52d65ce1c6d36a223f4c5a53b9dc3749238 Mon Sep 17 00:00:00 2001 From: Kirill Romanov Date: Wed, 29 Jun 2022 08:18:21 +0300 Subject: [PATCH] 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 --- src/vueWrapper.ts | 4 ++++ tests/exists.spec.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/src/vueWrapper.ts b/src/vueWrapper.ts index 46090ea05..6cc96473c 100644 --- a/src/vueWrapper.ts +++ b/src/vueWrapper.ts @@ -107,6 +107,10 @@ export class VueWrapper< return this.vm.$ } + exists() { + return !this.getCurrentComponent().isUnmounted + } + findAll( selector: K ): DOMWrapper[] diff --git a/tests/exists.spec.ts b/tests/exists.spec.ts index e53483190..c26be1799 100644 --- a/tests/exists.spec.ts +++ b/tests/exists.spec.ts @@ -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) + }) })