@@ -11,6 +11,7 @@ import {
1111} from '@vue/runtime-dom'
1212import {
1313 child ,
14+ createComponent ,
1415 createComponentWithFallback ,
1516 createSlot ,
1617 createVaporApp ,
@@ -1007,128 +1008,128 @@ describe('defineVaporCustomElement', () => {
10071008 } )
10081009 } )
10091010
1010- // describe('styles', () => {
1011- // function assertStyles(el: VaporElement, css: string[]) {
1012- // const styles = el.shadowRoot?.querySelectorAll('style')!
1013- // expect(styles.length).toBe(css.length) // should not duplicate multiple copies from Bar
1014- // for (let i = 0; i < css.length; i++) {
1015- // expect(styles[i].textContent).toBe(css[i])
1016- // }
1017- // }
1018-
1019- // test('should attach styles to shadow dom', async () => {
1020- // const def = defineVaporComponent({
1021- // __hmrId: 'foo',
1022- // styles: [`div { color: red; }`],
1023- // render() {
1024- // return h('div', 'hello')
1025- // },
1026- // })
1027- // const Foo = defineVaporCustomElement(def)
1028- // customElements.define('my-el-with-styles', Foo)
1029- // container.innerHTML = `<my-el-with-styles></my-el-with-styles>`
1030- // const el = container.childNodes[0] as VaporElement
1031- // const style = el.shadowRoot?.querySelector('style')!
1032- // expect(style.textContent).toBe(`div { color: red; }`)
1033-
1034- // // hmr
1035- // __VUE_HMR_RUNTIME__.reload('foo', {
1036- // ...def,
1037- // styles: [`div { color: blue; }`, `div { color: yellow; }`],
1038- // } as any)
1011+ describe ( 'styles' , ( ) => {
1012+ function assertStyles ( el : VaporElement , css : string [ ] ) {
1013+ const styles = el . shadowRoot ?. querySelectorAll ( 'style' ) !
1014+ expect ( styles . length ) . toBe ( css . length ) // should not duplicate multiple copies from Bar
1015+ for ( let i = 0 ; i < css . length ; i ++ ) {
1016+ expect ( styles [ i ] . textContent ) . toBe ( css [ i ] )
1017+ }
1018+ }
10391019
1040- // await nextTick()
1041- // assertStyles(el, [`div { color: blue; }`, `div { color: yellow; }`])
1042- // })
1020+ test ( 'should attach styles to shadow dom' , async ( ) => {
1021+ const def = defineVaporComponent ( {
1022+ __hmrId : 'foo' ,
1023+ styles : [ `div { color: red; }` ] ,
1024+ setup ( ) {
1025+ return template ( '<div>hello</div>' , true ) ( )
1026+ } ,
1027+ } as any )
1028+ const Foo = defineVaporCustomElement ( def )
1029+ customElements . define ( 'my-el-with-styles' , Foo )
1030+ container . innerHTML = `<my-el-with-styles></my-el-with-styles>`
1031+ const el = container . childNodes [ 0 ] as VaporElement
1032+ const style = el . shadowRoot ?. querySelector ( 'style' ) !
1033+ expect ( style . textContent ) . toBe ( `div { color: red; }` )
1034+
1035+ // hmr
1036+ __VUE_HMR_RUNTIME__ . reload ( 'foo' , {
1037+ ...def ,
1038+ styles : [ `div { color: blue; }` , `div { color: yellow; }` ] ,
1039+ } as any )
10431040
1044- // test("child components should inject styles to root element's shadow root", async () => {
1045- // const Baz = () => h(Bar)
1046- // const Bar = defineVaporComponent({
1047- // __hmrId: 'bar',
1048- // styles: [`div { color: green; }`, `div { color: blue; }`],
1049- // render() {
1050- // return 'bar'
1051- // },
1052- // })
1053- // const Foo = defineVaporCustomElement({
1054- // styles: [`div { color: red; }`],
1055- // render() {
1056- // return [h(Baz), h(Baz)]
1057- // },
1058- // })
1059- // customElements.define('my-el-with-child-styles', Foo)
1060- // container.innerHTML = `<my-el-with-child-styles></my-el-with-child-styles>`
1061- // const el = container.childNodes[0] as VaporElement
1041+ await nextTick ( )
1042+ assertStyles ( el , [ `div { color: blue; }` , `div { color: yellow; }` ] )
1043+ } )
10621044
1063- // // inject order should be child -> parent
1064- // assertStyles(el, [
1065- // `div { color: green; }`,
1066- // `div { color: blue; }`,
1067- // `div { color: red; }`,
1068- // ])
1045+ test ( "child components should inject styles to root element's shadow root" , async ( ) => {
1046+ const Baz = ( ) => createComponent ( Bar )
1047+ const Bar = defineVaporComponent ( {
1048+ __hmrId : 'bar' ,
1049+ styles : [ `div { color: green; }` , `div { color: blue; }` ] ,
1050+ setup ( ) {
1051+ return template ( 'bar' ) ( )
1052+ } ,
1053+ } as any )
1054+ const Foo = defineVaporCustomElement ( {
1055+ styles : [ `div { color: red; }` ] ,
1056+ setup ( ) {
1057+ return [ createComponent ( Baz ) , createComponent ( Baz ) ]
1058+ } ,
1059+ } )
1060+ customElements . define ( 'my-el-with-child-styles' , Foo )
1061+ container . innerHTML = `<my-el-with-child-styles></my-el-with-child-styles>`
1062+ const el = container . childNodes [ 0 ] as VaporElement
1063+
1064+ // inject order should be child -> parent
1065+ assertStyles ( el , [
1066+ `div { color: green; }` ,
1067+ `div { color: blue; }` ,
1068+ `div { color: red; }` ,
1069+ ] )
10691070
1070- // // hmr
1071- // __VUE_HMR_RUNTIME__.reload(Bar.__hmrId!, {
1072- // ...Bar,
1073- // styles: [`div { color: red; }`, `div { color: yellow; }`],
1074- // } as any)
1071+ // hmr
1072+ __VUE_HMR_RUNTIME__ . reload ( Bar . __hmrId ! , {
1073+ ...Bar ,
1074+ styles : [ `div { color: red; }` , `div { color: yellow; }` ] ,
1075+ } as any )
10751076
1076- // await nextTick()
1077- // assertStyles(el, [
1078- // `div { color: red; }`,
1079- // `div { color: yellow; }`,
1080- // `div { color: red; }`,
1081- // ])
1077+ await nextTick ( )
1078+ assertStyles ( el , [
1079+ `div { color: red; }` ,
1080+ `div { color: yellow; }` ,
1081+ `div { color: red; }` ,
1082+ ] )
10821083
1083- // __VUE_HMR_RUNTIME__.reload(Bar.__hmrId!, {
1084- // ...Bar,
1085- // styles: [`div { color: blue; }`],
1086- // } as any)
1087- // await nextTick()
1088- // assertStyles(el, [`div { color: blue; }`, `div { color: red; }`])
1089- // })
1084+ __VUE_HMR_RUNTIME__ . reload ( Bar . __hmrId ! , {
1085+ ...Bar ,
1086+ styles : [ `div { color: blue; }` ] ,
1087+ } as any )
1088+ await nextTick ( )
1089+ assertStyles ( el , [ `div { color: blue; }` , `div { color: red; }` ] )
1090+ } )
10901091
1091- // test("child components should not inject styles to root element's shadow root w/ shadowRoot false", async () => {
1092- // const Bar = defineVaporComponent({
1093- // styles: [`div { color: green; }`],
1094- // render() {
1095- // return 'bar'
1096- // },
1097- // })
1098- // const Baz = () => h(Bar)
1099- // const Foo = defineVaporCustomElement(
1100- // {
1101- // render() {
1102- // return [h(Baz)]
1103- // },
1104- // },
1105- // { shadowRoot: false },
1106- // )
1092+ // test("child components should not inject styles to root element's shadow root w/ shadowRoot false", async () => {
1093+ // const Bar = defineVaporComponent({
1094+ // styles: [`div { color: green; }`],
1095+ // render() {
1096+ // return 'bar'
1097+ // },
1098+ // })
1099+ // const Baz = () => h(Bar)
1100+ // const Foo = defineVaporCustomElement(
1101+ // {
1102+ // render() {
1103+ // return [h(Baz)]
1104+ // },
1105+ // },
1106+ // { shadowRoot: false },
1107+ // )
11071108
1108- // customElements.define('my-foo-with-shadowroot-false', Foo)
1109- // container.innerHTML = `<my-foo-with-shadowroot-false></my-foo-with-shadowroot-false>`
1110- // const el = container.childNodes[0] as VaporElement
1111- // const style = el.shadowRoot?.querySelector('style')
1112- // expect(style).toBeUndefined()
1113- // })
1109+ // customElements.define('my-foo-with-shadowroot-false', Foo)
1110+ // container.innerHTML = `<my-foo-with-shadowroot-false></my-foo-with-shadowroot-false>`
1111+ // const el = container.childNodes[0] as VaporElement
1112+ // const style = el.shadowRoot?.querySelector('style')
1113+ // expect(style).toBeUndefined()
1114+ // })
11141115
1115- // test('with nonce', () => {
1116- // const Foo = defineVaporCustomElement(
1117- // {
1118- // styles: [`div { color: red; }`],
1119- // render() {
1120- // return h('div', 'hello')
1121- // },
1122- // },
1123- // { nonce: 'xxx' },
1124- // )
1125- // customElements.define('my-el-with-nonce', Foo)
1126- // container.innerHTML = `<my-el-with-nonce></my-el-with-nonce>`
1127- // const el = container.childNodes[0] as VaporElement
1128- // const style = el.shadowRoot?.querySelector('style')!
1129- // expect(style.getAttribute('nonce')).toBe('xxx')
1130- // })
1131- // })
1116+ // test('with nonce', () => {
1117+ // const Foo = defineVaporCustomElement(
1118+ // {
1119+ // styles: [`div { color: red; }`],
1120+ // render() {
1121+ // return h('div', 'hello')
1122+ // },
1123+ // },
1124+ // { nonce: 'xxx' },
1125+ // )
1126+ // customElements.define('my-el-with-nonce', Foo)
1127+ // container.innerHTML = `<my-el-with-nonce></my-el-with-nonce>`
1128+ // const el = container.childNodes[0] as VaporElement
1129+ // const style = el.shadowRoot?.querySelector('style')!
1130+ // expect(style.getAttribute('nonce')).toBe('xxx')
1131+ // })
1132+ } )
11321133
11331134 // describe('async', () => {
11341135 // test('should work', async () => {
0 commit comments