customElement.spec.ts 23 KB

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