customElement.spec.ts 63 KB

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