data_spec.js 4.5 KB

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