compiler.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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('should not warn COMPILER_V_BIND_OBJECT_ORDER work with vFor', () => {
  81. const vm = new Vue({
  82. template: `<div><div v-bind="{ id: 'bar', class: 'baz' }" v-for="item in 5" /></div>`,
  83. }).$mount()
  84. expect(vm.$el).toBeInstanceOf(HTMLDivElement)
  85. expect(
  86. CompilerDeprecationTypes.COMPILER_V_BIND_OBJECT_ORDER,
  87. ).not.toHaveBeenWarned()
  88. })
  89. test('COMPILER_V_ON_NATIVE', () => {
  90. const spy = vi.fn()
  91. const vm = new Vue({
  92. template: `<child @click="spy" @click.native="spy" />`,
  93. components: {
  94. child: {
  95. template: `<button />`,
  96. },
  97. },
  98. methods: {
  99. spy,
  100. },
  101. }).$mount()
  102. expect(vm.$el).toBeInstanceOf(HTMLButtonElement)
  103. triggerEvent(vm.$el as HTMLButtonElement, 'click')
  104. expect(spy).toHaveBeenCalledTimes(1)
  105. expect(CompilerDeprecationTypes.COMPILER_V_ON_NATIVE).toHaveBeenWarned()
  106. })
  107. test('COMPILER_V_IF_V_FOR_PRECEDENCE', () => {
  108. new Vue({ template: `<div v-if="true" v-for="i in 1"/>` }).$mount()
  109. expect(
  110. CompilerDeprecationTypes.COMPILER_V_IF_V_FOR_PRECEDENCE,
  111. ).toHaveBeenWarned()
  112. })
  113. test('COMPILER_NATIVE_TEMPLATE', () => {
  114. const vm = new Vue({
  115. template: `<div><template><div/></template></div>`,
  116. }).$mount()
  117. expect(vm.$el).toBeInstanceOf(HTMLDivElement)
  118. expect(vm.$el.innerHTML).toBe(`<div></div>`)
  119. expect(CompilerDeprecationTypes.COMPILER_NATIVE_TEMPLATE).toHaveBeenWarned()
  120. })
  121. test('COMPILER_INLINE_TEMPLATE', () => {
  122. const vm = new Vue({
  123. template: `<foo inline-template><div>{{ n }}</div></foo>`,
  124. components: {
  125. foo: {
  126. data() {
  127. return { n: 123 }
  128. },
  129. },
  130. },
  131. }).$mount()
  132. expect(vm.$el).toBeInstanceOf(HTMLDivElement)
  133. expect(vm.$el?.outerHTML).toBe(`<div>123</div>`)
  134. expect(CompilerDeprecationTypes.COMPILER_INLINE_TEMPLATE).toHaveBeenWarned()
  135. })