customElement.spec.ts 21 KB

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