data_spec.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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('$add', function () {
  43. vm._digest = jasmine.createSpy()
  44. vm.$add('c', 1)
  45. expect(vm.c).toBe(1)
  46. expect(vm._data.c).toBe(1)
  47. expect(vm._digest).toHaveBeenCalled()
  48. // reserved key should not be proxied
  49. vm.$add('_c', 1)
  50. expect(vm._c).toBeUndefined()
  51. })
  52. it('$delete', function () {
  53. vm._digest = jasmine.createSpy()
  54. vm.$delete('a')
  55. expect(vm.hasOwnProperty('a')).toBe(false)
  56. expect(vm._data.hasOwnProperty('a')).toBe(false)
  57. expect(vm._digest).toHaveBeenCalled()
  58. // reserved key should not be deleted
  59. vm.$delete('_data')
  60. expect(vm._data).toBeTruthy()
  61. })
  62. it('$watch', function (done) {
  63. var spy = jasmine.createSpy()
  64. // test immediate invoke
  65. var unwatch = vm.$watch('a + b.c', spy, {
  66. immediate: true
  67. })
  68. expect(spy).toHaveBeenCalledWith(3)
  69. vm.a = 2
  70. nextTick(function () {
  71. expect(spy).toHaveBeenCalledWith(4, 3)
  72. // unwatch
  73. unwatch()
  74. vm.a = 3
  75. nextTick(function () {
  76. expect(spy.calls.count()).toBe(2)
  77. done()
  78. })
  79. })
  80. })
  81. it('function $watch', function (done) {
  82. var spy = jasmine.createSpy()
  83. // test immediate invoke
  84. var unwatch = vm.$watch(function () {
  85. return this.a + this.b.c
  86. }, spy, { immediate: true })
  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('deep $watch', function (done) {
  101. var oldB = vm.b
  102. var spy = jasmine.createSpy()
  103. vm.$watch('b', spy, {
  104. deep: true
  105. })
  106. vm.b.c = 3
  107. nextTick(function () {
  108. expect(spy).toHaveBeenCalledWith(oldB, oldB)
  109. vm.b = { c: 4 }
  110. nextTick(function () {
  111. expect(spy).toHaveBeenCalledWith(vm.b, oldB)
  112. done()
  113. })
  114. })
  115. })
  116. it('$watch with filters', function (done) {
  117. var spy = jasmine.createSpy()
  118. vm.$watch('a | double', spy)
  119. vm.a = 2
  120. nextTick(function () {
  121. expect(spy).toHaveBeenCalledWith(4, 2)
  122. done()
  123. })
  124. })
  125. it('$eval', function () {
  126. expect(vm.$eval('a')).toBe(1)
  127. expect(vm.$eval('b.c')).toBe(2)
  128. expect(vm.$eval('a + b.c | double')).toBe(6)
  129. })
  130. it('$interpolate', function () {
  131. expect(vm.$interpolate('abc')).toBe('abc')
  132. expect(vm.$interpolate('{{a}}')).toBe('1')
  133. expect(vm.$interpolate('{{a}} and {{a + b.c | double}}')).toBe('1 and 6')
  134. })
  135. if (typeof console !== 'undefined') {
  136. it('$log', function () {
  137. var oldLog = console.log
  138. var spy = jasmine.createSpy()
  139. console.log = function (val) {
  140. expect(val.a).toBe(1)
  141. expect(val.b.c).toBe(2)
  142. spy()
  143. }
  144. vm.$log()
  145. expect(spy.calls.count()).toBe(1)
  146. console.log = function (val) {
  147. expect(val.c).toBe(2)
  148. spy()
  149. }
  150. vm.$log('b')
  151. expect(spy.calls.count()).toBe(2)
  152. console.log = oldLog
  153. })
  154. }
  155. })