model-dynamic.spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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-else', done => {
  38. const data = {
  39. ok: true,
  40. type: null,
  41. test: 'b'
  42. }
  43. const vm = new Vue({
  44. data,
  45. template: `<div v-if="ok">haha</div><input v-else :type="type" v-model="test">`
  46. }).$mount()
  47. document.body.appendChild(vm.$el)
  48. expect(vm.$el.textContent).toBe('haha')
  49. vm.ok = false
  50. assertInputWorks(vm).then(done)
  51. })
  52. it('with v-else-if', done => {
  53. const vm = new Vue({
  54. data: {
  55. foo: true,
  56. bar: false,
  57. type: null,
  58. test: 'b'
  59. },
  60. template: `<div v-if="foo">text</div><input v-else-if="bar" :type="type" v-model="test">`
  61. }).$mount()
  62. document.body.appendChild(vm.$el)
  63. const chain = waitForUpdate(() => {
  64. expect(vm.$el.textContent).toBe('text')
  65. }).then(() => {
  66. vm.foo = false
  67. }).then(() => {
  68. expect(vm._vnode.isComment).toBe(true)
  69. }).then(() => {
  70. vm.bar = true
  71. })
  72. assertInputWorks(vm, chain).then(done)
  73. })
  74. it('with v-for', done => {
  75. const vm = new Vue({
  76. data: {
  77. data: {
  78. text: 'foo',
  79. checkbox: true
  80. },
  81. types: ['text', 'checkbox']
  82. },
  83. template: `<div>
  84. <input v-for="type in types" :type="type" v-model="data[type]">
  85. </div>`
  86. }).$mount()
  87. document.body.appendChild(vm.$el)
  88. let el1 = vm.$el.children[0]
  89. expect(el1.type).toBe('text')
  90. expect(el1.value).toBe('foo')
  91. el1.value = 'bar'
  92. triggerEvent(el1, 'input')
  93. expect(vm.data.text).toBe('bar')
  94. let el2 = vm.$el.children[1]
  95. expect(el2.type).toBe('checkbox')
  96. expect(el2.checked).toBe(true)
  97. el2.click()
  98. expect(vm.data.checkbox).toBe(false)
  99. // now in reverse!
  100. vm.types.reverse()
  101. waitForUpdate(() => {
  102. el1 = vm.$el.children[0]
  103. expect(el1.type).toBe('checkbox')
  104. expect(el1.checked).toBe(false)
  105. el1.click()
  106. expect(vm.data.checkbox).toBe(true)
  107. el2 = vm.$el.children[1]
  108. expect(el2.type).toBe('text')
  109. expect(el2.value).toBe('bar')
  110. el2.value = 'foo'
  111. triggerEvent(el2, 'input')
  112. expect(vm.data.text).toBe('foo')
  113. }).then(done)
  114. })
  115. })
  116. function assertInputWorks (vm, type, chain) {
  117. if (typeof type !== 'string') {
  118. if (!chain) chain = type
  119. type = 'type'
  120. }
  121. if (!chain) chain = waitForUpdate()
  122. chain.then(() => {
  123. expect(vm.$el.value).toBe('b')
  124. vm.test = 'a'
  125. }).then(() => {
  126. expect(vm.$el.value).toBe('a')
  127. vm.$el.value = 'c'
  128. triggerEvent(vm.$el, 'input')
  129. expect(vm.test).toBe('c')
  130. }).then(() => {
  131. // change it to password
  132. vm[type] = 'password'
  133. vm.test = 'b'
  134. }).then(() => {
  135. expect(vm.$el.type).toBe('password')
  136. expect(vm.$el.value).toBe('b')
  137. vm.$el.value = 'c'
  138. triggerEvent(vm.$el, 'input')
  139. expect(vm.test).toBe('c')
  140. }).then(() => {
  141. // change it to checkbox...
  142. vm[type] = 'checkbox'
  143. }).then(() => {
  144. expect(vm.$el.type).toBe('checkbox')
  145. expect(vm.$el.checked).toBe(true)
  146. }).then(() => {
  147. vm.$el.click()
  148. expect(vm.$el.checked).toBe(false)
  149. expect(vm.test).toBe(false)
  150. })
  151. return chain
  152. }