customElement.spec.ts 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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. },
  124. render() {
  125. return [this.foo, typeof this.foo, this.bar, typeof this.bar].join(
  126. ' '
  127. )
  128. }
  129. })
  130. customElements.define('my-el-props-cast', E)
  131. container.innerHTML = `<my-el-props-cast foo="1"></my-el-props-cast>`
  132. const e = container.childNodes[0] as VueElement
  133. expect(e.shadowRoot!.innerHTML).toBe(`1 number false boolean`)
  134. e.setAttribute('bar', '')
  135. await nextTick()
  136. expect(e.shadowRoot!.innerHTML).toBe(`1 number true boolean`)
  137. e.setAttribute('foo', '2e1')
  138. await nextTick()
  139. expect(e.shadowRoot!.innerHTML).toBe(`20 number true boolean`)
  140. })
  141. test('handling properties set before upgrading', () => {
  142. const E = defineCustomElement({
  143. props: ['foo'],
  144. render() {
  145. return `foo: ${this.foo}`
  146. }
  147. })
  148. const el = document.createElement('my-el-upgrade') as any
  149. el.foo = 'hello'
  150. container.appendChild(el)
  151. customElements.define('my-el-upgrade', E)
  152. expect(el.shadowRoot.innerHTML).toBe(`foo: hello`)
  153. })
  154. })
  155. describe('emits', () => {
  156. const E = defineCustomElement({
  157. setup(_, { emit }) {
  158. emit('created')
  159. return () =>
  160. h('div', {
  161. onClick: () => emit('my-click', 1)
  162. })
  163. }
  164. })
  165. customElements.define('my-el-emits', E)
  166. test('emit on connect', () => {
  167. const e = new E()
  168. const spy = jest.fn()
  169. e.addEventListener('created', spy)
  170. container.appendChild(e)
  171. expect(spy).toHaveBeenCalled()
  172. })
  173. test('emit on interaction', () => {
  174. container.innerHTML = `<my-el-emits></my-el-emits>`
  175. const e = container.childNodes[0] as VueElement
  176. const spy = jest.fn()
  177. e.addEventListener('my-click', spy)
  178. e.shadowRoot!.childNodes[0].dispatchEvent(new CustomEvent('click'))
  179. expect(spy).toHaveBeenCalled()
  180. expect(spy.mock.calls[0][0]).toMatchObject({
  181. detail: [1]
  182. })
  183. })
  184. })
  185. describe('slots', () => {
  186. const E = defineCustomElement({
  187. render() {
  188. return [
  189. h('div', null, [
  190. renderSlot(this.$slots, 'default', undefined, () => [
  191. h('div', 'fallback')
  192. ])
  193. ]),
  194. h('div', null, renderSlot(this.$slots, 'named'))
  195. ]
  196. }
  197. })
  198. customElements.define('my-el-slots', E)
  199. test('default slot', () => {
  200. container.innerHTML = `<my-el-slots><span>hi</span></my-el-slots>`
  201. const e = container.childNodes[0] as VueElement
  202. // native slots allocation does not affect innerHTML, so we just
  203. // verify that we've rendered the correct native slots here...
  204. expect(e.shadowRoot!.innerHTML).toBe(
  205. `<div><slot><div>fallback</div></slot></div><div><slot name="named"></slot></div>`
  206. )
  207. })
  208. })
  209. describe('provide/inject', () => {
  210. const Consumer = defineCustomElement({
  211. setup() {
  212. const foo = inject<Ref>('foo')!
  213. return () => h('div', foo.value)
  214. }
  215. })
  216. customElements.define('my-consumer', Consumer)
  217. test('over nested usage', async () => {
  218. const foo = ref('injected!')
  219. const Provider = defineCustomElement({
  220. provide: {
  221. foo
  222. },
  223. render() {
  224. return h('my-consumer')
  225. }
  226. })
  227. customElements.define('my-provider', Provider)
  228. container.innerHTML = `<my-provider><my-provider>`
  229. const provider = container.childNodes[0] as VueElement
  230. const consumer = provider.shadowRoot!.childNodes[0] as VueElement
  231. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`)
  232. foo.value = 'changed!'
  233. await nextTick()
  234. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
  235. })
  236. test('over slot composition', async () => {
  237. const foo = ref('injected!')
  238. const Provider = defineCustomElement({
  239. provide: {
  240. foo
  241. },
  242. render() {
  243. return renderSlot(this.$slots, 'default')
  244. }
  245. })
  246. customElements.define('my-provider-2', Provider)
  247. container.innerHTML = `<my-provider-2><my-consumer></my-consumer><my-provider-2>`
  248. const provider = container.childNodes[0]
  249. const consumer = provider.childNodes[0] as VueElement
  250. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>injected!</div>`)
  251. foo.value = 'changed!'
  252. await nextTick()
  253. expect(consumer.shadowRoot!.innerHTML).toBe(`<div>changed!</div>`)
  254. })
  255. })
  256. describe('styles', () => {
  257. test('should attach styles to shadow dom', () => {
  258. const Foo = defineCustomElement({
  259. styles: [`div { color: red; }`],
  260. render() {
  261. return h('div', 'hello')
  262. }
  263. })
  264. customElements.define('my-el-with-styles', Foo)
  265. container.innerHTML = `<my-el-with-styles></my-el-with-styles>`
  266. const el = container.childNodes[0] as VueElement
  267. const style = el.shadowRoot?.querySelector('style')!
  268. expect(style.textContent).toBe(`div { color: red; }`)
  269. })
  270. })
  271. describe('async', () => {
  272. test('should work', async () => {
  273. const loaderSpy = jest.fn()
  274. const E = defineCustomElement(
  275. defineAsyncComponent(() => {
  276. loaderSpy()
  277. return Promise.resolve({
  278. props: ['msg'],
  279. styles: [`div { color: red }`],
  280. render(this: any) {
  281. return h('div', null, this.msg)
  282. }
  283. })
  284. })
  285. )
  286. customElements.define('my-el-async', E)
  287. container.innerHTML =
  288. `<my-el-async msg="hello"></my-el-async>` +
  289. `<my-el-async msg="world"></my-el-async>`
  290. await new Promise(r => setTimeout(r))
  291. // loader should be called only once
  292. expect(loaderSpy).toHaveBeenCalledTimes(1)
  293. const e1 = container.childNodes[0] as VueElement
  294. const e2 = container.childNodes[1] as VueElement
  295. // should inject styles
  296. expect(e1.shadowRoot!.innerHTML).toBe(
  297. `<div>hello</div><style>div { color: red }</style>`
  298. )
  299. expect(e2.shadowRoot!.innerHTML).toBe(
  300. `<div>world</div><style>div { color: red }</style>`
  301. )
  302. // attr
  303. e1.setAttribute('msg', 'attr')
  304. await nextTick()
  305. expect((e1 as any).msg).toBe('attr')
  306. expect(e1.shadowRoot!.innerHTML).toBe(
  307. `<div>attr</div><style>div { color: red }</style>`
  308. )
  309. // props
  310. expect(`msg` in e1).toBe(true)
  311. ;(e1 as any).msg = 'prop'
  312. expect(e1.getAttribute('msg')).toBe('prop')
  313. expect(e1.shadowRoot!.innerHTML).toBe(
  314. `<div>prop</div><style>div { color: red }</style>`
  315. )
  316. })
  317. test('set DOM property before resolve', async () => {
  318. const E = defineCustomElement(
  319. defineAsyncComponent(() => {
  320. return Promise.resolve({
  321. props: ['msg'],
  322. render(this: any) {
  323. return h('div', this.msg)
  324. }
  325. })
  326. })
  327. )
  328. customElements.define('my-el-async-2', E)
  329. const e1 = new E()
  330. // set property before connect
  331. e1.msg = 'hello'
  332. const e2 = new E()
  333. container.appendChild(e1)
  334. container.appendChild(e2)
  335. // set property after connect but before resolve
  336. e2.msg = 'world'
  337. await new Promise(r => setTimeout(r))
  338. expect(e1.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  339. expect(e2.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  340. e1.msg = 'world'
  341. expect(e1.shadowRoot!.innerHTML).toBe(`<div>world</div>`)
  342. e2.msg = 'hello'
  343. expect(e2.shadowRoot!.innerHTML).toBe(`<div>hello</div>`)
  344. })
  345. })
  346. })