customElement.spec.ts 24 KB

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