misc.spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. import { triggerEvent } from './utils'
  9. import { h } from '@vue/runtime-core'
  10. beforeEach(() => {
  11. toggleDeprecationWarning(true)
  12. Vue.configureCompat({
  13. MODE: 2,
  14. GLOBAL_MOUNT: 'suppress-warning'
  15. })
  16. })
  17. afterEach(() => {
  18. toggleDeprecationWarning(false)
  19. Vue.configureCompat({ MODE: 3 })
  20. })
  21. test('mode as function', () => {
  22. const Foo = {
  23. name: 'Foo',
  24. render: (h: any) => h('div', 'foo')
  25. }
  26. const Bar = {
  27. name: 'Bar',
  28. data: () => ({ msg: 'bar' }),
  29. render: (ctx: any) => h('div', ctx.msg)
  30. }
  31. toggleDeprecationWarning(false)
  32. Vue.configureCompat({
  33. MODE: comp => (comp && comp.name === 'Bar' ? 3 : 2)
  34. })
  35. const vm = new Vue({
  36. components: { Foo, Bar },
  37. template: `<div><foo/><bar/></div>`
  38. }).$mount()
  39. expect(vm.$el.innerHTML).toBe(`<div>foo</div><div>bar</div>`)
  40. })
  41. test('WATCH_ARRAY', async () => {
  42. const spy = jest.fn()
  43. const vm = new Vue({
  44. data() {
  45. return {
  46. foo: []
  47. }
  48. },
  49. watch: {
  50. foo: spy
  51. }
  52. }) as any
  53. expect(
  54. deprecationData[DeprecationTypes.WATCH_ARRAY].message
  55. ).toHaveBeenWarned()
  56. expect(spy).not.toHaveBeenCalled()
  57. vm.foo.push(1)
  58. await nextTick()
  59. expect(spy).toHaveBeenCalledTimes(1)
  60. })
  61. test('PROPS_DEFAULT_THIS', () => {
  62. let thisCtx: any
  63. const Child = {
  64. customOption: 1,
  65. inject: ['provided'],
  66. props: {
  67. foo: null,
  68. bar: {
  69. default(this: any) {
  70. // copy values since injection must be sync
  71. thisCtx = {
  72. foo: this.foo,
  73. $options: this.$options,
  74. provided: this.provided
  75. }
  76. return this.foo + 1
  77. }
  78. }
  79. },
  80. template: `{{ bar }}`
  81. }
  82. const vm = new Vue({
  83. components: { Child },
  84. provide: {
  85. provided: 2
  86. },
  87. template: `<child :foo="0" />`
  88. }).$mount()
  89. expect(vm.$el.textContent).toBe('1')
  90. // other props
  91. expect(thisCtx.foo).toBe(0)
  92. // $options
  93. expect(thisCtx.$options.customOption).toBe(1)
  94. // injections
  95. expect(thisCtx.provided).toBe(2)
  96. expect(
  97. (deprecationData[DeprecationTypes.PROPS_DEFAULT_THIS].message as Function)(
  98. 'bar'
  99. )
  100. ).toHaveBeenWarned()
  101. })
  102. test('V_ON_KEYCODE_MODIFIER', () => {
  103. const spy = jest.fn()
  104. const vm = new Vue({
  105. template: `<input @keyup.1="spy">`,
  106. methods: { spy }
  107. }).$mount()
  108. triggerEvent(vm.$el, 'keyup', e => {
  109. e.key = '_'
  110. e.keyCode = 1
  111. })
  112. expect(spy).toHaveBeenCalled()
  113. expect(
  114. deprecationData[DeprecationTypes.V_ON_KEYCODE_MODIFIER].message
  115. ).toHaveBeenWarned()
  116. })
  117. test('CUSTOM_DIR', async () => {
  118. const myDir = {
  119. bind: jest.fn(),
  120. inserted: jest.fn(),
  121. update: jest.fn(),
  122. componentUpdated: jest.fn(),
  123. unbind: jest.fn()
  124. } as any
  125. const getCalls = () =>
  126. Object.keys(myDir).map(key => myDir[key].mock.calls.length)
  127. const vm = new Vue({
  128. data() {
  129. return {
  130. ok: true,
  131. foo: 1
  132. }
  133. },
  134. template: `<div v-if="ok" v-my-dir="foo"/>`,
  135. directives: {
  136. myDir
  137. }
  138. }).$mount() as any
  139. expect(getCalls()).toMatchObject([1, 1, 0, 0, 0])
  140. expect(
  141. (deprecationData[DeprecationTypes.CUSTOM_DIR].message as Function)(
  142. 'bind',
  143. 'beforeMount'
  144. )
  145. ).toHaveBeenWarned()
  146. expect(
  147. (deprecationData[DeprecationTypes.CUSTOM_DIR].message as Function)(
  148. 'inserted',
  149. 'mounted'
  150. )
  151. ).toHaveBeenWarned()
  152. vm.foo++
  153. await nextTick()
  154. expect(getCalls()).toMatchObject([1, 1, 1, 1, 0])
  155. expect(
  156. (deprecationData[DeprecationTypes.CUSTOM_DIR].message as Function)(
  157. 'update',
  158. 'updated'
  159. )
  160. ).toHaveBeenWarned()
  161. expect(
  162. (deprecationData[DeprecationTypes.CUSTOM_DIR].message as Function)(
  163. 'componentUpdated',
  164. 'updated'
  165. )
  166. ).toHaveBeenWarned()
  167. })
  168. test('ATTR_FALSE_VALUE', () => {
  169. const vm = new Vue({
  170. template: `<div :id="false" :foo="false"/>`
  171. }).$mount()
  172. expect(vm.$el.hasAttribute('id')).toBe(false)
  173. expect(vm.$el.hasAttribute('foo')).toBe(false)
  174. expect(
  175. (deprecationData[DeprecationTypes.ATTR_FALSE_VALUE].message as Function)(
  176. 'id'
  177. )
  178. ).toHaveBeenWarned()
  179. expect(
  180. (deprecationData[DeprecationTypes.ATTR_FALSE_VALUE].message as Function)(
  181. 'foo'
  182. )
  183. ).toHaveBeenWarned()
  184. })
  185. test('ATTR_ENUMERATED_COERCION', () => {
  186. const vm = new Vue({
  187. template: `<div :draggable="null" :spellcheck="0" contenteditable="foo" />`
  188. }).$mount()
  189. expect(vm.$el.getAttribute('draggable')).toBe('false')
  190. expect(vm.$el.getAttribute('spellcheck')).toBe('true')
  191. expect(vm.$el.getAttribute('contenteditable')).toBe('true')
  192. expect(
  193. (
  194. deprecationData[DeprecationTypes.ATTR_ENUMERATED_COERCION]
  195. .message as Function
  196. )('draggable', null, 'false')
  197. ).toHaveBeenWarned()
  198. expect(
  199. (
  200. deprecationData[DeprecationTypes.ATTR_ENUMERATED_COERCION]
  201. .message as Function
  202. )('spellcheck', 0, 'true')
  203. ).toHaveBeenWarned()
  204. expect(
  205. (
  206. deprecationData[DeprecationTypes.ATTR_ENUMERATED_COERCION]
  207. .message as Function
  208. )('contenteditable', 'foo', 'true')
  209. ).toHaveBeenWarned()
  210. })