binding.js 3.7 KB

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