bind.spec.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import Vue from 'vue'
  2. describe('Directive v-bind', () => {
  3. it('normal attr', done => {
  4. const vm = new Vue({
  5. el: '#app',
  6. template: '<div><span :test="foo">hello</span></div>',
  7. data: { foo: 'ok' }
  8. })
  9. expect(vm.$el.firstChild.getAttribute('test')).toBe('ok')
  10. vm.foo = 'again'
  11. waitForUpdate(() => {
  12. expect(vm.$el.firstChild.getAttribute('test')).toBe('again')
  13. vm.foo = null
  14. }).then(() => {
  15. expect(vm.$el.firstChild.hasAttribute('test')).toBe(false)
  16. vm.foo = false
  17. }).then(() => {
  18. expect(vm.$el.firstChild.hasAttribute('test')).toBe(false)
  19. vm.foo = true
  20. }).then(() => {
  21. expect(vm.$el.firstChild.getAttribute('test')).toBe('true')
  22. vm.foo = 0
  23. }).then(() => {
  24. expect(vm.$el.firstChild.getAttribute('test')).toBe('0')
  25. done()
  26. }).catch(done)
  27. })
  28. it('should set property for input value', done => {
  29. const vm = new Vue({
  30. el: '#app',
  31. template: `
  32. <div>
  33. <input type="text" :value="foo">
  34. <input type="checkbox" :checked="bar">
  35. </div>
  36. `,
  37. data: {
  38. foo: 'ok',
  39. bar: false
  40. }
  41. })
  42. expect(vm.$el.firstChild.value).toBe('ok')
  43. expect(vm.$el.lastChild.checked).toBe(false)
  44. vm.bar = true
  45. waitForUpdate(() => {
  46. expect(vm.$el.lastChild.checked).toBe(true)
  47. done()
  48. }).catch(done)
  49. })
  50. it('xlink', done => {
  51. const vm = new Vue({
  52. el: '#app',
  53. template: '<svg><a :xlink:special="foo"></a></svg>',
  54. data: {
  55. foo: 'ok'
  56. }
  57. })
  58. const xlinkNS = 'http://www.w3.org/1999/xlink'
  59. expect(vm.$el.firstChild.getAttributeNS(xlinkNS, 'special')).toBe('ok')
  60. vm.foo = 'again'
  61. waitForUpdate(() => {
  62. expect(vm.$el.firstChild.getAttributeNS(xlinkNS, 'special')).toBe('again')
  63. vm.foo = null
  64. }).then(() => {
  65. expect(vm.$el.firstChild.hasAttributeNS(xlinkNS, 'special')).toBe(false)
  66. vm.foo = true
  67. }).then(() => {
  68. expect(vm.$el.firstChild.getAttributeNS(xlinkNS, 'special')).toBe('true')
  69. done()
  70. }).catch(done)
  71. })
  72. it('enumrated attr', done => {
  73. const vm = new Vue({
  74. el: '#app',
  75. template: '<div><span :draggable="foo">hello</span></div>',
  76. data: { foo: true }
  77. })
  78. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('true')
  79. vm.foo = 'again'
  80. waitForUpdate(() => {
  81. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('true')
  82. vm.foo = null
  83. }).then(() => {
  84. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('false')
  85. vm.foo = ''
  86. }).then(() => {
  87. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('true')
  88. vm.foo = false
  89. }).then(() => {
  90. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('false')
  91. vm.foo = 'false'
  92. }).then(() => {
  93. expect(vm.$el.firstChild.getAttribute('draggable')).toBe('false')
  94. done()
  95. }).catch(done)
  96. })
  97. it('boolean attr', done => {
  98. const vm = new Vue({
  99. el: '#app',
  100. template: '<div><span :disabled="foo">hello</span></div>',
  101. data: { foo: true }
  102. })
  103. expect(vm.$el.firstChild.getAttribute('disabled')).toBe('disabled')
  104. vm.foo = 'again'
  105. waitForUpdate(() => {
  106. expect(vm.$el.firstChild.getAttribute('disabled')).toBe('disabled')
  107. vm.foo = null
  108. }).then(() => {
  109. expect(vm.$el.firstChild.hasAttribute('disabled')).toBe(false)
  110. vm.foo = ''
  111. }).then(() => {
  112. expect(vm.$el.firstChild.hasAttribute('disabled')).toBe(true)
  113. done()
  114. }).catch(done)
  115. })
  116. })