options.spec.ts 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import Vue from '@vue/compat'
  2. import { nextTick } from '../../runtime-core/src/scheduler'
  3. import {
  4. DeprecationTypes,
  5. deprecationData,
  6. toggleDeprecationWarning
  7. } from '../../runtime-core/src/compat/compatConfig'
  8. beforeEach(() => {
  9. toggleDeprecationWarning(true)
  10. Vue.configureCompat({
  11. MODE: 2,
  12. GLOBAL_MOUNT: 'suppress-warning',
  13. GLOBAL_EXTEND: 'suppress-warning'
  14. })
  15. })
  16. afterEach(() => {
  17. toggleDeprecationWarning(false)
  18. Vue.configureCompat({ MODE: 3 })
  19. })
  20. test('root data plain object', () => {
  21. const vm = new Vue({
  22. data: { foo: 1 } as any,
  23. template: `{{ foo }}`
  24. }).$mount()
  25. expect(vm.$el.textContent).toBe('1')
  26. expect(
  27. deprecationData[DeprecationTypes.OPTIONS_DATA_FN].message
  28. ).toHaveBeenWarned()
  29. })
  30. test('data deep merge', () => {
  31. const mixin = {
  32. data() {
  33. return {
  34. foo: {
  35. baz: 2
  36. }
  37. }
  38. }
  39. }
  40. const vm = new Vue({
  41. mixins: [mixin],
  42. data: () => ({
  43. foo: {
  44. bar: 1
  45. }
  46. }),
  47. template: `{{ foo }}`
  48. }).$mount()
  49. expect(vm.$el.textContent).toBe(JSON.stringify({ baz: 2, bar: 1 }, null, 2))
  50. expect(
  51. (deprecationData[DeprecationTypes.OPTIONS_DATA_MERGE].message as Function)(
  52. 'foo'
  53. )
  54. ).toHaveBeenWarned()
  55. })
  56. test('beforeDestroy/destroyed', async () => {
  57. const beforeDestroy = jest.fn()
  58. const destroyed = jest.fn()
  59. const child = {
  60. template: `foo`,
  61. beforeDestroy,
  62. destroyed
  63. }
  64. const vm = new Vue({
  65. template: `<child v-if="ok"/>`,
  66. data() {
  67. return { ok: true }
  68. },
  69. components: { child }
  70. }).$mount() as any
  71. vm.ok = false
  72. await nextTick()
  73. expect(beforeDestroy).toHaveBeenCalled()
  74. expect(destroyed).toHaveBeenCalled()
  75. expect(
  76. deprecationData[DeprecationTypes.OPTIONS_BEFORE_DESTROY].message
  77. ).toHaveBeenWarned()
  78. expect(
  79. deprecationData[DeprecationTypes.OPTIONS_DESTROYED].message
  80. ).toHaveBeenWarned()
  81. })
  82. test('beforeDestroy/destroyed in Vue.extend components', async () => {
  83. const beforeDestroy = jest.fn()
  84. const destroyed = jest.fn()
  85. const child = Vue.extend({
  86. template: `foo`,
  87. beforeDestroy,
  88. destroyed
  89. })
  90. const vm = new Vue({
  91. template: `<child v-if="ok"/>`,
  92. data() {
  93. return { ok: true }
  94. },
  95. components: { child }
  96. }).$mount() as any
  97. vm.ok = false
  98. await nextTick()
  99. expect(beforeDestroy).toHaveBeenCalled()
  100. expect(destroyed).toHaveBeenCalled()
  101. expect(
  102. deprecationData[DeprecationTypes.OPTIONS_BEFORE_DESTROY].message
  103. ).toHaveBeenWarned()
  104. expect(
  105. deprecationData[DeprecationTypes.OPTIONS_DESTROYED].message
  106. ).toHaveBeenWarned()
  107. })