componentFunctional.spec.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. describe('COMPONENT_FUNCTIONAL', () => {
  19. test('basic usage', async () => {
  20. const func = {
  21. name: 'Func',
  22. functional: true,
  23. props: {
  24. x: String,
  25. },
  26. inject: ['foo'],
  27. render: (h: any, { data, props, injections, slots }: any) => {
  28. return h('div', { id: props.x, class: data.class }, [
  29. h('div', { class: 'inject' }, injections.foo),
  30. h('div', { class: 'slot' }, slots().default),
  31. ])
  32. },
  33. }
  34. const vm = new Vue({
  35. provide() {
  36. return {
  37. foo: 123,
  38. }
  39. },
  40. components: {
  41. func,
  42. },
  43. template: `<func class="foo" x="foo">hello</func>`,
  44. }).$mount()
  45. expect(vm.$el.id).toBe('foo')
  46. expect(vm.$el.className).toBe('foo')
  47. expect(vm.$el.querySelector('.inject').textContent).toBe('123')
  48. expect(vm.$el.querySelector('.slot').textContent).toBe('hello')
  49. expect(vm.$el.outerHTML).toMatchInlineSnapshot(
  50. `"<div id="foo" class="foo"><div class="inject">123</div><div class="slot">hello</div></div>"`,
  51. )
  52. expect(
  53. (
  54. deprecationData[DeprecationTypes.COMPONENT_FUNCTIONAL]
  55. .message as Function
  56. )(func),
  57. ).toHaveBeenWarned()
  58. })
  59. test('copies compatConfig option', () => {
  60. const func = {
  61. name: 'Func',
  62. functional: true,
  63. compatConfig: {
  64. ATTR_FALSE_VALUE: 'suppress-warning' as const,
  65. },
  66. render: (h: any) => {
  67. // should not render required: false due to compatConfig
  68. return h('div', { 'data-some-attr': false })
  69. },
  70. }
  71. const vm = new Vue({
  72. components: { func },
  73. template: `<func class="foo" x="foo">hello</func>`,
  74. }).$mount()
  75. expect(vm.$el.outerHTML).toMatchInlineSnapshot(`"<div></div>"`)
  76. expect(
  77. (
  78. deprecationData[DeprecationTypes.COMPONENT_FUNCTIONAL]
  79. .message as Function
  80. )(func),
  81. ).toHaveBeenWarned()
  82. expect(
  83. (deprecationData[DeprecationTypes.ATTR_FALSE_VALUE].message as Function)(
  84. func,
  85. ),
  86. ).not.toHaveBeenWarned()
  87. })
  88. })