data_spec.js 3.8 KB

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