viewmodel.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*
  2. * Only tests the following:
  3. * - .$get()
  4. * - .$set()
  5. * - .$watch()
  6. * - .$unwatch()
  7. */
  8. describe('UNIT: ViewModel', function () {
  9. mock('vm-test', '{{a.b.c}}')
  10. var data = {
  11. b: {
  12. c: 12345
  13. }
  14. },
  15. arr = [1, 2, 3],
  16. vm = new Seed({
  17. el: '#vm-test',
  18. scope: {
  19. a: data,
  20. b: arr
  21. }
  22. })
  23. describe('.$get()', function () {
  24. it('should retrieve correct value', function () {
  25. assert.strictEqual(vm.$get('a.b.c'), data.b.c)
  26. })
  27. })
  28. describe('.$set()', function () {
  29. vm.$set('a.b.c', 54321)
  30. it('should set correct value', function () {
  31. assert.strictEqual(data.b.c, 54321)
  32. })
  33. })
  34. describe('.$watch()', function () {
  35. it('should trigger callback when a plain value changes', function () {
  36. var val
  37. vm.$watch('a.b.c', function (newVal) {
  38. val = newVal
  39. })
  40. data.b.c = 'new value!'
  41. assert.strictEqual(val, data.b.c)
  42. })
  43. it('should trigger callback when an object value changes', function () {
  44. var val, subVal, rootVal,
  45. target = { c: 'hohoho' }
  46. vm.$watch('a.b', function (newVal) {
  47. val = newVal
  48. })
  49. vm.$watch('a.b.c', function (newVal) {
  50. subVal = newVal
  51. })
  52. vm.$watch('a', function (newVal) {
  53. rootVal = newVal
  54. })
  55. data.b = target
  56. assert.strictEqual(val, target)
  57. assert.strictEqual(subVal, target.c)
  58. vm.a = 'hehehe'
  59. assert.strictEqual(rootVal, 'hehehe')
  60. })
  61. it('should trigger callback when an array mutates', function () {
  62. var val, mut
  63. vm.$watch('b', function (array, mutation) {
  64. val = array
  65. mut = mutation
  66. })
  67. arr.push(4)
  68. assert.strictEqual(val, arr)
  69. assert.strictEqual(mut.method, 'push')
  70. assert.strictEqual(mut.args.length, 1)
  71. assert.strictEqual(mut.args[0], 4)
  72. })
  73. })
  74. describe('.$unwatch()', function () {
  75. it('should unwatch the stuff', function () {
  76. var triggered = false
  77. vm.$watch('a.b.c', function () {
  78. triggered = true
  79. })
  80. vm.$watch('a', function () {
  81. triggered = true
  82. })
  83. vm.$watch('b', function () {
  84. triggered = true
  85. })
  86. vm.$unwatch('a')
  87. vm.$unwatch('b')
  88. vm.$unwatch('a.b.c')
  89. vm.a = { b: { c:123123 }}
  90. vm.b.push(5)
  91. assert.notOk(triggered)
  92. })
  93. })
  94. describe('.$on', function () {
  95. it('should register listener on vm\'s compiler\'s emitter', function () {
  96. var t = new Seed(),
  97. triggered = false,
  98. msg = 'on test'
  99. t.$on('test', function (m) {
  100. assert.strictEqual(m, msg)
  101. triggered = true
  102. })
  103. t.$compiler.emitter.emit('test', msg)
  104. assert.ok(triggered)
  105. })
  106. })
  107. describe('$off', function () {
  108. it('should turn off the listener', function () {
  109. var t = new Seed(),
  110. triggered1 = false,
  111. triggered2 = false,
  112. f1 = function () {
  113. triggered1 = true
  114. },
  115. f2 = function () {
  116. triggered2 = true
  117. }
  118. t.$on('test', f1)
  119. t.$on('test', f2)
  120. t.$off('test', f1)
  121. t.$compiler.emitter.emit('test')
  122. assert.notOk(triggered1)
  123. assert.ok(triggered2)
  124. })
  125. })
  126. describe('.$broadcast()', function () {
  127. it('should notify all child VMs', function () {
  128. var triggered = 0,
  129. msg = 'broadcast test'
  130. var Child = Seed.extend({
  131. init: function () {
  132. this.$on('hello', function (m) {
  133. assert.strictEqual(m, msg)
  134. triggered++
  135. })
  136. }
  137. })
  138. var Test = Seed.extend({
  139. template: '<div sd-viewmodel="test"></div><div sd-viewmodel="test"></div>',
  140. vms: {
  141. test: Child
  142. }
  143. })
  144. var t = new Test()
  145. t.$broadcast('hello', msg)
  146. assert.strictEqual(triggered, 2)
  147. })
  148. })
  149. describe('.$emit', function () {
  150. it('should notify all ancestor VMs', function (done) {
  151. var topTriggered = false,
  152. midTriggered = false,
  153. msg = 'emit test'
  154. var Bottom = Seed.extend({
  155. init: function () {
  156. var self = this
  157. setTimeout(function () {
  158. self.$emit('hello', msg)
  159. assert.ok(topTriggered)
  160. assert.ok(midTriggered)
  161. done()
  162. }, 0)
  163. }
  164. })
  165. var Middle = Seed.extend({
  166. template: '<div sd-viewmodel="bottom"></div>',
  167. vms: { bottom: Bottom },
  168. init: function () {
  169. this.$on('hello', function (m) {
  170. assert.strictEqual(m, msg)
  171. midTriggered = true
  172. })
  173. }
  174. })
  175. var Top = Seed.extend({
  176. template: '<div sd-viewmodel="middle"></div>',
  177. vms: { middle: Middle },
  178. init: function () {
  179. this.$on('hello', function (m) {
  180. assert.strictEqual(m, msg)
  181. topTriggered = true
  182. })
  183. }
  184. })
  185. new Top()
  186. })
  187. })
  188. })