|
1 | 1 | import {
|
| 2 | + defineComponent, |
2 | 3 | defineAsyncComponent,
|
3 | 4 | defineCustomElement,
|
4 | 5 | h,
|
@@ -339,6 +340,55 @@ describe('defineCustomElement', () => {
|
339 | 340 | const style = el.shadowRoot?.querySelector('style')!
|
340 | 341 | expect(style.textContent).toBe(`div { color: red; }`)
|
341 | 342 | })
|
| 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 | + }) |
342 | 392 | })
|
343 | 393 |
|
344 | 394 | describe('async', () => {
|
|
0 commit comments