state_spec.js 5.9 KB

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