Skip to content

Commit 83b49c2

Browse files
committed
wip: Test the injection of subcomponent styles in Vue custom elements
1 parent 60ea6ac commit 83b49c2

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed

Diff for: packages/runtime-dom/__tests__/customElement.spec.ts

+50
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
defineComponent,
23
defineAsyncComponent,
34
defineCustomElement,
45
h,
@@ -339,6 +340,55 @@ describe('defineCustomElement', () => {
339340
const style = el.shadowRoot?.querySelector('style')!
340341
expect(style.textContent).toBe(`div { color: red; }`)
341342
})
343+
344+
test('should attach styles of subcomponents to shadow dom', () => {
345+
const Bar = defineComponent({
346+
styles: [`span { color: green; }`],
347+
render() {
348+
return h('span', 'there')
349+
}
350+
})
351+
const Foo = defineCustomElement({
352+
styles: [`div { color: red; }`],
353+
components: { Bar },
354+
render() {
355+
return h('div', ['hello', h(Bar)])
356+
}
357+
})
358+
customElements.define('my-el-with-more-styles', Foo)
359+
container.innerHTML = `<my-el-with-more-styles></my-el-with-more-styles>`
360+
const el = container.childNodes[0] as VueElement
361+
const styles = el.shadowRoot?.querySelectorAll('style')!
362+
expect(styles.length).toBe(2)
363+
expect(styles[0].textContent).toBe(`div { color: red; }`)
364+
expect(styles[1].textContent).toBe(`span { color: green; }`)
365+
})
366+
367+
test('should attach styles of async subcomponents to shadow dom', async () => {
368+
const prom = Promise.resolve({
369+
styles: [`span { color: green; }`],
370+
render() {
371+
return h('span', 'there')
372+
}
373+
})
374+
const Bar = defineAsyncComponent(() => prom)
375+
const Foo = defineCustomElement({
376+
styles: [`div { color: red; }`],
377+
components: { Bar },
378+
render() {
379+
return h('div', ['hello', h(Bar)])
380+
}
381+
})
382+
customElements.define('my-el-with-async-styles', Foo)
383+
container.innerHTML = `<my-el-with-async-styles></my-el-with-async-styles>`
384+
385+
await new Promise(r => setTimeout(r))
386+
const el = container.childNodes[0] as VueElement
387+
const styles = el.shadowRoot?.querySelectorAll('style')!
388+
expect(styles.length).toBe(2)
389+
expect(styles[0].textContent).toBe(`div { color: red; }`)
390+
expect(styles[1].textContent).toBe(`span { color: green; }`)
391+
})
342392
})
343393

344394
describe('async', () => {

0 commit comments

Comments
 (0)