customElement.spec.ts 24 KB

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