2
0

data_spec.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. var el = document.createElement('div')
  8. el.setAttribute('prop', 'foo')
  9. vm = new Vue({
  10. el: el,
  11. props: ['prop'],
  12. data: {
  13. a: 1,
  14. b: {
  15. c: 2
  16. }
  17. },
  18. filters: {
  19. double: function (v) {
  20. return v * 2
  21. }
  22. },
  23. computed: {
  24. d: function () {
  25. return this.a + 1
  26. }
  27. }
  28. })
  29. })
  30. it('$get', function () {
  31. expect(vm.$get('a')).toBe(1)
  32. expect(vm.$get('b["c"]')).toBe(2)
  33. expect(vm.$get('a + b.c')).toBe(3)
  34. expect(vm.$get('c')).toBeUndefined()
  35. // invalid, should warn
  36. vm.$get('a(')
  37. expect('Invalid expression').toHaveBeenWarned()
  38. })
  39. it('$set', function () {
  40. vm.$set('a', 2)
  41. expect(vm.a).toBe(2)
  42. vm.$set('b["c"]', 3)
  43. expect(vm.b.c).toBe(3)
  44. // setting unexisting
  45. vm.$set('c.d', 2)
  46. expect(vm.c.d).toBe(2)
  47. // warn against setting unexisting
  48. expect('Consider pre-initializing').toHaveBeenWarned()
  49. })
  50. it('$set invalid', function () {
  51. vm.$set('c + d', 1)
  52. expect('Invalid setter expression').toHaveBeenWarned()
  53. })
  54. it('$delete', function () {
  55. vm._digest = jasmine.createSpy()
  56. vm.$delete('a')
  57. expect(_.hasOwn(vm, 'a')).toBe(false)
  58. expect(_.hasOwn(vm._data, 'a')).toBe(false)
  59. expect(vm._digest).toHaveBeenCalled()
  60. // reserved key should not be deleted
  61. vm.$delete('_data')
  62. expect(vm._data).toBeTruthy()
  63. })
  64. it('$watch', function (done) {
  65. var spy = jasmine.createSpy()
  66. // test immediate invoke
  67. var unwatch = vm.$watch('a + b.c', spy, {
  68. immediate: true
  69. })
  70. expect(spy).toHaveBeenCalledWith(3)
  71. vm.a = 2
  72. nextTick(function () {
  73. expect(spy).toHaveBeenCalledWith(4, 3)
  74. // unwatch
  75. unwatch()
  76. vm.a = 3
  77. nextTick(function () {
  78. expect(spy.calls.count()).toBe(2)
  79. done()
  80. })
  81. })
  82. })
  83. it('function $watch', function (done) {
  84. var spy = jasmine.createSpy()
  85. // test immediate invoke
  86. var unwatch = vm.$watch(function () {
  87. return this.a + this.b.c
  88. }, spy, { immediate: true })
  89. expect(spy).toHaveBeenCalledWith(3)
  90. vm.a = 2
  91. nextTick(function () {
  92. expect(spy).toHaveBeenCalledWith(4, 3)
  93. // unwatch
  94. unwatch()
  95. vm.a = 3
  96. nextTick(function () {
  97. expect(spy.calls.count()).toBe(2)
  98. done()
  99. })
  100. })
  101. })
  102. it('deep $watch', function (done) {
  103. var oldB = vm.b
  104. var spy = jasmine.createSpy()
  105. vm.$watch('b', spy, {
  106. deep: true
  107. })
  108. vm.b.c = 3
  109. nextTick(function () {
  110. expect(spy).toHaveBeenCalledWith(oldB, oldB)
  111. vm.b = { c: 4 }
  112. nextTick(function () {
  113. expect(spy).toHaveBeenCalledWith(vm.b, oldB)
  114. done()
  115. })
  116. })
  117. })
  118. it('$watch with filters', function (done) {
  119. var spy = jasmine.createSpy()
  120. vm.$watch('a | double', spy)
  121. vm.a = 2
  122. nextTick(function () {
  123. expect(spy).toHaveBeenCalledWith(4, 2)
  124. done()
  125. })
  126. })
  127. it('$eval', function () {
  128. expect(vm.$eval('a')).toBe(1)
  129. expect(vm.$eval('b.c')).toBe(2)
  130. expect(vm.$eval('a + b.c | double')).toBe(6)
  131. })
  132. it('$interpolate', function () {
  133. expect(vm.$interpolate('abc')).toBe('abc')
  134. expect(vm.$interpolate('{{a}}')).toBe('1')
  135. expect(vm.$interpolate('{{a}} and {{a + b.c | double}}')).toBe('1 and 6')
  136. })
  137. if (typeof console !== 'undefined') {
  138. it('$log', function () {
  139. var oldLog = console.log
  140. var spy = jasmine.createSpy()
  141. console.log = function (val) {
  142. expect(val.a).toBe(1)
  143. expect(val.b.c).toBe(2)
  144. expect(val.d).toBe(2)
  145. expect(val.prop).toBe('foo')
  146. spy()
  147. }
  148. vm.$log()
  149. expect(spy.calls.count()).toBe(1)
  150. console.log = function (val) {
  151. expect(val.c).toBe(2)
  152. spy()
  153. }
  154. vm.$log('b')
  155. expect(spy.calls.count()).toBe(2)
  156. console.log = oldLog
  157. })
  158. }
  159. })