compiler.spec.ts 3.6 KB

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