From fb465693be76579c68a5bd4a991a8876dab8c92c Mon Sep 17 00:00:00 2001 From: Thorsten Luenborg Date: Fri, 11 Jun 2021 19:19:56 +0200 Subject: [PATCH 1/3] fix: handle find(All) on components with comment root node in edge case Squashed commits: [00c8592] fix: apply same fix for findAll [acbc848] fix: handle find() when root node is placeholder comment --- src/vueWrapper.ts | 4 ++-- tests/find.spec.ts | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/vueWrapper.ts b/src/vueWrapper.ts index a147b7b60..ed13b0108 100644 --- a/src/vueWrapper.ts +++ b/src/vueWrapper.ts @@ -123,7 +123,7 @@ export class VueWrapper const result = this.parentElement['__vue_app__'] ? // force using the parentElement to allow finding the root element this.parentElement.querySelector(selector) - : this.element.querySelector(selector) + : this.element.querySelector && this.element.querySelector(selector) if (result) { return new DOMWrapper(result) @@ -223,7 +223,7 @@ export class VueWrapper findAll(selector: string): DOMWrapper[] { const results = this.parentElement['__vue_app__'] ? this.parentElement.querySelectorAll(selector) - : this.element.querySelectorAll(selector) + : this.element.querySelectorAll ? this.element.querySelectorAll(selector) : [] return Array.from(results).map((element) => new DOMWrapper(element)) } diff --git a/tests/find.spec.ts b/tests/find.spec.ts index 8cddc0d81..15213e902 100644 --- a/tests/find.spec.ts +++ b/tests/find.spec.ts @@ -99,6 +99,22 @@ describe('find', () => { ) expect(foundElement.exists()).toBeFalsy() }) + + test('handle empty root node', () => { + const EmptyTestComponent = { + name: 'EmptyTestComponent', + render: () => null + } + const Component = defineComponent({ + render() { + return h('div', [h(EmptyTestComponent)]) + } + }) + + const wrapper = mount(Component) + const etc = wrapper.findComponent({ name: 'EmptyTestComponent'}) + expect(etc.find('p').exists()).toBe(false) + }) }) describe('findAll', () => { @@ -178,4 +194,20 @@ describe('findAll', () => { expect(wrapper.find('#foo').find('#bar').exists()).toBe(true) }) + + test('handle empty/comment root node', () => { + const EmptyTestComponent = { + name: 'EmptyTestComponent', + render: () => null + } + const Component = defineComponent({ + render() { + return h('div', [h(EmptyTestComponent)]) + } + }) + + const wrapper = mount(Component) + const etc = wrapper.findComponent({ name: 'EmptyTestComponent'}) + expect(etc.findAll('p')).toHaveLength(0) + }) }) From e7cfe9418d028fdd1438dd62d26c3cfaf7b0fa74 Mon Sep 17 00:00:00 2001 From: Thorsten Luenborg Date: Fri, 11 Jun 2021 22:18:25 +0200 Subject: [PATCH 2/3] chore: formatting --- src/vueWrapper.ts | 4 +++- tests/find.spec.ts | 4 ++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/vueWrapper.ts b/src/vueWrapper.ts index ed13b0108..f2db0b2a2 100644 --- a/src/vueWrapper.ts +++ b/src/vueWrapper.ts @@ -223,7 +223,9 @@ export class VueWrapper findAll(selector: string): DOMWrapper[] { const results = this.parentElement['__vue_app__'] ? this.parentElement.querySelectorAll(selector) - : this.element.querySelectorAll ? this.element.querySelectorAll(selector) : [] + : this.element.querySelectorAll + ? this.element.querySelectorAll(selector) + : [] return Array.from(results).map((element) => new DOMWrapper(element)) } diff --git a/tests/find.spec.ts b/tests/find.spec.ts index 15213e902..aa54bb1f3 100644 --- a/tests/find.spec.ts +++ b/tests/find.spec.ts @@ -112,7 +112,7 @@ describe('find', () => { }) const wrapper = mount(Component) - const etc = wrapper.findComponent({ name: 'EmptyTestComponent'}) + const etc = wrapper.findComponent({ name: 'EmptyTestComponent' }) expect(etc.find('p').exists()).toBe(false) }) }) @@ -207,7 +207,7 @@ describe('findAll', () => { }) const wrapper = mount(Component) - const etc = wrapper.findComponent({ name: 'EmptyTestComponent'}) + const etc = wrapper.findComponent({ name: 'EmptyTestComponent' }) expect(etc.findAll('p')).toHaveLength(0) }) }) From 1c274089ed8d52ce7b84b69b1ad490c873b5860d Mon Sep 17 00:00:00 2001 From: Thorsten Luenborg Date: Fri, 11 Jun 2021 22:31:05 +0200 Subject: [PATCH 3/3] chore: fix typecasting --- src/vueWrapper.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/vueWrapper.ts b/src/vueWrapper.ts index f2db0b2a2..b2c6976db 100644 --- a/src/vueWrapper.ts +++ b/src/vueWrapper.ts @@ -225,7 +225,7 @@ export class VueWrapper ? this.parentElement.querySelectorAll(selector) : this.element.querySelectorAll ? this.element.querySelectorAll(selector) - : [] + : ([] as unknown as NodeListOf) return Array.from(results).map((element) => new DOMWrapper(element)) }