From 2ced31f4a5704a60d44bc8ba258a7d3c50c53169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=83=9D=E6=99=A8=E5=85=89?= <2293885211@qq.com> Date: Wed, 28 Feb 2024 22:29:36 +0800 Subject: [PATCH 1/4] fix: check wrapper element to fix #311 --- src/render.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/render.js b/src/render.js index 6f635cb..282a6d2 100644 --- a/src/render.js +++ b/src/render.js @@ -61,6 +61,7 @@ function cleanup() { function cleanupAtWrapper(wrapper) { if ( + wrapper.element && wrapper.element.parentNode && wrapper.element.parentNode.parentNode === document.body ) { From 11042b85802095be7db63dd5a24bc45319ed8577 Mon Sep 17 00:00:00 2001 From: haochenguang <2293885211@qq.com> Date: Thu, 7 Mar 2024 23:02:04 +0800 Subject: [PATCH 2/4] ci: add test case to fix testing-library#311 --- src/__tests__/render.js | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/src/__tests__/render.js b/src/__tests__/render.js index 151bd8b..98a665b 100644 --- a/src/__tests__/render.js +++ b/src/__tests__/render.js @@ -1,4 +1,5 @@ -import {render} from '..' +import {render, cleanup} from '..' +import {h, defineComponent} from 'vue' import '@testing-library/jest-dom' test('baseElement defaults to document.body', () => { @@ -87,3 +88,34 @@ test('unmounts', () => { expect(queryByTestId('node')).not.toBeInTheDocument() }) + +test('use unmount before cleanup', () => { + const ChildComponent = defineComponent(() => { + return () => + h( + 'div', + { + 'data-testid': 'node', + }, + ['Hi'], + ) + }) + + const ParentComponent = defineComponent((_, {slots}) => { + return () => slots.default?.() + }) + + const {getByTestId, unmount, queryByTestId} = render({ + render() { + return h(ParentComponent, {}, {default: () => h(ChildComponent)}) + }, + }) + + expect(getByTestId('node')).toBeInTheDocument() + + unmount() + + expect(queryByTestId('node')).not.toBeInTheDocument() + + expect(() => cleanup()).not.toThrow() +}) From 000b3e839014a5d6f3effa1c9ed452598baf6767 Mon Sep 17 00:00:00 2001 From: haochenguang <2293885211@qq.com> Date: Thu, 7 Mar 2024 23:07:45 +0800 Subject: [PATCH 3/4] ci: add test case to fix testing-library#311 --- src/__tests__/render.js | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/src/__tests__/render.js b/src/__tests__/render.js index 98a665b..46f80f8 100644 --- a/src/__tests__/render.js +++ b/src/__tests__/render.js @@ -90,24 +90,22 @@ test('unmounts', () => { }) test('use unmount before cleanup', () => { - const ChildComponent = defineComponent(() => { - return () => - h( - 'div', - { - 'data-testid': 'node', - }, - ['Hi'], - ) - }) - - const ParentComponent = defineComponent((_, {slots}) => { + const TestComponent = defineComponent((_, {slots}) => { return () => slots.default?.() }) const {getByTestId, unmount, queryByTestId} = render({ render() { - return h(ParentComponent, {}, {default: () => h(ChildComponent)}) + return h( + TestComponent, + {}, + { + default: () => + h('div', { + 'data-testid': 'node', + }), + }, + ) }, }) From 91379317844b8db7583c9f4d1e85dedd7cc57722 Mon Sep 17 00:00:00 2001 From: haochenguang <2293885211@qq.com> Date: Sun, 17 Mar 2024 17:10:34 +0800 Subject: [PATCH 4/4] fix: resolve conversation --- src/__tests__/render.js | 25 ++++--------------------- src/render.js | 6 +----- 2 files changed, 5 insertions(+), 26 deletions(-) diff --git a/src/__tests__/render.js b/src/__tests__/render.js index 46f80f8..ea55817 100644 --- a/src/__tests__/render.js +++ b/src/__tests__/render.js @@ -89,31 +89,14 @@ test('unmounts', () => { expect(queryByTestId('node')).not.toBeInTheDocument() }) -test('use unmount before cleanup', () => { - const TestComponent = defineComponent((_, {slots}) => { - return () => slots.default?.() - }) +test('unmounts when no wrapper element is present', () => { + const Comp = defineComponent((_, ctx) => () => ctx.slots.default?.()) - const {getByTestId, unmount, queryByTestId} = render({ - render() { - return h( - TestComponent, - {}, - { - default: () => - h('div', { - 'data-testid': 'node', - }), - }, - ) - }, + const {unmount} = render({ + render: () => h(Comp, () => h('div')), }) - expect(getByTestId('node')).toBeInTheDocument() - unmount() - expect(queryByTestId('node')).not.toBeInTheDocument() - expect(() => cleanup()).not.toThrow() }) diff --git a/src/render.js b/src/render.js index 282a6d2..997ba4c 100644 --- a/src/render.js +++ b/src/render.js @@ -60,11 +60,7 @@ function cleanup() { } function cleanupAtWrapper(wrapper) { - if ( - wrapper.element && - wrapper.element.parentNode && - wrapper.element.parentNode.parentNode === document.body - ) { + if (wrapper.element?.parentNode?.parentNode === document.body) { document.body.removeChild(wrapper.element.parentNode) }