componentVModel.spec.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import Vue from '@vue/compat'
  2. import { ComponentOptions } from '../../runtime-core/src/component'
  3. import { nextTick } from '../../runtime-core/src/scheduler'
  4. import {
  5. DeprecationTypes,
  6. deprecationData,
  7. toggleDeprecationWarning
  8. } from '../../runtime-core/src/compat/compatConfig'
  9. import { triggerEvent } from './utils'
  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. describe('COMPONENT_V_MODEL', () => {
  22. async function runTest(CustomInput: ComponentOptions) {
  23. const vm = new Vue({
  24. data() {
  25. return {
  26. text: 'foo'
  27. }
  28. },
  29. components: { CustomInput },
  30. template: `
  31. <div>
  32. <span>{{ text }}</span>
  33. <custom-input v-model="text"></custom-input>
  34. </div>
  35. `
  36. }).$mount() as any
  37. const input = vm.$el.querySelector('input')
  38. const span = vm.$el.querySelector('span')
  39. expect(input.value).toBe('foo')
  40. expect(span.textContent).toBe('foo')
  41. expect(
  42. (deprecationData[DeprecationTypes.COMPONENT_V_MODEL].message as Function)(
  43. CustomInput
  44. )
  45. ).toHaveBeenWarned()
  46. input.value = 'bar'
  47. triggerEvent(input, 'input')
  48. await nextTick()
  49. expect(input.value).toBe('bar')
  50. expect(span.textContent).toBe('bar')
  51. vm.text = 'baz'
  52. await nextTick()
  53. expect(input.value).toBe('baz')
  54. expect(span.textContent).toBe('baz')
  55. }
  56. test('basic usage', async () => {
  57. await runTest({
  58. name: 'CustomInput',
  59. props: ['value'],
  60. template: `<input :value="value" @input="$emit('input', $event.target.value)">`
  61. })
  62. })
  63. test('with model option', async () => {
  64. await runTest({
  65. name: 'CustomInput',
  66. props: ['input'],
  67. model: {
  68. prop: 'input',
  69. event: 'update'
  70. },
  71. template: `<input :value="input" @input="$emit('update', $event.target.value)">`
  72. })
  73. })
  74. })