customElement.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. import {
  2. defineAsyncComponent,
  3. defineCustomElement,
  4. h,
  5. inject,
  6. nextTick,
  7. Ref,
  8. ref,
  9. renderSlot,
  10. VueElement
  11. } from '../src'
  12. describe('defineCustomElement', () => {
  13. const container = document.createElement('div')
  14. document.body.appendChild(container)
  15. beforeEach(() => {
  16. container.innerHTML = ''
  17. })
  18. describe('mounting/unmount', () => {
  19. const E = defineCustomElement({
  20. props: {
  21. msg: {
  22. type: String,
  23. default: 'hello'
  24. }
  25. },
  26. render() {
  27. return h('div', this.msg)
  28. }
  29. })
  30. customElements.define('my-element', E)
  31. test('should work', () => {
  32. container.innerHTML = `<my-element></my-element>`
  33. const e = container.childNodes[0] as VueElement
  34. expect(e).toBeInstanceOf(E)
  35. expect(e._instance).toBeTruthy()
  36. expect(e.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  37. })
  38. test('should work w/ manual instantiation', () => {
  39. const e = new E({ msg: 'inline' })
  40. // should lazy init
  41. expect(e._instance).toBe(null)
  42. // should initialize on connect
  43. container.appendChild(e)
  44. expect(e._instance).toBeTruthy()
  45. expect(e.shadowRoot!.innerHTML).toBe(`<div>inline</div>`)
  46. })
  47. test('should unmount on remove', async () => {
  48. container.innerHTML = `<my-element></my-element>`
  49. const e = container.childNodes[0] as VueElement
  50. container.removeChild(e)
  51. await nextTick()
  52. expect(e._instance).toBe(null)
  53. expect(e.shadowRoot!.innerHTML).toBe('')
  54. })
  55. test('should not unmount on move', async () => {
  56. container.innerHTML = `<div><my-element></my-element></div>`
  57. const e = container.childNodes[0].childNodes[0] as VueElement
  58. const i = e._instance
  59. // moving from one parent to another - this will trigger both disconnect
  60. // and connected callbacks synchronously
  61. container.appendChild(e)
  62. await nextTick()
  63. // should be the same instance
  64. expect(e._instance).toBe(i)
  65. expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div>')
  66. })
  67. })
  68. describe('props', () => {
  69. const E = defineCustomElement({
  70. props: ['foo', 'bar', 'bazQux'],
  71. render() {
  72. return [
  73. h('div', null, this.foo),
  74. h('div', null, this.bazQux || (this.bar && this.bar.x))
  75. ]
  76. }
  77. })
  78. customElements.define('my-el-props', E)
  79. test('props via attribute', async () => {
  80. // bazQux should map to `baz-qux` attribute
  81. container.innerHTML = `<my-el-props foo="hello" baz-qux="bye"></my-el-props>`
  82. const e = container.childNodes[0] as VueElement
  83. expect(e.shadowRoot!.innerHTML).toBe('<div>hello</div><div>bye</div>')
  84. // change attr
  85. e.setAttribute('foo', 'changed')
  86. await nextTick()
  87. expect(e.shadowRoot!.innerHTML).toBe('<div>changed</div><div>bye</div>')
  88. e.setAttribute('baz-qux', 'changed')
  89. await nextTick()
  90. expect(e.shadowRoot!.innerHTML).toBe(
  91. '<div>changed</div><div>changed</div>'
  92. )
  93. })
  94. test('props via properties', async () => {
  95. const e = new E()
  96. e.foo = 'one'
  97. e.bar = { x: 'two' }
  98. container.appendChild(e)
  99. expect(e.shadowRoot!.innerHTML).toBe('<div>one</div><div>two</div>')
  100. // reflect
  101. // should reflect primitive value
  102. expect(e.getAttribute('foo')).toBe('one')
  103. // should not reflect rich data
  104. expect(e.hasAttribute('bar')).toBe(false)
  105. e.foo = 'three'
  106. await nextTick()
  107. expect(e.shadowRoot!.innerHTML).toBe('<div>three</div><div>two</div>')
  108. expect(e.getAttribute('foo')).toBe('three')
  109. e.foo = null
  110. await nextTick()
  111. expect(e.shadowRoot!.innerHTML).toBe('<div></div><div>two</div>')
  112. expect(e.hasAttribute('foo')).toBe(false)
  113. e.bazQux = 'four'
  114. await nextTick()
  115. expect(e.shadowRoot!.innerHTML).toBe('<div></div><div>four</div>')
  116. expect(e.getAttribute('baz-qux')).toBe('four')
  117. })
  118. test('attribute -> prop type casting', async () => {
  119. const E = defineCustomElement({
  120. props: {
  121. foo: Number,
  122. bar: Boolean,
  123. baz: String
  124. },
  125. render() {
  126. return [
  127. this.foo,
  128. typeof this.foo,
  129. this.bar,
  130. typeof this.bar,
  131. this.baz,
  132. typeof this.baz
  133. ].join(' ')
  134. }
  135. })
  136. customElements.define('my-el-props-cast', E)
  137. container.innerHTML = `<my-el-props-cast foo="1" baz="12345"></my-el-props-cast>`
  138. const e = container.childNodes[0] as VueElement
  139. expect(e.shadowRoot!.innerHTML).toBe(
  140. `1 number false boolean 12345 string`
  141. )
  142. e.setAttribute('bar', '')
  143. await nextTick()
  144. expect(e.shadowRoot!.innerHTML).toBe(`1 number true boolean 12345 string`)
  145. e.setAttribute('foo', '2e1')
  146. await nextTick()
  147. expect(e.shadowRoot!.innerHTML).toBe(
  148. `20 number true boolean 12345 string`
  149. )
  150. e.setAttribute('baz', '2e1')
  151. await nextTick()
  152. expect(e.shadowRoot!.innerHTML).toBe(`20 number true boolean 2e1 string`)
  153. })
  154. test('handling properties set before upgrading', () => {
  155. const E = defineCustomElement({
  156. props: ['foo'],
  157. render() {
  158. return `foo: ${this.foo}`
  159. }
  160. })
  161. const el = document.createElement('my-el-upgrade') as any
  162. el.foo = 'hello'
  163. container.appendChild(el)
  164. customElements.define('my-el-upgrade', E)
  165. expect(el.shadowRoot.innerHTML).toBe(`foo: hello`)
  166. })
  167. })
  168. describe('emits', () => {
  169. const E = defineCustomElement({
  170. setup(_, { emit }) {
  171. emit('created')
  172. return () =>
  173. h('div', {
  174. onClick: () => emit('my-click', 1)
  175. })
  176. }
  177. })
  178. customElements.define('my-el-emits', E)
  179. test('emit on connect', () => {
  180. const e = new E()
  181. const spy = jest.fn()
  182. e.addEventListener('created', spy)
  183. container.appendChild(e)
  184. expect(spy).toHaveBeenCalled()
  185. })
  186. test('emit on interaction', () => {
  187. container.innerHTML = `<my-el-emits></my-el-emits>`
  188. const e = container.childNodes[0] as VueElement
  189. const spy = jest.fn()
  190. e.addEventListener('my-click', spy)
  191. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click'))
  192. expect(spy).toHaveBeenCalled()
  193. expect(spy.mock.calls[0][0]).toMatchObject({
  194. detail: [1]
  195. })
  196. })
  197. })
  198. describe('slots', () => {
  199. const E = defineCustomElement({
  200. render() {
  201. return [
  202. h('div', null, [
  203. renderSlot(this.$slots, 'default', undefined, () => [
  204. h('div', 'fallback')
  205. ])
  206. ]),
  207. h('div', null, renderSlot(this.$slots, 'named'))
  208. ]
  209. }
  210. })
  211. customElements.define('my-el-slots', E)
  212. test('default slot', () => {
  213. container.innerHTML = `<my-el-slots><span>hi</span></my-el-slots>`
  214. const e = container.childNodes[0] as VueElement
  215. // native slots allocation does not affect innerHTML, so we just
  216. // verify that we've rendered the correct native slots here...
  217. expect(e.shadowRoot!.innerHTML).toBe(
  218. `<div><slot><div>fallback</div></slot></div><div><slot name="named"></slot></div>`
  219. )
  220. })
  221. })
  222. describe('provide/inject', () => {
  223. const Consumer = defineCustomElement({
  224. setup() {
  225. const foo = inject<Ref>('foo')!
  226. return () => h('div', foo.value)
  227. }
  228. })
  229. customElements.define('my-consumer', Consumer)
  230. test('over nested usage', async () => {
  231. const foo = ref('injected!')
  232. const Provider = defineCustomElement({
  233. provide: {
  234. foo
  235. },
  236. render() {
  237. return h('my-consumer')
  238. }
  239. })
  240. customElements.define('my-provider', Provider)
  241. container.innerHTML = `<my-provider><my-provider>`
  242. const provider = container.childNodes[0] as VueElement
  243. const consumer = provider.shadowRoot!.childNodes[0] as VueElement
  244. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`)
  245. foo.value = 'changed!'
  246. await nextTick()
  247. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
  248. })
  249. test('over slot composition', async () => {
  250. const foo = ref('injected!')
  251. const Provider = defineCustomElement({
  252. provide: {
  253. foo
  254. },
  255. render() {
  256. return renderSlot(this.$slots, 'default')
  257. }
  258. })
  259. customElements.define('my-provider-2', Provider)
  260. container.innerHTML = `<my-provider-2><my-consumer></my-consumer><my-provider-2>`
  261. const provider = container.childNodes[0]
  262. const consumer = provider.childNodes[0] as VueElement
  263. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`)
  264. foo.value = 'changed!'
  265. await nextTick()
  266. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
  267. })
  268. })
  269. describe('styles', () => {
  270. test('should attach styles to shadow dom', () => {
  271. const Foo = defineCustomElement({
  272. styles: [`div { color: red; }`],
  273. render() {
  274. return h('div', 'hello')
  275. }
  276. })
  277. customElements.define('my-el-with-styles', Foo)
  278. container.innerHTML = `<my-el-with-styles></my-el-with-styles>`
  279. const el = container.childNodes[0] as VueElement
  280. const style = el.shadowRoot?.querySelector('style')!
  281. expect(style.textContent).toBe(`div { color: red; }`)
  282. })
  283. })
  284. describe('async', () => {
  285. test('should work', async () => {
  286. const loaderSpy = jest.fn()
  287. const E = defineCustomElement(
  288. defineAsyncComponent(() => {
  289. loaderSpy()
  290. return Promise.resolve({
  291. props: ['msg'],
  292. styles: [`div { color: red }`],
  293. render(this: any) {
  294. return h('div', null, this.msg)
  295. }
  296. })
  297. })
  298. )
  299. customElements.define('my-el-async', E)
  300. container.innerHTML =
  301. `<my-el-async msg="hello"></my-el-async>` +
  302. `<my-el-async msg="world"></my-el-async>`
  303. await new Promise(r => setTimeout(r))
  304. // loader should be called only once
  305. expect(loaderSpy).toHaveBeenCalledTimes(1)
  306. const e1 = container.childNodes[0] as VueElement
  307. const e2 = container.childNodes[1] as VueElement
  308. // should inject styles
  309. expect(e1.shadowRoot!.innerHTML).toBe(
  310. `<div>hello</div><style>div { color: red }</style>`
  311. )
  312. expect(e2.shadowRoot!.innerHTML).toBe(
  313. `<div>world</div><style>div { color: red }</style>`
  314. )
  315. // attr
  316. e1.setAttribute('msg', 'attr')
  317. await nextTick()
  318. expect((e1 as any).msg).toBe('attr')
  319. expect(e1.shadowRoot!.innerHTML).toBe(
  320. `<div>attr</div><style>div { color: red }</style>`
  321. )
  322. // props
  323. expect(`msg` in e1).toBe(true)
  324. ;(e1 as any).msg = 'prop'
  325. expect(e1.getAttribute('msg')).toBe('prop')
  326. expect(e1.shadowRoot!.innerHTML).toBe(
  327. `<div>prop</div><style>div { color: red }</style>`
  328. )
  329. })
  330. test('set DOM property before resolve', async () => {
  331. const E = defineCustomElement(
  332. defineAsyncComponent(() => {
  333. return Promise.resolve({
  334. props: ['msg'],
  335. render(this: any) {
  336. return h('div', this.msg)
  337. }
  338. })
  339. })
  340. )
  341. customElements.define('my-el-async-2', E)
  342. const e1 = new E()
  343. // set property before connect
  344. e1.msg = 'hello'
  345. const e2 = new E()
  346. container.appendChild(e1)
  347. container.appendChild(e2)
  348. // set property after connect but before resolve
  349. e2.msg = 'world'
  350. await new Promise(r => setTimeout(r))
  351. expect(e1.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  352. expect(e2.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  353. e1.msg = 'world'
  354. expect(e1.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  355. e2.msg = 'hello'
  356. expect(e2.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  357. })
  358. test('Number prop casting before resolve', async () => {
  359. const E = defineCustomElement(
  360. defineAsyncComponent(() => {
  361. return Promise.resolve({
  362. props: { n: Number },
  363. render(this: any) {
  364. return h('div', this.n + ',' + typeof this.n)
  365. }
  366. })
  367. })
  368. )
  369. customElements.define('my-el-async-3', E)
  370. container.innerHTML = `<my-el-async-3 n="2e1"></my-el-async-3>`
  371. await new Promise(r => setTimeout(r))
  372. const e = container.childNodes[0] as VueElement
  373. expect(e.shadowRoot!.innerHTML).toBe(`<div>20,number</div>`)
  374. })
  375. })
  376. })