componentFunctional.spec.ts 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import Vue from '@vue/compat'
  2. import {
  3. DeprecationTypes,
  4. deprecationData,
  5. toggleDeprecationWarning
  6. } from '../../runtime-core/src/compat/compatConfig'
  7. beforeEach(() => {
  8. toggleDeprecationWarning(true)
  9. Vue.configureCompat({
  10. MODE: 2,
  11. GLOBAL_MOUNT: 'suppress-warning'
  12. })
  13. })
  14. afterEach(() => {
  15. toggleDeprecationWarning(false)
  16. Vue.configureCompat({ MODE: 3 })
  17. })
  18. test('COMPONENT_FUNCTIONAL', async () => {
  19. const func = {
  20. name: 'Func',
  21. functional: true,
  22. props: {
  23. x: String
  24. },
  25. inject: ['foo'],
  26. render: (h: any, { data, props, injections, slots }: any) => {
  27. return h('div', { id: props.x, class: data.class }, [
  28. h('div', { class: 'inject' }, injections.foo),
  29. h('div', { class: 'slot' }, slots().default)
  30. ])
  31. }
  32. }
  33. const vm = new Vue({
  34. provide() {
  35. return {
  36. foo: 123
  37. }
  38. },
  39. components: {
  40. func
  41. },
  42. template: `<func class="foo" x="foo">hello</func>`
  43. }).$mount()
  44. expect(vm.$el.id).toBe('foo')
  45. expect(vm.$el.className).toBe('foo')
  46. expect(vm.$el.querySelector('.inject').textContent).toBe('123')
  47. expect(vm.$el.querySelector('.slot').textContent).toBe('hello')
  48. expect(vm.$el.outerHTML).toMatchInlineSnapshot(
  49. `"<div id=\\"foo\\" class=\\"foo\\"><div class=\\"inject\\">123</div><div class=\\"slot\\">hello</div></div>"`
  50. )
  51. expect(
  52. (deprecationData[DeprecationTypes.COMPONENT_FUNCTIONAL]
  53. .message as Function)(func)
  54. ).toHaveBeenWarned()
  55. })