viewmodel.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  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('DOM methods', function () {
  191. var enterCalled,
  192. leaveCalled
  193. var v = new Vue({
  194. attributes: {
  195. 'v-transition': 'test'
  196. },
  197. transitions: {
  198. test: {
  199. enter: function (el, change) {
  200. enterCalled = true
  201. change()
  202. },
  203. leave: function (el, change) {
  204. leaveCalled = true
  205. change()
  206. }
  207. }
  208. }
  209. })
  210. function reset () {
  211. enterCalled = false
  212. leaveCalled = false
  213. }
  214. it('$appendTo', function () {
  215. reset()
  216. var parent = document.createElement('div')
  217. v.$appendTo(parent)
  218. assert.strictEqual(v.$el.parentNode, parent)
  219. assert.ok(enterCalled)
  220. })
  221. it('$before', function () {
  222. reset()
  223. var parent = document.createElement('div'),
  224. ref = document.createElement('div')
  225. parent.appendChild(ref)
  226. v.$before(ref)
  227. assert.strictEqual(v.$el.parentNode, parent)
  228. assert.strictEqual(v.$el.nextSibling, ref)
  229. assert.ok(enterCalled)
  230. })
  231. it('$after', function () {
  232. reset()
  233. var parent = document.createElement('div'),
  234. ref1 = document.createElement('div'),
  235. ref2 = document.createElement('div')
  236. parent.appendChild(ref1)
  237. parent.appendChild(ref2)
  238. v.$after(ref1)
  239. assert.strictEqual(v.$el.parentNode, parent)
  240. assert.strictEqual(v.$el.nextSibling, ref2)
  241. assert.strictEqual(ref1.nextSibling, v.$el)
  242. assert.ok(enterCalled)
  243. reset()
  244. v.$after(ref2)
  245. assert.strictEqual(v.$el.parentNode, parent)
  246. assert.notOk(v.$el.nextSibling)
  247. assert.strictEqual(ref2.nextSibling, v.$el)
  248. assert.ok(enterCalled)
  249. })
  250. it('$remove', function () {
  251. reset()
  252. var parent = document.createElement('div')
  253. v.$appendTo(parent)
  254. v.$remove()
  255. assert.notOk(v.$el.parentNode)
  256. assert.ok(enterCalled)
  257. assert.ok(leaveCalled)
  258. })
  259. })
  260. describe('.$destroy', function () {
  261. // since this simply delegates to Compiler.prototype.destroy(),
  262. // that's what we are actually testing here.
  263. var destroy = require('vue/src/compiler').prototype.destroy
  264. var beforeDestroyCalled = false,
  265. afterDestroyCalled = false,
  266. observerOffCalled = false,
  267. emitterOffCalled = false,
  268. dirUnbindCalled = false,
  269. expUnbindCalled = false,
  270. bindingUnbindCalled = false,
  271. unobserveCalled = 0,
  272. elRemoved = false,
  273. externalBindingUnbindCalled = false
  274. var dirMock = {
  275. binding: {
  276. compiler: null,
  277. instances: []
  278. },
  279. unbind: function () {
  280. dirUnbindCalled = true
  281. }
  282. }
  283. dirMock.binding.instances.push(dirMock)
  284. var bindingsMock = Object.create({
  285. 'test2': {
  286. unbind: function () {
  287. externalBindingUnbindCalled = true
  288. }
  289. }
  290. })
  291. bindingsMock.test = {
  292. root: true,
  293. key: 'test',
  294. value: {
  295. __observer__: {
  296. off: function () {
  297. unobserveCalled++
  298. return this
  299. }
  300. }
  301. },
  302. unbind: function () {
  303. bindingUnbindCalled = true
  304. }
  305. }
  306. var compilerMock = {
  307. options: {
  308. beforeDestroy: function () {
  309. beforeDestroyCalled = true
  310. },
  311. afterDestroy: function () {
  312. afterDestroyCalled = true
  313. }
  314. },
  315. observer: {
  316. off: function () {
  317. observerOffCalled = true
  318. },
  319. proxies: {
  320. 'test.': {}
  321. }
  322. },
  323. emitter: {
  324. off: function () {
  325. emitterOffCalled = true
  326. }
  327. },
  328. dirs: [dirMock],
  329. exps: [{
  330. unbind: function () {
  331. expUnbindCalled = true
  332. }
  333. }],
  334. bindings: bindingsMock,
  335. childId: 'test',
  336. parentCompiler: {
  337. childCompilers: [],
  338. vm: {
  339. $: {
  340. 'test': true
  341. }
  342. }
  343. },
  344. vm: {
  345. $remove: function () {
  346. elRemoved = true
  347. }
  348. },
  349. execHook: function (id) {
  350. this.options[id].call(this)
  351. }
  352. }
  353. compilerMock.parentCompiler.childCompilers.push(compilerMock)
  354. destroy.call(compilerMock)
  355. it('should call the pre and post destroy hooks', function () {
  356. assert.ok(beforeDestroyCalled)
  357. assert.ok(afterDestroyCalled)
  358. })
  359. it('should turn observer and emitter off', function () {
  360. assert.ok(observerOffCalled)
  361. assert.ok(emitterOffCalled)
  362. })
  363. it('should unbind all directives', function () {
  364. assert.ok(dirUnbindCalled)
  365. })
  366. it('should remove directives from external bindings', function () {
  367. assert.strictEqual(dirMock.binding.instances.indexOf(dirMock), -1)
  368. })
  369. it('should unbind all expressions', function () {
  370. assert.ok(expUnbindCalled)
  371. })
  372. it('should unbind and unobserve own bindings', function () {
  373. assert.ok(bindingUnbindCalled)
  374. assert.strictEqual(unobserveCalled, 3)
  375. })
  376. it('should not unbind external bindings', function () {
  377. assert.notOk(externalBindingUnbindCalled)
  378. })
  379. it('should remove self from parentCompiler', function () {
  380. var parent = compilerMock.parentCompiler
  381. assert.ok(parent.childCompilers.indexOf(compilerMock), -1)
  382. assert.strictEqual(parent.vm.$[compilerMock.childId], undefined)
  383. })
  384. it('should remove the dom element', function () {
  385. assert.ok(elRemoved)
  386. })
  387. })
  388. })