customElement.spec.ts 42 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424
  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 hyphen property', async () => {
  205. const Comp = defineCustomElement({
  206. props: {
  207. fooBar: Boolean,
  208. },
  209. render() {
  210. return 'Comp'
  211. },
  212. })
  213. customElements.define('my-el-comp', Comp)
  214. render(h('my-el-comp', { 'foo-bar': true }), container)
  215. const el = container.children[0]
  216. expect((el as any).outerHTML).toBe('<my-el-comp foo-bar=""></my-el-comp>')
  217. })
  218. test('attribute -> prop type casting', async () => {
  219. const E = defineCustomElement({
  220. props: {
  221. fooBar: Number, // test casting of camelCase prop names
  222. bar: Boolean,
  223. baz: String,
  224. },
  225. render() {
  226. return [
  227. this.fooBar,
  228. typeof this.fooBar,
  229. this.bar,
  230. typeof this.bar,
  231. this.baz,
  232. typeof this.baz,
  233. ].join(' ')
  234. },
  235. })
  236. customElements.define('my-el-props-cast', E)
  237. container.innerHTML = `<my-el-props-cast foo-bar="1" baz="12345"></my-el-props-cast>`
  238. const e = container.childNodes[0] as VueElement
  239. expect(e.shadowRoot!.innerHTML).toBe(
  240. `1 number false boolean 12345 string`,
  241. )
  242. e.setAttribute('bar', '')
  243. await nextTick()
  244. expect(e.shadowRoot!.innerHTML).toBe(`1 number true boolean 12345 string`)
  245. e.setAttribute('foo-bar', '2e1')
  246. await nextTick()
  247. expect(e.shadowRoot!.innerHTML).toBe(
  248. `20 number true boolean 12345 string`,
  249. )
  250. e.setAttribute('baz', '2e1')
  251. await nextTick()
  252. expect(e.shadowRoot!.innerHTML).toBe(`20 number true boolean 2e1 string`)
  253. })
  254. // #4772
  255. test('attr casting w/ programmatic creation', () => {
  256. const E = defineCustomElement({
  257. props: {
  258. foo: Number,
  259. },
  260. render() {
  261. return `foo type: ${typeof this.foo}`
  262. },
  263. })
  264. customElements.define('my-element-programmatic', E)
  265. const el = document.createElement('my-element-programmatic') as any
  266. el.setAttribute('foo', '123')
  267. container.appendChild(el)
  268. expect(el.shadowRoot.innerHTML).toBe(`foo type: number`)
  269. })
  270. test('handling properties set before upgrading', () => {
  271. const E = defineCustomElement({
  272. props: {
  273. foo: String,
  274. dataAge: Number,
  275. },
  276. setup(props) {
  277. expect(props.foo).toBe('hello')
  278. expect(props.dataAge).toBe(5)
  279. },
  280. render() {
  281. return h('div', `foo: ${this.foo}`)
  282. },
  283. })
  284. const el = document.createElement('my-el-upgrade') as any
  285. el.foo = 'hello'
  286. el.dataset.age = 5
  287. el.notProp = 1
  288. container.appendChild(el)
  289. customElements.define('my-el-upgrade', E)
  290. expect(el.shadowRoot.firstChild.innerHTML).toBe(`foo: hello`)
  291. // should not reflect if not declared as a prop
  292. expect(el.hasAttribute('not-prop')).toBe(false)
  293. })
  294. test('handle properties set before connecting', () => {
  295. const obj = { a: 1 }
  296. const E = defineCustomElement({
  297. props: {
  298. foo: String,
  299. post: Object,
  300. },
  301. setup(props) {
  302. expect(props.foo).toBe('hello')
  303. expect(props.post).toBe(obj)
  304. },
  305. render() {
  306. return JSON.stringify(this.post)
  307. },
  308. })
  309. customElements.define('my-el-preconnect', E)
  310. const el = document.createElement('my-el-preconnect') as any
  311. el.foo = 'hello'
  312. el.post = obj
  313. container.appendChild(el)
  314. expect(el.shadowRoot.innerHTML).toBe(JSON.stringify(obj))
  315. })
  316. // https://github.com/vuejs/core/issues/6163
  317. test('handle components with no props', async () => {
  318. const E = defineCustomElement({
  319. render() {
  320. return h('div', 'foo')
  321. },
  322. })
  323. customElements.define('my-element-noprops', E)
  324. const el = document.createElement('my-element-noprops')
  325. container.appendChild(el)
  326. await nextTick()
  327. expect(el.shadowRoot!.innerHTML).toMatchInlineSnapshot('"<div>foo</div>"')
  328. })
  329. // #5793
  330. test('set number value in dom property', () => {
  331. const E = defineCustomElement({
  332. props: {
  333. 'max-age': Number,
  334. },
  335. render() {
  336. // @ts-expect-error
  337. return `max age: ${this.maxAge}/type: ${typeof this.maxAge}`
  338. },
  339. })
  340. customElements.define('my-element-number-property', E)
  341. const el = document.createElement('my-element-number-property') as any
  342. container.appendChild(el)
  343. el.maxAge = 50
  344. expect(el.maxAge).toBe(50)
  345. expect(el.shadowRoot.innerHTML).toBe('max age: 50/type: number')
  346. })
  347. // #9006
  348. test('should reflect default value', () => {
  349. const E = defineCustomElement({
  350. props: {
  351. value: {
  352. type: String,
  353. default: 'hi',
  354. },
  355. },
  356. render() {
  357. return this.value
  358. },
  359. })
  360. customElements.define('my-el-default-val', E)
  361. container.innerHTML = `<my-el-default-val></my-el-default-val>`
  362. const e = container.childNodes[0] as any
  363. expect(e.value).toBe('hi')
  364. })
  365. test('support direct setup function syntax with extra options', () => {
  366. const E = defineCustomElement(
  367. props => {
  368. return () => props.text
  369. },
  370. {
  371. props: {
  372. text: String,
  373. },
  374. },
  375. )
  376. customElements.define('my-el-setup-with-props', E)
  377. container.innerHTML = `<my-el-setup-with-props text="hello"></my-el-setup-with-props>`
  378. const e = container.childNodes[0] as VueElement
  379. expect(e.shadowRoot!.innerHTML).toBe('hello')
  380. })
  381. })
  382. describe('attrs', () => {
  383. const E = defineCustomElement({
  384. render() {
  385. return [h('div', null, this.$attrs.foo as string)]
  386. },
  387. })
  388. customElements.define('my-el-attrs', E)
  389. test('attrs via attribute', async () => {
  390. container.innerHTML = `<my-el-attrs foo="hello"></my-el-attrs>`
  391. const e = container.childNodes[0] as VueElement
  392. expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div>')
  393. e.setAttribute('foo', 'changed')
  394. await nextTick()
  395. expect(e.shadowRoot!.innerHTML).toBe('<div>changed</div>')
  396. })
  397. test('non-declared properties should not show up in $attrs', () => {
  398. const e = new E()
  399. // @ts-expect-error
  400. e.foo = '123'
  401. container.appendChild(e)
  402. expect(e.shadowRoot!.innerHTML).toBe('<div></div>')
  403. })
  404. })
  405. describe('emits', () => {
  406. const CompDef = defineComponent({
  407. setup(_, { emit }) {
  408. emit('created')
  409. return () =>
  410. h('div', {
  411. onClick: () => {
  412. emit('my-click', 1)
  413. },
  414. onMousedown: () => {
  415. emit('myEvent', 1) // validate hyphenation
  416. },
  417. onWheel: () => {
  418. emit('my-wheel', { bubbles: true }, 1)
  419. },
  420. })
  421. },
  422. })
  423. const E = defineCustomElement(CompDef)
  424. customElements.define('my-el-emits', E)
  425. test('emit on connect', () => {
  426. const e = new E()
  427. const spy = vi.fn()
  428. e.addEventListener('created', spy)
  429. container.appendChild(e)
  430. expect(spy).toHaveBeenCalled()
  431. })
  432. test('emit on interaction', () => {
  433. container.innerHTML = `<my-el-emits></my-el-emits>`
  434. const e = container.childNodes[0] as VueElement
  435. const spy = vi.fn()
  436. e.addEventListener('my-click', spy)
  437. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click'))
  438. expect(spy).toHaveBeenCalledTimes(1)
  439. expect(spy.mock.calls[0][0]).toMatchObject({
  440. detail: [1],
  441. })
  442. })
  443. // #5373
  444. test('case transform for camelCase event', () => {
  445. container.innerHTML = `<my-el-emits></my-el-emits>`
  446. const e = container.childNodes[0] as VueElement
  447. const spy1 = vi.fn()
  448. e.addEventListener('myEvent', spy1)
  449. const spy2 = vi.fn()
  450. // emitting myEvent, but listening for my-event. This happens when
  451. // using the custom element in a Vue template
  452. e.addEventListener('my-event', spy2)
  453. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('mousedown'))
  454. expect(spy1).toHaveBeenCalledTimes(1)
  455. expect(spy2).toHaveBeenCalledTimes(1)
  456. })
  457. test('emit from within async component wrapper', async () => {
  458. const p = new Promise<typeof CompDef>(res => res(CompDef as any))
  459. const E = defineCustomElement(defineAsyncComponent(() => p))
  460. customElements.define('my-async-el-emits', E)
  461. container.innerHTML = `<my-async-el-emits></my-async-el-emits>`
  462. const e = container.childNodes[0] as VueElement
  463. const spy = vi.fn()
  464. e.addEventListener('my-click', spy)
  465. // this feels brittle but seems necessary to reach the node in the DOM.
  466. await customElements.whenDefined('my-async-el-emits')
  467. await nextTick()
  468. await nextTick()
  469. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click'))
  470. expect(spy).toHaveBeenCalled()
  471. expect(spy.mock.calls[0][0]).toMatchObject({
  472. detail: [1],
  473. })
  474. })
  475. // #7293
  476. test('emit in an async component wrapper with properties bound', async () => {
  477. const E = defineCustomElement(
  478. defineAsyncComponent(
  479. () => new Promise<typeof CompDef>(res => res(CompDef as any)),
  480. ),
  481. )
  482. customElements.define('my-async-el-props-emits', E)
  483. container.innerHTML = `<my-async-el-props-emits id="my_async_el_props_emits"></my-async-el-props-emits>`
  484. const e = container.childNodes[0] as VueElement
  485. const spy = vi.fn()
  486. e.addEventListener('my-click', spy)
  487. await customElements.whenDefined('my-async-el-props-emits')
  488. await nextTick()
  489. await nextTick()
  490. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click'))
  491. expect(spy).toHaveBeenCalled()
  492. expect(spy.mock.calls[0][0]).toMatchObject({
  493. detail: [1],
  494. })
  495. })
  496. test('emit with options', async () => {
  497. container.innerHTML = `<my-el-emits></my-el-emits>`
  498. const e = container.childNodes[0] as VueElement
  499. const spy = vi.fn()
  500. e.addEventListener('my-wheel', spy)
  501. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('wheel'))
  502. expect(spy).toHaveBeenCalledTimes(1)
  503. expect(spy.mock.calls[0][0]).toMatchObject({
  504. bubbles: true,
  505. detail: [{ bubbles: true }, 1],
  506. })
  507. })
  508. })
  509. describe('slots', () => {
  510. const E = defineCustomElement({
  511. render() {
  512. return [
  513. h('div', null, [
  514. renderSlot(this.$slots, 'default', undefined, () => [
  515. h('div', 'fallback'),
  516. ]),
  517. ]),
  518. h('div', null, renderSlot(this.$slots, 'named')),
  519. ]
  520. },
  521. })
  522. customElements.define('my-el-slots', E)
  523. test('render slots correctly', () => {
  524. container.innerHTML = `<my-el-slots><span>hi</span></my-el-slots>`
  525. const e = container.childNodes[0] as VueElement
  526. // native slots allocation does not affect innerHTML, so we just
  527. // verify that we've rendered the correct native slots here...
  528. expect(e.shadowRoot!.innerHTML).toBe(
  529. `<div><slot><div>fallback</div></slot></div><div><slot name="named"></slot></div>`,
  530. )
  531. })
  532. })
  533. describe('provide/inject', () => {
  534. const Consumer = defineCustomElement({
  535. setup() {
  536. const foo = inject<Ref>('foo')!
  537. return () => h('div', foo.value)
  538. },
  539. })
  540. customElements.define('my-consumer', Consumer)
  541. test('over nested usage', async () => {
  542. const foo = ref('injected!')
  543. const Provider = defineCustomElement({
  544. provide: {
  545. foo,
  546. },
  547. render() {
  548. return h('my-consumer')
  549. },
  550. })
  551. customElements.define('my-provider', Provider)
  552. container.innerHTML = `<my-provider><my-provider>`
  553. const provider = container.childNodes[0] as VueElement
  554. const consumer = provider.shadowRoot!.childNodes[0] as VueElement
  555. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`)
  556. foo.value = 'changed!'
  557. await nextTick()
  558. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
  559. })
  560. test('over slot composition', async () => {
  561. const foo = ref('injected!')
  562. const Provider = defineCustomElement({
  563. provide: {
  564. foo,
  565. },
  566. render() {
  567. return renderSlot(this.$slots, 'default')
  568. },
  569. })
  570. customElements.define('my-provider-2', Provider)
  571. container.innerHTML = `<my-provider-2><my-consumer></my-consumer><my-provider-2>`
  572. const provider = container.childNodes[0]
  573. const consumer = provider.childNodes[0] as VueElement
  574. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`)
  575. foo.value = 'changed!'
  576. await nextTick()
  577. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
  578. })
  579. test('inherited from ancestors', async () => {
  580. const fooA = ref('FooA!')
  581. const fooB = ref('FooB!')
  582. const ProviderA = defineCustomElement({
  583. provide: {
  584. fooA,
  585. },
  586. render() {
  587. return h('provider-b')
  588. },
  589. })
  590. const ProviderB = defineCustomElement({
  591. provide: {
  592. fooB,
  593. },
  594. render() {
  595. return h('my-multi-consumer')
  596. },
  597. })
  598. const Consumer = defineCustomElement({
  599. setup() {
  600. const fooA = inject<Ref>('fooA')!
  601. const fooB = inject<Ref>('fooB')!
  602. return () => h('div', `${fooA.value} ${fooB.value}`)
  603. },
  604. })
  605. customElements.define('provider-a', ProviderA)
  606. customElements.define('provider-b', ProviderB)
  607. customElements.define('my-multi-consumer', Consumer)
  608. container.innerHTML = `<provider-a><provider-a>`
  609. const providerA = container.childNodes[0] as VueElement
  610. const providerB = providerA.shadowRoot!.childNodes[0] as VueElement
  611. const consumer = providerB.shadowRoot!.childNodes[0] as VueElement
  612. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>FooA! FooB!</div>`)
  613. fooA.value = 'changedA!'
  614. fooB.value = 'changedB!'
  615. await nextTick()
  616. expect(consumer.shadowRoot!.innerHTML).toBe(
  617. `<div>changedA! changedB!</div>`,
  618. )
  619. })
  620. })
  621. describe('styles', () => {
  622. function assertStyles(el: VueElement, css: string[]) {
  623. const styles = el.shadowRoot?.querySelectorAll('style')!
  624. expect(styles.length).toBe(css.length) // should not duplicate multiple copies from Bar
  625. for (let i = 0; i < css.length; i++) {
  626. expect(styles[i].textContent).toBe(css[i])
  627. }
  628. }
  629. test('should attach styles to shadow dom', async () => {
  630. const def = defineComponent({
  631. __hmrId: 'foo',
  632. styles: [`div { color: red; }`],
  633. render() {
  634. return h('div', 'hello')
  635. },
  636. })
  637. const Foo = defineCustomElement(def)
  638. customElements.define('my-el-with-styles', Foo)
  639. container.innerHTML = `<my-el-with-styles></my-el-with-styles>`
  640. const el = container.childNodes[0] as VueElement
  641. const style = el.shadowRoot?.querySelector('style')!
  642. expect(style.textContent).toBe(`div { color: red; }`)
  643. // hmr
  644. __VUE_HMR_RUNTIME__.reload('foo', {
  645. ...def,
  646. styles: [`div { color: blue; }`, `div { color: yellow; }`],
  647. } as any)
  648. await nextTick()
  649. assertStyles(el, [`div { color: blue; }`, `div { color: yellow; }`])
  650. })
  651. test("child components should inject styles to root element's shadow root", async () => {
  652. const Baz = () => h(Bar)
  653. const Bar = defineComponent({
  654. __hmrId: 'bar',
  655. styles: [`div { color: green; }`, `div { color: blue; }`],
  656. render() {
  657. return 'bar'
  658. },
  659. })
  660. const Foo = defineCustomElement({
  661. styles: [`div { color: red; }`],
  662. render() {
  663. return [h(Baz), h(Baz)]
  664. },
  665. })
  666. customElements.define('my-el-with-child-styles', Foo)
  667. container.innerHTML = `<my-el-with-child-styles></my-el-with-child-styles>`
  668. const el = container.childNodes[0] as VueElement
  669. // inject order should be child -> parent
  670. assertStyles(el, [
  671. `div { color: green; }`,
  672. `div { color: blue; }`,
  673. `div { color: red; }`,
  674. ])
  675. // hmr
  676. __VUE_HMR_RUNTIME__.reload(Bar.__hmrId!, {
  677. ...Bar,
  678. styles: [`div { color: red; }`, `div { color: yellow; }`],
  679. } as any)
  680. await nextTick()
  681. assertStyles(el, [
  682. `div { color: red; }`,
  683. `div { color: yellow; }`,
  684. `div { color: red; }`,
  685. ])
  686. __VUE_HMR_RUNTIME__.reload(Bar.__hmrId!, {
  687. ...Bar,
  688. styles: [`div { color: blue; }`],
  689. } as any)
  690. await nextTick()
  691. assertStyles(el, [`div { color: blue; }`, `div { color: red; }`])
  692. })
  693. test('with nonce', () => {
  694. const Foo = defineCustomElement(
  695. {
  696. styles: [`div { color: red; }`],
  697. render() {
  698. return h('div', 'hello')
  699. },
  700. },
  701. { nonce: 'xxx' },
  702. )
  703. customElements.define('my-el-with-nonce', Foo)
  704. container.innerHTML = `<my-el-with-nonce></my-el-with-nonce>`
  705. const el = container.childNodes[0] as VueElement
  706. const style = el.shadowRoot?.querySelector('style')!
  707. expect(style.getAttribute('nonce')).toBe('xxx')
  708. })
  709. })
  710. describe('async', () => {
  711. test('should work', async () => {
  712. const loaderSpy = vi.fn()
  713. const E = defineCustomElement(
  714. defineAsyncComponent(() => {
  715. loaderSpy()
  716. return Promise.resolve({
  717. props: ['msg'],
  718. styles: [`div { color: red }`],
  719. render(this: any) {
  720. return h('div', null, this.msg)
  721. },
  722. })
  723. }),
  724. )
  725. customElements.define('my-el-async', E)
  726. container.innerHTML =
  727. `<my-el-async msg="hello"></my-el-async>` +
  728. `<my-el-async msg="world"></my-el-async>`
  729. await new Promise(r => setTimeout(r))
  730. // loader should be called only once
  731. expect(loaderSpy).toHaveBeenCalledTimes(1)
  732. const e1 = container.childNodes[0] as VueElement
  733. const e2 = container.childNodes[1] as VueElement
  734. // should inject styles
  735. expect(e1.shadowRoot!.innerHTML).toBe(
  736. `<style>div { color: red }</style><div>hello</div>`,
  737. )
  738. expect(e2.shadowRoot!.innerHTML).toBe(
  739. `<style>div { color: red }</style><div>world</div>`,
  740. )
  741. // attr
  742. e1.setAttribute('msg', 'attr')
  743. await nextTick()
  744. expect((e1 as any).msg).toBe('attr')
  745. expect(e1.shadowRoot!.innerHTML).toBe(
  746. `<style>div { color: red }</style><div>attr</div>`,
  747. )
  748. // props
  749. expect(`msg` in e1).toBe(true)
  750. ;(e1 as any).msg = 'prop'
  751. expect(e1.getAttribute('msg')).toBe('prop')
  752. expect(e1.shadowRoot!.innerHTML).toBe(
  753. `<style>div { color: red }</style><div>prop</div>`,
  754. )
  755. })
  756. test('set DOM property before resolve', async () => {
  757. const E = defineCustomElement(
  758. defineAsyncComponent(() => {
  759. return Promise.resolve({
  760. props: ['msg'],
  761. setup(props) {
  762. expect(typeof props.msg).toBe('string')
  763. },
  764. render(this: any) {
  765. return h('div', this.msg)
  766. },
  767. })
  768. }),
  769. )
  770. customElements.define('my-el-async-2', E)
  771. const e1 = new E()
  772. // set property before connect
  773. e1.msg = 'hello'
  774. const e2 = new E()
  775. container.appendChild(e1)
  776. container.appendChild(e2)
  777. // set property after connect but before resolve
  778. e2.msg = 'world'
  779. await new Promise(r => setTimeout(r))
  780. expect(e1.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  781. expect(e2.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  782. e1.msg = 'world'
  783. expect(e1.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  784. e2.msg = 'hello'
  785. expect(e2.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  786. })
  787. test('Number prop casting before resolve', async () => {
  788. const E = defineCustomElement(
  789. defineAsyncComponent(() => {
  790. return Promise.resolve({
  791. props: { n: Number },
  792. setup(props) {
  793. expect(props.n).toBe(20)
  794. },
  795. render(this: any) {
  796. return h('div', this.n + ',' + typeof this.n)
  797. },
  798. })
  799. }),
  800. )
  801. customElements.define('my-el-async-3', E)
  802. container.innerHTML = `<my-el-async-3 n="2e1"></my-el-async-3>`
  803. await new Promise(r => setTimeout(r))
  804. const e = container.childNodes[0] as VueElement
  805. expect(e.shadowRoot!.innerHTML).toBe(`<div>20,number</div>`)
  806. })
  807. test('with slots', async () => {
  808. const E = defineCustomElement(
  809. defineAsyncComponent(() => {
  810. return Promise.resolve({
  811. render(this: any) {
  812. return [
  813. h('div', null, [
  814. renderSlot(this.$slots, 'default', undefined, () => [
  815. h('div', 'fallback'),
  816. ]),
  817. ]),
  818. h('div', null, renderSlot(this.$slots, 'named')),
  819. ]
  820. },
  821. })
  822. }),
  823. )
  824. customElements.define('my-el-async-slots', E)
  825. container.innerHTML = `<my-el-async-slots><span>hi</span></my-el-async-slots>`
  826. await new Promise(r => setTimeout(r))
  827. const e = container.childNodes[0] as VueElement
  828. expect(e.shadowRoot!.innerHTML).toBe(
  829. `<div><slot><div>fallback</div></slot></div><div><slot name="named"></slot></div>`,
  830. )
  831. })
  832. })
  833. describe('shadowRoot: false', () => {
  834. const E = defineCustomElement({
  835. shadowRoot: false,
  836. props: {
  837. msg: {
  838. type: String,
  839. default: 'hello',
  840. },
  841. },
  842. render() {
  843. return h('div', this.msg)
  844. },
  845. })
  846. customElements.define('my-el-shadowroot-false', E)
  847. test('should work', async () => {
  848. function raf() {
  849. return new Promise(resolve => {
  850. requestAnimationFrame(resolve)
  851. })
  852. }
  853. container.innerHTML = `<my-el-shadowroot-false></my-el-shadowroot-false>`
  854. const e = container.childNodes[0] as VueElement
  855. await raf()
  856. expect(e).toBeInstanceOf(E)
  857. expect(e._instance).toBeTruthy()
  858. expect(e.innerHTML).toBe(`<div>hello</div>`)
  859. expect(e.shadowRoot).toBe(null)
  860. })
  861. const toggle = ref(true)
  862. const ES = defineCustomElement(
  863. {
  864. render() {
  865. return [
  866. renderSlot(this.$slots, 'default'),
  867. toggle.value ? renderSlot(this.$slots, 'named') : null,
  868. renderSlot(this.$slots, 'omitted', {}, () => [
  869. h('div', 'fallback'),
  870. ]),
  871. ]
  872. },
  873. },
  874. { shadowRoot: false },
  875. )
  876. customElements.define('my-el-shadowroot-false-slots', ES)
  877. test('should render slots', async () => {
  878. container.innerHTML =
  879. `<my-el-shadowroot-false-slots>` +
  880. `<span>default</span>text` +
  881. `<div slot="named">named</div>` +
  882. `</my-el-shadowroot-false-slots>`
  883. const e = container.childNodes[0] as VueElement
  884. // native slots allocation does not affect innerHTML, so we just
  885. // verify that we've rendered the correct native slots here...
  886. expect(e.innerHTML).toBe(
  887. `<span>default</span>text` +
  888. `<div slot="named">named</div>` +
  889. `<div>fallback</div>`,
  890. )
  891. toggle.value = false
  892. await nextTick()
  893. expect(e.innerHTML).toBe(
  894. `<span>default</span>text` + `<!---->` + `<div>fallback</div>`,
  895. )
  896. })
  897. test('render nested customElement w/ shadowRoot false', async () => {
  898. const calls: string[] = []
  899. const Child = defineCustomElement(
  900. {
  901. setup() {
  902. calls.push('child rendering')
  903. onMounted(() => {
  904. calls.push('child mounted')
  905. })
  906. },
  907. render() {
  908. return renderSlot(this.$slots, 'default')
  909. },
  910. },
  911. { shadowRoot: false },
  912. )
  913. customElements.define('my-child', Child)
  914. const Parent = defineCustomElement(
  915. {
  916. setup() {
  917. calls.push('parent rendering')
  918. onMounted(() => {
  919. calls.push('parent mounted')
  920. })
  921. },
  922. render() {
  923. return renderSlot(this.$slots, 'default')
  924. },
  925. },
  926. { shadowRoot: false },
  927. )
  928. customElements.define('my-parent', Parent)
  929. const App = {
  930. render() {
  931. return h('my-parent', null, {
  932. default: () => [
  933. h('my-child', null, {
  934. default: () => [h('span', null, 'default')],
  935. }),
  936. ],
  937. })
  938. },
  939. }
  940. const app = createApp(App)
  941. app.mount(container)
  942. await nextTick()
  943. const e = container.childNodes[0] as VueElement
  944. expect(e.innerHTML).toBe(
  945. `<my-child data-v-app=""><span>default</span></my-child>`,
  946. )
  947. expect(calls).toEqual([
  948. 'parent rendering',
  949. 'parent mounted',
  950. 'child rendering',
  951. 'child mounted',
  952. ])
  953. app.unmount()
  954. })
  955. test('render nested Teleport w/ shadowRoot false', async () => {
  956. const target = document.createElement('div')
  957. const Child = defineCustomElement(
  958. {
  959. render() {
  960. return h(
  961. Teleport,
  962. { to: target },
  963. {
  964. default: () => [renderSlot(this.$slots, 'default')],
  965. },
  966. )
  967. },
  968. },
  969. { shadowRoot: false },
  970. )
  971. customElements.define('my-el-teleport-child', Child)
  972. const Parent = defineCustomElement(
  973. {
  974. render() {
  975. return renderSlot(this.$slots, 'default')
  976. },
  977. },
  978. { shadowRoot: false },
  979. )
  980. customElements.define('my-el-teleport-parent', Parent)
  981. const App = {
  982. render() {
  983. return h('my-el-teleport-parent', null, {
  984. default: () => [
  985. h('my-el-teleport-child', null, {
  986. default: () => [h('span', null, 'default')],
  987. }),
  988. ],
  989. })
  990. },
  991. }
  992. const app = createApp(App)
  993. app.mount(container)
  994. await nextTick()
  995. expect(target.innerHTML).toBe(`<span>default</span>`)
  996. app.unmount()
  997. })
  998. })
  999. describe('helpers', () => {
  1000. test('useHost', () => {
  1001. const Foo = defineCustomElement({
  1002. setup() {
  1003. const host = useHost()!
  1004. host.setAttribute('id', 'host')
  1005. return () => h('div', 'hello')
  1006. },
  1007. })
  1008. customElements.define('my-el-use-host', Foo)
  1009. container.innerHTML = `<my-el-use-host>`
  1010. const el = container.childNodes[0] as VueElement
  1011. expect(el.id).toBe('host')
  1012. })
  1013. test('useShadowRoot for style injection', () => {
  1014. const Foo = defineCustomElement({
  1015. setup() {
  1016. const root = useShadowRoot()!
  1017. const style = document.createElement('style')
  1018. style.innerHTML = `div { color: red; }`
  1019. root.appendChild(style)
  1020. return () => h('div', 'hello')
  1021. },
  1022. })
  1023. customElements.define('my-el-use-shadow-root', Foo)
  1024. container.innerHTML = `<my-el-use-shadow-root>`
  1025. const el = container.childNodes[0] as VueElement
  1026. const style = el.shadowRoot?.querySelector('style')!
  1027. expect(style.textContent).toBe(`div { color: red; }`)
  1028. })
  1029. })
  1030. describe('expose', () => {
  1031. test('expose attributes and callback', async () => {
  1032. type SetValue = (value: string) => void
  1033. let fn: MockedFunction<SetValue>
  1034. const E = defineCustomElement({
  1035. setup(_, { expose }) {
  1036. const value = ref('hello')
  1037. const setValue = (fn = vi.fn((_value: string) => {
  1038. value.value = _value
  1039. }))
  1040. expose({
  1041. setValue,
  1042. value,
  1043. })
  1044. return () => h('div', null, [value.value])
  1045. },
  1046. })
  1047. customElements.define('my-el-expose', E)
  1048. container.innerHTML = `<my-el-expose></my-el-expose>`
  1049. const e = container.childNodes[0] as VueElement & {
  1050. value: string
  1051. setValue: MockedFunction<SetValue>
  1052. }
  1053. expect(e.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  1054. expect(e.value).toBe('hello')
  1055. expect(e.setValue).toBe(fn!)
  1056. e.setValue('world')
  1057. expect(e.value).toBe('world')
  1058. await nextTick()
  1059. expect(e.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  1060. })
  1061. test('warning when exposing an existing property', () => {
  1062. const E = defineCustomElement({
  1063. props: {
  1064. value: String,
  1065. },
  1066. setup(props, { expose }) {
  1067. expose({
  1068. value: 'hello',
  1069. })
  1070. return () => h('div', null, [props.value])
  1071. },
  1072. })
  1073. customElements.define('my-el-expose-two', E)
  1074. container.innerHTML = `<my-el-expose-two value="world"></my-el-expose-two>`
  1075. expect(
  1076. `[Vue warn]: Exposed property "value" already exists on custom element.`,
  1077. ).toHaveBeenWarned()
  1078. })
  1079. })
  1080. test('async & nested custom elements', async () => {
  1081. let fooVal: string | undefined = ''
  1082. const E = defineCustomElement(
  1083. defineAsyncComponent(() => {
  1084. return Promise.resolve({
  1085. setup(props) {
  1086. provide('foo', 'foo')
  1087. },
  1088. render(this: any) {
  1089. return h('div', null, [renderSlot(this.$slots, 'default')])
  1090. },
  1091. })
  1092. }),
  1093. )
  1094. const EChild = defineCustomElement({
  1095. setup(props) {
  1096. fooVal = inject('foo')
  1097. },
  1098. render(this: any) {
  1099. return h('div', null, 'child')
  1100. },
  1101. })
  1102. customElements.define('my-el-async-nested-ce', E)
  1103. customElements.define('slotted-child', EChild)
  1104. container.innerHTML = `<my-el-async-nested-ce><div><slotted-child></slotted-child></div></my-el-async-nested-ce>`
  1105. await new Promise(r => setTimeout(r))
  1106. const e = container.childNodes[0] as VueElement
  1107. expect(e.shadowRoot!.innerHTML).toBe(`<div><slot></slot></div>`)
  1108. expect(fooVal).toBe('foo')
  1109. })
  1110. test('async & multiple levels of nested custom elements', async () => {
  1111. let fooVal: string | undefined = ''
  1112. let barVal: string | undefined = ''
  1113. const E = defineCustomElement(
  1114. defineAsyncComponent(() => {
  1115. return Promise.resolve({
  1116. setup(props) {
  1117. provide('foo', 'foo')
  1118. },
  1119. render(this: any) {
  1120. return h('div', null, [renderSlot(this.$slots, 'default')])
  1121. },
  1122. })
  1123. }),
  1124. )
  1125. const EChild = defineCustomElement({
  1126. setup(props) {
  1127. provide('bar', 'bar')
  1128. },
  1129. render(this: any) {
  1130. return h('div', null, [renderSlot(this.$slots, 'default')])
  1131. },
  1132. })
  1133. const EChild2 = defineCustomElement({
  1134. setup(props) {
  1135. fooVal = inject('foo')
  1136. barVal = inject('bar')
  1137. },
  1138. render(this: any) {
  1139. return h('div', null, 'child')
  1140. },
  1141. })
  1142. customElements.define('my-el-async-nested-m-ce', E)
  1143. customElements.define('slotted-child-m', EChild)
  1144. customElements.define('slotted-child2-m', EChild2)
  1145. container.innerHTML =
  1146. `<my-el-async-nested-m-ce>` +
  1147. `<div><slotted-child-m>` +
  1148. `<slotted-child2-m></slotted-child2-m>` +
  1149. `</slotted-child-m></div>` +
  1150. `</my-el-async-nested-m-ce>`
  1151. await new Promise(r => setTimeout(r))
  1152. const e = container.childNodes[0] as VueElement
  1153. expect(e.shadowRoot!.innerHTML).toBe(`<div><slot></slot></div>`)
  1154. expect(fooVal).toBe('foo')
  1155. expect(barVal).toBe('bar')
  1156. })
  1157. describe('configureApp', () => {
  1158. test('should work', () => {
  1159. const E = defineCustomElement(
  1160. () => {
  1161. const msg = inject('msg')
  1162. return () => h('div', msg!)
  1163. },
  1164. {
  1165. configureApp(app) {
  1166. app.provide('msg', 'app-injected')
  1167. },
  1168. },
  1169. )
  1170. customElements.define('my-element-with-app', E)
  1171. container.innerHTML = `<my-element-with-app></my-element-with-app>`
  1172. const e = container.childNodes[0] as VueElement
  1173. expect(e.shadowRoot?.innerHTML).toBe('<div>app-injected</div>')
  1174. })
  1175. })
  1176. // #9885
  1177. test('avoid double mount when prop is set immediately after mount', () => {
  1178. customElements.define(
  1179. 'my-input-dupe',
  1180. defineCustomElement({
  1181. props: {
  1182. value: String,
  1183. },
  1184. render() {
  1185. return 'hello'
  1186. },
  1187. }),
  1188. )
  1189. const container = document.createElement('div')
  1190. document.body.appendChild(container)
  1191. createApp({
  1192. render() {
  1193. return h('div', [
  1194. h('my-input-dupe', {
  1195. onVnodeMounted(vnode) {
  1196. vnode.el!.value = 'fesfes'
  1197. },
  1198. }),
  1199. ])
  1200. },
  1201. }).mount(container)
  1202. expect(container.children[0].children[0].shadowRoot?.innerHTML).toBe(
  1203. 'hello',
  1204. )
  1205. })
  1206. // #11081
  1207. test('Props can be casted when mounting custom elements in component rendering functions', async () => {
  1208. const E = defineCustomElement(
  1209. defineAsyncComponent(() =>
  1210. Promise.resolve({
  1211. props: ['fooValue'],
  1212. setup(props) {
  1213. expect(props.fooValue).toBe('fooValue')
  1214. return () => h('div', props.fooValue)
  1215. },
  1216. }),
  1217. ),
  1218. )
  1219. customElements.define('my-el-async-4', E)
  1220. const R = defineComponent({
  1221. setup() {
  1222. const fooValue = ref('fooValue')
  1223. return () => {
  1224. return h('div', null, [
  1225. h('my-el-async-4', {
  1226. fooValue: fooValue.value,
  1227. }),
  1228. ])
  1229. }
  1230. },
  1231. })
  1232. const app = createApp(R)
  1233. app.mount(container)
  1234. await new Promise(r => setTimeout(r))
  1235. const e = container.querySelector('my-el-async-4') as VueElement
  1236. expect(e.shadowRoot!.innerHTML).toBe(`<div>fooValue</div>`)
  1237. app.unmount()
  1238. })
  1239. // #11276
  1240. test('delete prop on attr removal', async () => {
  1241. const E = defineCustomElement({
  1242. props: {
  1243. boo: {
  1244. type: Boolean,
  1245. },
  1246. },
  1247. render() {
  1248. return this.boo + ',' + typeof this.boo
  1249. },
  1250. })
  1251. customElements.define('el-attr-removal', E)
  1252. container.innerHTML = '<el-attr-removal boo>'
  1253. const e = container.childNodes[0] as VueElement
  1254. expect(e.shadowRoot!.innerHTML).toBe(`true,boolean`)
  1255. e.removeAttribute('boo')
  1256. await nextTick()
  1257. expect(e.shadowRoot!.innerHTML).toBe(`false,boolean`)
  1258. })
  1259. test('hyphenated attr removal', async () => {
  1260. const E = defineCustomElement({
  1261. props: {
  1262. fooBar: {
  1263. type: Boolean,
  1264. },
  1265. },
  1266. render() {
  1267. return this.fooBar
  1268. },
  1269. })
  1270. customElements.define('el-hyphenated-attr-removal', E)
  1271. const toggle = ref(true)
  1272. const Comp = {
  1273. render() {
  1274. return h('el-hyphenated-attr-removal', {
  1275. 'foo-bar': toggle.value ? '' : null,
  1276. })
  1277. },
  1278. }
  1279. render(h(Comp), container)
  1280. const el = container.children[0]
  1281. expect(el.hasAttribute('foo-bar')).toBe(true)
  1282. expect((el as any).outerHTML).toBe(
  1283. `<el-hyphenated-attr-removal foo-bar=""></el-hyphenated-attr-removal>`,
  1284. )
  1285. toggle.value = false
  1286. await nextTick()
  1287. expect(el.hasAttribute('foo-bar')).toBe(false)
  1288. expect((el as any).outerHTML).toBe(
  1289. `<el-hyphenated-attr-removal></el-hyphenated-attr-removal>`,
  1290. )
  1291. })
  1292. })