data_spec.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. var Vue = require('../../../../src/vue')
  2. var _ = require('../../../../src/util')
  3. var nextTick = _.nextTick
  4. describe('Data API', function () {
  5. var vm
  6. beforeEach(function () {
  7. spyOn(_, 'warn')
  8. vm = new Vue({
  9. data: {
  10. a: 1,
  11. b: {
  12. c: 2
  13. }
  14. },
  15. filters: {
  16. double: function (v) {
  17. return v * 2
  18. }
  19. }
  20. })
  21. })
  22. it('$get', function () {
  23. expect(vm.$get('a')).toBe(1)
  24. expect(vm.$get('b["c"]')).toBe(2)
  25. expect(vm.$get('a + b.c')).toBe(3)
  26. expect(vm.$get('c')).toBeUndefined()
  27. // invalid, should warn
  28. vm.$get('a(')
  29. expect(_.warn).toHaveBeenCalled()
  30. })
  31. it('$set', function () {
  32. vm.$set('a', 2)
  33. expect(vm.a).toBe(2)
  34. vm.$set('b["c"]', 3)
  35. expect(vm.b.c).toBe(3)
  36. // setting unexisting
  37. vm.$set('c.d', 2)
  38. expect(vm.c.d).toBe(2)
  39. // invalid, should throw
  40. if (leftHandThrows()) {
  41. // if creating a function with invalid left hand
  42. // expression throws, the exp parser will catch the
  43. // error and warn.
  44. vm.$set('c + d', 1)
  45. expect(_.warn).toHaveBeenCalled()
  46. } else {
  47. // otherwise it will throw when calling the setter.
  48. expect(function () {
  49. try {
  50. vm.$set('c + d', 1)
  51. } catch (e) {
  52. return true
  53. }
  54. }()).toBe(true)
  55. }
  56. })
  57. it('$add', function () {
  58. vm._digest = jasmine.createSpy()
  59. vm.$add('c', 1)
  60. expect(vm.c).toBe(1)
  61. expect(vm._data.c).toBe(1)
  62. expect(vm._digest).toHaveBeenCalled()
  63. // reserved key should warn
  64. vm.$add('_c', 1)
  65. expect(vm._c).toBeUndefined()
  66. expect(_.warn).toHaveBeenCalled()
  67. })
  68. it('$delete', function () {
  69. vm._digest = jasmine.createSpy()
  70. vm.$delete('a')
  71. expect(vm.hasOwnProperty('a')).toBe(false)
  72. expect(vm._data.hasOwnProperty('a')).toBe(false)
  73. expect(vm._digest).toHaveBeenCalled()
  74. // reserved key should warn
  75. vm.$delete('_data')
  76. expect(vm._data).toBeTruthy()
  77. expect(_.warn).toHaveBeenCalled()
  78. })
  79. it('$watch', function (done) {
  80. var spy = jasmine.createSpy()
  81. // test immediate invoke
  82. var unwatch = vm.$watch('a + b.c', spy, false, true)
  83. expect(spy).toHaveBeenCalledWith(3, undefined)
  84. vm.a = 2
  85. nextTick(function () {
  86. expect(spy).toHaveBeenCalledWith(4, 3)
  87. // reuse same watcher
  88. var spy2 = jasmine.createSpy()
  89. var unwatch2 = vm.$watch('a + b.c', spy2)
  90. expect(vm._watcherList.length).toBe(1)
  91. vm.b = { c: 3 }
  92. nextTick(function () {
  93. expect(spy).toHaveBeenCalledWith(5, 4)
  94. expect(spy2).toHaveBeenCalledWith(5, 4)
  95. // unwatch
  96. unwatch()
  97. unwatch2()
  98. vm.a = 3
  99. nextTick(function () {
  100. expect(spy.calls.count()).toBe(3)
  101. expect(spy2.calls.count()).toBe(1)
  102. done()
  103. })
  104. })
  105. })
  106. })
  107. it('deep $watch', function (done) {
  108. var oldB = vm.b
  109. var spy = jasmine.createSpy()
  110. vm.$watch('b', spy, true)
  111. vm.b.c = 3
  112. nextTick(function () {
  113. expect(spy).toHaveBeenCalledWith(oldB, oldB)
  114. vm.b = { c: 4 }
  115. nextTick(function () {
  116. expect(spy).toHaveBeenCalledWith(vm.b, oldB)
  117. done()
  118. })
  119. })
  120. })
  121. it('$eval', function () {
  122. expect(vm.$eval('a')).toBe(1)
  123. expect(vm.$eval('b.c')).toBe(2)
  124. expect(vm.$eval('a + b.c | double')).toBe(6)
  125. })
  126. it('$interpolate', function () {
  127. expect(vm.$interpolate('abc')).toBe('abc')
  128. expect(vm.$interpolate('{{a}} and {{a + b.c | double}}')).toBe('1 and 6')
  129. })
  130. if (typeof console !== 'undefined') {
  131. it('$log', function () {
  132. var oldLog = console.log
  133. var spy = jasmine.createSpy()
  134. console.log = function (val) {
  135. expect(val.a).toBe(1)
  136. expect(val.b.c).toBe(2)
  137. spy()
  138. }
  139. vm.$log()
  140. expect(spy.calls.count()).toBe(1)
  141. console.log = function (val) {
  142. expect(val.c).toBe(2)
  143. spy()
  144. }
  145. vm.$log('b')
  146. expect(spy.calls.count()).toBe(2)
  147. console.log = oldLog
  148. })
  149. }
  150. })
  151. /**
  152. * check if creating a new Function with invalid left-hand
  153. * assignment would throw
  154. */
  155. function leftHandThrows () {
  156. try {
  157. var fn = new Function('a + b = 1')
  158. } catch (e) {
  159. return true
  160. }
  161. }