viewmodel.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. describe('UNIT: ViewModel', function () {
  2. mock('vm-test', '{{a.b.c}}')
  3. var data = {
  4. b: {
  5. c: 12345
  6. }
  7. },
  8. arr = [1, 2, 3],
  9. vm = new Seed({
  10. el: '#vm-test',
  11. scope: {
  12. a: data,
  13. b: arr
  14. }
  15. })
  16. describe('.$set()', function () {
  17. vm.$set('a.b.c', 54321)
  18. it('should set correct value', function () {
  19. assert.strictEqual(data.b.c, 54321)
  20. })
  21. })
  22. describe('.$watch()', function () {
  23. it('should trigger callback when a plain value changes', function () {
  24. var val
  25. vm.$watch('a.b.c', function (newVal) {
  26. val = newVal
  27. })
  28. data.b.c = 'new value!'
  29. assert.strictEqual(val, data.b.c)
  30. })
  31. it('should trigger callback when an object value changes', function () {
  32. var val, subVal, rootVal,
  33. target = { c: 'hohoho' }
  34. vm.$watch('a.b', function (newVal) {
  35. val = newVal
  36. })
  37. vm.$watch('a.b.c', function (newVal) {
  38. subVal = newVal
  39. })
  40. vm.$watch('a', function (newVal) {
  41. rootVal = newVal
  42. })
  43. data.b = target
  44. assert.strictEqual(val, target)
  45. assert.strictEqual(subVal, target.c)
  46. vm.a = 'hehehe'
  47. assert.strictEqual(rootVal, 'hehehe')
  48. })
  49. it('should trigger callback when an array mutates', function () {
  50. var val, mut
  51. vm.$watch('b', function (array, mutation) {
  52. val = array
  53. mut = mutation
  54. })
  55. arr.push(4)
  56. assert.strictEqual(val, arr)
  57. assert.strictEqual(mut.method, 'push')
  58. assert.strictEqual(mut.args.length, 1)
  59. assert.strictEqual(mut.args[0], 4)
  60. })
  61. })
  62. describe('.$unwatch()', function () {
  63. it('should unwatch the stuff', function () {
  64. var triggered = false
  65. vm.$watch('a.b.c', function () {
  66. triggered = true
  67. })
  68. vm.$watch('a', function () {
  69. triggered = true
  70. })
  71. vm.$watch('b', function () {
  72. triggered = true
  73. })
  74. vm.$unwatch('a')
  75. vm.$unwatch('b')
  76. vm.$unwatch('a.b.c')
  77. vm.a = { b: { c:123123 }}
  78. vm.b.push(5)
  79. assert.notOk(triggered)
  80. })
  81. })
  82. describe('.$on', function () {
  83. it('should register listener on vm\'s compiler\'s emitter', function () {
  84. var t = new Seed(),
  85. triggered = false,
  86. msg = 'on test'
  87. t.$on('test', function (m) {
  88. assert.strictEqual(m, msg)
  89. triggered = true
  90. })
  91. t.$compiler.emitter.emit('test', msg)
  92. assert.ok(triggered)
  93. })
  94. })
  95. describe('.$once', function () {
  96. it('should invoke the listener only once', function () {
  97. var t = new Seed(),
  98. triggered = 0,
  99. msg = 'on once'
  100. t.$once('test', function (m) {
  101. assert.strictEqual(m, msg)
  102. triggered++
  103. })
  104. t.$compiler.emitter.emit('test', msg)
  105. t.$compiler.emitter.emit('test', msg)
  106. assert.strictEqual(triggered, 1)
  107. })
  108. })
  109. describe('$off', function () {
  110. it('should turn off the listener', function () {
  111. var t = new Seed(),
  112. triggered1 = false,
  113. triggered2 = false,
  114. f1 = function () {
  115. triggered1 = true
  116. },
  117. f2 = function () {
  118. triggered2 = true
  119. }
  120. t.$on('test', f1)
  121. t.$on('test', f2)
  122. t.$off('test', f1)
  123. t.$compiler.emitter.emit('test')
  124. assert.notOk(triggered1)
  125. assert.ok(triggered2)
  126. })
  127. })
  128. describe('.$broadcast()', function () {
  129. it('should notify all child VMs', function () {
  130. var triggered = 0,
  131. msg = 'broadcast test'
  132. var Child = Seed.extend({
  133. init: function () {
  134. this.$on('hello', function (m) {
  135. assert.strictEqual(m, msg)
  136. triggered++
  137. })
  138. }
  139. })
  140. var Test = Seed.extend({
  141. template: '<div sd-component="test"></div><div sd-component="test"></div>',
  142. components: {
  143. test: Child
  144. }
  145. })
  146. var t = new Test()
  147. t.$broadcast('hello', msg)
  148. assert.strictEqual(triggered, 2)
  149. })
  150. })
  151. describe('.$emit', function () {
  152. it('should notify all ancestor VMs', function (done) {
  153. var topTriggered = false,
  154. midTriggered = false,
  155. msg = 'emit test'
  156. var Bottom = Seed.extend({
  157. init: function () {
  158. var self = this
  159. setTimeout(function () {
  160. self.$emit('hello', msg)
  161. assert.ok(topTriggered)
  162. assert.ok(midTriggered)
  163. done()
  164. }, 0)
  165. }
  166. })
  167. var Middle = Seed.extend({
  168. template: '<div sd-component="bottom"></div>',
  169. components: { bottom: Bottom },
  170. init: function () {
  171. this.$on('hello', function (m) {
  172. assert.strictEqual(m, msg)
  173. midTriggered = true
  174. })
  175. }
  176. })
  177. var Top = Seed.extend({
  178. template: '<div sd-component="middle"></div>',
  179. components: { middle: Middle },
  180. init: function () {
  181. this.$on('hello', function (m) {
  182. assert.strictEqual(m, msg)
  183. topTriggered = true
  184. })
  185. }
  186. })
  187. new Top()
  188. })
  189. })
  190. describe('.$destroy', function () {
  191. // since this simply delegates to Compiler.prototype.destroy(),
  192. // that's what we are actually testing here.
  193. var destroy = require('seed/src/compiler').prototype.destroy
  194. var tearDownCalled = false,
  195. observerOffCalled = false,
  196. emitterOffCalled = false,
  197. dirUnbindCalled = false,
  198. expUnbindCalled = false,
  199. bindingUnbindCalled = false,
  200. unobserveCalled = 0,
  201. elRemoved = false,
  202. externalBindingUnbindCalled = false
  203. var dirMock = {
  204. binding: {
  205. compiler: null,
  206. instances: []
  207. },
  208. unbind: function () {
  209. dirUnbindCalled = true
  210. }
  211. }
  212. dirMock.binding.instances.push(dirMock)
  213. var bindingsMock = Object.create({
  214. 'test2': {
  215. unbind: function () {
  216. externalBindingUnbindCalled = true
  217. }
  218. }
  219. })
  220. bindingsMock.test = {
  221. root: true,
  222. key: 'test',
  223. value: {
  224. __observer__: {
  225. off: function () {
  226. unobserveCalled++
  227. return this
  228. }
  229. }
  230. },
  231. unbind: function () {
  232. bindingUnbindCalled = true
  233. }
  234. }
  235. var compilerMock = {
  236. options: {
  237. teardown: function () {
  238. tearDownCalled = true
  239. }
  240. },
  241. observer: {
  242. off: function () {
  243. observerOffCalled = true
  244. },
  245. proxies: {
  246. 'test.': {}
  247. }
  248. },
  249. emitter: {
  250. off: function () {
  251. emitterOffCalled = true
  252. }
  253. },
  254. dirs: [dirMock],
  255. exps: [{
  256. unbind: function () {
  257. expUnbindCalled = true
  258. }
  259. }],
  260. bindings: bindingsMock,
  261. childId: 'test',
  262. parentCompiler: {
  263. childCompilers: [],
  264. vm: {
  265. $: {
  266. 'test': true
  267. }
  268. }
  269. },
  270. el: {
  271. getAttribute: function () {},
  272. parentNode: {
  273. removeChild: function () {
  274. elRemoved = true
  275. }
  276. }
  277. }
  278. }
  279. compilerMock.parentCompiler.childCompilers.push(compilerMock)
  280. destroy.call(compilerMock)
  281. it('should call the teardown option', function () {
  282. assert.ok(tearDownCalled)
  283. })
  284. it('should turn observer and emitter off', function () {
  285. assert.ok(observerOffCalled)
  286. assert.ok(emitterOffCalled)
  287. })
  288. it('should unbind all directives', function () {
  289. assert.ok(dirUnbindCalled)
  290. })
  291. it('should remove directives from external bindings', function () {
  292. assert.strictEqual(dirMock.binding.instances.indexOf(dirMock), -1)
  293. })
  294. it('should unbind all expressions', function () {
  295. assert.ok(expUnbindCalled)
  296. })
  297. it('should unbind and unobserve own bindings', function () {
  298. assert.ok(bindingUnbindCalled)
  299. assert.strictEqual(unobserveCalled, 3)
  300. })
  301. it('should not unbind external bindings', function () {
  302. assert.notOk(externalBindingUnbindCalled)
  303. })
  304. it('should remove self from parentCompiler', function () {
  305. var parent = compilerMock.parentCompiler
  306. assert.ok(parent.childCompilers.indexOf(compilerMock), -1)
  307. assert.strictEqual(parent.vm.$[compilerMock.childId], undefined)
  308. })
  309. it('should remove the dom element', function () {
  310. assert.ok(elRemoved)
  311. })
  312. })
  313. })