model-dynamic.spec.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import Vue from 'vue'
  2. describe('Directive v-model dynamic input type', () => {
  3. it('should work', done => {
  4. const vm = new Vue({
  5. data: {
  6. inputType: null,
  7. test: 'b'
  8. },
  9. template: `<input :type="inputType" v-model="test">`
  10. }).$mount()
  11. document.body.appendChild(vm.$el)
  12. // test text
  13. assertInputWorks(vm, 'inputType').then(done)
  14. })
  15. it('with v-if', done => {
  16. const vm = new Vue({
  17. data: {
  18. ok: true,
  19. type: null,
  20. test: 'b'
  21. },
  22. template: `<input v-if="ok" :type="type" v-model="test"><div v-else>haha</div>`
  23. }).$mount()
  24. document.body.appendChild(vm.$el)
  25. const chain = assertInputWorks(vm).then(() => {
  26. vm.ok = false
  27. }).then(() => {
  28. expect(vm.$el.textContent).toBe('haha')
  29. }).then(() => {
  30. // reset
  31. vm.ok = true
  32. vm.type = null
  33. vm.test = 'b'
  34. })
  35. assertInputWorks(vm, chain).then(done)
  36. })
  37. it('with v-for', done => {
  38. const vm = new Vue({
  39. data: {
  40. data: {
  41. text: 'foo',
  42. checkbox: true
  43. },
  44. types: ['text', 'checkbox']
  45. },
  46. template: `<div>
  47. <input v-for="type in types" :type="type" v-model="data[type]">
  48. </div>`
  49. }).$mount()
  50. document.body.appendChild(vm.$el)
  51. let el1 = vm.$el.children[0]
  52. expect(el1.type).toBe('text')
  53. expect(el1.value).toBe('foo')
  54. el1.value = 'bar'
  55. triggerEvent(el1, 'input')
  56. expect(vm.data.text).toBe('bar')
  57. let el2 = vm.$el.children[1]
  58. expect(el2.type).toBe('checkbox')
  59. expect(el2.checked).toBe(true)
  60. el2.click()
  61. expect(vm.data.checkbox).toBe(false)
  62. // now in reverse!
  63. vm.types.reverse()
  64. waitForUpdate(() => {
  65. el1 = vm.$el.children[0]
  66. expect(el1.type).toBe('checkbox')
  67. expect(el1.checked).toBe(false)
  68. el1.click()
  69. expect(vm.data.checkbox).toBe(true)
  70. el2 = vm.$el.children[1]
  71. expect(el2.type).toBe('text')
  72. expect(el2.value).toBe('bar')
  73. el2.value = 'foo'
  74. triggerEvent(el2, 'input')
  75. expect(vm.data.text).toBe('foo')
  76. }).then(done)
  77. })
  78. })
  79. function assertInputWorks (vm, type, chain) {
  80. if (typeof type !== 'string') {
  81. if (!chain) chain = type
  82. type = 'type'
  83. }
  84. if (!chain) chain = waitForUpdate()
  85. chain.then(() => {
  86. expect(vm.$el.value).toBe('b')
  87. vm.test = 'a'
  88. }).then(() => {
  89. expect(vm.$el.value).toBe('a')
  90. vm.$el.value = 'c'
  91. triggerEvent(vm.$el, 'input')
  92. expect(vm.test).toBe('c')
  93. }).then(() => {
  94. // change it to password
  95. vm[type] = 'password'
  96. vm.test = 'b'
  97. }).then(() => {
  98. expect(vm.$el.type).toBe('password')
  99. expect(vm.$el.value).toBe('b')
  100. vm.$el.value = 'c'
  101. triggerEvent(vm.$el, 'input')
  102. expect(vm.test).toBe('c')
  103. }).then(() => {
  104. // change it to checkbox...
  105. vm[type] = 'checkbox'
  106. }).then(() => {
  107. expect(vm.$el.type).toBe('checkbox')
  108. expect(vm.$el.checked).toBe(true)
  109. }).then(() => {
  110. vm.$el.click()
  111. expect(vm.$el.checked).toBe(false)
  112. expect(vm.test).toBe(false)
  113. })
  114. return chain
  115. }