compiler.spec.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import Vue from '@vue/compat'
  2. import { nextTick } from '@vue/runtime-core'
  3. import { CompilerDeprecationTypes } from '../../compiler-core/src'
  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.outerHTML).toBe(`<div>text</div>`)
  29. expect(CompilerDeprecationTypes.COMPILER_IS_ON_ELEMENT).toHaveBeenWarned()
  30. })
  31. test('COMPILER_IS_ON_ELEMENT (dynamic)', () => {
  32. const MyButton = {
  33. template: `<div><slot/></div>`
  34. }
  35. const vm = new Vue({
  36. template: `<button :is="'MyButton'">text</button>`,
  37. components: {
  38. MyButton
  39. }
  40. }).$mount()
  41. expect(vm.$el.outerHTML).toBe(`<div>text</div>`)
  42. expect(CompilerDeprecationTypes.COMPILER_IS_ON_ELEMENT).toHaveBeenWarned()
  43. })
  44. test('COMPILER_V_BIND_SYNC', async () => {
  45. const MyButton = {
  46. props: ['foo'],
  47. template: `<button @click="$emit('update:foo', 1)">{{ foo }}</button>`
  48. }
  49. const vm = new Vue({
  50. data() {
  51. return {
  52. foo: 0
  53. }
  54. },
  55. template: `<my-button :foo.sync="foo" />`,
  56. components: {
  57. MyButton
  58. }
  59. }).$mount()
  60. expect(vm.$el.textContent).toBe(`0`)
  61. triggerEvent(vm.$el, 'click')
  62. await nextTick()
  63. expect(vm.$el.textContent).toBe(`1`)
  64. expect(CompilerDeprecationTypes.COMPILER_V_BIND_SYNC).toHaveBeenWarned()
  65. })
  66. test('COMPILER_V_BIND_PROP', () => {
  67. const vm = new Vue({
  68. template: `<div :id.prop="'foo'"/>`
  69. }).$mount()
  70. expect(vm.$el.id).toBe('foo')
  71. expect(CompilerDeprecationTypes.COMPILER_V_BIND_PROP).toHaveBeenWarned()
  72. })
  73. test('COMPILER_V_BIND_OBJECT_ORDER', () => {
  74. const vm = new Vue({
  75. template: `<div id="foo" v-bind="{ id: 'bar', class: 'baz' }" />`
  76. }).$mount()
  77. expect(vm.$el.id).toBe('foo')
  78. expect(vm.$el.className).toBe('baz')
  79. expect(
  80. CompilerDeprecationTypes.COMPILER_V_BIND_OBJECT_ORDER
  81. ).toHaveBeenWarned()
  82. })
  83. test('COMPILER_V_ON_NATIVE', () => {
  84. const spy = jest.fn()
  85. const vm = new Vue({
  86. template: `<child @click="spy" @click.native="spy" />`,
  87. components: {
  88. child: {
  89. template: `<button />`
  90. }
  91. },
  92. methods: {
  93. spy
  94. }
  95. }).$mount()
  96. triggerEvent(vm.$el, 'click')
  97. expect(spy).toHaveBeenCalledTimes(1)
  98. expect(CompilerDeprecationTypes.COMPILER_V_ON_NATIVE).toHaveBeenWarned()
  99. })
  100. test('COMPILER_V_IF_V_FOR_PRECEDENCE', () => {
  101. new Vue({ template: `<div v-if="true" v-for="i in 1"/>` }).$mount()
  102. expect(
  103. CompilerDeprecationTypes.COMPILER_V_IF_V_FOR_PRECEDENCE
  104. ).toHaveBeenWarned()
  105. })
  106. test('COMPILER_NATIVE_TEMPLATE', () => {
  107. const vm = new Vue({
  108. template: `<div><template><div/></template></div>`
  109. }).$mount()
  110. expect(vm.$el.innerHTML).toBe(`<div></div>`)
  111. expect(CompilerDeprecationTypes.COMPILER_NATIVE_TEMPLATE).toHaveBeenWarned()
  112. })
  113. test('COMPILER_INLINE_TEMPLATE', () => {
  114. const vm = new Vue({
  115. template: `<foo inline-template><div>{{ n }}</div></foo>`,
  116. components: {
  117. foo: {
  118. data() {
  119. return { n: 123 }
  120. }
  121. }
  122. }
  123. }).$mount()
  124. expect(vm.$el.outerHTML).toBe(`<div>123</div>`)
  125. expect(CompilerDeprecationTypes.COMPILER_INLINE_TEMPLATE).toHaveBeenWarned()
  126. })