compiler.spec.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import Vue from '@vue/compat'
  2. import { nextTick } from '@vue/runtime-core'
  3. import { CompilerDeprecationTypes } from '@vue/compiler-core'
  4. import { toggleDeprecationWarning } from '../../runtime-core/src/compat/compatConfig'
  5. import { triggerEvent } from './utils'
  6. beforeEach(() => {
  7. toggleDeprecationWarning(false)
  8. Vue.configureCompat({
  9. MODE: 2,
  10. })
  11. })
  12. afterEach(() => {
  13. toggleDeprecationWarning(false)
  14. Vue.configureCompat({ MODE: 3 })
  15. })
  16. // COMPILER_V_FOR_REF is tested in ./refInfor.spec.ts
  17. // COMPILER_FILTERS is tested in ./filters.spec.ts
  18. test('COMPILER_IS_ON_ELEMENT', () => {
  19. const MyButton = {
  20. template: `<div><slot/></div>`,
  21. }
  22. const vm = new Vue({
  23. template: `<button is="my-button">text</button>`,
  24. components: {
  25. MyButton,
  26. },
  27. }).$mount()
  28. expect(vm.$el).toBeInstanceOf(HTMLDivElement)
  29. expect(vm.$el.outerHTML).toBe(`<div>text</div>`)
  30. expect(CompilerDeprecationTypes.COMPILER_IS_ON_ELEMENT).toHaveBeenWarned()
  31. })
  32. test('COMPILER_IS_ON_ELEMENT (dynamic)', () => {
  33. const MyButton = {
  34. template: `<div><slot/></div>`,
  35. }
  36. const vm = new Vue({
  37. template: `<button :is="'MyButton'">text</button>`,
  38. components: {
  39. MyButton,
  40. },
  41. }).$mount()
  42. expect(vm.$el).toBeInstanceOf(HTMLDivElement)
  43. expect(vm.$el.outerHTML).toBe(`<div>text</div>`)
  44. expect(CompilerDeprecationTypes.COMPILER_IS_ON_ELEMENT).toHaveBeenWarned()
  45. })
  46. test('COMPILER_V_BIND_SYNC', async () => {
  47. const MyButton = {
  48. props: ['foo'],
  49. template: `<button @click="$emit('update:foo', 1)">{{ foo }}</button>`,
  50. }
  51. const vm = new Vue({
  52. data() {
  53. return {
  54. foo: 0,
  55. }
  56. },
  57. template: `<my-button :foo.sync="foo" />`,
  58. components: {
  59. MyButton,
  60. },
  61. }).$mount()
  62. expect(vm.$el).toBeInstanceOf(HTMLButtonElement)
  63. expect(vm.$el.textContent).toBe(`0`)
  64. triggerEvent(vm.$el as Element, 'click')
  65. await nextTick()
  66. expect(vm.$el.textContent).toBe(`1`)
  67. expect(CompilerDeprecationTypes.COMPILER_V_BIND_SYNC).toHaveBeenWarned()
  68. })
  69. test('COMPILER_V_BIND_OBJECT_ORDER', () => {
  70. const vm = new Vue({
  71. template: `<div id="foo" v-bind="{ id: 'bar', class: 'baz' }" />`,
  72. }).$mount()
  73. expect(vm.$el).toBeInstanceOf(HTMLDivElement)
  74. expect(vm.$el.id).toBe('foo')
  75. expect(vm.$el.className).toBe('baz')
  76. expect(
  77. CompilerDeprecationTypes.COMPILER_V_BIND_OBJECT_ORDER,
  78. ).toHaveBeenWarned()
  79. })
  80. test('COMPILER_V_ON_NATIVE', () => {
  81. const spy = vi.fn()
  82. const vm = new Vue({
  83. template: `<child @click="spy" @click.native="spy" />`,
  84. components: {
  85. child: {
  86. template: `<button />`,
  87. },
  88. },
  89. methods: {
  90. spy,
  91. },
  92. }).$mount()
  93. expect(vm.$el).toBeInstanceOf(HTMLButtonElement)
  94. triggerEvent(vm.$el as HTMLButtonElement, 'click')
  95. expect(spy).toHaveBeenCalledTimes(1)
  96. expect(CompilerDeprecationTypes.COMPILER_V_ON_NATIVE).toHaveBeenWarned()
  97. })
  98. test('COMPILER_V_IF_V_FOR_PRECEDENCE', () => {
  99. new Vue({ template: `<div v-if="true" v-for="i in 1"/>` }).$mount()
  100. expect(
  101. CompilerDeprecationTypes.COMPILER_V_IF_V_FOR_PRECEDENCE,
  102. ).toHaveBeenWarned()
  103. })
  104. test('COMPILER_NATIVE_TEMPLATE', () => {
  105. const vm = new Vue({
  106. template: `<div><template><div/></template></div>`,
  107. }).$mount()
  108. expect(vm.$el).toBeInstanceOf(HTMLDivElement)
  109. expect(vm.$el.innerHTML).toBe(`<div></div>`)
  110. expect(CompilerDeprecationTypes.COMPILER_NATIVE_TEMPLATE).toHaveBeenWarned()
  111. })
  112. test('COMPILER_INLINE_TEMPLATE', () => {
  113. const vm = new Vue({
  114. template: `<foo inline-template><div>{{ n }}</div></foo>`,
  115. components: {
  116. foo: {
  117. data() {
  118. return { n: 123 }
  119. },
  120. },
  121. },
  122. }).$mount()
  123. expect(vm.$el).toBeInstanceOf(HTMLDivElement)
  124. expect(vm.$el?.outerHTML).toBe(`<div>123</div>`)
  125. expect(CompilerDeprecationTypes.COMPILER_INLINE_TEMPLATE).toHaveBeenWarned()
  126. })