From 199239f6fa1e8fdbf858350bdd31164993fb070e Mon Sep 17 00:00:00 2001 From: JeremyWuuuuu <591449570@qq.com> Date: Thu, 20 Aug 2020 20:51:51 +0800 Subject: [PATCH 1/3] bugfix/unmounting-on-multi-root - Fix the logic which causes unmounting fail when it's multi-root component --- src/vueWrapper.ts | 5 +---- tests/unmount.spec.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) create mode 100644 tests/unmount.spec.ts diff --git a/src/vueWrapper.ts b/src/vueWrapper.ts index bf0f8fcc9..283a36a7e 100644 --- a/src/vueWrapper.ts +++ b/src/vueWrapper.ts @@ -216,10 +216,7 @@ export class VueWrapper { ) } - if (this.parentElement) { - this.parentElement.removeChild(this.element) - } - this.__app.unmount(this.element) + this.__app.unmount(this.parentElement) } } diff --git a/tests/unmount.spec.ts b/tests/unmount.spec.ts new file mode 100644 index 000000000..083913382 --- /dev/null +++ b/tests/unmount.spec.ts @@ -0,0 +1,32 @@ +import { mount } from '../src' + +const AXIOM = 'Rem is the best girl' + +describe('Unmount', () => { + it('works on single root component', () => { + const Component = { + template: ` +
${AXIOM}
+ `, + onErrorCaptured(err: Error) { + throw err + } + } + const wrapper = mount(Component) + expect(() => wrapper.unmount()).not.toThrowError() + }) + + it('works on multi-root component', () => { + const Component = { + template: ` +
${AXIOM}
+
${AXIOM}
+ `, + onErrorCaptured(err: Error) { + throw err + } + } + const wrapper = mount(Component) + wrapper.unmount() + }) +}) From 4795af47e540df0a8d8e8563f6585715c3707e21 Mon Sep 17 00:00:00 2001 From: JeremyWuuuuu <591449570@qq.com> Date: Thu, 20 Aug 2020 21:17:05 +0800 Subject: [PATCH 2/3] - Enrich the test cases --- tests/unmount.spec.ts | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/tests/unmount.spec.ts b/tests/unmount.spec.ts index 083913382..736931e3f 100644 --- a/tests/unmount.spec.ts +++ b/tests/unmount.spec.ts @@ -1,32 +1,46 @@ +import { defineComponent } from 'vue' + import { mount } from '../src' const AXIOM = 'Rem is the best girl' describe('Unmount', () => { it('works on single root component', () => { + const errorHandler = jest.fn() const Component = { template: `
${AXIOM}
- `, - onErrorCaptured(err: Error) { - throw err - } + ` } - const wrapper = mount(Component) - expect(() => wrapper.unmount()).not.toThrowError() + const wrapper = mount(Component, { + props: {}, + global: { + config: { + errorHandler + } + } + } as any) // The type checking keeps complaning about unmatched type which might be a bug + wrapper.unmount() + expect(errorHandler).not.toHaveBeenCalled() }) it('works on multi-root component', () => { - const Component = { + const errorHandler = jest.fn() + const Component = defineComponent({ template: `
${AXIOM}
${AXIOM}
- `, - onErrorCaptured(err: Error) { - throw err + ` + }) + const wrapper = mount(Component, { + props: {}, + global: { + config: { + errorHandler + } } - } - const wrapper = mount(Component) + } as any) wrapper.unmount() + expect(errorHandler).not.toHaveBeenCalled() }) }) From 15fdd8730e736e1d14b4a36981669760ed9c9f6c Mon Sep 17 00:00:00 2001 From: JeremyWuuuuu <591449570@qq.com> Date: Fri, 21 Aug 2020 20:19:05 +0800 Subject: [PATCH 3/3] Remove mounting text --- tests/unmount.spec.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/tests/unmount.spec.ts b/tests/unmount.spec.ts index 736931e3f..f9f824763 100644 --- a/tests/unmount.spec.ts +++ b/tests/unmount.spec.ts @@ -2,14 +2,12 @@ import { defineComponent } from 'vue' import { mount } from '../src' -const AXIOM = 'Rem is the best girl' - describe('Unmount', () => { it('works on single root component', () => { const errorHandler = jest.fn() const Component = { template: ` -
${AXIOM}
+
` } const wrapper = mount(Component, { @@ -28,8 +26,8 @@ describe('Unmount', () => { const errorHandler = jest.fn() const Component = defineComponent({ template: ` -
${AXIOM}
-
${AXIOM}
+
+
` }) const wrapper = mount(Component, {