binding.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. describe('UNIT: Binding', function () {
  2. var Binding = require('vue/src/binding'),
  3. nextTick = require('vue/src/utils').nextTick
  4. describe('instantiation', function () {
  5. it('should have root==true with a root key', function () {
  6. var b = new Binding(null, 'test')
  7. assert.ok(b.root)
  8. })
  9. it('should have root==false with a non-root key', function () {
  10. var b = new Binding(null, 'test.key')
  11. assert.ok(!b.root)
  12. })
  13. it('should have root==false if its key is an expression', function () {
  14. var b = new Binding(null, 'test', true)
  15. assert.ok(!b.root)
  16. })
  17. it('should have instances, subs and deps as Arrays', function () {
  18. var b = new Binding(null, 'test')
  19. assert.ok(Array.isArray(b.instances), 'instances')
  20. assert.ok(Array.isArray(b.subs), 'subs')
  21. assert.ok(Array.isArray(b.deps), 'deps')
  22. })
  23. })
  24. describe('.update()', function () {
  25. var b = new Binding(null, 'test'),
  26. val = 123,
  27. updated = 0,
  28. pubbed = false,
  29. numInstances = 3,
  30. instance = {
  31. update: function (value) {
  32. updated += value
  33. }
  34. }
  35. for (var i = 0; i < numInstances; i++) {
  36. b.instances.push(instance)
  37. }
  38. b.pub = function () {
  39. pubbed = true
  40. }
  41. before(function (done) {
  42. b.update(val)
  43. nextTick(function () {
  44. done()
  45. })
  46. })
  47. it('should set the binding\'s value', function () {
  48. assert.strictEqual(b.value, val)
  49. })
  50. it('should update the binding\'s instances', function () {
  51. assert.strictEqual(updated, val * numInstances)
  52. })
  53. it('should call the binding\'s pub() method', function () {
  54. assert.ok(pubbed)
  55. })
  56. it('should not set the value if it is computed unless a function', function () {
  57. var b1 = new Binding(null, 'test'),
  58. b2 = new Binding(null, 'test', false, true)
  59. b1.isComputed = true
  60. b2.isComputed = true
  61. var ov = { $get: function () {} }
  62. b1.value = ov
  63. b2.value = function () {}
  64. b1.update(1)
  65. b2.update(1)
  66. assert.strictEqual(b1.value, ov)
  67. assert.strictEqual(b2.value, 1)
  68. })
  69. })
  70. describe('.val()', function () {
  71. it('should return the raw value for non-computed and function bindings', function () {
  72. var b1 = new Binding(null, 'test'),
  73. b2 = new Binding(null, 'test', false, true)
  74. b2.isComputed = true
  75. b1.value = 1
  76. b2.value = 2
  77. assert.strictEqual(b1.val(), 1)
  78. assert.strictEqual(b2.val(), 2)
  79. })
  80. it('should return computed value for computed bindings', function () {
  81. var b = new Binding(null, 'test')
  82. b.isComputed = true
  83. b.value = {
  84. $get: function () {
  85. return 3
  86. }
  87. }
  88. assert.strictEqual(b.val(), 3)
  89. })
  90. })
  91. describe('.pub()', function () {
  92. var b = new Binding(null, 'test'),
  93. refreshed = 0,
  94. numSubs = 3,
  95. sub = {
  96. update: function () {
  97. refreshed++
  98. }
  99. }
  100. for (var i = 0; i < numSubs; i++) {
  101. b.subs.push(sub)
  102. }
  103. b.pub()
  104. it('should call update() of all subscribers', function () {
  105. assert.strictEqual(refreshed, numSubs)
  106. })
  107. })
  108. describe('.unbind()', function () {
  109. var b = new Binding(null, 'test'),
  110. unbound = 0,
  111. numInstances = 3,
  112. instance = {
  113. unbind: function () {
  114. unbound++
  115. }
  116. }
  117. for (var i = 0; i < numInstances; i++) {
  118. b.instances.push(instance)
  119. }
  120. // mock deps
  121. var dep1 = { subs: [1, 2, 3, b] },
  122. dep2 = { subs: [2, b, 4, 6] }
  123. b.deps.push(dep1, dep2)
  124. b.unbind()
  125. it('should call unbind() of all instances', function () {
  126. assert.strictEqual(unbound, numInstances)
  127. })
  128. it('should remove itself from the subs list of all its dependencies', function () {
  129. var notInSubs1 = dep1.subs.indexOf(b) === -1,
  130. notInSubs2 = dep2.subs.indexOf(b) === -1
  131. assert.ok(notInSubs1)
  132. assert.ok(notInSubs2)
  133. })
  134. })
  135. })