scope_spec.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. var Vue = require('../../../../src/vue')
  2. describe('Instance Scope', 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 (done) {
  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. Vue.nextTick(function () {
  132. // one-way
  133. expect(vm.a).toBe(1)
  134. // one-time
  135. expect(vm.b).toBe(2)
  136. // two-way
  137. expect(vm.c).toBe(4)
  138. done()
  139. })
  140. })
  141. })
  142. describe('computed', function () {
  143. var Test = Vue.extend({
  144. computed: {
  145. c: function () {
  146. expect(this).toBe(vm)
  147. return this.a + this.b
  148. },
  149. d: {
  150. get: function () {
  151. expect(this).toBe(vm)
  152. return this.a + this.b
  153. },
  154. set: function (newVal) {
  155. expect(this).toBe(vm)
  156. var vals = newVal.split(' ')
  157. this.a = vals[0]
  158. this.b = vals[1]
  159. }
  160. }
  161. }
  162. })
  163. var vm = new Test({
  164. data: {
  165. a: 'a',
  166. b: 'b'
  167. }
  168. })
  169. it('get', function () {
  170. expect(vm.c).toBe('ab')
  171. expect(vm.d).toBe('ab')
  172. })
  173. it('set', function () {
  174. vm.c = 123 // should do nothing
  175. vm.d = 'c d'
  176. expect(vm.a).toBe('c')
  177. expect(vm.b).toBe('d')
  178. expect(vm.c).toBe('cd')
  179. expect(vm.d).toBe('cd')
  180. })
  181. it('inherit', function () {
  182. var child = vm.$addChild({
  183. inherit: true
  184. })
  185. expect(child.c).toBe('cd')
  186. child.d = 'e f'
  187. expect(vm.a).toBe('e')
  188. expect(vm.b).toBe('f')
  189. expect(vm.c).toBe('ef')
  190. expect(vm.d).toBe('ef')
  191. expect(child.a).toBe('e')
  192. expect(child.b).toBe('f')
  193. expect(child.c).toBe('ef')
  194. expect(child.d).toBe('ef')
  195. })
  196. it('same definition object bound to different instance', function () {
  197. vm = new Test({
  198. data: {
  199. a: 'A',
  200. b: 'B'
  201. }
  202. })
  203. expect(vm.c).toBe('AB')
  204. expect(vm.d).toBe('AB')
  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. })
  211. })
  212. describe('methods', function () {
  213. it('should work and have correct context', function () {
  214. var vm = new Vue({
  215. data: {
  216. a: 1
  217. },
  218. methods: {
  219. test: function () {
  220. expect(this instanceof Vue).toBe(true)
  221. return this.a
  222. }
  223. }
  224. })
  225. expect(vm.test()).toBe(1)
  226. var child = vm.$addChild({
  227. inherit: true
  228. })
  229. expect(child.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. })