data_spec.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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('$delete', function () {
  62. vm._digest = jasmine.createSpy()
  63. vm.$delete('a')
  64. expect(vm.hasOwnProperty('a')).toBe(false)
  65. expect(vm._data.hasOwnProperty('a')).toBe(false)
  66. expect(vm._digest).toHaveBeenCalled()
  67. // reserved key should not be deleted
  68. vm.$delete('_data')
  69. expect(vm._data).toBeTruthy()
  70. })
  71. it('$watch', function (done) {
  72. var spy = jasmine.createSpy()
  73. // test immediate invoke
  74. var unwatch = vm.$watch('a + b.c', spy, {
  75. immediate: true
  76. })
  77. expect(spy).toHaveBeenCalledWith(3)
  78. vm.a = 2
  79. nextTick(function () {
  80. expect(spy).toHaveBeenCalledWith(4, 3)
  81. // unwatch
  82. unwatch()
  83. vm.a = 3
  84. nextTick(function () {
  85. expect(spy.calls.count()).toBe(2)
  86. done()
  87. })
  88. })
  89. })
  90. it('function $watch', function (done) {
  91. var spy = jasmine.createSpy()
  92. // test immediate invoke
  93. var unwatch = vm.$watch(function () {
  94. return this.a + this.b.c
  95. }, spy, { immediate: true })
  96. expect(spy).toHaveBeenCalledWith(3)
  97. vm.a = 2
  98. nextTick(function () {
  99. expect(spy).toHaveBeenCalledWith(4, 3)
  100. // unwatch
  101. unwatch()
  102. vm.a = 3
  103. nextTick(function () {
  104. expect(spy.calls.count()).toBe(2)
  105. done()
  106. })
  107. })
  108. })
  109. it('deep $watch', function (done) {
  110. var oldB = vm.b
  111. var spy = jasmine.createSpy()
  112. vm.$watch('b', spy, {
  113. deep: true
  114. })
  115. vm.b.c = 3
  116. nextTick(function () {
  117. expect(spy).toHaveBeenCalledWith(oldB, oldB)
  118. vm.b = { c: 4 }
  119. nextTick(function () {
  120. expect(spy).toHaveBeenCalledWith(vm.b, oldB)
  121. done()
  122. })
  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}} 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. spy()
  142. }
  143. vm.$log()
  144. expect(spy.calls.count()).toBe(1)
  145. console.log = function (val) {
  146. expect(val.c).toBe(2)
  147. spy()
  148. }
  149. vm.$log('b')
  150. expect(spy.calls.count()).toBe(2)
  151. console.log = oldLog
  152. })
  153. }
  154. })
  155. /**
  156. * check if creating a new Function with invalid left-hand
  157. * assignment would throw
  158. */
  159. function leftHandThrows () {
  160. try {
  161. new Function('a + b = 1')
  162. } catch (e) {
  163. return true
  164. }
  165. }