options.spec.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. })
  14. })
  15. afterEach(() => {
  16. toggleDeprecationWarning(false)
  17. Vue.configureCompat({ MODE: 3 })
  18. })
  19. test('root data plain object', () => {
  20. const vm = new Vue({
  21. data: { foo: 1 } as any,
  22. template: `{{ foo }}`
  23. }).$mount()
  24. expect(vm.$el.textContent).toBe('1')
  25. expect(
  26. deprecationData[DeprecationTypes.OPTIONS_DATA_FN].message
  27. ).toHaveBeenWarned()
  28. })
  29. test('data deep merge', () => {
  30. const mixin = {
  31. data() {
  32. return {
  33. foo: {
  34. baz: 2
  35. }
  36. }
  37. }
  38. }
  39. const vm = new Vue({
  40. mixins: [mixin],
  41. data: () => ({
  42. foo: {
  43. bar: 1
  44. }
  45. }),
  46. template: `{{ foo }}`
  47. }).$mount()
  48. expect(vm.$el.textContent).toBe(JSON.stringify({ baz: 2, bar: 1 }, null, 2))
  49. expect(
  50. (deprecationData[DeprecationTypes.OPTIONS_DATA_MERGE].message as Function)(
  51. 'foo'
  52. )
  53. ).toHaveBeenWarned()
  54. })
  55. test('beforeDestroy/destroyed', async () => {
  56. const beforeDestroy = jest.fn()
  57. const destroyed = jest.fn()
  58. const child = {
  59. template: `foo`,
  60. beforeDestroy,
  61. destroyed
  62. }
  63. const vm = new Vue({
  64. template: `<child v-if="ok"/>`,
  65. data() {
  66. return { ok: true }
  67. },
  68. components: { child }
  69. }).$mount() as any
  70. vm.ok = false
  71. await nextTick()
  72. expect(beforeDestroy).toHaveBeenCalled()
  73. expect(destroyed).toHaveBeenCalled()
  74. expect(
  75. deprecationData[DeprecationTypes.OPTIONS_BEFORE_DESTROY].message
  76. ).toHaveBeenWarned()
  77. expect(
  78. deprecationData[DeprecationTypes.OPTIONS_DESTROYED].message
  79. ).toHaveBeenWarned()
  80. })