data_spec.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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(hasWarned(_, 'Invalid expression')).toBe(true)
  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. // warn against setting unexisting
  40. expect(hasWarned(_, 'Consider pre-initializing')).toBe(true)
  41. })
  42. it('$set invalid', function () {
  43. // invalid, should throw
  44. if (leftHandThrows()) {
  45. // if creating a function with invalid left hand
  46. // expression throws, the exp parser will catch the
  47. // error and warn.
  48. vm.$set('c + d', 1)
  49. expect(hasWarned(_, 'Invalid setter function body')).toBe(true)
  50. } else {
  51. // otherwise it will throw when calling the setter.
  52. expect(function () {
  53. try {
  54. vm.$set('c + d', 1)
  55. } catch (e) {
  56. return true
  57. }
  58. }()).toBe(true)
  59. }
  60. })
  61. it('$add', function () {
  62. vm._digest = jasmine.createSpy()
  63. vm.$add('c', 1)
  64. expect(vm.c).toBe(1)
  65. expect(vm._data.c).toBe(1)
  66. expect(vm._digest).toHaveBeenCalled()
  67. // reserved key should not be proxied
  68. vm.$add('_c', 1)
  69. expect(vm._c).toBeUndefined()
  70. })
  71. it('$delete', function () {
  72. vm._digest = jasmine.createSpy()
  73. vm.$delete('a')
  74. expect(vm.hasOwnProperty('a')).toBe(false)
  75. expect(vm._data.hasOwnProperty('a')).toBe(false)
  76. expect(vm._digest).toHaveBeenCalled()
  77. // reserved key should not be deleted
  78. vm.$delete('_data')
  79. expect(vm._data).toBeTruthy()
  80. })
  81. it('$watch', function (done) {
  82. var spy = jasmine.createSpy()
  83. // test immediate invoke
  84. var unwatch = vm.$watch('a + b.c', spy, {
  85. immediate: true
  86. })
  87. expect(spy).toHaveBeenCalledWith(3)
  88. vm.a = 2
  89. nextTick(function () {
  90. expect(spy).toHaveBeenCalledWith(4, 3)
  91. // unwatch
  92. unwatch()
  93. vm.a = 3
  94. nextTick(function () {
  95. expect(spy.calls.count()).toBe(2)
  96. done()
  97. })
  98. })
  99. })
  100. it('function $watch', function (done) {
  101. var spy = jasmine.createSpy()
  102. // test immediate invoke
  103. var unwatch = vm.$watch(function () {
  104. return this.a + this.b.c
  105. }, spy, { immediate: true })
  106. expect(spy).toHaveBeenCalledWith(3)
  107. vm.a = 2
  108. nextTick(function () {
  109. expect(spy).toHaveBeenCalledWith(4, 3)
  110. // unwatch
  111. unwatch()
  112. vm.a = 3
  113. nextTick(function () {
  114. expect(spy.calls.count()).toBe(2)
  115. done()
  116. })
  117. })
  118. })
  119. it('deep $watch', function (done) {
  120. var oldB = vm.b
  121. var spy = jasmine.createSpy()
  122. vm.$watch('b', spy, {
  123. deep: true
  124. })
  125. vm.b.c = 3
  126. nextTick(function () {
  127. expect(spy).toHaveBeenCalledWith(oldB, oldB)
  128. vm.b = { c: 4 }
  129. nextTick(function () {
  130. expect(spy).toHaveBeenCalledWith(vm.b, oldB)
  131. done()
  132. })
  133. })
  134. })
  135. it('$eval', function () {
  136. expect(vm.$eval('a')).toBe(1)
  137. expect(vm.$eval('b.c')).toBe(2)
  138. expect(vm.$eval('a + b.c | double')).toBe(6)
  139. })
  140. it('$interpolate', function () {
  141. expect(vm.$interpolate('abc')).toBe('abc')
  142. expect(vm.$interpolate('{{a}}')).toBe('1')
  143. expect(vm.$interpolate('{{a}} and {{a + b.c | double}}')).toBe('1 and 6')
  144. })
  145. if (typeof console !== 'undefined') {
  146. it('$log', function () {
  147. var oldLog = console.log
  148. var spy = jasmine.createSpy()
  149. console.log = function (val) {
  150. expect(val.a).toBe(1)
  151. expect(val.b.c).toBe(2)
  152. spy()
  153. }
  154. vm.$log()
  155. expect(spy.calls.count()).toBe(1)
  156. console.log = function (val) {
  157. expect(val.c).toBe(2)
  158. spy()
  159. }
  160. vm.$log('b')
  161. expect(spy.calls.count()).toBe(2)
  162. console.log = oldLog
  163. })
  164. }
  165. })
  166. /**
  167. * check if creating a new Function with invalid left-hand
  168. * assignment would throw
  169. */
  170. function leftHandThrows () {
  171. try {
  172. new Function('a + b = 1')
  173. } catch (e) {
  174. return true
  175. }
  176. }