customElement.spec.ts 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113
  1. import type { MockedFunction } from 'vitest'
  2. import type { VaporElement } from '../src/apiDefineVaporCustomElement'
  3. import {
  4. type HMRRuntime,
  5. type Ref,
  6. inject,
  7. nextTick,
  8. onMounted,
  9. provide,
  10. ref,
  11. toDisplayString,
  12. useHost,
  13. useShadowRoot,
  14. } from '@vue/runtime-dom'
  15. import {
  16. VaporTeleport,
  17. child,
  18. createComponent,
  19. createIf,
  20. createPlainElement,
  21. createSlot,
  22. createVaporApp,
  23. defineVaporAsyncComponent,
  24. defineVaporComponent,
  25. defineVaporCustomElement,
  26. delegateEvents,
  27. next,
  28. on,
  29. renderEffect,
  30. setInsertionState,
  31. setText,
  32. setValue,
  33. template,
  34. txt,
  35. withVaporCtx,
  36. } from '../src'
  37. declare var __VUE_HMR_RUNTIME__: HMRRuntime
  38. describe('defineVaporCustomElement', () => {
  39. const container = document.createElement('div')
  40. document.body.appendChild(container)
  41. beforeEach(() => {
  42. container.innerHTML = ''
  43. })
  44. delegateEvents('input', 'click', 'mousedown')
  45. function render(tag: string, props: any) {
  46. const root = document.createElement('div')
  47. document.body.appendChild(root)
  48. createVaporApp({
  49. setup() {
  50. return createPlainElement(tag, props, null, true)
  51. },
  52. }).mount(root)
  53. return {
  54. container: root,
  55. }
  56. }
  57. describe('mounting/unmount', () => {
  58. const E = defineVaporCustomElement({
  59. props: {
  60. msg: {
  61. type: String,
  62. default: 'hello',
  63. },
  64. },
  65. setup(props: any) {
  66. const n0 = template('<div> </div>', true)() as any
  67. const x0 = txt(n0) as any
  68. renderEffect(() => setText(x0, toDisplayString(props.msg)))
  69. return n0
  70. },
  71. })
  72. customElements.define('my-element', E)
  73. test('should work', () => {
  74. container.innerHTML = `<my-element></my-element>`
  75. const e = container.childNodes[0] as VaporElement
  76. expect(e).toBeInstanceOf(E)
  77. expect(e._instance).toBeTruthy()
  78. expect(e.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  79. })
  80. test('should work w/ manual instantiation', () => {
  81. const e = new E({ msg: 'inline' })
  82. // should lazy init
  83. expect(e._instance).toBe(null)
  84. // should initialize on connect
  85. container.appendChild(e)
  86. expect(e._instance).toBeTruthy()
  87. expect(e.shadowRoot!.innerHTML).toBe(`<div>inline</div>`)
  88. })
  89. test('should unmount on remove', async () => {
  90. container.innerHTML = `<my-element></my-element>`
  91. const e = container.childNodes[0] as VaporElement
  92. container.removeChild(e)
  93. await nextTick()
  94. expect(e._instance).toBe(null)
  95. expect(e.shadowRoot!.innerHTML).toBe('')
  96. })
  97. test('When elements move, avoid prematurely disconnecting MutationObserver', async () => {
  98. const CustomInput = defineVaporCustomElement({
  99. props: ['value'],
  100. emits: ['update'],
  101. setup(props: any, { emit }: any) {
  102. const n0 = template('<input type="number">', true)() as any
  103. n0.$evtinput = () => {
  104. const num = (n0 as HTMLInputElement).valueAsNumber
  105. emit('update', Number.isNaN(num) ? null : num)
  106. }
  107. renderEffect(() => {
  108. setValue(n0, props.value)
  109. })
  110. return n0
  111. },
  112. })
  113. customElements.define('my-el-input', CustomInput)
  114. const num = ref('12')
  115. const containerComp = defineVaporComponent({
  116. setup() {
  117. const n1 = template('<div><div id="move"></div></div>', true)() as any
  118. setInsertionState(n1, 0, true)
  119. createPlainElement('my-el-input', {
  120. value: () => num.value,
  121. onInput: () => ($event: CustomEvent) => {
  122. num.value = $event.detail[0]
  123. },
  124. })
  125. return n1
  126. },
  127. })
  128. const app = createVaporApp(containerComp)
  129. const container = document.createElement('div')
  130. document.body.appendChild(container)
  131. app.mount(container)
  132. const myInputEl = container.querySelector('my-el-input')!
  133. const inputEl = myInputEl.shadowRoot!.querySelector('input')!
  134. await nextTick()
  135. expect(inputEl.value).toBe('12')
  136. const moveEl = container.querySelector('#move')!
  137. moveEl.append(myInputEl)
  138. await nextTick()
  139. myInputEl.removeAttribute('value')
  140. await nextTick()
  141. expect(inputEl.value).toBe('')
  142. })
  143. test('should not unmount on move', async () => {
  144. container.innerHTML = `<div><my-element></my-element></div>`
  145. const e = container.childNodes[0].childNodes[0] as VaporElement
  146. const i = e._instance
  147. // moving from one parent to another - this will trigger both disconnect
  148. // and connected callbacks synchronously
  149. container.appendChild(e)
  150. await nextTick()
  151. // should be the same instance
  152. expect(e._instance).toBe(i)
  153. expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div>')
  154. })
  155. test('remove then insert again', async () => {
  156. container.innerHTML = `<my-element></my-element>`
  157. const e = container.childNodes[0] as VaporElement
  158. container.removeChild(e)
  159. await nextTick()
  160. expect(e._instance).toBe(null)
  161. expect(e.shadowRoot!.innerHTML).toBe('')
  162. container.appendChild(e)
  163. expect(e._instance).toBeTruthy()
  164. expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div>')
  165. })
  166. })
  167. describe('props', () => {
  168. const E = defineVaporCustomElement({
  169. props: {
  170. foo: [String, null],
  171. bar: Object,
  172. bazQux: null,
  173. value: null,
  174. },
  175. setup(props: any) {
  176. const n0 = template('<div> </div>', true)() as any
  177. const x0 = txt(n0) as any
  178. const n1 = template('<div> </div>', true)() as any
  179. const x1 = txt(n1) as any
  180. renderEffect(() => setText(x0, props.foo || ''))
  181. renderEffect(() =>
  182. setText(x1, props.bazQux || (props.bar && props.bar.x)),
  183. )
  184. return [n0, n1]
  185. },
  186. })
  187. customElements.define('my-el-props', E)
  188. test('renders custom element w/ correct object prop value', () => {
  189. const { container } = render('my-el-props', {
  190. value: () => ({
  191. x: 1,
  192. }),
  193. })
  194. const el = container.children[0]
  195. expect((el as any).value).toEqual({ x: 1 })
  196. })
  197. test('props via attribute', async () => {
  198. // bazQux should map to `baz-qux` attribute
  199. container.innerHTML = `<my-el-props foo="hello" baz-qux="bye"></my-el-props>`
  200. const e = container.childNodes[0] as VaporElement
  201. expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div><div>bye</div>')
  202. // change attr
  203. e.setAttribute('foo', 'changed')
  204. await nextTick()
  205. expect(e.shadowRoot!.innerHTML).toBe('<div>changed</div><div>bye</div>')
  206. e.setAttribute('baz-qux', 'changed')
  207. await nextTick()
  208. expect(e.shadowRoot!.innerHTML).toBe(
  209. '<div>changed</div><div>changed</div>',
  210. )
  211. })
  212. test('props via properties', async () => {
  213. // TODO remove this after type inference done
  214. const e = new E() as any
  215. e.foo = 'one'
  216. e.bar = { x: 'two' }
  217. container.appendChild(e)
  218. expect(e.shadowRoot!.innerHTML).toBe('<div>one</div><div>two</div>')
  219. // reflect
  220. // should reflect primitive value
  221. expect(e.getAttribute('foo')).toBe('one')
  222. // should not reflect rich data
  223. expect(e.hasAttribute('bar')).toBe(false)
  224. e.foo = 'three'
  225. await nextTick()
  226. expect(e.shadowRoot!.innerHTML).toBe('<div>three</div><div>two</div>')
  227. expect(e.getAttribute('foo')).toBe('three')
  228. e.foo = null
  229. await nextTick()
  230. expect(e.shadowRoot!.innerHTML).toBe('<div></div><div>two</div>')
  231. expect(e.hasAttribute('foo')).toBe(false)
  232. e.foo = undefined
  233. await nextTick()
  234. expect(e.shadowRoot!.innerHTML).toBe('<div></div><div>two</div>')
  235. expect(e.hasAttribute('foo')).toBe(false)
  236. expect(e.foo).toBe(undefined)
  237. e.bazQux = 'four'
  238. await nextTick()
  239. expect(e.shadowRoot!.innerHTML).toBe('<div></div><div>four</div>')
  240. expect(e.getAttribute('baz-qux')).toBe('four')
  241. })
  242. test('props via attributes and properties changed together', async () => {
  243. // TODO remove this after type inference done
  244. const e = new E() as any
  245. e.foo = 'foo1'
  246. e.bar = { x: 'bar1' }
  247. container.appendChild(e)
  248. await nextTick()
  249. expect(e.shadowRoot!.innerHTML).toBe('<div>foo1</div><div>bar1</div>')
  250. // change attr then property
  251. e.setAttribute('foo', 'foo2')
  252. e.bar = { x: 'bar2' }
  253. await nextTick()
  254. expect(e.shadowRoot!.innerHTML).toBe('<div>foo2</div><div>bar2</div>')
  255. expect(e.getAttribute('foo')).toBe('foo2')
  256. expect(e.hasAttribute('bar')).toBe(false)
  257. // change prop then attr
  258. e.bar = { x: 'bar3' }
  259. e.setAttribute('foo', 'foo3')
  260. await nextTick()
  261. expect(e.shadowRoot!.innerHTML).toBe('<div>foo3</div><div>bar3</div>')
  262. expect(e.getAttribute('foo')).toBe('foo3')
  263. expect(e.hasAttribute('bar')).toBe(false)
  264. })
  265. test('props via hyphen property', async () => {
  266. const Comp = defineVaporCustomElement({
  267. props: {
  268. fooBar: Boolean,
  269. },
  270. setup() {
  271. return template('Comp')()
  272. },
  273. })
  274. customElements.define('my-el-comp', Comp)
  275. const { container } = render('my-el-comp', {
  276. 'foo-bar': () => true,
  277. })
  278. const el = container.children[0]
  279. expect((el as any).outerHTML).toBe('<my-el-comp foo-bar=""></my-el-comp>')
  280. })
  281. test('attribute -> prop type casting', async () => {
  282. const E = defineVaporCustomElement({
  283. props: {
  284. fooBar: Number, // test casting of camelCase prop names
  285. bar: Boolean,
  286. baz: String,
  287. },
  288. setup(props: any) {
  289. const n0 = template(' ')() as any
  290. renderEffect(() => {
  291. const texts = []
  292. texts.push(
  293. toDisplayString(props.fooBar),
  294. toDisplayString(typeof props.fooBar),
  295. toDisplayString(props.bar),
  296. toDisplayString(typeof props.bar),
  297. toDisplayString(props.baz),
  298. toDisplayString(typeof props.baz),
  299. )
  300. setText(n0, texts.join(' '))
  301. })
  302. return n0
  303. },
  304. })
  305. customElements.define('my-el-props-cast', E)
  306. container.innerHTML = `<my-el-props-cast foo-bar="1" baz="12345"></my-el-props-cast>`
  307. const e = container.childNodes[0] as VaporElement
  308. expect(e.shadowRoot!.innerHTML).toBe(
  309. `1 number false boolean 12345 string`,
  310. )
  311. e.setAttribute('bar', '')
  312. await nextTick()
  313. expect(e.shadowRoot!.innerHTML).toBe(`1 number true boolean 12345 string`)
  314. e.setAttribute('foo-bar', '2e1')
  315. await nextTick()
  316. expect(e.shadowRoot!.innerHTML).toBe(
  317. `20 number true boolean 12345 string`,
  318. )
  319. e.setAttribute('baz', '2e1')
  320. await nextTick()
  321. expect(e.shadowRoot!.innerHTML).toBe(`20 number true boolean 2e1 string`)
  322. })
  323. test('attr casting w/ programmatic creation', () => {
  324. const E = defineVaporCustomElement({
  325. props: {
  326. foo: Number,
  327. },
  328. setup(props: any) {
  329. const n0 = template(' ')() as any
  330. renderEffect(() => {
  331. setText(n0, `foo type: ${typeof props.foo}`)
  332. })
  333. return n0
  334. },
  335. })
  336. customElements.define('my-element-programmatic', E)
  337. const el = document.createElement('my-element-programmatic') as any
  338. el.setAttribute('foo', '123')
  339. container.appendChild(el)
  340. expect(el.shadowRoot.innerHTML).toBe(`foo type: number`)
  341. })
  342. test('handling properties set before upgrading', () => {
  343. const E = defineVaporCustomElement({
  344. props: {
  345. foo: String,
  346. dataAge: Number,
  347. },
  348. setup(props: any) {
  349. expect(props.foo).toBe('hello')
  350. expect(props.dataAge).toBe(5)
  351. const n0 = template('<div> </div>', true)() as any
  352. const x0 = txt(n0) as any
  353. renderEffect(() => setText(x0, `foo: ${props.foo}`))
  354. return n0
  355. },
  356. })
  357. const el = document.createElement('my-el-upgrade') as any
  358. el.foo = 'hello'
  359. el.dataset.age = 5
  360. el.notProp = 1
  361. container.appendChild(el)
  362. customElements.define('my-el-upgrade', E)
  363. expect(el.shadowRoot.firstChild.innerHTML).toBe(`foo: hello`)
  364. // should not reflect if not declared as a prop
  365. expect(el.hasAttribute('not-prop')).toBe(false)
  366. })
  367. test('handle properties set before connecting', () => {
  368. const obj = { a: 1 }
  369. const E = defineVaporCustomElement({
  370. props: {
  371. foo: String,
  372. post: Object,
  373. },
  374. setup(props: any) {
  375. expect(props.foo).toBe('hello')
  376. expect(props.post).toBe(obj)
  377. const n0 = template(' ', true)() as any
  378. renderEffect(() => setText(n0, JSON.stringify(props.post)))
  379. return n0
  380. },
  381. })
  382. customElements.define('my-el-preconnect', E)
  383. const el = document.createElement('my-el-preconnect') as any
  384. el.foo = 'hello'
  385. el.post = obj
  386. container.appendChild(el)
  387. expect(el.shadowRoot.innerHTML).toBe(JSON.stringify(obj))
  388. })
  389. test('handle components with no props', async () => {
  390. const E = defineVaporCustomElement({
  391. setup() {
  392. return template('<div>foo</div>', true)()
  393. },
  394. })
  395. customElements.define('my-element-noprops', E)
  396. const el = document.createElement('my-element-noprops')
  397. container.appendChild(el)
  398. await nextTick()
  399. expect(el.shadowRoot!.innerHTML).toMatchInlineSnapshot('"<div>foo</div>"')
  400. })
  401. test('set number value in dom property', () => {
  402. const E = defineVaporCustomElement({
  403. props: {
  404. 'max-age': Number,
  405. },
  406. setup(props: any) {
  407. const n0 = template(' ')() as any
  408. renderEffect(() => {
  409. setText(n0, `max age: ${props.maxAge}/type: ${typeof props.maxAge}`)
  410. })
  411. return n0
  412. },
  413. })
  414. customElements.define('my-element-number-property', E)
  415. const el = document.createElement('my-element-number-property') as any
  416. container.appendChild(el)
  417. el.maxAge = 50
  418. expect(el.maxAge).toBe(50)
  419. expect(el.shadowRoot.innerHTML).toBe('max age: 50/type: number')
  420. })
  421. test('should reflect default value', () => {
  422. const E = defineVaporCustomElement({
  423. props: {
  424. value: {
  425. type: String,
  426. default: 'hi',
  427. },
  428. },
  429. setup(props: any) {
  430. const n0 = template(' ')() as any
  431. renderEffect(() => setText(n0, props.value))
  432. return n0
  433. },
  434. })
  435. customElements.define('my-el-default-val', E)
  436. container.innerHTML = `<my-el-default-val></my-el-default-val>`
  437. const e = container.childNodes[0] as any
  438. expect(e.value).toBe('hi')
  439. })
  440. test('Boolean prop with default true', async () => {
  441. const E = defineVaporCustomElement({
  442. props: {
  443. foo: {
  444. type: Boolean,
  445. default: true,
  446. },
  447. },
  448. setup(props: any) {
  449. const n0 = template(' ')() as any
  450. renderEffect(() => setText(n0, String(props.foo)))
  451. return n0
  452. },
  453. })
  454. customElements.define('my-el-default-true', E)
  455. container.innerHTML = `<my-el-default-true></my-el-default-true>`
  456. const e = container.childNodes[0] as HTMLElement & { foo: any },
  457. shadowRoot = e.shadowRoot as ShadowRoot
  458. expect(shadowRoot.innerHTML).toBe('true')
  459. e.foo = undefined
  460. await nextTick()
  461. expect(shadowRoot.innerHTML).toBe('true')
  462. e.foo = false
  463. await nextTick()
  464. expect(shadowRoot.innerHTML).toBe('false')
  465. e.foo = null
  466. await nextTick()
  467. expect(shadowRoot.innerHTML).toBe('null')
  468. e.foo = ''
  469. await nextTick()
  470. expect(shadowRoot.innerHTML).toBe('true')
  471. })
  472. test('support direct setup function syntax with extra options', () => {
  473. const E = defineVaporCustomElement(
  474. (props: any) => {
  475. const n0 = template(' ')() as any
  476. renderEffect(() => setText(n0, props.text))
  477. return n0
  478. },
  479. {
  480. props: {
  481. text: String,
  482. },
  483. },
  484. )
  485. customElements.define('my-el-setup-with-props', E)
  486. container.innerHTML = `<my-el-setup-with-props text="hello"></my-el-setup-with-props>`
  487. const e = container.childNodes[0] as VaporElement
  488. expect(e.shadowRoot!.innerHTML).toBe('hello')
  489. })
  490. test('prop types validation', async () => {
  491. const E = defineVaporCustomElement({
  492. props: {
  493. num: {
  494. type: [Number, String],
  495. },
  496. bool: {
  497. type: Boolean,
  498. },
  499. },
  500. setup(props: any) {
  501. const n0 = template(
  502. '<div><span> </span><span> </span></div>',
  503. true,
  504. )() as any
  505. const n1 = child(n0) as any
  506. const n2 = next(n1) as any
  507. const x0 = txt(n1) as any
  508. const x1 = txt(n2) as any
  509. renderEffect(() => setText(x0, `${props.num} is ${typeof props.num}`))
  510. renderEffect(() =>
  511. setText(x1, `${props.bool} is ${typeof props.bool}`),
  512. )
  513. return n0
  514. },
  515. })
  516. customElements.define('my-el-with-type-props', E)
  517. const { container } = render('my-el-with-type-props', {
  518. num: () => 1,
  519. bool: () => true,
  520. })
  521. const e = container.childNodes[0] as VaporElement
  522. // @ts-expect-error
  523. expect(e.num).toBe(1)
  524. // @ts-expect-error
  525. expect(e.bool).toBe(true)
  526. expect(e.shadowRoot!.innerHTML).toBe(
  527. '<div><span>1 is number</span><span>true is boolean</span></div>',
  528. )
  529. })
  530. })
  531. describe('attrs', () => {
  532. const E = defineVaporCustomElement({
  533. setup(_: any, { attrs }: any) {
  534. const n0 = template('<div> </div>')() as any
  535. const x0 = txt(n0) as any
  536. renderEffect(() => setText(x0, toDisplayString(attrs.foo)))
  537. return [n0]
  538. },
  539. })
  540. customElements.define('my-el-attrs', E)
  541. test('attrs via attribute', async () => {
  542. container.innerHTML = `<my-el-attrs foo="hello"></my-el-attrs>`
  543. const e = container.childNodes[0] as VaporElement
  544. expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div>')
  545. e.setAttribute('foo', 'changed')
  546. await nextTick()
  547. expect(e.shadowRoot!.innerHTML).toBe('<div>changed</div>')
  548. })
  549. test('non-declared properties should not show up in $attrs', () => {
  550. const e = new E()
  551. // @ts-expect-error
  552. e.foo = '123'
  553. container.appendChild(e)
  554. expect(e.shadowRoot!.innerHTML).toBe('<div></div>')
  555. })
  556. // https://github.com/vuejs/core/issues/12964
  557. // Disabled because of missing support for `delegatesFocus` in jsdom
  558. // https://github.com/jsdom/jsdom/issues/3418
  559. // use vitest browser mode instead
  560. test.todo('shadowRoot should be initialized with delegatesFocus', () => {
  561. const E = defineVaporCustomElement(
  562. {
  563. setup() {
  564. return template('<input tabindex="1">', true)()
  565. },
  566. },
  567. { shadowRootOptions: { delegatesFocus: true } } as any,
  568. )
  569. customElements.define('my-el-with-delegate-focus', E)
  570. const e = new E()
  571. container.appendChild(e)
  572. expect(e.shadowRoot!.delegatesFocus).toBe(true)
  573. })
  574. })
  575. describe('emits', () => {
  576. const CompDef = defineVaporComponent({
  577. setup(_, { emit }) {
  578. emit('created')
  579. const n0 = template('<div></div>', true)() as any
  580. n0.$evtclick = () => {
  581. emit('my-click', 1)
  582. }
  583. n0.$evtmousedown = () => {
  584. emit('myEvent', 1) // validate hyphenation
  585. }
  586. on(n0, 'wheel', () => {
  587. emit('my-wheel', { bubbles: true }, 1)
  588. })
  589. return n0
  590. },
  591. })
  592. const E = defineVaporCustomElement(CompDef)
  593. customElements.define('my-el-emits', E)
  594. test('emit on connect', () => {
  595. const e = new E()
  596. const spy = vi.fn()
  597. e.addEventListener('created', spy)
  598. container.appendChild(e)
  599. expect(spy).toHaveBeenCalled()
  600. })
  601. test('emit on interaction', () => {
  602. container.innerHTML = `<my-el-emits></my-el-emits>`
  603. const e = container.childNodes[0] as VaporElement
  604. const spy = vi.fn()
  605. e.addEventListener('my-click', spy)
  606. // Use click() method which triggers a real click event
  607. // with bubbles: true and composed: true
  608. ;(e.shadowRoot!.childNodes[0] as HTMLElement).click()
  609. expect(spy).toHaveBeenCalledTimes(1)
  610. expect(spy.mock.calls[0][0]).toMatchObject({
  611. detail: [1],
  612. })
  613. })
  614. test('case transform for camelCase event', () => {
  615. container.innerHTML = `<my-el-emits></my-el-emits>`
  616. const e = container.childNodes[0] as VaporElement
  617. const spy1 = vi.fn()
  618. e.addEventListener('myEvent', spy1)
  619. const spy2 = vi.fn()
  620. // emitting myEvent, but listening for my-event. This happens when
  621. // using the custom element in a Vue template
  622. e.addEventListener('my-event', spy2)
  623. e.shadowRoot!.childNodes[0].dispatchEvent(
  624. new CustomEvent('mousedown', {
  625. bubbles: true,
  626. composed: true,
  627. }),
  628. )
  629. expect(spy1).toHaveBeenCalledTimes(1)
  630. expect(spy2).toHaveBeenCalledTimes(1)
  631. })
  632. test('emit from within async component wrapper', async () => {
  633. const p = new Promise<typeof CompDef>(res => res(CompDef as any))
  634. const E = defineVaporCustomElement(defineVaporAsyncComponent(() => p))
  635. customElements.define('my-async-el-emits', E)
  636. container.innerHTML = `<my-async-el-emits></my-async-el-emits>`
  637. const e = container.childNodes[0] as VaporElement
  638. const spy = vi.fn()
  639. e.addEventListener('my-click', spy)
  640. // this feels brittle but seems necessary to reach the node in the DOM.
  641. await customElements.whenDefined('my-async-el-emits')
  642. await nextTick()
  643. await nextTick()
  644. e.shadowRoot!.childNodes[0].dispatchEvent(
  645. new CustomEvent('click', {
  646. bubbles: true,
  647. composed: true,
  648. }),
  649. )
  650. expect(spy).toHaveBeenCalled()
  651. expect(spy.mock.calls[0][0]).toMatchObject({
  652. detail: [1],
  653. })
  654. })
  655. test('emit in an async component wrapper with properties bound', async () => {
  656. const E = defineVaporCustomElement(
  657. defineVaporAsyncComponent(
  658. () => new Promise<typeof CompDef>(res => res(CompDef as any)),
  659. ),
  660. )
  661. customElements.define('my-async-el-props-emits', E)
  662. container.innerHTML = `<my-async-el-props-emits id="my_async_el_props_emits"></my-async-el-props-emits>`
  663. const e = container.childNodes[0] as VaporElement
  664. const spy = vi.fn()
  665. e.addEventListener('my-click', spy)
  666. await customElements.whenDefined('my-async-el-props-emits')
  667. await nextTick()
  668. await nextTick()
  669. e.shadowRoot!.childNodes[0].dispatchEvent(
  670. new CustomEvent('click', {
  671. bubbles: true,
  672. composed: true,
  673. }),
  674. )
  675. expect(spy).toHaveBeenCalled()
  676. expect(spy.mock.calls[0][0]).toMatchObject({
  677. detail: [1],
  678. })
  679. })
  680. test('emit with options', async () => {
  681. container.innerHTML = `<my-el-emits></my-el-emits>`
  682. const e = container.childNodes[0] as VaporElement
  683. const spy = vi.fn()
  684. e.addEventListener('my-wheel', spy)
  685. e.shadowRoot!.childNodes[0].dispatchEvent(
  686. new CustomEvent('wheel', {
  687. bubbles: true,
  688. composed: true,
  689. }),
  690. )
  691. expect(spy).toHaveBeenCalledTimes(1)
  692. expect(spy.mock.calls[0][0]).toMatchObject({
  693. bubbles: true,
  694. detail: [{ bubbles: true }, 1],
  695. })
  696. })
  697. })
  698. describe('slots', () => {
  699. const E = defineVaporCustomElement({
  700. setup() {
  701. const t0 = template('<div>fallback</div>')
  702. const t1 = template('<div></div>')
  703. const n3 = t1() as any
  704. setInsertionState(n3, null, true)
  705. createSlot('default', null, () => {
  706. const n2 = t0()
  707. return n2
  708. })
  709. const n5 = t1() as any
  710. setInsertionState(n5, null, true)
  711. createSlot('named', null)
  712. return [n3, n5]
  713. },
  714. })
  715. customElements.define('my-el-slots', E)
  716. test('render slots correctly', () => {
  717. container.innerHTML = `<my-el-slots><span>hi</span></my-el-slots>`
  718. const e = container.childNodes[0] as VaporElement
  719. // native slots allocation does not affect innerHTML, so we just
  720. // verify that we've rendered the correct native slots here...
  721. expect(e.shadowRoot!.innerHTML).toBe(
  722. `<div>` +
  723. `<slot><div>fallback</div></slot><!--slot-->` +
  724. `</div>` +
  725. `<div>` +
  726. `<slot name="named"></slot><!--slot-->` +
  727. `</div>`,
  728. )
  729. })
  730. test('render slot props', async () => {
  731. const foo = ref('foo')
  732. const E = defineVaporCustomElement({
  733. setup() {
  734. const n0 = template('<div></div>')() as any
  735. setInsertionState(n0, null)
  736. createSlot('default', { class: () => foo.value })
  737. return [n0]
  738. },
  739. })
  740. customElements.define('my-el-slot-props', E)
  741. container.innerHTML = `<my-el-slot-props><span>hi</span></my-el-slot-props>`
  742. const e = container.childNodes[0] as VaporElement
  743. expect(e.shadowRoot!.innerHTML).toBe(
  744. `<div><slot class="foo"></slot><!--slot--></div>`,
  745. )
  746. foo.value = 'bar'
  747. await nextTick()
  748. expect(e.shadowRoot!.innerHTML).toBe(
  749. `<div><slot class="bar"></slot><!--slot--></div>`,
  750. )
  751. })
  752. })
  753. describe('provide/inject', () => {
  754. const Consumer = defineVaporCustomElement({
  755. setup() {
  756. const foo = inject<Ref>('foo')!
  757. const n0 = template('<div> </div>', true)() as any
  758. const x0 = txt(n0) as any
  759. renderEffect(() => setText(x0, toDisplayString(foo.value)))
  760. return n0
  761. },
  762. })
  763. customElements.define('my-consumer', Consumer)
  764. test('over nested usage', async () => {
  765. const foo = ref('injected!')
  766. const Provider = defineVaporCustomElement({
  767. setup() {
  768. provide('foo', foo)
  769. return createPlainElement('my-consumer')
  770. },
  771. })
  772. customElements.define('my-provider', Provider)
  773. container.innerHTML = `<my-provider><my-provider>`
  774. const provider = container.childNodes[0] as VaporElement
  775. const consumer = provider.shadowRoot!.childNodes[0] as VaporElement
  776. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`)
  777. foo.value = 'changed!'
  778. await nextTick()
  779. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
  780. })
  781. test('over slot composition', async () => {
  782. const foo = ref('injected!')
  783. const Provider = defineVaporCustomElement({
  784. setup() {
  785. provide('foo', foo)
  786. return createSlot('default', null)
  787. },
  788. })
  789. customElements.define('my-provider-2', Provider)
  790. container.innerHTML = `<my-provider-2><my-consumer></my-consumer><my-provider-2>`
  791. const provider = container.childNodes[0]
  792. const consumer = provider.childNodes[0] as VaporElement
  793. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`)
  794. foo.value = 'changed!'
  795. await nextTick()
  796. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
  797. })
  798. test('inherited from ancestors', async () => {
  799. const fooA = ref('FooA!')
  800. const fooB = ref('FooB!')
  801. const ProviderA = defineVaporCustomElement({
  802. setup() {
  803. provide('fooA', fooA)
  804. return createPlainElement('provider-b')
  805. },
  806. })
  807. const ProviderB = defineVaporCustomElement({
  808. setup() {
  809. provide('fooB', fooB)
  810. return createPlainElement('my-multi-consumer')
  811. },
  812. })
  813. const Consumer = defineVaporCustomElement({
  814. setup() {
  815. const fooA = inject<Ref>('fooA')!
  816. const fooB = inject<Ref>('fooB')!
  817. const n0 = template('<div> </div>', true)() as any
  818. const x0 = txt(n0) as any
  819. renderEffect(() => setText(x0, `${fooA.value} ${fooB.value}`))
  820. return n0
  821. },
  822. })
  823. customElements.define('provider-a', ProviderA)
  824. customElements.define('provider-b', ProviderB)
  825. customElements.define('my-multi-consumer', Consumer)
  826. container.innerHTML = `<provider-a><provider-a>`
  827. const providerA = container.childNodes[0] as VaporElement
  828. const providerB = providerA.shadowRoot!.childNodes[0] as VaporElement
  829. const consumer = providerB.shadowRoot!.childNodes[0] as VaporElement
  830. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>FooA! FooB!</div>`)
  831. fooA.value = 'changedA!'
  832. fooB.value = 'changedB!'
  833. await nextTick()
  834. expect(consumer.shadowRoot!.innerHTML).toBe(
  835. `<div>changedA! changedB!</div>`,
  836. )
  837. })
  838. test('inherited from app context within nested elements', async () => {
  839. const outerValues: (string | undefined)[] = []
  840. const innerValues: (string | undefined)[] = []
  841. const innerChildValues: (string | undefined)[] = []
  842. const Outer = defineVaporCustomElement(
  843. {
  844. setup() {
  845. outerValues.push(
  846. inject<string>('shared'),
  847. inject<string>('outer'),
  848. inject<string>('inner'),
  849. )
  850. const n0 = template('<div></div>', true)() as any
  851. setInsertionState(n0, null)
  852. createSlot('default', null)
  853. return n0
  854. },
  855. },
  856. {
  857. configureApp(app: any) {
  858. app.provide('shared', 'shared')
  859. app.provide('outer', 'outer')
  860. },
  861. } as any,
  862. )
  863. const Inner = defineVaporCustomElement(
  864. {
  865. setup() {
  866. // ensure values are not self-injected
  867. provide('inner', 'inner-child')
  868. innerValues.push(
  869. inject<string>('shared'),
  870. inject<string>('outer'),
  871. inject<string>('inner'),
  872. )
  873. const n0 = template('<div></div>', true)() as any
  874. setInsertionState(n0, null)
  875. createSlot('default', null)
  876. return n0
  877. },
  878. },
  879. {
  880. configureApp(app: any) {
  881. app.provide('outer', 'override-outer')
  882. app.provide('inner', 'inner')
  883. },
  884. } as any,
  885. )
  886. const InnerChild = defineVaporCustomElement({
  887. setup() {
  888. innerChildValues.push(
  889. inject<string>('shared'),
  890. inject<string>('outer'),
  891. inject<string>('inner'),
  892. )
  893. const n0 = template('<div></div>', true)() as any
  894. return n0
  895. },
  896. })
  897. customElements.define('provide-from-app-outer', Outer)
  898. customElements.define('provide-from-app-inner', Inner)
  899. customElements.define('provide-from-app-inner-child', InnerChild)
  900. container.innerHTML =
  901. '<provide-from-app-outer>' +
  902. '<provide-from-app-inner>' +
  903. '<provide-from-app-inner-child></provide-from-app-inner-child>' +
  904. '</provide-from-app-inner>' +
  905. '</provide-from-app-outer>'
  906. const outer = container.childNodes[0] as VaporElement
  907. expect(outer.shadowRoot!.innerHTML).toBe(
  908. '<div><slot></slot><!--slot--></div>',
  909. )
  910. expect('[Vue warn]: injection "inner" not found.').toHaveBeenWarnedTimes(
  911. 1,
  912. )
  913. expect(
  914. '[Vue warn]: App already provides property with key "outer" inherited from its parent element. ' +
  915. 'It will be overwritten with the new value.',
  916. ).toHaveBeenWarnedTimes(1)
  917. expect(outerValues).toEqual(['shared', 'outer', undefined])
  918. expect(innerValues).toEqual(['shared', 'override-outer', 'inner'])
  919. expect(innerChildValues).toEqual([
  920. 'shared',
  921. 'override-outer',
  922. 'inner-child',
  923. ])
  924. })
  925. })
  926. describe('styles', () => {
  927. function assertStyles(el: VaporElement, css: string[]) {
  928. const styles = el.shadowRoot?.querySelectorAll('style')!
  929. expect(styles.length).toBe(css.length) // should not duplicate multiple copies from Bar
  930. for (let i = 0; i < css.length; i++) {
  931. expect(styles[i].textContent).toBe(css[i])
  932. }
  933. }
  934. test('should attach styles to shadow dom', async () => {
  935. const def = defineVaporComponent({
  936. __hmrId: 'foo',
  937. styles: [`div { color: red; }`],
  938. setup() {
  939. return template('<div>hello</div>', true)()
  940. },
  941. } as any)
  942. const Foo = defineVaporCustomElement(def)
  943. customElements.define('my-el-with-styles', Foo)
  944. container.innerHTML = `<my-el-with-styles></my-el-with-styles>`
  945. const el = container.childNodes[0] as VaporElement
  946. const style = el.shadowRoot?.querySelector('style')!
  947. expect(style.textContent).toBe(`div { color: red; }`)
  948. // hmr
  949. __VUE_HMR_RUNTIME__.reload('foo', {
  950. ...def,
  951. styles: [`div { color: blue; }`, `div { color: yellow; }`],
  952. } as any)
  953. await nextTick()
  954. assertStyles(el, [`div { color: blue; }`, `div { color: yellow; }`])
  955. })
  956. test("child components should inject styles to root element's shadow root", async () => {
  957. const Baz = () => createComponent(Bar)
  958. const Bar = defineVaporComponent({
  959. __hmrId: 'bar',
  960. styles: [`div { color: green; }`, `div { color: blue; }`],
  961. setup() {
  962. return template('bar')()
  963. },
  964. } as any)
  965. const Foo = defineVaporCustomElement({
  966. styles: [`div { color: red; }`],
  967. setup() {
  968. return [createComponent(Baz), createComponent(Baz)]
  969. },
  970. })
  971. customElements.define('my-el-with-child-styles', Foo)
  972. container.innerHTML = `<my-el-with-child-styles></my-el-with-child-styles>`
  973. const el = container.childNodes[0] as VaporElement
  974. // inject order should be child -> parent
  975. assertStyles(el, [
  976. `div { color: green; }`,
  977. `div { color: blue; }`,
  978. `div { color: red; }`,
  979. ])
  980. // hmr
  981. __VUE_HMR_RUNTIME__.reload(Bar.__hmrId!, {
  982. ...Bar,
  983. styles: [`div { color: red; }`, `div { color: yellow; }`],
  984. } as any)
  985. await nextTick()
  986. assertStyles(el, [
  987. `div { color: red; }`,
  988. `div { color: yellow; }`,
  989. `div { color: red; }`,
  990. ])
  991. __VUE_HMR_RUNTIME__.reload(Bar.__hmrId!, {
  992. ...Bar,
  993. styles: [`div { color: blue; }`],
  994. } as any)
  995. await nextTick()
  996. assertStyles(el, [`div { color: blue; }`, `div { color: red; }`])
  997. })
  998. test("child components should not inject styles to root element's shadow root w/ shadowRoot false", async () => {
  999. const Bar = defineVaporComponent({
  1000. styles: [`div { color: green; }`],
  1001. setup() {
  1002. return template('bar')()
  1003. },
  1004. } as any)
  1005. const Baz = () => createComponent(Bar)
  1006. const Foo = defineVaporCustomElement(
  1007. {
  1008. setup() {
  1009. return [createComponent(Baz)]
  1010. },
  1011. },
  1012. { shadowRoot: false } as any,
  1013. )
  1014. customElements.define('my-foo-with-shadowroot-false', Foo)
  1015. container.innerHTML = `<my-foo-with-shadowroot-false></my-foo-with-shadowroot-false>`
  1016. const el = container.childNodes[0] as VaporElement
  1017. const style = el.shadowRoot?.querySelector('style')
  1018. expect(style).toBeUndefined()
  1019. })
  1020. test('with nonce', () => {
  1021. const Foo = defineVaporCustomElement(
  1022. {
  1023. styles: [`div { color: red; }`],
  1024. setup() {
  1025. return template('<div>hello</div>', true)()
  1026. },
  1027. },
  1028. { nonce: 'xxx' } as any,
  1029. )
  1030. customElements.define('my-el-with-nonce', Foo)
  1031. container.innerHTML = `<my-el-with-nonce></my-el-with-nonce>`
  1032. const el = container.childNodes[0] as VaporElement
  1033. const style = el.shadowRoot?.querySelector('style')!
  1034. expect(style.getAttribute('nonce')).toBe('xxx')
  1035. })
  1036. })
  1037. describe('async', () => {
  1038. test('should work', async () => {
  1039. const loaderSpy = vi.fn()
  1040. const E = defineVaporCustomElement(
  1041. defineVaporAsyncComponent(() => {
  1042. loaderSpy()
  1043. return Promise.resolve({
  1044. props: ['msg'],
  1045. styles: [`div { color: red }`],
  1046. setup(props: any) {
  1047. const n0 = template('<div> </div>', true)() as any
  1048. const x0 = txt(n0) as any
  1049. renderEffect(() => setText(x0, props.msg))
  1050. return n0
  1051. },
  1052. })
  1053. }),
  1054. )
  1055. customElements.define('my-el-async', E)
  1056. container.innerHTML =
  1057. `<my-el-async msg="hello"></my-el-async>` +
  1058. `<my-el-async msg="world"></my-el-async>`
  1059. await new Promise(r => setTimeout(r))
  1060. // loader should be called only once
  1061. expect(loaderSpy).toHaveBeenCalledTimes(1)
  1062. const e1 = container.childNodes[0] as VaporElement
  1063. const e2 = container.childNodes[1] as VaporElement
  1064. // should inject styles
  1065. expect(e1.shadowRoot!.innerHTML).toBe(
  1066. `<style>div { color: red }</style><div>hello</div>`,
  1067. )
  1068. expect(e2.shadowRoot!.innerHTML).toBe(
  1069. `<style>div { color: red }</style><div>world</div>`,
  1070. )
  1071. // attr
  1072. e1.setAttribute('msg', 'attr')
  1073. await nextTick()
  1074. expect((e1 as any).msg).toBe('attr')
  1075. expect(e1.shadowRoot!.innerHTML).toBe(
  1076. `<style>div { color: red }</style><div>attr</div>`,
  1077. )
  1078. // props
  1079. expect(`msg` in e1).toBe(true)
  1080. ;(e1 as any).msg = 'prop'
  1081. expect(e1.getAttribute('msg')).toBe('prop')
  1082. expect(e1.shadowRoot!.innerHTML).toBe(
  1083. `<style>div { color: red }</style><div>prop</div>`,
  1084. )
  1085. })
  1086. test('set DOM property before resolve', async () => {
  1087. const E = defineVaporCustomElement(
  1088. defineVaporAsyncComponent(() => {
  1089. return Promise.resolve({
  1090. props: ['msg'],
  1091. setup(props: any) {
  1092. expect(typeof props.msg).toBe('string')
  1093. const n0 = template('<div> </div>', true)() as any
  1094. const x0 = txt(n0) as any
  1095. renderEffect(() => setText(x0, props.msg))
  1096. return n0
  1097. },
  1098. })
  1099. }),
  1100. )
  1101. customElements.define('my-el-async-2', E)
  1102. const e1 = new E() as any
  1103. // set property before connect
  1104. e1.msg = 'hello'
  1105. const e2 = new E() as any
  1106. container.appendChild(e1)
  1107. container.appendChild(e2)
  1108. // set property after connect but before resolve
  1109. e2.msg = 'world'
  1110. await new Promise(r => setTimeout(r))
  1111. expect(e1.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  1112. expect(e2.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  1113. e1.msg = 'world'
  1114. expect(e1.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  1115. e2.msg = 'hello'
  1116. expect(e2.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  1117. })
  1118. test('Number prop casting before resolve', async () => {
  1119. const E = defineVaporCustomElement(
  1120. defineVaporAsyncComponent(() => {
  1121. return Promise.resolve({
  1122. props: { n: Number },
  1123. setup(props: any) {
  1124. expect(props.n).toBe(20)
  1125. const n0 = template('<div> </div>', true)() as any
  1126. const x0 = txt(n0) as any
  1127. renderEffect(() => setText(x0, `${props.n},${typeof props.n}`))
  1128. return n0
  1129. },
  1130. })
  1131. }),
  1132. )
  1133. customElements.define('my-el-async-3', E)
  1134. container.innerHTML = `<my-el-async-3 n="2e1"></my-el-async-3>`
  1135. await new Promise(r => setTimeout(r))
  1136. const e = container.childNodes[0] as VaporElement
  1137. expect(e.shadowRoot!.innerHTML).toBe(`<div>20,number</div>`)
  1138. })
  1139. test('with slots', async () => {
  1140. const E = defineVaporCustomElement(
  1141. defineVaporAsyncComponent(() => {
  1142. return Promise.resolve({
  1143. setup() {
  1144. const t0 = template('<div>fallback</div>')
  1145. const t1 = template('<div></div>')
  1146. const n3 = t1() as any
  1147. setInsertionState(n3, null)
  1148. createSlot('default', null, () => {
  1149. const n2 = t0()
  1150. return n2
  1151. })
  1152. const n5 = t1() as any
  1153. setInsertionState(n5, null)
  1154. createSlot('named', null)
  1155. return [n3, n5]
  1156. },
  1157. })
  1158. }),
  1159. )
  1160. customElements.define('my-el-async-slots', E)
  1161. container.innerHTML = `<my-el-async-slots><span>hi</span></my-el-async-slots>`
  1162. await new Promise(r => setTimeout(r))
  1163. const e = container.childNodes[0] as VaporElement
  1164. expect(e.shadowRoot!.innerHTML).toBe(
  1165. `<div>` +
  1166. `<slot><div>fallback</div></slot><!--slot-->` +
  1167. `</div><div>` +
  1168. `<slot name="named"></slot><!--slot-->` +
  1169. `</div>`,
  1170. )
  1171. })
  1172. })
  1173. describe('shadowRoot: false', () => {
  1174. const E = defineVaporCustomElement({
  1175. shadowRoot: false,
  1176. props: {
  1177. msg: {
  1178. type: String,
  1179. default: 'hello',
  1180. },
  1181. },
  1182. setup(props: any) {
  1183. const n0 = template('<div> </div>')() as any
  1184. const x0 = txt(n0) as any
  1185. renderEffect(() => setText(x0, toDisplayString(props.msg)))
  1186. return n0
  1187. },
  1188. })
  1189. customElements.define('my-el-shadowroot-false', E)
  1190. test('should work', async () => {
  1191. function raf() {
  1192. return new Promise(resolve => {
  1193. requestAnimationFrame(resolve)
  1194. })
  1195. }
  1196. container.innerHTML = `<my-el-shadowroot-false></my-el-shadowroot-false>`
  1197. const e = container.childNodes[0] as VaporElement
  1198. await raf()
  1199. expect(e).toBeInstanceOf(E)
  1200. expect(e._instance).toBeTruthy()
  1201. expect(e.innerHTML).toBe(`<div>hello</div>`)
  1202. expect(e.shadowRoot).toBe(null)
  1203. })
  1204. const toggle = ref(true)
  1205. const ES = defineVaporCustomElement(
  1206. {
  1207. setup() {
  1208. const n0 = createSlot('default')
  1209. const n1 = createIf(
  1210. () => toggle.value,
  1211. () => createSlot('named'),
  1212. )
  1213. const n2 = createSlot('omitted', null, () =>
  1214. template('<div>fallback</div>')(),
  1215. )
  1216. return [n0, n1, n2]
  1217. },
  1218. },
  1219. { shadowRoot: false } as any,
  1220. )
  1221. customElements.define('my-el-shadowroot-false-slots', ES)
  1222. test('should render slots', async () => {
  1223. container.innerHTML =
  1224. `<my-el-shadowroot-false-slots>` +
  1225. `<span>default</span>text` +
  1226. `<div slot="named">named</div>` +
  1227. `</my-el-shadowroot-false-slots>`
  1228. const e = container.childNodes[0] as VaporElement
  1229. // native slots allocation does not affect innerHTML, so we just
  1230. // verify that we've rendered the correct native slots here...
  1231. expect(e.innerHTML).toBe(
  1232. `<span>default</span>text<!--slot-->` +
  1233. `<div slot="named">named</div><!--slot--><!--if-->` +
  1234. `<div>fallback</div><!--slot-->`,
  1235. )
  1236. toggle.value = false
  1237. await nextTick()
  1238. expect(e.innerHTML).toBe(
  1239. `<span>default</span>text<!--slot-->` +
  1240. `<!--if-->` +
  1241. `<div>fallback</div><!--slot-->`,
  1242. )
  1243. })
  1244. test('render nested customElement w/ shadowRoot false', async () => {
  1245. const calls: string[] = []
  1246. const Child = defineVaporCustomElement(
  1247. {
  1248. setup() {
  1249. calls.push('child rendering')
  1250. onMounted(() => {
  1251. calls.push('child mounted')
  1252. })
  1253. return createSlot('default')
  1254. },
  1255. },
  1256. { shadowRoot: false } as any,
  1257. )
  1258. customElements.define('my-child', Child)
  1259. const Parent = defineVaporCustomElement(
  1260. {
  1261. setup() {
  1262. calls.push('parent rendering')
  1263. onMounted(() => {
  1264. calls.push('parent mounted')
  1265. })
  1266. return createSlot('default')
  1267. },
  1268. },
  1269. { shadowRoot: false } as any,
  1270. )
  1271. customElements.define('my-parent', Parent)
  1272. const App = {
  1273. setup() {
  1274. return createPlainElement('my-parent', null, {
  1275. default: withVaporCtx(() =>
  1276. createPlainElement('my-child', null, {
  1277. default: withVaporCtx(() => template('<span>default</span>')()),
  1278. }),
  1279. ),
  1280. })
  1281. },
  1282. }
  1283. const app = createVaporApp(App)
  1284. app.mount(container)
  1285. await nextTick()
  1286. const e = container.childNodes[0] as VaporElement
  1287. expect(e.innerHTML).toBe(
  1288. `<my-child data-v-app=""><span>default</span><!--slot--></my-child><!--slot-->`,
  1289. )
  1290. expect(calls).toEqual([
  1291. 'parent rendering',
  1292. 'parent mounted',
  1293. 'child rendering',
  1294. 'child mounted',
  1295. ])
  1296. app.unmount()
  1297. })
  1298. test('render nested Teleport w/ shadowRoot false', async () => {
  1299. const target = document.createElement('div')
  1300. const Child = defineVaporCustomElement(
  1301. {
  1302. setup() {
  1303. return createComponent(
  1304. VaporTeleport,
  1305. { to: () => target },
  1306. {
  1307. default: withVaporCtx(() => createSlot('default')),
  1308. },
  1309. )
  1310. },
  1311. },
  1312. { shadowRoot: false } as any,
  1313. )
  1314. customElements.define('my-el-teleport-child', Child)
  1315. const Parent = defineVaporCustomElement(
  1316. {
  1317. setup() {
  1318. return createSlot('default')
  1319. },
  1320. },
  1321. { shadowRoot: false } as any,
  1322. )
  1323. customElements.define('my-el-teleport-parent', Parent)
  1324. const App = {
  1325. setup() {
  1326. return createPlainElement('my-el-teleport-parent', null, {
  1327. default: withVaporCtx(() =>
  1328. createPlainElement('my-el-teleport-child', null, {
  1329. default: withVaporCtx(() => template('<span>default</span>')()),
  1330. }),
  1331. ),
  1332. })
  1333. },
  1334. }
  1335. const app = createVaporApp(App)
  1336. app.mount(container)
  1337. await nextTick()
  1338. expect(target.innerHTML).toBe(`<span>default</span><!--slot-->`)
  1339. app.unmount()
  1340. })
  1341. test('render two Teleports w/ shadowRoot false', async () => {
  1342. const target1 = document.createElement('div')
  1343. const target2 = document.createElement('span')
  1344. const Child = defineVaporCustomElement(
  1345. {
  1346. setup() {
  1347. return [
  1348. createComponent(
  1349. VaporTeleport,
  1350. { to: () => target1 },
  1351. {
  1352. default: withVaporCtx(() => createSlot('header')),
  1353. },
  1354. ),
  1355. createComponent(
  1356. VaporTeleport,
  1357. { to: () => target2 },
  1358. {
  1359. default: withVaporCtx(() => createSlot('body')),
  1360. },
  1361. ),
  1362. ]
  1363. },
  1364. },
  1365. { shadowRoot: false } as any,
  1366. )
  1367. customElements.define('my-el-two-teleport-child', Child)
  1368. const App = {
  1369. setup() {
  1370. return createPlainElement('my-el-two-teleport-child', null, {
  1371. default: withVaporCtx(() => [
  1372. template('<div slot="header">header</div>')(),
  1373. template('<span slot="body">body</span>')(),
  1374. ]),
  1375. })
  1376. },
  1377. }
  1378. const app = createVaporApp(App)
  1379. app.mount(container)
  1380. await nextTick()
  1381. expect(target1.outerHTML).toBe(
  1382. `<div><div slot="header">header</div><!--slot--></div>`,
  1383. )
  1384. expect(target2.outerHTML).toBe(
  1385. `<span><span slot="body">body</span><!--slot--></span>`,
  1386. )
  1387. app.unmount()
  1388. })
  1389. test('render two Teleports w/ shadowRoot false (with disabled)', async () => {
  1390. const target1 = document.createElement('div')
  1391. const target2 = document.createElement('span')
  1392. const Child = defineVaporCustomElement(
  1393. {
  1394. setup() {
  1395. return [
  1396. createComponent(
  1397. VaporTeleport,
  1398. // with disabled: true
  1399. { to: () => target1, disabled: () => true },
  1400. {
  1401. default: withVaporCtx(() => createSlot('header')),
  1402. },
  1403. ),
  1404. createComponent(
  1405. VaporTeleport,
  1406. { to: () => target2 },
  1407. {
  1408. default: withVaporCtx(() => createSlot('body')),
  1409. },
  1410. ),
  1411. ]
  1412. },
  1413. },
  1414. { shadowRoot: false } as any,
  1415. )
  1416. customElements.define('my-el-two-teleport-child-0', Child)
  1417. const App = {
  1418. setup() {
  1419. return createPlainElement('my-el-two-teleport-child-0', null, {
  1420. default: withVaporCtx(() => [
  1421. template('<div slot="header">header</div>')(),
  1422. template('<span slot="body">body</span>')(),
  1423. ]),
  1424. })
  1425. },
  1426. }
  1427. const app = createVaporApp(App)
  1428. app.mount(container)
  1429. await nextTick()
  1430. expect(target1.outerHTML).toBe(`<div></div>`)
  1431. expect(target2.outerHTML).toBe(
  1432. `<span><span slot="body">body</span><!--slot--></span>`,
  1433. )
  1434. app.unmount()
  1435. })
  1436. test('toggle nested custom element with shadowRoot: false', async () => {
  1437. customElements.define(
  1438. 'my-el-child-shadow-false',
  1439. defineVaporCustomElement(
  1440. {
  1441. setup() {
  1442. const n0 = template('<div></div>')() as any
  1443. setInsertionState(n0, null)
  1444. createSlot('default', null)
  1445. return n0
  1446. },
  1447. },
  1448. { shadowRoot: false } as any,
  1449. ),
  1450. )
  1451. const ChildWrapper = {
  1452. setup() {
  1453. return createPlainElement('my-el-child-shadow-false', null, {
  1454. default: withVaporCtx(() => template('child')()),
  1455. })
  1456. },
  1457. }
  1458. customElements.define(
  1459. 'my-el-parent-shadow-false',
  1460. defineVaporCustomElement(
  1461. {
  1462. props: {
  1463. isShown: { type: Boolean, required: true },
  1464. },
  1465. setup(props: any) {
  1466. return createIf(
  1467. () => props.isShown,
  1468. () => {
  1469. const n0 = template('<div></div>')() as any
  1470. setInsertionState(n0, null)
  1471. createSlot('default', null)
  1472. return n0
  1473. },
  1474. )
  1475. },
  1476. },
  1477. { shadowRoot: false } as any,
  1478. ),
  1479. )
  1480. const ParentWrapper = {
  1481. props: {
  1482. isShown: { type: Boolean, required: true },
  1483. },
  1484. setup(props: any) {
  1485. return createPlainElement(
  1486. 'my-el-parent-shadow-false',
  1487. { isShown: () => props.isShown },
  1488. {
  1489. default: withVaporCtx(() => createSlot('default')),
  1490. },
  1491. )
  1492. },
  1493. }
  1494. const isShown = ref(true)
  1495. const App = {
  1496. setup() {
  1497. return createComponent(
  1498. ParentWrapper,
  1499. { isShown: () => isShown.value },
  1500. {
  1501. default: withVaporCtx(() => createComponent(ChildWrapper)),
  1502. },
  1503. )
  1504. },
  1505. }
  1506. const container = document.createElement('div')
  1507. document.body.appendChild(container)
  1508. const app = createVaporApp(App)
  1509. app.mount(container)
  1510. expect(container.innerHTML).toBe(
  1511. `<my-el-parent-shadow-false is-shown="" data-v-app="">` +
  1512. `<div>` +
  1513. `<my-el-child-shadow-false data-v-app="">` +
  1514. `<div>child<!--slot--></div>` +
  1515. `</my-el-child-shadow-false><!--slot--><!--slot-->` +
  1516. `</div><!--if-->` +
  1517. `</my-el-parent-shadow-false>`,
  1518. )
  1519. isShown.value = false
  1520. await nextTick()
  1521. expect(container.innerHTML).toBe(
  1522. `<my-el-parent-shadow-false data-v-app=""><!--if--></my-el-parent-shadow-false>`,
  1523. )
  1524. isShown.value = true
  1525. await nextTick()
  1526. expect(container.innerHTML).toBe(
  1527. `<my-el-parent-shadow-false data-v-app="" is-shown="">` +
  1528. `<div>` +
  1529. `<my-el-child-shadow-false data-v-app="">` +
  1530. `<div>child<!--slot--></div>` +
  1531. `</my-el-child-shadow-false><!--slot--><!--slot-->` +
  1532. `</div><!--if-->` +
  1533. `</my-el-parent-shadow-false>`,
  1534. )
  1535. })
  1536. })
  1537. describe('helpers', () => {
  1538. test('useHost', () => {
  1539. const Foo = defineVaporCustomElement({
  1540. setup() {
  1541. const host = useHost()!
  1542. host.setAttribute('id', 'host')
  1543. return template('<div>hello</div>')()
  1544. },
  1545. })
  1546. customElements.define('my-el-use-host', Foo)
  1547. container.innerHTML = `<my-el-use-host>`
  1548. const el = container.childNodes[0] as VaporElement
  1549. expect(el.id).toBe('host')
  1550. })
  1551. test('useShadowRoot for style injection', () => {
  1552. const Foo = defineVaporCustomElement({
  1553. setup() {
  1554. const root = useShadowRoot()!
  1555. const style = document.createElement('style')
  1556. style.innerHTML = `div { color: red; }`
  1557. root.appendChild(style)
  1558. return template('<div>hello</div>')()
  1559. },
  1560. })
  1561. customElements.define('my-el-use-shadow-root', Foo)
  1562. container.innerHTML = `<my-el-use-shadow-root>`
  1563. const el = container.childNodes[0] as VaporElement
  1564. const style = el.shadowRoot?.querySelector('style')!
  1565. expect(style.textContent).toBe(`div { color: red; }`)
  1566. })
  1567. })
  1568. describe('expose', () => {
  1569. test('expose w/ options api', async () => {
  1570. const E = defineVaporCustomElement({
  1571. setup(_: any, { expose }: any) {
  1572. const value = ref(0)
  1573. const foo = () => {
  1574. value.value++
  1575. }
  1576. expose({ foo })
  1577. const n0 = template('<div> </div>', true)() as any
  1578. const x0 = txt(n0) as any
  1579. renderEffect(() => setText(x0, `${value.value}`))
  1580. return n0
  1581. },
  1582. })
  1583. customElements.define('my-el-expose-options-api', E)
  1584. container.innerHTML = `<my-el-expose-options-api></my-el-expose-options-api>`
  1585. const e = container.childNodes[0] as VaporElement & {
  1586. foo: () => void
  1587. }
  1588. expect(e.shadowRoot!.innerHTML).toBe(`<div>0</div>`)
  1589. e.foo()
  1590. await nextTick()
  1591. expect(e.shadowRoot!.innerHTML).toBe(`<div>1</div>`)
  1592. })
  1593. test('expose attributes and callback', async () => {
  1594. type SetValue = (value: string) => void
  1595. let fn: MockedFunction<SetValue>
  1596. const E = defineVaporCustomElement({
  1597. setup(_: any, { expose }: any) {
  1598. const value = ref('hello')
  1599. const setValue = (fn = vi.fn((_value: string) => {
  1600. value.value = _value
  1601. }))
  1602. expose({
  1603. setValue,
  1604. value,
  1605. })
  1606. const n0 = template('<div> </div>', true)() as any
  1607. const x0 = txt(n0) as any
  1608. renderEffect(() => setText(x0, value.value))
  1609. return n0
  1610. },
  1611. })
  1612. customElements.define('my-el-expose', E)
  1613. container.innerHTML = `<my-el-expose></my-el-expose>`
  1614. const e = container.childNodes[0] as VaporElement & {
  1615. value: string
  1616. setValue: MockedFunction<SetValue>
  1617. }
  1618. expect(e.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  1619. expect(e.value).toBe('hello')
  1620. expect(e.setValue).toBe(fn!)
  1621. e.setValue('world')
  1622. expect(e.value).toBe('world')
  1623. await nextTick()
  1624. expect(e.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  1625. })
  1626. test('warning when exposing an existing property', () => {
  1627. const E = defineVaporCustomElement({
  1628. props: {
  1629. value: String,
  1630. },
  1631. setup(props: any, { expose }: any) {
  1632. expose({
  1633. value: 'hello',
  1634. })
  1635. const n0 = template('<div> </div>', true)() as any
  1636. const x0 = txt(n0) as any
  1637. renderEffect(() => setText(x0, props.value))
  1638. return n0
  1639. },
  1640. })
  1641. customElements.define('my-el-expose-two', E)
  1642. container.innerHTML = `<my-el-expose-two value="world"></my-el-expose-two>`
  1643. expect(
  1644. `[Vue warn]: Exposed property "value" already exists on custom element.`,
  1645. ).toHaveBeenWarned()
  1646. })
  1647. })
  1648. test('async & nested custom elements', async () => {
  1649. let fooVal: string | undefined = ''
  1650. const E = defineVaporCustomElement(
  1651. defineVaporAsyncComponent(() => {
  1652. return Promise.resolve({
  1653. setup() {
  1654. provide('foo', 'foo')
  1655. const n0 = template('<div></div>')() as any
  1656. setInsertionState(n0, null)
  1657. createSlot('default', null)
  1658. return n0
  1659. },
  1660. })
  1661. }),
  1662. )
  1663. const EChild = defineVaporCustomElement({
  1664. setup() {
  1665. fooVal = inject('foo')
  1666. const n0 = template('<div>child</div>')()
  1667. return n0
  1668. },
  1669. })
  1670. customElements.define('my-el-async-nested-ce', E)
  1671. customElements.define('slotted-child', EChild)
  1672. container.innerHTML = `<my-el-async-nested-ce><div><slotted-child></slotted-child></div></my-el-async-nested-ce>`
  1673. await new Promise(r => setTimeout(r))
  1674. const e = container.childNodes[0] as VaporElement
  1675. expect(e.shadowRoot!.innerHTML).toBe(`<div><slot></slot><!--slot--></div>`)
  1676. expect(fooVal).toBe('foo')
  1677. })
  1678. test('async & multiple levels of nested custom elements', async () => {
  1679. let fooVal: string | undefined = ''
  1680. let barVal: string | undefined = ''
  1681. const E = defineVaporCustomElement(
  1682. defineVaporAsyncComponent(() => {
  1683. return Promise.resolve({
  1684. setup() {
  1685. provide('foo', 'foo')
  1686. const n0 = template('<div></div>')() as any
  1687. setInsertionState(n0, null)
  1688. createSlot('default', null)
  1689. return n0
  1690. },
  1691. })
  1692. }),
  1693. )
  1694. const EChild = defineVaporCustomElement({
  1695. setup() {
  1696. provide('bar', 'bar')
  1697. const n0 = template('<div></div>')() as any
  1698. setInsertionState(n0, null)
  1699. createSlot('default', null)
  1700. return n0
  1701. },
  1702. })
  1703. const EChild2 = defineVaporCustomElement({
  1704. setup() {
  1705. fooVal = inject('foo')
  1706. barVal = inject('bar')
  1707. const n0 = template('<div>child</div>')()
  1708. return n0
  1709. },
  1710. })
  1711. customElements.define('my-el-async-nested-m-ce', E)
  1712. customElements.define('slotted-child-m', EChild)
  1713. customElements.define('slotted-child2-m', EChild2)
  1714. container.innerHTML =
  1715. `<my-el-async-nested-m-ce>` +
  1716. `<div><slotted-child-m>` +
  1717. `<slotted-child2-m></slotted-child2-m>` +
  1718. `</slotted-child-m></div>` +
  1719. `</my-el-async-nested-m-ce>`
  1720. await new Promise(r => setTimeout(r))
  1721. const e = container.childNodes[0] as VaporElement
  1722. expect(e.shadowRoot!.innerHTML).toBe(`<div><slot></slot><!--slot--></div>`)
  1723. expect(fooVal).toBe('foo')
  1724. expect(barVal).toBe('bar')
  1725. })
  1726. describe('configureApp', () => {
  1727. test('should work', () => {
  1728. const E = defineVaporCustomElement(
  1729. () => {
  1730. const msg = inject('msg')
  1731. const n0 = template('<div> </div>', true)() as any
  1732. const x0 = txt(n0) as any
  1733. renderEffect(() => setText(x0, msg as string))
  1734. return n0
  1735. },
  1736. {
  1737. configureApp(app: any) {
  1738. app.provide('msg', 'app-injected')
  1739. },
  1740. } as any,
  1741. )
  1742. customElements.define('my-element-with-app', E)
  1743. container.innerHTML = `<my-element-with-app></my-element-with-app>`
  1744. const e = container.childNodes[0] as VaporElement
  1745. expect(e.shadowRoot?.innerHTML).toBe('<div>app-injected</div>')
  1746. })
  1747. test('work with async component', async () => {
  1748. const AsyncComp = defineVaporAsyncComponent(() => {
  1749. return Promise.resolve({
  1750. setup() {
  1751. const msg = inject('msg')
  1752. const n0 = template('<div> </div>', true)() as any
  1753. const x0 = txt(n0) as any
  1754. renderEffect(() => setText(x0, msg as string))
  1755. return n0
  1756. },
  1757. } as any)
  1758. })
  1759. const E = defineVaporCustomElement(AsyncComp, {
  1760. configureApp(app: any) {
  1761. app.provide('msg', 'app-injected')
  1762. },
  1763. } as any)
  1764. customElements.define('my-async-element-with-app', E)
  1765. container.innerHTML = `<my-async-element-with-app></my-async-element-with-app>`
  1766. const e = container.childNodes[0] as VaporElement
  1767. await new Promise(r => setTimeout(r))
  1768. expect(e.shadowRoot?.innerHTML).toBe('<div>app-injected</div>')
  1769. })
  1770. test('with hmr reload', async () => {
  1771. const __hmrId = '__hmrWithApp'
  1772. const def = defineVaporComponent({
  1773. __hmrId,
  1774. setup() {
  1775. const msg = inject('msg')
  1776. const n0 = template('<div><span> </span></div>')() as any
  1777. const n1 = child(n0) as any
  1778. const x1 = txt(n1) as any
  1779. renderEffect(() => setText(x1, msg as string))
  1780. return n0
  1781. },
  1782. })
  1783. const E = defineVaporCustomElement(def, {
  1784. configureApp(app: any) {
  1785. app.provide('msg', 'app-injected')
  1786. },
  1787. } as any)
  1788. customElements.define('my-element-with-app-hmr', E)
  1789. container.innerHTML = `<my-element-with-app-hmr></my-element-with-app-hmr>`
  1790. const el = container.childNodes[0] as VaporElement
  1791. expect(el.shadowRoot?.innerHTML).toBe(
  1792. `<div><span>app-injected</span></div>`,
  1793. )
  1794. // hmr
  1795. __VUE_HMR_RUNTIME__.reload(__hmrId, def as any)
  1796. await nextTick()
  1797. expect(el.shadowRoot?.innerHTML).toBe(
  1798. `<div><span>app-injected</span></div>`,
  1799. )
  1800. })
  1801. })
  1802. // #9885
  1803. // test('avoid double mount when prop is set immediately after mount', () => {
  1804. // customElements.define(
  1805. // 'my-input-dupe',
  1806. // defineVaporCustomElement({
  1807. // props: {
  1808. // value: String,
  1809. // },
  1810. // render() {
  1811. // return 'hello'
  1812. // },
  1813. // }),
  1814. // )
  1815. // const container = document.createElement('div')
  1816. // document.body.appendChild(container)
  1817. // createVaporApp({
  1818. // // render() {
  1819. // // return h('div', [
  1820. // // h('my-input-dupe', {
  1821. // // onVnodeMounted(vnode) {
  1822. // // vnode.el!.value = 'fesfes'
  1823. // // },
  1824. // // }),
  1825. // // ])
  1826. // // },
  1827. // setup() {
  1828. // // const n0 = template('<div></div>')() as any
  1829. // }
  1830. // }).mount(container)
  1831. // expect(container.children[0].children[0].shadowRoot?.innerHTML).toBe(
  1832. // 'hello',
  1833. // )
  1834. // })
  1835. test('Props can be casted when mounting custom elements in component rendering functions', async () => {
  1836. const E = defineVaporCustomElement(
  1837. defineVaporAsyncComponent(() =>
  1838. Promise.resolve({
  1839. props: ['fooValue'],
  1840. setup(props: any) {
  1841. expect(props.fooValue).toBe('fooValue')
  1842. const n0 = template('<div> </div>', true)() as any
  1843. const x0 = txt(n0) as any
  1844. renderEffect(() => setText(x0, props.fooValue))
  1845. return n0
  1846. },
  1847. }),
  1848. ),
  1849. )
  1850. customElements.define('my-el-async-4', E)
  1851. const R = defineVaporComponent({
  1852. setup() {
  1853. const fooValue = ref('fooValue')
  1854. const n0 = template('<div></div>')() as any
  1855. setInsertionState(n0, null)
  1856. createPlainElement('my-el-async-4', {
  1857. fooValue: () => fooValue.value,
  1858. })
  1859. return n0
  1860. },
  1861. })
  1862. const app = createVaporApp(R)
  1863. app.mount(container)
  1864. await new Promise(r => setTimeout(r))
  1865. const e = container.querySelector('my-el-async-4') as VaporElement
  1866. expect(e.shadowRoot!.innerHTML).toBe(`<div>fooValue</div>`)
  1867. app.unmount()
  1868. })
  1869. test('delete prop on attr removal', async () => {
  1870. const E = defineVaporCustomElement({
  1871. props: {
  1872. boo: {
  1873. type: Boolean,
  1874. },
  1875. },
  1876. setup(props: any) {
  1877. const n0 = template(' ')() as any
  1878. renderEffect(() => setText(n0, `${props.boo},${typeof props.boo}`))
  1879. return n0
  1880. },
  1881. })
  1882. customElements.define('el-attr-removal', E)
  1883. container.innerHTML = '<el-attr-removal boo>'
  1884. const e = container.childNodes[0] as VaporElement
  1885. expect(e.shadowRoot!.innerHTML).toBe(`true,boolean`)
  1886. e.removeAttribute('boo')
  1887. await nextTick()
  1888. expect(e.shadowRoot!.innerHTML).toBe(`false,boolean`)
  1889. })
  1890. test('hyphenated attr removal', async () => {
  1891. const E = defineVaporCustomElement({
  1892. props: {
  1893. fooBar: {
  1894. type: Boolean,
  1895. },
  1896. },
  1897. setup(props: any) {
  1898. const n0 = template(' ')() as any
  1899. renderEffect(() => setText(n0, toDisplayString(props.fooBar)))
  1900. return n0
  1901. },
  1902. })
  1903. customElements.define('el-hyphenated-attr-removal', E)
  1904. const toggle = ref(true)
  1905. const { container } = render('el-hyphenated-attr-removal', {
  1906. 'foo-bar': () => (toggle.value ? '' : null),
  1907. })
  1908. const el = container.children[0]
  1909. expect(el.hasAttribute('foo-bar')).toBe(true)
  1910. expect((el as any).outerHTML).toBe(
  1911. `<el-hyphenated-attr-removal foo-bar=""></el-hyphenated-attr-removal>`,
  1912. )
  1913. toggle.value = false
  1914. await nextTick()
  1915. expect(el.hasAttribute('foo-bar')).toBe(false)
  1916. expect((el as any).outerHTML).toBe(
  1917. `<el-hyphenated-attr-removal></el-hyphenated-attr-removal>`,
  1918. )
  1919. })
  1920. test('no unexpected mutation of the 1st argument', () => {
  1921. const Foo = {
  1922. __vapor: true,
  1923. name: 'Foo',
  1924. }
  1925. defineVaporCustomElement(Foo, { shadowRoot: false } as any)
  1926. expect(Foo).toEqual({
  1927. __vapor: true,
  1928. name: 'Foo',
  1929. })
  1930. })
  1931. })