customElement.spec.ts 21 KB

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