compiler.spec.ts 3.2 KB

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