state_spec.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. it('replace $data and handle props', function () {
  91. var el = document.createElement('div')
  92. var vm = new Vue({
  93. el: el,
  94. template: '<test a="{{a}}" b="{{*b}}" c="{{@c}}"></test>',
  95. data: {
  96. a: 1,
  97. b: 2,
  98. c: 3
  99. },
  100. components: {
  101. test: {
  102. props: ['a', 'b', 'c', 'd'],
  103. data: function () {
  104. return {
  105. a: null // should be overwritten
  106. }
  107. }
  108. }
  109. }
  110. })
  111. var child = vm.$children[0]
  112. expect(child.a).toBe(1)
  113. expect(child.b).toBe(2)
  114. expect(child.c).toBe(3)
  115. // test new data without prop fields:
  116. // should just copy
  117. child.$data = {}
  118. expect(child.a).toBe(1)
  119. expect(child.b).toBe(2)
  120. expect(child.c).toBe(3)
  121. // test new data with value:
  122. child.$data = {
  123. a: 2, // one-way
  124. b: 3, // one-time
  125. c: 4 // two-way
  126. }
  127. expect(child.a).toBe(2)
  128. expect(child.b).toBe(3)
  129. expect(child.c).toBe(4)
  130. // assert parent state
  131. // one-way
  132. expect(vm.a).toBe(1)
  133. // one-time
  134. expect(vm.b).toBe(2)
  135. // two-way
  136. expect(vm.c).toBe(4)
  137. })
  138. })
  139. describe('computed', function () {
  140. var spyE = jasmine.createSpy('computed e')
  141. var spyF = jasmine.createSpy('cached computed f')
  142. var spyCachedWatcher = jasmine.createSpy('cached computed watcher')
  143. var Test = Vue.extend({
  144. computed: {
  145. c: function () {
  146. return this.a + this.b
  147. },
  148. d: {
  149. get: function () {
  150. return this.a + this.b
  151. },
  152. set: function (newVal) {
  153. var vals = newVal.split(' ')
  154. this.a = vals[0]
  155. this.b = vals[1]
  156. }
  157. },
  158. // chained computed
  159. e: function () {
  160. return this.c + 'e'
  161. },
  162. // cached
  163. f: {
  164. cache: true,
  165. get: function () {
  166. spyF()
  167. return this.ff
  168. }
  169. },
  170. // chained cached
  171. g: function () {
  172. return this.f + 1
  173. },
  174. // another cached, for watcher test
  175. h: {
  176. cache: true,
  177. get: function () {
  178. return this.hh
  179. }
  180. }
  181. }
  182. })
  183. var vm = new Test({
  184. data: {
  185. a: 'a',
  186. b: 'b',
  187. ff: 0,
  188. hh: 0
  189. },
  190. watch: {
  191. e: spyE,
  192. h: spyCachedWatcher
  193. }
  194. })
  195. it('get', function () {
  196. expect(vm.c).toBe('ab')
  197. expect(vm.d).toBe('ab')
  198. expect(vm.e).toBe('abe')
  199. })
  200. it('set', function (done) {
  201. vm.c = 123 // should do nothing
  202. vm.d = 'c d'
  203. expect(vm.a).toBe('c')
  204. expect(vm.b).toBe('d')
  205. expect(vm.c).toBe('cd')
  206. expect(vm.d).toBe('cd')
  207. expect(vm.e).toBe('cde')
  208. Vue.nextTick(function () {
  209. expect(spyE).toHaveBeenCalledWith('cde', 'abe')
  210. done()
  211. })
  212. })
  213. it('inherit', function (done) {
  214. var child = vm.$addChild({
  215. inherit: true
  216. })
  217. expect(child.c).toBe('cd')
  218. child.d = 'e f'
  219. expect(vm.a).toBe('e')
  220. expect(vm.b).toBe('f')
  221. expect(vm.c).toBe('ef')
  222. expect(vm.d).toBe('ef')
  223. expect(vm.e).toBe('efe')
  224. expect(child.a).toBe('e')
  225. expect(child.b).toBe('f')
  226. expect(child.c).toBe('ef')
  227. expect(child.d).toBe('ef')
  228. expect(vm.e).toBe('efe')
  229. Vue.nextTick(function () {
  230. expect(spyE).toHaveBeenCalledWith('efe', 'cde')
  231. done()
  232. })
  233. })
  234. it('cached computed', function () {
  235. expect(spyF).not.toHaveBeenCalled()
  236. var f = vm.f
  237. var g = vm.g
  238. expect(spyF.calls.count()).toBe(1)
  239. expect(f).toBe(0)
  240. expect(g).toBe(1)
  241. // get again
  242. f = vm.f
  243. g = vm.g
  244. // should not be evaluated again
  245. expect(spyF.calls.count()).toBe(1)
  246. expect(f).toBe(0)
  247. expect(g).toBe(1)
  248. // update dep
  249. vm.ff = 1
  250. f = vm.f
  251. g = vm.g
  252. expect(spyF.calls.count()).toBe(2)
  253. expect(f).toBe(1)
  254. expect(g).toBe(2)
  255. })
  256. it('watching cached computed', function (done) {
  257. expect(spyCachedWatcher).not.toHaveBeenCalled()
  258. vm.hh = 2
  259. Vue.nextTick(function () {
  260. expect(spyCachedWatcher).toHaveBeenCalledWith(2, 0)
  261. done()
  262. })
  263. })
  264. it('same definition object bound to different instance', function () {
  265. var vm = new Test({
  266. data: {
  267. a: 'A',
  268. b: 'B'
  269. }
  270. })
  271. expect(vm.c).toBe('AB')
  272. expect(vm.d).toBe('AB')
  273. vm.d = 'C D'
  274. expect(vm.a).toBe('C')
  275. expect(vm.b).toBe('D')
  276. expect(vm.c).toBe('CD')
  277. expect(vm.d).toBe('CD')
  278. expect(vm.e).toBe('CDe')
  279. })
  280. })
  281. describe('methods', function () {
  282. it('should work and have correct context', function () {
  283. var vm = new Vue({
  284. data: {
  285. a: 1
  286. },
  287. methods: {
  288. test: function () {
  289. expect(this instanceof Vue).toBe(true)
  290. return this.a
  291. }
  292. }
  293. })
  294. expect(vm.test()).toBe(1)
  295. var child = vm.$addChild({
  296. inherit: true
  297. })
  298. expect(child.test()).toBe(1)
  299. })
  300. })
  301. describe('meta', function () {
  302. var vm = new Vue({
  303. _meta: {
  304. $index: 0,
  305. $value: 'test'
  306. }
  307. })
  308. it('should define metas only on vm', function () {
  309. expect(vm.$index).toBe(0)
  310. expect(vm.$value).toBe('test')
  311. expect('$index' in vm.$data).toBe(false)
  312. expect('$value' in vm.$data).toBe(false)
  313. })
  314. })
  315. })