computed.spec.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. import {
  2. computed,
  3. reactive,
  4. ref,
  5. isReadonly,
  6. // toRaw,
  7. WritableComputedRef
  8. // DebuggerEvent
  9. } from 'vca/index'
  10. import { effect } from 'vca/reactivity/effect'
  11. import { nextTick } from 'core/util'
  12. // import { TrackOpTypes, TriggerOpTypes } from 'vca/reactivity/operations'
  13. describe('reactivity/computed', () => {
  14. it('should return updated value', () => {
  15. const value = reactive({ foo: 1 })
  16. const cValue = computed(() => value.foo)
  17. expect(cValue.value).toBe(1)
  18. value.foo = 2
  19. expect(cValue.value).toBe(2)
  20. })
  21. it('should compute lazily', () => {
  22. const value = reactive<{ foo?: number }>({ foo: undefined })
  23. const getter = vi.fn(() => value.foo)
  24. const cValue = computed(getter)
  25. // lazy
  26. expect(getter).not.toHaveBeenCalled()
  27. expect(cValue.value).toBe(undefined)
  28. expect(getter).toHaveBeenCalledTimes(1)
  29. // should not compute again
  30. cValue.value
  31. expect(getter).toHaveBeenCalledTimes(1)
  32. // should not compute until needed
  33. value.foo = 1
  34. expect(getter).toHaveBeenCalledTimes(1)
  35. // now it should compute
  36. expect(cValue.value).toBe(1)
  37. expect(getter).toHaveBeenCalledTimes(2)
  38. // should not compute again
  39. cValue.value
  40. expect(getter).toHaveBeenCalledTimes(2)
  41. })
  42. it('should trigger effect', () => {
  43. const value = reactive<{ foo?: number }>({ foo: undefined })
  44. const cValue = computed(() => value.foo)
  45. let dummy
  46. effect(() => {
  47. dummy = cValue.value
  48. })
  49. expect(dummy).toBe(undefined)
  50. value.foo = 1
  51. expect(dummy).toBe(1)
  52. })
  53. it('should work when chained', () => {
  54. const value = reactive({ foo: 0 })
  55. const c1 = computed(() => value.foo)
  56. const c2 = computed(() => c1.value + 1)
  57. expect(c2.value).toBe(1)
  58. expect(c1.value).toBe(0)
  59. value.foo++
  60. expect(c2.value).toBe(2)
  61. expect(c1.value).toBe(1)
  62. })
  63. it('should trigger effect when chained', () => {
  64. const value = reactive({ foo: 0 })
  65. const getter1 = vi.fn(() => value.foo)
  66. const getter2 = vi.fn(() => {
  67. return c1.value + 1
  68. })
  69. const c1 = computed(getter1)
  70. const c2 = computed(getter2)
  71. let dummy
  72. effect(() => {
  73. dummy = c2.value
  74. })
  75. expect(dummy).toBe(1)
  76. expect(getter1).toHaveBeenCalledTimes(1)
  77. expect(getter2).toHaveBeenCalledTimes(1)
  78. value.foo++
  79. expect(dummy).toBe(2)
  80. // should not result in duplicate calls
  81. expect(getter1).toHaveBeenCalledTimes(2)
  82. expect(getter2).toHaveBeenCalledTimes(2)
  83. })
  84. it('should trigger effect when chained (mixed invocations)', async () => {
  85. const value = reactive({ foo: 0 })
  86. const getter1 = vi.fn(() => value.foo)
  87. const getter2 = vi.fn(() => {
  88. return c1.value + 1
  89. })
  90. const c1 = computed(getter1)
  91. const c2 = computed(getter2)
  92. let dummy
  93. // @discrepancy Vue 2 chained computed doesn't work with sync watchers
  94. effect(() => {
  95. dummy = c1.value + c2.value
  96. }, nextTick)
  97. expect(dummy).toBe(1)
  98. expect(getter1).toHaveBeenCalledTimes(1)
  99. expect(getter2).toHaveBeenCalledTimes(1)
  100. value.foo++
  101. await nextTick()
  102. expect(dummy).toBe(3)
  103. // should not result in duplicate calls
  104. expect(getter1).toHaveBeenCalledTimes(2)
  105. expect(getter2).toHaveBeenCalledTimes(2)
  106. })
  107. it('should no longer update when stopped', () => {
  108. const value = reactive<{ foo?: number }>({ foo: undefined })
  109. const cValue = computed(() => value.foo)
  110. let dummy
  111. effect(() => {
  112. dummy = cValue.value
  113. })
  114. expect(dummy).toBe(undefined)
  115. value.foo = 1
  116. expect(dummy).toBe(1)
  117. cValue.effect.teardown()
  118. value.foo = 2
  119. expect(dummy).toBe(1)
  120. })
  121. it('should support setter', () => {
  122. const n = ref(1)
  123. const plusOne = computed({
  124. get: () => n.value + 1,
  125. set: val => {
  126. n.value = val - 1
  127. }
  128. })
  129. expect(plusOne.value).toBe(2)
  130. n.value++
  131. expect(plusOne.value).toBe(3)
  132. plusOne.value = 0
  133. expect(n.value).toBe(-1)
  134. })
  135. it('should trigger effect w/ setter', () => {
  136. const n = ref(1)
  137. const plusOne = computed({
  138. get: () => n.value + 1,
  139. set: val => {
  140. n.value = val - 1
  141. }
  142. })
  143. let dummy
  144. effect(() => {
  145. dummy = n.value
  146. })
  147. expect(dummy).toBe(1)
  148. plusOne.value = 0
  149. expect(dummy).toBe(-1)
  150. })
  151. // #5720
  152. it('should invalidate before non-computed effects', async () => {
  153. let plusOneValues: number[] = []
  154. const n = ref(0)
  155. const plusOne = computed(() => n.value + 1)
  156. effect(() => {
  157. n.value
  158. plusOneValues.push(plusOne.value)
  159. }, nextTick)
  160. expect(plusOneValues).toMatchObject([1])
  161. // access plusOne, causing it to be non-dirty
  162. plusOne.value
  163. // mutate n
  164. n.value++
  165. await nextTick()
  166. // on the 2nd run, plusOne.value should have already updated.
  167. expect(plusOneValues).toMatchObject([1, 2])
  168. })
  169. it('should warn if trying to set a readonly computed', () => {
  170. const n = ref(1)
  171. const plusOne = computed(() => n.value + 1)
  172. ;(plusOne as WritableComputedRef<number>).value++ // Type cast to prevent TS from preventing the error
  173. expect(
  174. 'Write operation failed: computed value is readonly'
  175. ).toHaveBeenWarnedLast()
  176. })
  177. it('should be readonly', () => {
  178. let a = { a: 1 }
  179. const x = computed(() => a)
  180. expect(isReadonly(x)).toBe(true)
  181. expect(isReadonly(x.value)).toBe(false)
  182. expect(isReadonly(x.value.a)).toBe(false)
  183. const z = computed<typeof a>({
  184. get() {
  185. return a
  186. },
  187. set(v) {
  188. a = v
  189. }
  190. })
  191. expect(isReadonly(z)).toBe(false)
  192. expect(isReadonly(z.value.a)).toBe(false)
  193. })
  194. it('should expose value when stopped', () => {
  195. const x = computed(() => 1)
  196. x.effect.teardown()
  197. expect(x.value).toBe(1)
  198. })
  199. // TODO
  200. // it('debug: onTrack', () => {
  201. // let events: DebuggerEvent[] = []
  202. // const onTrack = vi.fn((e: DebuggerEvent) => {
  203. // events.push(e)
  204. // })
  205. // const obj = reactive({ foo: 1, bar: 2 })
  206. // const c = computed(() => (obj.foo, 'bar' in obj, Object.keys(obj)), {
  207. // onTrack
  208. // })
  209. // expect(c.value).toEqual(['foo', 'bar'])
  210. // expect(onTrack).toHaveBeenCalledTimes(3)
  211. // expect(events).toEqual([
  212. // {
  213. // effect: c.effect,
  214. // target: toRaw(obj),
  215. // type: TrackOpTypes.GET,
  216. // key: 'foo'
  217. // },
  218. // {
  219. // effect: c.effect,
  220. // target: toRaw(obj),
  221. // type: TrackOpTypes.HAS,
  222. // key: 'bar'
  223. // },
  224. // {
  225. // effect: c.effect,
  226. // target: toRaw(obj),
  227. // type: TrackOpTypes.ITERATE,
  228. // key: ITERATE_KEY
  229. // }
  230. // ])
  231. // })
  232. // TODO
  233. // it('debug: onTrigger', () => {
  234. // let events: DebuggerEvent[] = []
  235. // const onTrigger = vi.fn((e: DebuggerEvent) => {
  236. // events.push(e)
  237. // })
  238. // const obj = reactive({ foo: 1 })
  239. // const c = computed(() => obj.foo, { onTrigger })
  240. // // computed won't trigger compute until accessed
  241. // c.value
  242. // obj.foo++
  243. // expect(c.value).toBe(2)
  244. // expect(onTrigger).toHaveBeenCalledTimes(1)
  245. // expect(events[0]).toEqual({
  246. // effect: c.effect,
  247. // target: toRaw(obj),
  248. // type: TriggerOpTypes.SET,
  249. // key: 'foo',
  250. // oldValue: 1,
  251. // newValue: 2
  252. // })
  253. // // @ts-ignore
  254. // delete obj.foo
  255. // expect(c.value).toBeUndefined()
  256. // expect(onTrigger).toHaveBeenCalledTimes(2)
  257. // expect(events[1]).toEqual({
  258. // effect: c.effect,
  259. // target: toRaw(obj),
  260. // type: TriggerOpTypes.DELETE,
  261. // key: 'foo',
  262. // oldValue: 2
  263. // })
  264. // })
  265. })