customElement.spec.ts 23 KB

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