Skip to content

Commit 0890d07

Browse files
authored
Merge pull request #174 from vuejs/issue-173
fix: return correct html depending if outer element is root Vue app
2 parents e3daca6 + fc045f7 commit 0890d07

File tree

3 files changed

+91
-1
lines changed

3 files changed

+91
-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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,4 +170,60 @@ describe('findComponent', () => {
170170
const wrapper = mount(compA)
171171
expect(wrapper.findComponent(Hello).unmount).toThrowError()
172172
})
173+
174+
// https://github.com/vuejs/vue-test-utils-next/issues/173
175+
const ComponentA = {
176+
name: 'ComponentA',
177+
template: `<div><slot></slot></div>`
178+
}
179+
180+
const ComponentB = {
181+
name: 'ComponentB',
182+
template: '<div><slot></slot></div>'
183+
}
184+
185+
it('finds nested components and obtains expected html and innerText', () => {
186+
const wrapper = mount({
187+
components: {
188+
ComponentA,
189+
ComponentB
190+
},
191+
template: `
192+
<ComponentA>
193+
<ComponentB>1</ComponentB>
194+
<ComponentB>2</ComponentB>
195+
<ComponentB>3</ComponentB>
196+
</ComponentA>
197+
`
198+
})
199+
const com1 = wrapper.findComponent(ComponentB)
200+
expect(com1.html()).toBe('<div>1</div>')
201+
})
202+
203+
it('finds nested components and obtains expected html and innerText', () => {
204+
const wrapper = mount({
205+
components: {
206+
ComponentA,
207+
ComponentB
208+
},
209+
template: `
210+
<ComponentA>
211+
<ComponentB>
212+
<div class="content" id="1">1</div>
213+
</ComponentB>
214+
<ComponentB>
215+
<div class="content" id="2">2</div>
216+
</ComponentB>
217+
<ComponentB>
218+
<div class="content" id="3">3</div>
219+
</ComponentB>
220+
</ComponentA>
221+
`
222+
})
223+
224+
const compB = wrapper.findAllComponents(ComponentB)
225+
expect(compB[0].find('.content').text()).toBe('1')
226+
expect(compB[0].vm.$el.querySelector('.content').innerHTML).toBe('1')
227+
expect(compB[0].vm.$el.querySelector('.content').textContent).toBe('1')
228+
})
173229
})

tests/html.spec.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,33 @@ describe('html', () => {
5050
const wrapper = mount(Component)
5151
expect(wrapper.html()).toEqual('<div class="Foo">FOO</div>')
5252
})
53+
54+
it('returns the html with findComponent in a nested Suspense component', () => {
55+
const Foo = defineComponent({
56+
template: '<div class="Foo">FOO</div>',
57+
setup() {
58+
return {}
59+
}
60+
})
61+
const Component = {
62+
template: `
63+
<div>
64+
<span>
65+
<Suspense>
66+
<template #default>
67+
<Foo />
68+
</template>
69+
70+
<template #fallback>
71+
Fallback
72+
</template>
73+
</Suspense>
74+
</span>
75+
</div>`,
76+
components: { Foo }
77+
}
78+
const wrapper = mount(Component)
79+
const foo = wrapper.findComponent(Foo)
80+
expect(foo.html()).toEqual('<div class="Foo">FOO</div>')
81+
})
5382
})

0 commit comments

Comments
 (0)