state_spec.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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. // uncached
  146. c: {
  147. cache: false,
  148. get: function () {
  149. return this.a + this.b
  150. }
  151. },
  152. // with setter
  153. d: {
  154. get: function () {
  155. return this.a + this.b
  156. },
  157. set: function (newVal) {
  158. var vals = newVal.split(' ')
  159. this.a = vals[0]
  160. this.b = vals[1]
  161. }
  162. },
  163. // chained computed
  164. e: function () {
  165. return this.c + 'e'
  166. },
  167. // cached
  168. f: {
  169. get: function () {
  170. spyF()
  171. return this.ff
  172. }
  173. },
  174. // chained cached
  175. g: function () {
  176. return this.f + 1
  177. },
  178. // another cached, for watcher test
  179. h: {
  180. get: function () {
  181. return this.hh
  182. }
  183. }
  184. }
  185. })
  186. var vm = new Test({
  187. data: {
  188. a: 'a',
  189. b: 'b',
  190. ff: 0,
  191. hh: 0
  192. },
  193. watch: {
  194. e: spyE,
  195. h: spyCachedWatcher
  196. }
  197. })
  198. it('get', function () {
  199. expect(vm.c).toBe('ab')
  200. expect(vm.d).toBe('ab')
  201. expect(vm.e).toBe('abe')
  202. })
  203. it('set', function (done) {
  204. vm.c = 123 // should do nothing
  205. vm.d = 'c d'
  206. expect(vm.a).toBe('c')
  207. expect(vm.b).toBe('d')
  208. expect(vm.c).toBe('cd')
  209. expect(vm.d).toBe('cd')
  210. expect(vm.e).toBe('cde')
  211. Vue.nextTick(function () {
  212. expect(spyE).toHaveBeenCalledWith('cde', 'abe')
  213. done()
  214. })
  215. })
  216. it('inherit', function (done) {
  217. var child = vm.$addChild({
  218. inherit: true
  219. })
  220. expect(child.c).toBe('cd')
  221. child.d = 'e f'
  222. expect(vm.a).toBe('e')
  223. expect(vm.b).toBe('f')
  224. expect(vm.c).toBe('ef')
  225. expect(vm.d).toBe('ef')
  226. expect(vm.e).toBe('efe')
  227. expect(child.a).toBe('e')
  228. expect(child.b).toBe('f')
  229. expect(child.c).toBe('ef')
  230. expect(child.d).toBe('ef')
  231. expect(vm.e).toBe('efe')
  232. Vue.nextTick(function () {
  233. expect(spyE).toHaveBeenCalledWith('efe', 'cde')
  234. done()
  235. })
  236. })
  237. it('cached computed', function () {
  238. expect(spyF).not.toHaveBeenCalled()
  239. var f = vm.f
  240. var g = vm.g
  241. expect(spyF.calls.count()).toBe(1)
  242. expect(f).toBe(0)
  243. expect(g).toBe(1)
  244. // get again
  245. f = vm.f
  246. g = vm.g
  247. // should not be evaluated again
  248. expect(spyF.calls.count()).toBe(1)
  249. expect(f).toBe(0)
  250. expect(g).toBe(1)
  251. // update dep
  252. vm.ff = 1
  253. f = vm.f
  254. g = vm.g
  255. expect(spyF.calls.count()).toBe(2)
  256. expect(f).toBe(1)
  257. expect(g).toBe(2)
  258. })
  259. it('watching cached computed', function (done) {
  260. expect(spyCachedWatcher).not.toHaveBeenCalled()
  261. vm.hh = 2
  262. Vue.nextTick(function () {
  263. expect(spyCachedWatcher).toHaveBeenCalledWith(2, 0)
  264. done()
  265. })
  266. })
  267. it('same definition object bound to different instance', function () {
  268. var vm = new Test({
  269. data: {
  270. a: 'A',
  271. b: 'B'
  272. }
  273. })
  274. expect(vm.c).toBe('AB')
  275. expect(vm.d).toBe('AB')
  276. vm.d = 'C D'
  277. expect(vm.a).toBe('C')
  278. expect(vm.b).toBe('D')
  279. expect(vm.c).toBe('CD')
  280. expect(vm.d).toBe('CD')
  281. expect(vm.e).toBe('CDe')
  282. })
  283. })
  284. describe('methods', function () {
  285. it('should work and have correct context', function () {
  286. var vm = new Vue({
  287. data: {
  288. a: 1
  289. },
  290. methods: {
  291. test: function () {
  292. expect(this instanceof Vue).toBe(true)
  293. return this.a
  294. }
  295. }
  296. })
  297. expect(vm.test()).toBe(1)
  298. var child = vm.$addChild({
  299. inherit: true
  300. })
  301. expect(child.test()).toBe(1)
  302. })
  303. })
  304. describe('meta', function () {
  305. var vm = new Vue({
  306. _meta: {
  307. $index: 0,
  308. $value: 'test'
  309. }
  310. })
  311. it('should define metas only on vm', function () {
  312. expect(vm.$index).toBe(0)
  313. expect(vm.$value).toBe('test')
  314. expect('$index' in vm.$data).toBe(false)
  315. expect('$value' in vm.$data).toBe(false)
  316. })
  317. })
  318. })