data_spec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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, false, true)
  85. expect(spy).toHaveBeenCalledWith(3, undefined)
  86. vm.a = 2
  87. nextTick(function () {
  88. expect(spy).toHaveBeenCalledWith(4, 3)
  89. // unwatch
  90. unwatch()
  91. vm.a = 3
  92. nextTick(function () {
  93. expect(spy.calls.count()).toBe(2)
  94. done()
  95. })
  96. })
  97. })
  98. it('deep $watch', function (done) {
  99. var oldB = vm.b
  100. var spy = jasmine.createSpy()
  101. vm.$watch('b', spy, true)
  102. vm.b.c = 3
  103. nextTick(function () {
  104. expect(spy).toHaveBeenCalledWith(oldB, oldB)
  105. vm.b = { c: 4 }
  106. nextTick(function () {
  107. expect(spy).toHaveBeenCalledWith(vm.b, oldB)
  108. done()
  109. })
  110. })
  111. })
  112. it('$eval', function () {
  113. expect(vm.$eval('a')).toBe(1)
  114. expect(vm.$eval('b.c')).toBe(2)
  115. expect(vm.$eval('a + b.c | double')).toBe(6)
  116. })
  117. it('$interpolate', function () {
  118. expect(vm.$interpolate('abc')).toBe('abc')
  119. expect(vm.$interpolate('{{a}} and {{a + b.c | double}}')).toBe('1 and 6')
  120. })
  121. if (typeof console !== 'undefined') {
  122. it('$log', function () {
  123. var oldLog = console.log
  124. var spy = jasmine.createSpy()
  125. console.log = function (val) {
  126. expect(val.a).toBe(1)
  127. expect(val.b.c).toBe(2)
  128. spy()
  129. }
  130. vm.$log()
  131. expect(spy.calls.count()).toBe(1)
  132. console.log = function (val) {
  133. expect(val.c).toBe(2)
  134. spy()
  135. }
  136. vm.$log('b')
  137. expect(spy.calls.count()).toBe(2)
  138. console.log = oldLog
  139. })
  140. }
  141. })
  142. /**
  143. * check if creating a new Function with invalid left-hand
  144. * assignment would throw
  145. */
  146. function leftHandThrows () {
  147. try {
  148. var fn = new Function('a + b = 1')
  149. } catch (e) {
  150. return true
  151. }
  152. }