viewmodel.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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 Vue({
  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 Vue(),
  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 Vue(),
  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 Vue(),
  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 = Vue.extend({
  133. ready: function () {
  134. this.$on('hello', function (m) {
  135. assert.strictEqual(m, msg)
  136. triggered++
  137. })
  138. }
  139. })
  140. var Test = Vue.extend({
  141. template: '<div v-component="test"></div><div v-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 = Vue.extend({
  157. ready: 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 = Vue.extend({
  168. template: '<div v-component="bottom"></div>',
  169. components: { bottom: Bottom },
  170. ready: function () {
  171. this.$on('hello', function (m) {
  172. assert.strictEqual(m, msg)
  173. midTriggered = true
  174. })
  175. }
  176. })
  177. var Top = Vue.extend({
  178. template: '<div v-component="middle"></div>',
  179. components: { middle: Middle },
  180. ready: 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('vue/src/compiler').prototype.destroy
  194. var beforeDestroyCalled = false,
  195. afterDestroyCalled = false,
  196. observerOffCalled = false,
  197. emitterOffCalled = false,
  198. dirUnbindCalled = false,
  199. expUnbindCalled = false,
  200. bindingUnbindCalled = false,
  201. unobserveCalled = 0,
  202. elRemoved = false,
  203. externalBindingUnbindCalled = false
  204. var dirMock = {
  205. binding: {
  206. compiler: null,
  207. instances: []
  208. },
  209. unbind: function () {
  210. dirUnbindCalled = true
  211. }
  212. }
  213. dirMock.binding.instances.push(dirMock)
  214. var bindingsMock = Object.create({
  215. 'test2': {
  216. unbind: function () {
  217. externalBindingUnbindCalled = true
  218. }
  219. }
  220. })
  221. bindingsMock.test = {
  222. root: true,
  223. key: 'test',
  224. value: {
  225. __observer__: {
  226. off: function () {
  227. unobserveCalled++
  228. return this
  229. }
  230. }
  231. },
  232. unbind: function () {
  233. bindingUnbindCalled = true
  234. }
  235. }
  236. var compilerMock = {
  237. options: {
  238. beforeDestroy: function () {
  239. beforeDestroyCalled = true
  240. },
  241. afterDestroy: function () {
  242. afterDestroyCalled = true
  243. }
  244. },
  245. observer: {
  246. off: function () {
  247. observerOffCalled = true
  248. },
  249. proxies: {
  250. 'test.': {}
  251. }
  252. },
  253. emitter: {
  254. off: function () {
  255. emitterOffCalled = true
  256. }
  257. },
  258. dirs: [dirMock],
  259. exps: [{
  260. unbind: function () {
  261. expUnbindCalled = true
  262. }
  263. }],
  264. bindings: bindingsMock,
  265. childId: 'test',
  266. parentCompiler: {
  267. childCompilers: [],
  268. vm: {
  269. $: {
  270. 'test': true
  271. }
  272. }
  273. },
  274. vm: {
  275. $remove: function () {
  276. elRemoved = true
  277. }
  278. },
  279. execHook: function (id) {
  280. this.options[id].call(this)
  281. }
  282. }
  283. compilerMock.parentCompiler.childCompilers.push(compilerMock)
  284. destroy.call(compilerMock)
  285. it('should call the pre and post destroy hooks', function () {
  286. assert.ok(beforeDestroyCalled)
  287. assert.ok(afterDestroyCalled)
  288. })
  289. it('should turn observer and emitter off', function () {
  290. assert.ok(observerOffCalled)
  291. assert.ok(emitterOffCalled)
  292. })
  293. it('should unbind all directives', function () {
  294. assert.ok(dirUnbindCalled)
  295. })
  296. it('should remove directives from external bindings', function () {
  297. assert.strictEqual(dirMock.binding.instances.indexOf(dirMock), -1)
  298. })
  299. it('should unbind all expressions', function () {
  300. assert.ok(expUnbindCalled)
  301. })
  302. it('should unbind and unobserve own bindings', function () {
  303. assert.ok(bindingUnbindCalled)
  304. assert.strictEqual(unobserveCalled, 3)
  305. })
  306. it('should not unbind external bindings', function () {
  307. assert.notOk(externalBindingUnbindCalled)
  308. })
  309. it('should remove self from parentCompiler', function () {
  310. var parent = compilerMock.parentCompiler
  311. assert.ok(parent.childCompilers.indexOf(compilerMock), -1)
  312. assert.strictEqual(parent.vm.$[compilerMock.childId], undefined)
  313. })
  314. it('should remove the dom element', function () {
  315. assert.ok(elRemoved)
  316. })
  317. })
  318. })