data_spec.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. var Vue = require('src')
  2. var _ = require('src/util')
  3. var nextTick = _.nextTick
  4. describe('Data API', function () {
  5. var vm
  6. beforeEach(function () {
  7. spyWarns()
  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. vm.$set('c + d', 1)
  49. expect(hasWarned('Invalid setter expression')).toBe(true)
  50. })
  51. it('$delete', function () {
  52. vm._digest = jasmine.createSpy()
  53. vm.$delete('a')
  54. expect(_.hasOwn(vm, 'a')).toBe(false)
  55. expect(_.hasOwn(vm._data, 'a')).toBe(false)
  56. expect(vm._digest).toHaveBeenCalled()
  57. // reserved key should not be deleted
  58. vm.$delete('_data')
  59. expect(vm._data).toBeTruthy()
  60. })
  61. it('$watch', function (done) {
  62. var spy = jasmine.createSpy()
  63. // test immediate invoke
  64. var unwatch = vm.$watch('a + b.c', spy, {
  65. immediate: true
  66. })
  67. expect(spy).toHaveBeenCalledWith(3)
  68. vm.a = 2
  69. nextTick(function () {
  70. expect(spy).toHaveBeenCalledWith(4, 3)
  71. // unwatch
  72. unwatch()
  73. vm.a = 3
  74. nextTick(function () {
  75. expect(spy.calls.count()).toBe(2)
  76. done()
  77. })
  78. })
  79. })
  80. it('function $watch', function (done) {
  81. var spy = jasmine.createSpy()
  82. // test immediate invoke
  83. var unwatch = vm.$watch(function () {
  84. return this.a + this.b.c
  85. }, spy, { immediate: true })
  86. expect(spy).toHaveBeenCalledWith(3)
  87. vm.a = 2
  88. nextTick(function () {
  89. expect(spy).toHaveBeenCalledWith(4, 3)
  90. // unwatch
  91. unwatch()
  92. vm.a = 3
  93. nextTick(function () {
  94. expect(spy.calls.count()).toBe(2)
  95. done()
  96. })
  97. })
  98. })
  99. it('deep $watch', function (done) {
  100. var oldB = vm.b
  101. var spy = jasmine.createSpy()
  102. vm.$watch('b', spy, {
  103. deep: true
  104. })
  105. vm.b.c = 3
  106. nextTick(function () {
  107. expect(spy).toHaveBeenCalledWith(oldB, oldB)
  108. vm.b = { c: 4 }
  109. nextTick(function () {
  110. expect(spy).toHaveBeenCalledWith(vm.b, oldB)
  111. done()
  112. })
  113. })
  114. })
  115. it('$watch with filters', function (done) {
  116. var spy = jasmine.createSpy()
  117. vm.$watch('a | double', spy)
  118. vm.a = 2
  119. nextTick(function () {
  120. expect(spy).toHaveBeenCalledWith(4, 2)
  121. done()
  122. })
  123. })
  124. it('$eval', function () {
  125. expect(vm.$eval('a')).toBe(1)
  126. expect(vm.$eval('b.c')).toBe(2)
  127. expect(vm.$eval('a + b.c | double')).toBe(6)
  128. })
  129. it('$interpolate', function () {
  130. expect(vm.$interpolate('abc')).toBe('abc')
  131. expect(vm.$interpolate('{{a}}')).toBe('1')
  132. expect(vm.$interpolate('{{a}} and {{a + b.c | double}}')).toBe('1 and 6')
  133. })
  134. if (typeof console !== 'undefined') {
  135. it('$log', function () {
  136. var oldLog = console.log
  137. var spy = jasmine.createSpy()
  138. console.log = function (val) {
  139. expect(val.a).toBe(1)
  140. expect(val.b.c).toBe(2)
  141. expect(val.d).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. })