data_spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 not be proxied
  64. vm.$add('_c', 1)
  65. expect(vm._c).toBeUndefined()
  66. })
  67. it('$delete', function () {
  68. vm._digest = jasmine.createSpy()
  69. vm.$delete('a')
  70. expect(vm.hasOwnProperty('a')).toBe(false)
  71. expect(vm._data.hasOwnProperty('a')).toBe(false)
  72. expect(vm._digest).toHaveBeenCalled()
  73. // reserved key should not be deleted
  74. vm.$delete('_data')
  75. expect(vm._data).toBeTruthy()
  76. })
  77. it('$watch', function (done) {
  78. var spy = jasmine.createSpy()
  79. // test immediate invoke
  80. var unwatch = vm.$watch('a + b.c', spy, false, true)
  81. expect(spy).toHaveBeenCalledWith(3, undefined)
  82. vm.a = 2
  83. nextTick(function () {
  84. expect(spy).toHaveBeenCalledWith(4, 3)
  85. // reuse same watcher
  86. var spy2 = jasmine.createSpy()
  87. var unwatch2 = vm.$watch('a + b.c', spy2)
  88. expect(vm._watcherList.length).toBe(1)
  89. vm.b = { c: 3 }
  90. nextTick(function () {
  91. expect(spy).toHaveBeenCalledWith(5, 4)
  92. expect(spy2).toHaveBeenCalledWith(5, 4)
  93. // unwatch
  94. unwatch()
  95. unwatch2()
  96. vm.a = 3
  97. nextTick(function () {
  98. expect(spy.calls.count()).toBe(3)
  99. expect(spy2.calls.count()).toBe(1)
  100. done()
  101. })
  102. })
  103. })
  104. })
  105. it('deep $watch', function (done) {
  106. var oldB = vm.b
  107. var spy = jasmine.createSpy()
  108. vm.$watch('b', spy, true)
  109. vm.b.c = 3
  110. nextTick(function () {
  111. expect(spy).toHaveBeenCalledWith(oldB, oldB)
  112. vm.b = { c: 4 }
  113. nextTick(function () {
  114. expect(spy).toHaveBeenCalledWith(vm.b, oldB)
  115. done()
  116. })
  117. })
  118. })
  119. it('$eval', function () {
  120. expect(vm.$eval('a')).toBe(1)
  121. expect(vm.$eval('b.c')).toBe(2)
  122. expect(vm.$eval('a + b.c | double')).toBe(6)
  123. })
  124. it('$interpolate', function () {
  125. expect(vm.$interpolate('abc')).toBe('abc')
  126. expect(vm.$interpolate('{{a}} and {{a + b.c | double}}')).toBe('1 and 6')
  127. })
  128. if (typeof console !== 'undefined') {
  129. it('$log', function () {
  130. var oldLog = console.log
  131. var spy = jasmine.createSpy()
  132. console.log = function (val) {
  133. expect(val.a).toBe(1)
  134. expect(val.b.c).toBe(2)
  135. spy()
  136. }
  137. vm.$log()
  138. expect(spy.calls.count()).toBe(1)
  139. console.log = function (val) {
  140. expect(val.c).toBe(2)
  141. spy()
  142. }
  143. vm.$log('b')
  144. expect(spy.calls.count()).toBe(2)
  145. console.log = oldLog
  146. })
  147. }
  148. })
  149. /**
  150. * check if creating a new Function with invalid left-hand
  151. * assignment would throw
  152. */
  153. function leftHandThrows () {
  154. try {
  155. var fn = new Function('a + b = 1')
  156. } catch (e) {
  157. return true
  158. }
  159. }