state_spec.js 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. var Vue = require('../../../../src/vue')
  2. describe('Instance state initialization', function () {
  3. describe('data proxy', function () {
  4. var data = {
  5. a: 0,
  6. b: 0
  7. }
  8. var vm = new Vue({
  9. data: data
  10. })
  11. it('initial', function () {
  12. expect(vm.a).toBe(data.a)
  13. expect(vm.b).toBe(data.b)
  14. })
  15. it('vm => data', function () {
  16. vm.a = 1
  17. expect(data.a).toBe(1)
  18. expect(vm.a).toBe(data.a)
  19. })
  20. it('data => vm', function () {
  21. data.b = 2
  22. expect(vm.b).toBe(2)
  23. expect(vm.b).toBe(data.b)
  24. })
  25. })
  26. describe('$data', function () {
  27. it('should initialize props', function () {
  28. var vm = new Vue({
  29. el: document.createElement('div'),
  30. props: ['c']
  31. })
  32. expect(vm.hasOwnProperty('c')).toBe(true)
  33. })
  34. it('should use default prop value if prop not provided', function () {
  35. var vm = new Vue({
  36. el: document.createElement('div'),
  37. props: ['c'],
  38. data: {
  39. c: 1
  40. }
  41. })
  42. expect(vm.c).toBe(1)
  43. })
  44. it('external prop should overwrite default value', function () {
  45. var el = document.createElement('div')
  46. el.setAttribute('c', '2')
  47. el.textContent = '{{c}}'
  48. var vm = new Vue({
  49. el: el,
  50. props: ['c'],
  51. data: {
  52. c: 1
  53. }
  54. })
  55. expect(vm.c).toBe(2)
  56. expect(el.textContent).toBe('2')
  57. })
  58. it('props should be available in data() and create()', function () {
  59. var el = document.createElement('div')
  60. el.setAttribute('c', '2')
  61. var vm = new Vue({
  62. el: el,
  63. props: ['c'],
  64. data: function () {
  65. expect(this.c).toBe(2)
  66. expect(this._data.c).toBe(2)
  67. return {
  68. d: this.c + 1
  69. }
  70. },
  71. created: function () {
  72. expect(this.c).toBe(2)
  73. expect(this._data.c).toBe(2)
  74. }
  75. })
  76. expect(vm.d).toBe(3)
  77. })
  78. it('replace $data', function () {
  79. var vm = new Vue({
  80. data: {
  81. a: 1
  82. }
  83. })
  84. vm.$data = { b: 2 }
  85. // proxy new key
  86. expect(vm.b).toBe(2)
  87. // unproxy old key that's no longer present
  88. expect(vm.hasOwnProperty('a')).toBe(false)
  89. })
  90. })
  91. describe('computed', function () {
  92. var spyE = jasmine.createSpy('computed e')
  93. var spyF = jasmine.createSpy('cached computed f')
  94. var spyCachedWatcher = jasmine.createSpy('cached computed watcher')
  95. var Test = Vue.extend({
  96. computed: {
  97. // uncached
  98. c: {
  99. cache: false,
  100. get: function () {
  101. return this.a + this.b
  102. }
  103. },
  104. // with setter
  105. d: {
  106. get: function () {
  107. return this.a + this.b
  108. },
  109. set: function (newVal) {
  110. var vals = newVal.split(' ')
  111. this.a = vals[0]
  112. this.b = vals[1]
  113. }
  114. },
  115. // chained computed
  116. e: function () {
  117. return this.c + 'e'
  118. },
  119. // cached
  120. f: {
  121. get: function () {
  122. spyF()
  123. return this.ff
  124. }
  125. },
  126. // chained cached
  127. g: function () {
  128. return this.f + 1
  129. },
  130. // another cached, for watcher test
  131. h: {
  132. get: function () {
  133. return this.hh
  134. }
  135. }
  136. }
  137. })
  138. var vm = new Test({
  139. data: {
  140. a: 'a',
  141. b: 'b',
  142. ff: 0,
  143. hh: 0
  144. },
  145. watch: {
  146. e: spyE,
  147. h: spyCachedWatcher
  148. }
  149. })
  150. it('get', function () {
  151. expect(vm.c).toBe('ab')
  152. expect(vm.d).toBe('ab')
  153. expect(vm.e).toBe('abe')
  154. })
  155. it('set', function (done) {
  156. vm.c = 123 // should do nothing
  157. vm.d = 'c d'
  158. expect(vm.a).toBe('c')
  159. expect(vm.b).toBe('d')
  160. expect(vm.c).toBe('cd')
  161. expect(vm.d).toBe('cd')
  162. expect(vm.e).toBe('cde')
  163. Vue.nextTick(function () {
  164. expect(spyE).toHaveBeenCalledWith('cde', 'abe')
  165. done()
  166. })
  167. })
  168. it('cached computed', function () {
  169. expect(spyF).not.toHaveBeenCalled()
  170. var f = vm.f
  171. var g = vm.g
  172. expect(spyF.calls.count()).toBe(1)
  173. expect(f).toBe(0)
  174. expect(g).toBe(1)
  175. // get again
  176. f = vm.f
  177. g = vm.g
  178. // should not be evaluated again
  179. expect(spyF.calls.count()).toBe(1)
  180. expect(f).toBe(0)
  181. expect(g).toBe(1)
  182. // update dep
  183. vm.ff = 1
  184. f = vm.f
  185. g = vm.g
  186. expect(spyF.calls.count()).toBe(2)
  187. expect(f).toBe(1)
  188. expect(g).toBe(2)
  189. })
  190. it('watching cached computed', function (done) {
  191. expect(spyCachedWatcher).not.toHaveBeenCalled()
  192. vm.hh = 2
  193. Vue.nextTick(function () {
  194. expect(spyCachedWatcher).toHaveBeenCalledWith(2, 0)
  195. done()
  196. })
  197. })
  198. it('same definition object bound to different instance', function () {
  199. var vm = new Test({
  200. data: {
  201. a: 'A',
  202. b: 'B'
  203. }
  204. })
  205. expect(vm.c).toBe('AB')
  206. expect(vm.d).toBe('AB')
  207. vm.d = 'C D'
  208. expect(vm.a).toBe('C')
  209. expect(vm.b).toBe('D')
  210. expect(vm.c).toBe('CD')
  211. expect(vm.d).toBe('CD')
  212. expect(vm.e).toBe('CDe')
  213. })
  214. })
  215. describe('methods', function () {
  216. it('should work and have correct context', function () {
  217. var vm = new Vue({
  218. data: {
  219. a: 1
  220. },
  221. methods: {
  222. test: function () {
  223. expect(this instanceof Vue).toBe(true)
  224. return this.a
  225. }
  226. }
  227. })
  228. expect(vm.test()).toBe(1)
  229. })
  230. })
  231. describe('meta', function () {
  232. var vm = new Vue({
  233. _meta: {
  234. $index: 0,
  235. $value: 'test'
  236. }
  237. })
  238. it('should define metas only on vm', function () {
  239. expect(vm.$index).toBe(0)
  240. expect(vm.$value).toBe('test')
  241. expect('$index' in vm.$data).toBe(false)
  242. expect('$value' in vm.$data).toBe(false)
  243. })
  244. })
  245. })