customElement.spec.ts 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805
  1. import {
  2. type Ref,
  3. type VueElement,
  4. createApp,
  5. defineAsyncComponent,
  6. defineComponent,
  7. defineCustomElement,
  8. h,
  9. inject,
  10. nextTick,
  11. ref,
  12. render,
  13. renderSlot,
  14. } from '../src'
  15. describe('defineCustomElement', () => {
  16. const container = document.createElement('div')
  17. document.body.appendChild(container)
  18. beforeEach(() => {
  19. container.innerHTML = ''
  20. })
  21. describe('mounting/unmount', () => {
  22. const E = defineCustomElement({
  23. props: {
  24. msg: {
  25. type: String,
  26. default: 'hello',
  27. },
  28. },
  29. render() {
  30. return h('div', this.msg)
  31. },
  32. })
  33. customElements.define('my-element', E)
  34. test('should work', () => {
  35. container.innerHTML = `<my-element></my-element>`
  36. const e = container.childNodes[0] as VueElement
  37. expect(e).toBeInstanceOf(E)
  38. expect(e._instance).toBeTruthy()
  39. expect(e.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  40. })
  41. test('should work w/ manual instantiation', () => {
  42. const e = new E({ msg: 'inline' })
  43. // should lazy init
  44. expect(e._instance).toBe(null)
  45. // should initialize on connect
  46. container.appendChild(e)
  47. expect(e._instance).toBeTruthy()
  48. expect(e.shadowRoot!.innerHTML).toBe(`<div>inline</div>`)
  49. })
  50. test('should unmount on remove', async () => {
  51. container.innerHTML = `<my-element></my-element>`
  52. const e = container.childNodes[0] as VueElement
  53. container.removeChild(e)
  54. await nextTick()
  55. expect(e._instance).toBe(null)
  56. expect(e.shadowRoot!.innerHTML).toBe('')
  57. })
  58. // #10610
  59. test('When elements move, avoid prematurely disconnecting MutationObserver', async () => {
  60. const CustomInput = defineCustomElement({
  61. props: ['value'],
  62. emits: ['update'],
  63. setup(props, { emit }) {
  64. return () =>
  65. h('input', {
  66. type: 'number',
  67. value: props.value,
  68. onInput: (e: InputEvent) => {
  69. const num = (e.target! as HTMLInputElement).valueAsNumber
  70. emit('update', Number.isNaN(num) ? null : num)
  71. },
  72. })
  73. },
  74. })
  75. customElements.define('my-el-input', CustomInput)
  76. const num = ref('12')
  77. const containerComp = defineComponent({
  78. setup() {
  79. return () => {
  80. return h('div', [
  81. h('my-el-input', {
  82. value: num.value,
  83. onUpdate: ($event: CustomEvent) => {
  84. num.value = $event.detail[0]
  85. },
  86. }),
  87. h('div', { id: 'move' }),
  88. ])
  89. }
  90. },
  91. })
  92. const app = createApp(containerComp)
  93. app.mount(container)
  94. const myInputEl = container.querySelector('my-el-input')!
  95. const inputEl = myInputEl.shadowRoot!.querySelector('input')!
  96. await nextTick()
  97. expect(inputEl.value).toBe('12')
  98. const moveEl = container.querySelector('#move')!
  99. moveEl.append(myInputEl)
  100. await nextTick()
  101. myInputEl.removeAttribute('value')
  102. await nextTick()
  103. expect(inputEl.value).toBe('')
  104. app.unmount()
  105. })
  106. test('should not unmount on move', async () => {
  107. container.innerHTML = `<div><my-element></my-element></div>`
  108. const e = container.childNodes[0].childNodes[0] as VueElement
  109. const i = e._instance
  110. // moving from one parent to another - this will trigger both disconnect
  111. // and connected callbacks synchronously
  112. container.appendChild(e)
  113. await nextTick()
  114. // should be the same instance
  115. expect(e._instance).toBe(i)
  116. expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div>')
  117. })
  118. test('remove then insert again', async () => {
  119. container.innerHTML = `<my-element></my-element>`
  120. const e = container.childNodes[0] as VueElement
  121. container.removeChild(e)
  122. await nextTick()
  123. expect(e._instance).toBe(null)
  124. expect(e.shadowRoot!.innerHTML).toBe('')
  125. container.appendChild(e)
  126. expect(e._instance).toBeTruthy()
  127. expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div>')
  128. })
  129. })
  130. describe('props', () => {
  131. const E = defineCustomElement({
  132. props: ['foo', 'bar', 'bazQux', 'value'],
  133. render() {
  134. return [
  135. h('div', null, this.foo),
  136. h('div', null, this.bazQux || (this.bar && this.bar.x)),
  137. ]
  138. },
  139. })
  140. customElements.define('my-el-props', E)
  141. test('renders custom element w/ correct object prop value', () => {
  142. render(h('my-el-props', { value: { x: 1 } }), container)
  143. const el = container.children[0]
  144. expect((el as any).value).toEqual({ x: 1 })
  145. })
  146. test('props via attribute', async () => {
  147. // bazQux should map to `baz-qux` attribute
  148. container.innerHTML = `<my-el-props foo="hello" baz-qux="bye"></my-el-props>`
  149. const e = container.childNodes[0] as VueElement
  150. expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div><div>bye</div>')
  151. // change attr
  152. e.setAttribute('foo', 'changed')
  153. await nextTick()
  154. expect(e.shadowRoot!.innerHTML).toBe('<div>changed</div><div>bye</div>')
  155. e.setAttribute('baz-qux', 'changed')
  156. await nextTick()
  157. expect(e.shadowRoot!.innerHTML).toBe(
  158. '<div>changed</div><div>changed</div>',
  159. )
  160. })
  161. test('props via properties', async () => {
  162. const e = new E()
  163. e.foo = 'one'
  164. e.bar = { x: 'two' }
  165. container.appendChild(e)
  166. expect(e.shadowRoot!.innerHTML).toBe('<div>one</div><div>two</div>')
  167. // reflect
  168. // should reflect primitive value
  169. expect(e.getAttribute('foo')).toBe('one')
  170. // should not reflect rich data
  171. expect(e.hasAttribute('bar')).toBe(false)
  172. e.foo = 'three'
  173. await nextTick()
  174. expect(e.shadowRoot!.innerHTML).toBe('<div>three</div><div>two</div>')
  175. expect(e.getAttribute('foo')).toBe('three')
  176. e.foo = null
  177. await nextTick()
  178. expect(e.shadowRoot!.innerHTML).toBe('<div></div><div>two</div>')
  179. expect(e.hasAttribute('foo')).toBe(false)
  180. e.foo = undefined
  181. await nextTick()
  182. expect(e.shadowRoot!.innerHTML).toBe('<div></div><div>two</div>')
  183. expect(e.hasAttribute('foo')).toBe(false)
  184. expect(e.foo).toBe(undefined)
  185. e.bazQux = 'four'
  186. await nextTick()
  187. expect(e.shadowRoot!.innerHTML).toBe('<div></div><div>four</div>')
  188. expect(e.getAttribute('baz-qux')).toBe('four')
  189. })
  190. test('attribute -> prop type casting', async () => {
  191. const E = defineCustomElement({
  192. props: {
  193. fooBar: Number, // test casting of camelCase prop names
  194. bar: Boolean,
  195. baz: String,
  196. },
  197. render() {
  198. return [
  199. this.fooBar,
  200. typeof this.fooBar,
  201. this.bar,
  202. typeof this.bar,
  203. this.baz,
  204. typeof this.baz,
  205. ].join(' ')
  206. },
  207. })
  208. customElements.define('my-el-props-cast', E)
  209. container.innerHTML = `<my-el-props-cast foo-bar="1" baz="12345"></my-el-props-cast>`
  210. const e = container.childNodes[0] as VueElement
  211. expect(e.shadowRoot!.innerHTML).toBe(
  212. `1 number false boolean 12345 string`,
  213. )
  214. e.setAttribute('bar', '')
  215. await nextTick()
  216. expect(e.shadowRoot!.innerHTML).toBe(`1 number true boolean 12345 string`)
  217. e.setAttribute('foo-bar', '2e1')
  218. await nextTick()
  219. expect(e.shadowRoot!.innerHTML).toBe(
  220. `20 number true boolean 12345 string`,
  221. )
  222. e.setAttribute('baz', '2e1')
  223. await nextTick()
  224. expect(e.shadowRoot!.innerHTML).toBe(`20 number true boolean 2e1 string`)
  225. })
  226. // #4772
  227. test('attr casting w/ programmatic creation', () => {
  228. const E = defineCustomElement({
  229. props: {
  230. foo: Number,
  231. },
  232. render() {
  233. return `foo type: ${typeof this.foo}`
  234. },
  235. })
  236. customElements.define('my-element-programmatic', E)
  237. const el = document.createElement('my-element-programmatic') as any
  238. el.setAttribute('foo', '123')
  239. container.appendChild(el)
  240. expect(el.shadowRoot.innerHTML).toBe(`foo type: number`)
  241. })
  242. test('handling properties set before upgrading', () => {
  243. const E = defineCustomElement({
  244. props: {
  245. foo: String,
  246. dataAge: Number,
  247. },
  248. setup(props) {
  249. expect(props.foo).toBe('hello')
  250. expect(props.dataAge).toBe(5)
  251. },
  252. render() {
  253. return h('div', `foo: ${this.foo}`)
  254. },
  255. })
  256. const el = document.createElement('my-el-upgrade') as any
  257. el.foo = 'hello'
  258. el.dataset.age = 5
  259. el.notProp = 1
  260. container.appendChild(el)
  261. customElements.define('my-el-upgrade', E)
  262. expect(el.shadowRoot.firstChild.innerHTML).toBe(`foo: hello`)
  263. // should not reflect if not declared as a prop
  264. expect(el.hasAttribute('not-prop')).toBe(false)
  265. })
  266. test('handle properties set before connecting', () => {
  267. const obj = { a: 1 }
  268. const E = defineCustomElement({
  269. props: {
  270. foo: String,
  271. post: Object,
  272. },
  273. setup(props) {
  274. expect(props.foo).toBe('hello')
  275. expect(props.post).toBe(obj)
  276. },
  277. render() {
  278. return JSON.stringify(this.post)
  279. },
  280. })
  281. customElements.define('my-el-preconnect', E)
  282. const el = document.createElement('my-el-preconnect') as any
  283. el.foo = 'hello'
  284. el.post = obj
  285. container.appendChild(el)
  286. expect(el.shadowRoot.innerHTML).toBe(JSON.stringify(obj))
  287. })
  288. // https://github.com/vuejs/core/issues/6163
  289. test('handle components with no props', async () => {
  290. const E = defineCustomElement({
  291. render() {
  292. return h('div', 'foo')
  293. },
  294. })
  295. customElements.define('my-element-noprops', E)
  296. const el = document.createElement('my-element-noprops')
  297. container.appendChild(el)
  298. await nextTick()
  299. expect(el.shadowRoot!.innerHTML).toMatchInlineSnapshot('"<div>foo</div>"')
  300. })
  301. // # 5793
  302. test('set number value in dom property', () => {
  303. const E = defineCustomElement({
  304. props: {
  305. 'max-age': Number,
  306. },
  307. render() {
  308. // @ts-expect-error
  309. return `max age: ${this.maxAge}/type: ${typeof this.maxAge}`
  310. },
  311. })
  312. customElements.define('my-element-number-property', E)
  313. const el = document.createElement('my-element-number-property') as any
  314. container.appendChild(el)
  315. el.maxAge = 50
  316. expect(el.maxAge).toBe(50)
  317. expect(el.shadowRoot.innerHTML).toBe('max age: 50/type: number')
  318. })
  319. test('support direct setup function syntax with extra options', () => {
  320. const E = defineCustomElement(
  321. props => {
  322. return () => props.text
  323. },
  324. {
  325. props: {
  326. text: String,
  327. },
  328. },
  329. )
  330. customElements.define('my-el-setup-with-props', E)
  331. container.innerHTML = `<my-el-setup-with-props text="hello"></my-el-setup-with-props>`
  332. const e = container.childNodes[0] as VueElement
  333. expect(e.shadowRoot!.innerHTML).toBe('hello')
  334. })
  335. })
  336. describe('attrs', () => {
  337. const E = defineCustomElement({
  338. render() {
  339. return [h('div', null, this.$attrs.foo as string)]
  340. },
  341. })
  342. customElements.define('my-el-attrs', E)
  343. test('attrs via attribute', async () => {
  344. container.innerHTML = `<my-el-attrs foo="hello"></my-el-attrs>`
  345. const e = container.childNodes[0] as VueElement
  346. expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div>')
  347. e.setAttribute('foo', 'changed')
  348. await nextTick()
  349. expect(e.shadowRoot!.innerHTML).toBe('<div>changed</div>')
  350. })
  351. test('non-declared properties should not show up in $attrs', () => {
  352. const e = new E()
  353. // @ts-expect-error
  354. e.foo = '123'
  355. container.appendChild(e)
  356. expect(e.shadowRoot!.innerHTML).toBe('<div></div>')
  357. })
  358. })
  359. describe('emits', () => {
  360. const CompDef = defineComponent({
  361. setup(_, { emit }) {
  362. emit('created')
  363. return () =>
  364. h('div', {
  365. onClick: () => {
  366. emit('my-click', 1)
  367. },
  368. onMousedown: () => {
  369. emit('myEvent', 1) // validate hyphenation
  370. },
  371. })
  372. },
  373. })
  374. const E = defineCustomElement(CompDef)
  375. customElements.define('my-el-emits', E)
  376. test('emit on connect', () => {
  377. const e = new E()
  378. const spy = vi.fn()
  379. e.addEventListener('created', spy)
  380. container.appendChild(e)
  381. expect(spy).toHaveBeenCalled()
  382. })
  383. test('emit on interaction', () => {
  384. container.innerHTML = `<my-el-emits></my-el-emits>`
  385. const e = container.childNodes[0] as VueElement
  386. const spy = vi.fn()
  387. e.addEventListener('my-click', spy)
  388. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click'))
  389. expect(spy).toHaveBeenCalledTimes(1)
  390. expect(spy.mock.calls[0][0]).toMatchObject({
  391. detail: [1],
  392. })
  393. })
  394. // #5373
  395. test('case transform for camelCase event', () => {
  396. container.innerHTML = `<my-el-emits></my-el-emits>`
  397. const e = container.childNodes[0] as VueElement
  398. const spy1 = vi.fn()
  399. e.addEventListener('myEvent', spy1)
  400. const spy2 = vi.fn()
  401. // emitting myEvent, but listening for my-event. This happens when
  402. // using the custom element in a Vue template
  403. e.addEventListener('my-event', spy2)
  404. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('mousedown'))
  405. expect(spy1).toHaveBeenCalledTimes(1)
  406. expect(spy2).toHaveBeenCalledTimes(1)
  407. })
  408. test('emit from within async component wrapper', async () => {
  409. const p = new Promise<typeof CompDef>(res => res(CompDef as any))
  410. const E = defineCustomElement(defineAsyncComponent(() => p))
  411. customElements.define('my-async-el-emits', E)
  412. container.innerHTML = `<my-async-el-emits></my-async-el-emits>`
  413. const e = container.childNodes[0] as VueElement
  414. const spy = vi.fn()
  415. e.addEventListener('my-click', spy)
  416. // this feels brittle but seems necessary to reach the node in the DOM.
  417. await customElements.whenDefined('my-async-el-emits')
  418. await nextTick()
  419. await nextTick()
  420. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click'))
  421. expect(spy).toHaveBeenCalled()
  422. expect(spy.mock.calls[0][0]).toMatchObject({
  423. detail: [1],
  424. })
  425. })
  426. // #7293
  427. test('emit in an async component wrapper with properties bound', async () => {
  428. const E = defineCustomElement(
  429. defineAsyncComponent(
  430. () => new Promise<typeof CompDef>(res => res(CompDef as any)),
  431. ),
  432. )
  433. customElements.define('my-async-el-props-emits', E)
  434. container.innerHTML = `<my-async-el-props-emits id="my_async_el_props_emits"></my-async-el-props-emits>`
  435. const e = container.childNodes[0] as VueElement
  436. const spy = vi.fn()
  437. e.addEventListener('my-click', spy)
  438. await customElements.whenDefined('my-async-el-props-emits')
  439. await nextTick()
  440. await nextTick()
  441. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click'))
  442. expect(spy).toHaveBeenCalled()
  443. expect(spy.mock.calls[0][0]).toMatchObject({
  444. detail: [1],
  445. })
  446. })
  447. })
  448. describe('slots', () => {
  449. const E = defineCustomElement({
  450. render() {
  451. return [
  452. h('div', null, [
  453. renderSlot(this.$slots, 'default', undefined, () => [
  454. h('div', 'fallback'),
  455. ]),
  456. ]),
  457. h('div', null, renderSlot(this.$slots, 'named')),
  458. ]
  459. },
  460. })
  461. customElements.define('my-el-slots', E)
  462. test('default slot', () => {
  463. container.innerHTML = `<my-el-slots><span>hi</span></my-el-slots>`
  464. const e = container.childNodes[0] as VueElement
  465. // native slots allocation does not affect innerHTML, so we just
  466. // verify that we've rendered the correct native slots here...
  467. expect(e.shadowRoot!.innerHTML).toBe(
  468. `<div><slot><div>fallback</div></slot></div><div><slot name="named"></slot></div>`,
  469. )
  470. })
  471. })
  472. describe('provide/inject', () => {
  473. const Consumer = defineCustomElement({
  474. setup() {
  475. const foo = inject<Ref>('foo')!
  476. return () => h('div', foo.value)
  477. },
  478. })
  479. customElements.define('my-consumer', Consumer)
  480. test('over nested usage', async () => {
  481. const foo = ref('injected!')
  482. const Provider = defineCustomElement({
  483. provide: {
  484. foo,
  485. },
  486. render() {
  487. return h('my-consumer')
  488. },
  489. })
  490. customElements.define('my-provider', Provider)
  491. container.innerHTML = `<my-provider><my-provider>`
  492. const provider = container.childNodes[0] as VueElement
  493. const consumer = provider.shadowRoot!.childNodes[0] as VueElement
  494. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`)
  495. foo.value = 'changed!'
  496. await nextTick()
  497. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
  498. })
  499. test('over slot composition', async () => {
  500. const foo = ref('injected!')
  501. const Provider = defineCustomElement({
  502. provide: {
  503. foo,
  504. },
  505. render() {
  506. return renderSlot(this.$slots, 'default')
  507. },
  508. })
  509. customElements.define('my-provider-2', Provider)
  510. container.innerHTML = `<my-provider-2><my-consumer></my-consumer><my-provider-2>`
  511. const provider = container.childNodes[0]
  512. const consumer = provider.childNodes[0] as VueElement
  513. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`)
  514. foo.value = 'changed!'
  515. await nextTick()
  516. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
  517. })
  518. test('inherited from ancestors', async () => {
  519. const fooA = ref('FooA!')
  520. const fooB = ref('FooB!')
  521. const ProviderA = defineCustomElement({
  522. provide: {
  523. fooA,
  524. },
  525. render() {
  526. return h('provider-b')
  527. },
  528. })
  529. const ProviderB = defineCustomElement({
  530. provide: {
  531. fooB,
  532. },
  533. render() {
  534. return h('my-multi-consumer')
  535. },
  536. })
  537. const Consumer = defineCustomElement({
  538. setup() {
  539. const fooA = inject<Ref>('fooA')!
  540. const fooB = inject<Ref>('fooB')!
  541. return () => h('div', `${fooA.value} ${fooB.value}`)
  542. },
  543. })
  544. customElements.define('provider-a', ProviderA)
  545. customElements.define('provider-b', ProviderB)
  546. customElements.define('my-multi-consumer', Consumer)
  547. container.innerHTML = `<provider-a><provider-a>`
  548. const providerA = container.childNodes[0] as VueElement
  549. const providerB = providerA.shadowRoot!.childNodes[0] as VueElement
  550. const consumer = providerB.shadowRoot!.childNodes[0] as VueElement
  551. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>FooA! FooB!</div>`)
  552. fooA.value = 'changedA!'
  553. fooB.value = 'changedB!'
  554. await nextTick()
  555. expect(consumer.shadowRoot!.innerHTML).toBe(
  556. `<div>changedA! changedB!</div>`,
  557. )
  558. })
  559. })
  560. describe('styles', () => {
  561. test('should attach styles to shadow dom', () => {
  562. const Foo = defineCustomElement({
  563. styles: [`div { color: red; }`],
  564. render() {
  565. return h('div', 'hello')
  566. },
  567. })
  568. customElements.define('my-el-with-styles', Foo)
  569. container.innerHTML = `<my-el-with-styles></my-el-with-styles>`
  570. const el = container.childNodes[0] as VueElement
  571. const style = el.shadowRoot?.querySelector('style')!
  572. expect(style.textContent).toBe(`div { color: red; }`)
  573. })
  574. })
  575. describe('async', () => {
  576. test('should work', async () => {
  577. const loaderSpy = vi.fn()
  578. const E = defineCustomElement(
  579. defineAsyncComponent(() => {
  580. loaderSpy()
  581. return Promise.resolve({
  582. props: ['msg'],
  583. styles: [`div { color: red }`],
  584. render(this: any) {
  585. return h('div', null, this.msg)
  586. },
  587. })
  588. }),
  589. )
  590. customElements.define('my-el-async', E)
  591. container.innerHTML =
  592. `<my-el-async msg="hello"></my-el-async>` +
  593. `<my-el-async msg="world"></my-el-async>`
  594. await new Promise(r => setTimeout(r))
  595. // loader should be called only once
  596. expect(loaderSpy).toHaveBeenCalledTimes(1)
  597. const e1 = container.childNodes[0] as VueElement
  598. const e2 = container.childNodes[1] as VueElement
  599. // should inject styles
  600. expect(e1.shadowRoot!.innerHTML).toBe(
  601. `<style>div { color: red }</style><div>hello</div>`,
  602. )
  603. expect(e2.shadowRoot!.innerHTML).toBe(
  604. `<style>div { color: red }</style><div>world</div>`,
  605. )
  606. // attr
  607. e1.setAttribute('msg', 'attr')
  608. await nextTick()
  609. expect((e1 as any).msg).toBe('attr')
  610. expect(e1.shadowRoot!.innerHTML).toBe(
  611. `<style>div { color: red }</style><div>attr</div>`,
  612. )
  613. // props
  614. expect(`msg` in e1).toBe(true)
  615. ;(e1 as any).msg = 'prop'
  616. expect(e1.getAttribute('msg')).toBe('prop')
  617. expect(e1.shadowRoot!.innerHTML).toBe(
  618. `<style>div { color: red }</style><div>prop</div>`,
  619. )
  620. })
  621. test('set DOM property before resolve', async () => {
  622. const E = defineCustomElement(
  623. defineAsyncComponent(() => {
  624. return Promise.resolve({
  625. props: ['msg'],
  626. setup(props) {
  627. expect(typeof props.msg).toBe('string')
  628. },
  629. render(this: any) {
  630. return h('div', this.msg)
  631. },
  632. })
  633. }),
  634. )
  635. customElements.define('my-el-async-2', E)
  636. const e1 = new E()
  637. // set property before connect
  638. e1.msg = 'hello'
  639. const e2 = new E()
  640. container.appendChild(e1)
  641. container.appendChild(e2)
  642. // set property after connect but before resolve
  643. e2.msg = 'world'
  644. await new Promise(r => setTimeout(r))
  645. expect(e1.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  646. expect(e2.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  647. e1.msg = 'world'
  648. expect(e1.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  649. e2.msg = 'hello'
  650. expect(e2.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  651. })
  652. test('Number prop casting before resolve', async () => {
  653. const E = defineCustomElement(
  654. defineAsyncComponent(() => {
  655. return Promise.resolve({
  656. props: { n: Number },
  657. setup(props) {
  658. expect(props.n).toBe(20)
  659. },
  660. render(this: any) {
  661. return h('div', this.n + ',' + typeof this.n)
  662. },
  663. })
  664. }),
  665. )
  666. customElements.define('my-el-async-3', E)
  667. container.innerHTML = `<my-el-async-3 n="2e1"></my-el-async-3>`
  668. await new Promise(r => setTimeout(r))
  669. const e = container.childNodes[0] as VueElement
  670. expect(e.shadowRoot!.innerHTML).toBe(`<div>20,number</div>`)
  671. })
  672. test('with slots', async () => {
  673. const E = defineCustomElement(
  674. defineAsyncComponent(() => {
  675. return Promise.resolve({
  676. render(this: any) {
  677. return [
  678. h('div', null, [
  679. renderSlot(this.$slots, 'default', undefined, () => [
  680. h('div', 'fallback'),
  681. ]),
  682. ]),
  683. h('div', null, renderSlot(this.$slots, 'named')),
  684. ]
  685. },
  686. })
  687. }),
  688. )
  689. customElements.define('my-el-async-slots', E)
  690. container.innerHTML = `<my-el-async-slots><span>hi</span></my-el-async-slots>`
  691. await new Promise(r => setTimeout(r))
  692. const e = container.childNodes[0] as VueElement
  693. expect(e.shadowRoot!.innerHTML).toBe(
  694. `<div><slot><div>fallback</div></slot></div><div><slot name="named"></slot></div>`,
  695. )
  696. })
  697. })
  698. // #9885
  699. test('avoid double mount when prop is set immediately after mount', () => {
  700. customElements.define(
  701. 'my-input-dupe',
  702. defineCustomElement({
  703. props: {
  704. value: String,
  705. },
  706. render() {
  707. return 'hello'
  708. },
  709. }),
  710. )
  711. createApp({
  712. render() {
  713. return h('div', [
  714. h('my-input-dupe', {
  715. onVnodeMounted(vnode) {
  716. vnode.el!.value = 'fesfes'
  717. },
  718. }),
  719. ])
  720. },
  721. }).mount(container)
  722. expect(container.children[0].children[0].shadowRoot?.innerHTML).toBe(
  723. 'hello',
  724. )
  725. })
  726. })