2
0

bind.spec.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. import Vue from 'vue'
  2. describe('Directive v-bind', () => {
  3. it('normal attr', done => {
  4. const vm = new Vue({
  5. template: '<div><span :test="foo">hello</span></div>',
  6. data: { foo: 'ok' }
  7. }).$mount()
  8. expect(vm.$el.firstChild.getAttribute('test')).toBe('ok')
  9. vm.foo = 'again'
  10. waitForUpdate(() => {
  11. expect(vm.$el.firstChild.getAttribute('test')).toBe('again')
  12. vm.foo = null
  13. }).then(() => {
  14. expect(vm.$el.firstChild.hasAttribute('test')).toBe(false)
  15. vm.foo = false
  16. }).then(() => {
  17. expect(vm.$el.firstChild.hasAttribute('test')).toBe(false)
  18. vm.foo = true
  19. }).then(() => {
  20. expect(vm.$el.firstChild.getAttribute('test')).toBe('true')
  21. vm.foo = 0
  22. }).then(() => {
  23. expect(vm.$el.firstChild.getAttribute('test')).toBe('0')
  24. }).then(done)
  25. })
  26. it('should set property for input value', done => {
  27. const vm = new Vue({
  28. template: `
  29. <div>
  30. <input type="text" :value="foo">
  31. <input type="checkbox" :checked="bar">
  32. </div>
  33. `,
  34. data: {
  35. foo: 'ok',
  36. bar: false
  37. }
  38. }).$mount()
  39. expect(vm.$el.firstChild.value).toBe('ok')
  40. expect(vm.$el.lastChild.checked).toBe(false)
  41. vm.bar = true
  42. waitForUpdate(() => {
  43. expect(vm.$el.lastChild.checked).toBe(true)
  44. }).then(done)
  45. })
  46. it('xlink', done => {
  47. const vm = new Vue({
  48. template: '<svg><a :xlink:special="foo"></a></svg>',
  49. data: {
  50. foo: 'ok'
  51. }
  52. }).$mount()
  53. const xlinkNS = 'http://www.w3.org/1999/xlink'
  54. expect(vm.$el.firstChild.getAttributeNS(xlinkNS, 'special')).toBe('ok')
  55. vm.foo = 'again'
  56. waitForUpdate(() => {
  57. expect(vm.$el.firstChild.getAttributeNS(xlinkNS, 'special')).toBe('again')
  58. vm.foo = null
  59. }).then(() => {
  60. expect(vm.$el.firstChild.hasAttributeNS(xlinkNS, 'special')).toBe(false)
  61. vm.foo = true
  62. }).then(() => {
  63. expect(vm.$el.firstChild.getAttributeNS(xlinkNS, 'special')).toBe('true')
  64. }).then(done)
  65. })
  66. it('enumrated attr', done => {
  67. const vm = new Vue({
  68. template: '<div><span :draggable="foo">hello</span></div>',
  69. data: { foo: true }
  70. }).$mount()
  71. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('true')
  72. vm.foo = 'again'
  73. waitForUpdate(() => {
  74. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('true')
  75. vm.foo = null
  76. }).then(() => {
  77. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('false')
  78. vm.foo = ''
  79. }).then(() => {
  80. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('true')
  81. vm.foo = false
  82. }).then(() => {
  83. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('false')
  84. vm.foo = 'false'
  85. }).then(() => {
  86. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('false')
  87. }).then(done)
  88. })
  89. it('boolean attr', done => {
  90. const vm = new Vue({
  91. template: '<div><span :disabled="foo">hello</span></div>',
  92. data: { foo: true }
  93. }).$mount()
  94. expect(vm.$el.firstChild.getAttribute('disabled')).toBe('disabled')
  95. vm.foo = 'again'
  96. waitForUpdate(() => {
  97. expect(vm.$el.firstChild.getAttribute('disabled')).toBe('disabled')
  98. vm.foo = null
  99. }).then(() => {
  100. expect(vm.$el.firstChild.hasAttribute('disabled')).toBe(false)
  101. vm.foo = ''
  102. }).then(() => {
  103. expect(vm.$el.firstChild.hasAttribute('disabled')).toBe(true)
  104. }).then(done)
  105. })
  106. it('bind as prop', () => {
  107. const vm = new Vue({
  108. template: '<div><span v-bind:text-content.prop="foo"></span><span ::inner-html="bar"></span></div>',
  109. data: {
  110. foo: 'hello',
  111. bar: '<span>qux</span>'
  112. }
  113. }).$mount()
  114. expect(vm.$el.children[0].textContent).toBe('hello')
  115. expect(vm.$el.children[1].innerHTML).toBe('<span>qux</span>')
  116. })
  117. it('bind object', done => {
  118. const vm = new Vue({
  119. template: '<input v-bind="test">',
  120. data: {
  121. test: {
  122. id: 'test',
  123. class: 'ok',
  124. value: 'hello'
  125. }
  126. }
  127. }).$mount()
  128. expect(vm.$el.getAttribute('id')).toBe('test')
  129. expect(vm.$el.getAttribute('class')).toBe('ok')
  130. expect(vm.$el.value).toBe('hello')
  131. vm.test.id = 'hi'
  132. vm.test.value = 'bye'
  133. waitForUpdate(() => {
  134. expect(vm.$el.getAttribute('id')).toBe('hi')
  135. expect(vm.$el.getAttribute('class')).toBe('ok')
  136. expect(vm.$el.value).toBe('bye')
  137. }).then(done)
  138. })
  139. it('bind object as prop', done => {
  140. const vm = new Vue({
  141. template: '<input v-bind.prop="test">',
  142. data: {
  143. test: {
  144. id: 'test',
  145. className: 'ok',
  146. value: 'hello'
  147. }
  148. }
  149. }).$mount()
  150. expect(vm.$el.id).toBe('test')
  151. expect(vm.$el.className).toBe('ok')
  152. expect(vm.$el.value).toBe('hello')
  153. vm.test.id = 'hi'
  154. vm.test.className = 'okay'
  155. vm.test.value = 'bye'
  156. waitForUpdate(() => {
  157. expect(vm.$el.id).toBe('hi')
  158. expect(vm.$el.className).toBe('okay')
  159. expect(vm.$el.value).toBe('bye')
  160. }).then(done)
  161. })
  162. it('bind array', done => {
  163. const vm = new Vue({
  164. template: '<input v-bind="test">',
  165. data: {
  166. test: [
  167. { id: 'test', class: 'ok' },
  168. { value: 'hello' }
  169. ]
  170. }
  171. }).$mount()
  172. expect(vm.$el.getAttribute('id')).toBe('test')
  173. expect(vm.$el.getAttribute('class')).toBe('ok')
  174. expect(vm.$el.value).toBe('hello')
  175. vm.test[0].id = 'hi'
  176. vm.test[1].value = 'bye'
  177. waitForUpdate(() => {
  178. expect(vm.$el.getAttribute('id')).toBe('hi')
  179. expect(vm.$el.getAttribute('class')).toBe('ok')
  180. expect(vm.$el.value).toBe('bye')
  181. }).then(done)
  182. })
  183. it('warn expect object', () => {
  184. new Vue({
  185. template: '<input v-bind="test">',
  186. data: {
  187. test: 1
  188. }
  189. }).$mount()
  190. expect('v-bind without argument expects an Object or Array value').toHaveBeenWarned()
  191. })
  192. })