binding.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. })
  57. describe('.refresh()', function () {
  58. var b = new Binding(null, 'test'),
  59. refreshed = 0,
  60. numInstances = 3,
  61. instance = {
  62. refresh: function () {
  63. refreshed++
  64. }
  65. }
  66. for (var i = 0; i < numInstances; i++) {
  67. b.instances.push(instance)
  68. }
  69. before(function (done) {
  70. b.refresh()
  71. nextTick(function () {
  72. done()
  73. })
  74. })
  75. it('should call refresh() of all instances', function () {
  76. assert.strictEqual(refreshed, numInstances)
  77. })
  78. })
  79. describe('.pub()', function () {
  80. var b = new Binding(null, 'test'),
  81. refreshed = 0,
  82. numSubs = 3,
  83. sub = {
  84. refresh: function () {
  85. refreshed++
  86. }
  87. }
  88. for (var i = 0; i < numSubs; i++) {
  89. b.subs.push(sub)
  90. }
  91. b.pub()
  92. it('should call refresh() of all subscribers', function () {
  93. assert.strictEqual(refreshed, numSubs)
  94. })
  95. })
  96. describe('.unbind()', function () {
  97. var b = new Binding(null, 'test'),
  98. unbound = 0,
  99. numInstances = 3,
  100. instance = {
  101. unbind: function () {
  102. unbound++
  103. }
  104. }
  105. for (var i = 0; i < numInstances; i++) {
  106. b.instances.push(instance)
  107. }
  108. // mock deps
  109. var dep1 = { subs: [1, 2, 3, b] },
  110. dep2 = { subs: [2, b, 4, 6] }
  111. b.deps.push(dep1, dep2)
  112. b.unbind()
  113. it('should call unbind() of all instances', function () {
  114. assert.strictEqual(unbound, numInstances)
  115. })
  116. it('should remove itself from the subs list of all its dependencies', function () {
  117. var notInSubs1 = dep1.subs.indexOf(b) === -1,
  118. notInSubs2 = dep2.subs.indexOf(b) === -1
  119. assert.ok(notInSubs1)
  120. assert.ok(notInSubs2)
  121. })
  122. })
  123. })