From 49e046ed56ce1c04f178a0ad3684d60b6db05aec Mon Sep 17 00:00:00 2001 From: Kirill Romanov Date: Wed, 29 Jun 2022 00:02:43 +0300 Subject: [PATCH 1/2] fix: check if component unmounted in Wrapper.exists() --- src/baseWrapper.ts | 2 +- tests/exists.spec.ts | 27 +++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/baseWrapper.ts b/src/baseWrapper.ts index 0a1f0e462..aca052172 100644 --- a/src/baseWrapper.ts +++ b/src/baseWrapper.ts @@ -248,7 +248,7 @@ export default abstract class BaseWrapper } exists() { - return true + return !this.getCurrentComponent()?.isUnmounted } get( 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) + }) }) From 4afcab8ecc170cd165a9219de0211140e418e14d Mon Sep 17 00:00:00 2001 From: Illya Klymov Date: Wed, 29 Jun 2022 02:06:53 +0300 Subject: [PATCH 2/2] chore(exists): move exists check to VueWrapper --- src/baseWrapper.ts | 2 +- src/vueWrapper.ts | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/baseWrapper.ts b/src/baseWrapper.ts index aca052172..0a1f0e462 100644 --- a/src/baseWrapper.ts +++ b/src/baseWrapper.ts @@ -248,7 +248,7 @@ export default abstract class BaseWrapper } exists() { - return !this.getCurrentComponent()?.isUnmounted + return true } get( 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[]