customElement.spec.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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. test('support direct setup function syntax with extra options', () => {
  313. const E = defineCustomElement(
  314. props => {
  315. return () => props.text
  316. },
  317. {
  318. props: {
  319. text: String,
  320. },
  321. },
  322. )
  323. customElements.define('my-el-setup-with-props', E)
  324. container.innerHTML = `<my-el-setup-with-props text="hello"></my-el-setup-with-props>`
  325. const e = container.childNodes[0] as VueElement
  326. expect(e.shadowRoot!.innerHTML).toBe('hello')
  327. })
  328. })
  329. describe('attrs', () => {
  330. const E = defineCustomElement({
  331. render() {
  332. return [h('div', null, this.$attrs.foo as string)]
  333. },
  334. })
  335. customElements.define('my-el-attrs', E)
  336. test('attrs via attribute', async () => {
  337. container.innerHTML = `<my-el-attrs foo="hello"></my-el-attrs>`
  338. const e = container.childNodes[0] as VueElement
  339. expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div>')
  340. e.setAttribute('foo', 'changed')
  341. await nextTick()
  342. expect(e.shadowRoot!.innerHTML).toBe('<div>changed</div>')
  343. })
  344. test('non-declared properties should not show up in $attrs', () => {
  345. const e = new E()
  346. // @ts-expect-error
  347. e.foo = '123'
  348. container.appendChild(e)
  349. expect(e.shadowRoot!.innerHTML).toBe('<div></div>')
  350. })
  351. })
  352. describe('emits', () => {
  353. const CompDef = defineComponent({
  354. setup(_, { emit }) {
  355. emit('created')
  356. return () =>
  357. h('div', {
  358. onClick: () => {
  359. emit('my-click', 1)
  360. },
  361. onMousedown: () => {
  362. emit('myEvent', 1) // validate hyphenation
  363. },
  364. })
  365. },
  366. })
  367. const E = defineCustomElement(CompDef)
  368. customElements.define('my-el-emits', E)
  369. test('emit on connect', () => {
  370. const e = new E()
  371. const spy = vi.fn()
  372. e.addEventListener('created', spy)
  373. container.appendChild(e)
  374. expect(spy).toHaveBeenCalled()
  375. })
  376. test('emit on interaction', () => {
  377. container.innerHTML = `<my-el-emits></my-el-emits>`
  378. const e = container.childNodes[0] as VueElement
  379. const spy = vi.fn()
  380. e.addEventListener('my-click', spy)
  381. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click'))
  382. expect(spy).toHaveBeenCalledTimes(1)
  383. expect(spy.mock.calls[0][0]).toMatchObject({
  384. detail: [1],
  385. })
  386. })
  387. // #5373
  388. test('case transform for camelCase event', () => {
  389. container.innerHTML = `<my-el-emits></my-el-emits>`
  390. const e = container.childNodes[0] as VueElement
  391. const spy1 = vi.fn()
  392. e.addEventListener('myEvent', spy1)
  393. const spy2 = vi.fn()
  394. // emitting myEvent, but listening for my-event. This happens when
  395. // using the custom element in a Vue template
  396. e.addEventListener('my-event', spy2)
  397. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('mousedown'))
  398. expect(spy1).toHaveBeenCalledTimes(1)
  399. expect(spy2).toHaveBeenCalledTimes(1)
  400. })
  401. test('emit from within async component wrapper', async () => {
  402. const p = new Promise<typeof CompDef>(res => res(CompDef as any))
  403. const E = defineCustomElement(defineAsyncComponent(() => p))
  404. customElements.define('my-async-el-emits', E)
  405. container.innerHTML = `<my-async-el-emits></my-async-el-emits>`
  406. const e = container.childNodes[0] as VueElement
  407. const spy = vi.fn()
  408. e.addEventListener('my-click', spy)
  409. // this feels brittle but seems necessary to reach the node in the DOM.
  410. await customElements.whenDefined('my-async-el-emits')
  411. await nextTick()
  412. await nextTick()
  413. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click'))
  414. expect(spy).toHaveBeenCalled()
  415. expect(spy.mock.calls[0][0]).toMatchObject({
  416. detail: [1],
  417. })
  418. })
  419. // #7293
  420. test('emit in an async component wrapper with properties bound', async () => {
  421. const E = defineCustomElement(
  422. defineAsyncComponent(
  423. () => new Promise<typeof CompDef>(res => res(CompDef as any)),
  424. ),
  425. )
  426. customElements.define('my-async-el-props-emits', E)
  427. container.innerHTML = `<my-async-el-props-emits id="my_async_el_props_emits"></my-async-el-props-emits>`
  428. const e = container.childNodes[0] as VueElement
  429. const spy = vi.fn()
  430. e.addEventListener('my-click', spy)
  431. await customElements.whenDefined('my-async-el-props-emits')
  432. await nextTick()
  433. await nextTick()
  434. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click'))
  435. expect(spy).toHaveBeenCalled()
  436. expect(spy.mock.calls[0][0]).toMatchObject({
  437. detail: [1],
  438. })
  439. })
  440. })
  441. describe('slots', () => {
  442. const E = defineCustomElement({
  443. render() {
  444. return [
  445. h('div', null, [
  446. renderSlot(this.$slots, 'default', undefined, () => [
  447. h('div', 'fallback'),
  448. ]),
  449. ]),
  450. h('div', null, renderSlot(this.$slots, 'named')),
  451. ]
  452. },
  453. })
  454. customElements.define('my-el-slots', E)
  455. test('default slot', () => {
  456. container.innerHTML = `<my-el-slots><span>hi</span></my-el-slots>`
  457. const e = container.childNodes[0] as VueElement
  458. // native slots allocation does not affect innerHTML, so we just
  459. // verify that we've rendered the correct native slots here...
  460. expect(e.shadowRoot!.innerHTML).toBe(
  461. `<div><slot><div>fallback</div></slot></div><div><slot name="named"></slot></div>`,
  462. )
  463. })
  464. })
  465. describe('provide/inject', () => {
  466. const Consumer = defineCustomElement({
  467. setup() {
  468. const foo = inject<Ref>('foo')!
  469. return () => h('div', foo.value)
  470. },
  471. })
  472. customElements.define('my-consumer', Consumer)
  473. test('over nested usage', async () => {
  474. const foo = ref('injected!')
  475. const Provider = defineCustomElement({
  476. provide: {
  477. foo,
  478. },
  479. render() {
  480. return h('my-consumer')
  481. },
  482. })
  483. customElements.define('my-provider', Provider)
  484. container.innerHTML = `<my-provider><my-provider>`
  485. const provider = container.childNodes[0] as VueElement
  486. const consumer = provider.shadowRoot!.childNodes[0] as VueElement
  487. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`)
  488. foo.value = 'changed!'
  489. await nextTick()
  490. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
  491. })
  492. test('over slot composition', async () => {
  493. const foo = ref('injected!')
  494. const Provider = defineCustomElement({
  495. provide: {
  496. foo,
  497. },
  498. render() {
  499. return renderSlot(this.$slots, 'default')
  500. },
  501. })
  502. customElements.define('my-provider-2', Provider)
  503. container.innerHTML = `<my-provider-2><my-consumer></my-consumer><my-provider-2>`
  504. const provider = container.childNodes[0]
  505. const consumer = provider.childNodes[0] as VueElement
  506. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`)
  507. foo.value = 'changed!'
  508. await nextTick()
  509. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
  510. })
  511. test('inherited from ancestors', async () => {
  512. const fooA = ref('FooA!')
  513. const fooB = ref('FooB!')
  514. const ProviderA = defineCustomElement({
  515. provide: {
  516. fooA,
  517. },
  518. render() {
  519. return h('provider-b')
  520. },
  521. })
  522. const ProviderB = defineCustomElement({
  523. provide: {
  524. fooB,
  525. },
  526. render() {
  527. return h('my-multi-consumer')
  528. },
  529. })
  530. const Consumer = defineCustomElement({
  531. setup() {
  532. const fooA = inject<Ref>('fooA')!
  533. const fooB = inject<Ref>('fooB')!
  534. return () => h('div', `${fooA.value} ${fooB.value}`)
  535. },
  536. })
  537. customElements.define('provider-a', ProviderA)
  538. customElements.define('provider-b', ProviderB)
  539. customElements.define('my-multi-consumer', Consumer)
  540. container.innerHTML = `<provider-a><provider-a>`
  541. const providerA = container.childNodes[0] as VueElement
  542. const providerB = providerA.shadowRoot!.childNodes[0] as VueElement
  543. const consumer = providerB.shadowRoot!.childNodes[0] as VueElement
  544. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>FooA! FooB!</div>`)
  545. fooA.value = 'changedA!'
  546. fooB.value = 'changedB!'
  547. await nextTick()
  548. expect(consumer.shadowRoot!.innerHTML).toBe(
  549. `<div>changedA! changedB!</div>`,
  550. )
  551. })
  552. })
  553. describe('styles', () => {
  554. test('should attach styles to shadow dom', () => {
  555. const Foo = defineCustomElement({
  556. styles: [`div { color: red; }`],
  557. render() {
  558. return h('div', 'hello')
  559. },
  560. })
  561. customElements.define('my-el-with-styles', Foo)
  562. container.innerHTML = `<my-el-with-styles></my-el-with-styles>`
  563. const el = container.childNodes[0] as VueElement
  564. const style = el.shadowRoot?.querySelector('style')!
  565. expect(style.textContent).toBe(`div { color: red; }`)
  566. })
  567. })
  568. describe('async', () => {
  569. test('should work', async () => {
  570. const loaderSpy = vi.fn()
  571. const E = defineCustomElement(
  572. defineAsyncComponent(() => {
  573. loaderSpy()
  574. return Promise.resolve({
  575. props: ['msg'],
  576. styles: [`div { color: red }`],
  577. render(this: any) {
  578. return h('div', null, this.msg)
  579. },
  580. })
  581. }),
  582. )
  583. customElements.define('my-el-async', E)
  584. container.innerHTML =
  585. `<my-el-async msg="hello"></my-el-async>` +
  586. `<my-el-async msg="world"></my-el-async>`
  587. await new Promise(r => setTimeout(r))
  588. // loader should be called only once
  589. expect(loaderSpy).toHaveBeenCalledTimes(1)
  590. const e1 = container.childNodes[0] as VueElement
  591. const e2 = container.childNodes[1] as VueElement
  592. // should inject styles
  593. expect(e1.shadowRoot!.innerHTML).toBe(
  594. `<style>div { color: red }</style><div>hello</div>`,
  595. )
  596. expect(e2.shadowRoot!.innerHTML).toBe(
  597. `<style>div { color: red }</style><div>world</div>`,
  598. )
  599. // attr
  600. e1.setAttribute('msg', 'attr')
  601. await nextTick()
  602. expect((e1 as any).msg).toBe('attr')
  603. expect(e1.shadowRoot!.innerHTML).toBe(
  604. `<style>div { color: red }</style><div>attr</div>`,
  605. )
  606. // props
  607. expect(`msg` in e1).toBe(true)
  608. ;(e1 as any).msg = 'prop'
  609. expect(e1.getAttribute('msg')).toBe('prop')
  610. expect(e1.shadowRoot!.innerHTML).toBe(
  611. `<style>div { color: red }</style><div>prop</div>`,
  612. )
  613. })
  614. test('set DOM property before resolve', async () => {
  615. const E = defineCustomElement(
  616. defineAsyncComponent(() => {
  617. return Promise.resolve({
  618. props: ['msg'],
  619. setup(props) {
  620. expect(typeof props.msg).toBe('string')
  621. },
  622. render(this: any) {
  623. return h('div', this.msg)
  624. },
  625. })
  626. }),
  627. )
  628. customElements.define('my-el-async-2', E)
  629. const e1 = new E()
  630. // set property before connect
  631. e1.msg = 'hello'
  632. const e2 = new E()
  633. container.appendChild(e1)
  634. container.appendChild(e2)
  635. // set property after connect but before resolve
  636. e2.msg = 'world'
  637. await new Promise(r => setTimeout(r))
  638. expect(e1.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  639. expect(e2.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  640. e1.msg = 'world'
  641. expect(e1.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  642. e2.msg = 'hello'
  643. expect(e2.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  644. })
  645. test('Number prop casting before resolve', async () => {
  646. const E = defineCustomElement(
  647. defineAsyncComponent(() => {
  648. return Promise.resolve({
  649. props: { n: Number },
  650. setup(props) {
  651. expect(props.n).toBe(20)
  652. },
  653. render(this: any) {
  654. return h('div', this.n + ',' + typeof this.n)
  655. },
  656. })
  657. }),
  658. )
  659. customElements.define('my-el-async-3', E)
  660. container.innerHTML = `<my-el-async-3 n="2e1"></my-el-async-3>`
  661. await new Promise(r => setTimeout(r))
  662. const e = container.childNodes[0] as VueElement
  663. expect(e.shadowRoot!.innerHTML).toBe(`<div>20,number</div>`)
  664. })
  665. test('with slots', async () => {
  666. const E = defineCustomElement(
  667. defineAsyncComponent(() => {
  668. return Promise.resolve({
  669. render(this: any) {
  670. return [
  671. h('div', null, [
  672. renderSlot(this.$slots, 'default', undefined, () => [
  673. h('div', 'fallback'),
  674. ]),
  675. ]),
  676. h('div', null, renderSlot(this.$slots, 'named')),
  677. ]
  678. },
  679. })
  680. }),
  681. )
  682. customElements.define('my-el-async-slots', E)
  683. container.innerHTML = `<my-el-async-slots><span>hi</span></my-el-async-slots>`
  684. await new Promise(r => setTimeout(r))
  685. const e = container.childNodes[0] as VueElement
  686. expect(e.shadowRoot!.innerHTML).toBe(
  687. `<div><slot><div>fallback</div></slot></div><div><slot name="named"></slot></div>`,
  688. )
  689. })
  690. })
  691. })