binding.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. var assert = require('assert'),
  2. Binding = require('../../src/binding')
  3. describe('UNIT: Binding', function () {
  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))
  20. assert.ok(Array.isArray(b.subs))
  21. assert.ok(Array.isArray(b.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. b.update(val)
  42. it('should set the binding\'s value', function () {
  43. assert.ok(b.value === val)
  44. })
  45. it('should update the binding\'s instances', function () {
  46. assert.ok(updated === val * numInstances)
  47. })
  48. it('should call the binding\'s pub() method', function () {
  49. assert.ok(pubbed)
  50. })
  51. })
  52. describe('.refresh()', function () {
  53. var b = new Binding(null, 'test'),
  54. refreshed = 0,
  55. numInstances = 3,
  56. instance = {
  57. refresh: function () {
  58. refreshed++
  59. }
  60. }
  61. for (var i = 0; i < numInstances; i++) {
  62. b.instances.push(instance)
  63. }
  64. b.refresh()
  65. it('should call refresh() of all instances', function () {
  66. assert.ok(refreshed === numInstances)
  67. })
  68. })
  69. describe('.pub()', function () {
  70. var b = new Binding(null, 'test'),
  71. refreshed = 0,
  72. numSubs = 3,
  73. sub = {
  74. refresh: function () {
  75. refreshed++
  76. }
  77. }
  78. for (var i = 0; i < numSubs; i++) {
  79. b.subs.push(sub)
  80. }
  81. b.pub()
  82. it('should call refresh() of all subscribers', function () {
  83. assert.ok(refreshed === numSubs)
  84. })
  85. })
  86. describe('.unbind()', function () {
  87. var b = new Binding(null, 'test'),
  88. unbound = 0,
  89. pubbed = false,
  90. numInstances = 3,
  91. instance = {
  92. unbind: function () {
  93. unbound++
  94. }
  95. }
  96. for (var i = 0; i < numInstances; i++) {
  97. b.instances.push(instance)
  98. }
  99. // mock deps
  100. var dep1 = { subs: [1, 2, 3, b] },
  101. dep2 = { subs: [2, b, 4, 6] }
  102. b.deps.push(dep1, dep2)
  103. b.unbind()
  104. it('should call unbind() of all instances', function () {
  105. assert.ok(unbound === numInstances)
  106. })
  107. it('should remove itself from the subs list of all its dependencies', function () {
  108. assert.ok(dep1.subs.indexOf(b) === -1)
  109. assert.ok(dep2.subs.indexOf(b) === -1)
  110. })
  111. it('should unref all instance props', function () {
  112. assert.ok(b.compiler === null)
  113. assert.ok(b.pubs === null)
  114. assert.ok(b.subs === null)
  115. assert.ok(b.instances === null)
  116. assert.ok(b.deps === null)
  117. })
  118. })
  119. })