2
0

computed.spec.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. import Vue from 'vue'
  2. import testObjectOption from '../../../helpers/test-object-option'
  3. describe('Options computed', () => {
  4. testObjectOption('computed')
  5. it('basic usage', done => {
  6. const vm = new Vue({
  7. template: '<div>{{ b }}</div>',
  8. data: {
  9. a: 1
  10. },
  11. computed: {
  12. b () {
  13. return this.a + 1
  14. }
  15. }
  16. }).$mount()
  17. expect(vm.b).toBe(2)
  18. expect(vm.$el.textContent).toBe('2')
  19. vm.a = 2
  20. expect(vm.b).toBe(3)
  21. waitForUpdate(() => {
  22. expect(vm.$el.textContent).toBe('3')
  23. }).then(done)
  24. })
  25. it('with setter', done => {
  26. const vm = new Vue({
  27. template: '<div>{{ b }}</div>',
  28. data: {
  29. a: 1
  30. },
  31. computed: {
  32. b: {
  33. get () { return this.a + 1 },
  34. set (v) { this.a = v - 1 }
  35. }
  36. }
  37. }).$mount()
  38. expect(vm.b).toBe(2)
  39. expect(vm.$el.textContent).toBe('2')
  40. vm.a = 2
  41. expect(vm.b).toBe(3)
  42. waitForUpdate(() => {
  43. expect(vm.$el.textContent).toBe('3')
  44. vm.b = 1
  45. expect(vm.a).toBe(0)
  46. }).then(() => {
  47. expect(vm.$el.textContent).toBe('1')
  48. }).then(done)
  49. })
  50. it('warn with setter and no getter', () => {
  51. const vm = new Vue({
  52. template: `
  53. <div>
  54. <test></test>
  55. </div>
  56. `,
  57. components: {
  58. test: {
  59. data () {
  60. return {
  61. a: 1
  62. }
  63. },
  64. computed: {
  65. b: {
  66. set (v) { this.a = v }
  67. }
  68. },
  69. template: `<div>{{a}}</div>`
  70. }
  71. }
  72. }).$mount()
  73. expect(vm.$el.innerHTML).toBe('<div>1</div>')
  74. expect('Getter is missing for computed property "b".').toHaveBeenWarned()
  75. })
  76. it('warn assigning to computed with no setter', () => {
  77. const vm = new Vue({
  78. computed: {
  79. b () {
  80. return 1
  81. }
  82. }
  83. })
  84. vm.b = 2
  85. expect(`Computed property "b" was assigned to but it has no setter.`).toHaveBeenWarned()
  86. })
  87. it('watching computed', done => {
  88. const spy = jasmine.createSpy('watch computed')
  89. const vm = new Vue({
  90. data: {
  91. a: 1
  92. },
  93. computed: {
  94. b () { return this.a + 1 }
  95. }
  96. })
  97. vm.$watch('b', spy)
  98. vm.a = 2
  99. waitForUpdate(() => {
  100. expect(spy).toHaveBeenCalledWith(3, 2)
  101. }).then(done)
  102. })
  103. it('caching', () => {
  104. const spy = jasmine.createSpy('cached computed')
  105. const vm = new Vue({
  106. data: {
  107. a: 1
  108. },
  109. computed: {
  110. b () {
  111. spy()
  112. return this.a + 1
  113. }
  114. }
  115. })
  116. expect(spy.calls.count()).toBe(0)
  117. vm.b
  118. expect(spy.calls.count()).toBe(1)
  119. vm.b
  120. expect(spy.calls.count()).toBe(1)
  121. })
  122. it('cache: false', () => {
  123. const spy = jasmine.createSpy('cached computed')
  124. const vm = new Vue({
  125. data: {
  126. a: 1
  127. },
  128. computed: {
  129. b: {
  130. cache: false,
  131. get () {
  132. spy()
  133. return this.a + 1
  134. }
  135. }
  136. }
  137. })
  138. expect(spy.calls.count()).toBe(0)
  139. vm.b
  140. expect(spy.calls.count()).toBe(1)
  141. vm.b
  142. expect(spy.calls.count()).toBe(2)
  143. })
  144. it('as component', done => {
  145. const Comp = Vue.extend({
  146. template: `<div>{{ b }} {{ c }}</div>`,
  147. data () {
  148. return { a: 1 }
  149. },
  150. computed: {
  151. // defined on prototype
  152. b () {
  153. return this.a + 1
  154. }
  155. }
  156. })
  157. const vm = new Comp({
  158. computed: {
  159. // defined at instantiation
  160. c () {
  161. return this.b + 1
  162. }
  163. }
  164. }).$mount()
  165. expect(vm.b).toBe(2)
  166. expect(vm.c).toBe(3)
  167. expect(vm.$el.textContent).toBe('2 3')
  168. vm.a = 2
  169. expect(vm.b).toBe(3)
  170. expect(vm.c).toBe(4)
  171. waitForUpdate(() => {
  172. expect(vm.$el.textContent).toBe('3 4')
  173. }).then(done)
  174. })
  175. it('warn conflict with data', () => {
  176. new Vue({
  177. data: {
  178. a: 1
  179. },
  180. computed: {
  181. a: () => 2
  182. }
  183. })
  184. expect(`computed property "a" is already defined in data`).toHaveBeenWarned()
  185. })
  186. it('warn conflict with props', () => {
  187. new Vue({
  188. props: ['a'],
  189. propsData: { a: 1 },
  190. computed: {
  191. a: () => 2
  192. }
  193. })
  194. expect(`computed property "a" is already defined as a prop`).toHaveBeenWarned()
  195. })
  196. it('warn conflict with methods', () => {
  197. new Vue({
  198. computed: {
  199. a: () => 2
  200. },
  201. methods: {
  202. a: () => {}
  203. }
  204. })
  205. expect(`computed property "a" is already defined as a method`).toHaveBeenWarned()
  206. })
  207. it('rethrow computed error', () => {
  208. const vm = new Vue({
  209. computed: {
  210. a: () => {
  211. throw new Error('rethrow')
  212. }
  213. }
  214. })
  215. expect(() => vm.a).toThrowError('rethrow')
  216. })
  217. })