Skip to content

Commit 6a6dd41

Browse files
committed
fix: return correct html depending if outer element is root Vue app
1 parent 4139219 commit 6a6dd41

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

src/vueWrapper.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,12 @@ export class VueWrapper<T extends ComponentPublicInstance> {
7979
}
8080

8181
html() {
82-
return this.parentElement.innerHTML
82+
// cover cases like <Suspense>, multiple root nodes.
83+
if (this.parentElement['__vue_app__']) {
84+
return this.parentElement.innerHTML
85+
}
86+
87+
return this.element.outerHTML
8388
}
8489

8590
text() {

tests/findComponent.spec.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,32 @@ describe('findComponent', () => {
170170
const wrapper = mount(compA)
171171
expect(wrapper.findComponent(Hello).unmount).toThrowError()
172172
})
173+
174+
it('finds nested componens', () => {
175+
// https://github.com/vuejs/vue-test-utils-next/issues/173
176+
const ComponentA = {
177+
name: 'ComponentA',
178+
template: `<div><slot></slot></div>`
179+
}
180+
181+
const ComponentB = {
182+
name: 'ComponentB',
183+
template: '<div><slot></slot></div>'
184+
}
185+
const wrapper = mount({
186+
components: {
187+
ComponentA,
188+
ComponentB
189+
},
190+
template: `
191+
<ComponentA>
192+
<ComponentB>1</ComponentB>
193+
<ComponentB>2</ComponentB>
194+
<ComponentB>3</ComponentB>
195+
</ComponentA>
196+
`
197+
})
198+
const com1 = wrapper.findComponent(ComponentB)
199+
expect(com1.html()).toBe('<div>1</div>')
200+
})
173201
})

0 commit comments

Comments
 (0)