computed.spec.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import Vue from 'vue'
  2. import testObjectOption from '../../../helpers/test-object-option'
  3. describe('Options computed', () => {
  4. it('basic usage', done => {
  5. const vm = new Vue({
  6. template: '<div>{{ b }}</div>',
  7. data: {
  8. a: 1
  9. },
  10. computed: {
  11. b () {
  12. return this.a + 1
  13. }
  14. }
  15. }).$mount()
  16. expect(vm.b).toBe(2)
  17. expect(vm.$el.textContent).toBe('2')
  18. vm.a = 2
  19. expect(vm.b).toBe(3)
  20. waitForUpdate(() => {
  21. expect(vm.$el.textContent).toBe('3')
  22. }).then(done)
  23. })
  24. it('with setter', done => {
  25. const vm = new Vue({
  26. template: '<div>{{ b }}</div>',
  27. data: {
  28. a: 1
  29. },
  30. computed: {
  31. b: {
  32. get () { return this.a + 1 },
  33. set (v) { this.a = v - 1 }
  34. }
  35. }
  36. }).$mount()
  37. expect(vm.b).toBe(2)
  38. expect(vm.$el.textContent).toBe('2')
  39. vm.a = 2
  40. expect(vm.b).toBe(3)
  41. waitForUpdate(() => {
  42. expect(vm.$el.textContent).toBe('3')
  43. vm.b = 1
  44. expect(vm.a).toBe(0)
  45. }).then(() => {
  46. expect(vm.$el.textContent).toBe('1')
  47. }).then(done)
  48. })
  49. testObjectOption('computed')
  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('rethrow computed error', () => {
  197. const vm = new Vue({
  198. computed: {
  199. a: () => {
  200. throw new Error('rethrow')
  201. }
  202. }
  203. })
  204. expect(() => vm.a).toThrowError('rethrow')
  205. })
  206. })