ref.spec.ts 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. import {
  2. ref,
  3. effect,
  4. reactive,
  5. isRef,
  6. toRef,
  7. toRefs,
  8. Ref,
  9. isReactive
  10. } from '../src/index'
  11. import { computed } from '@vue/runtime-dom'
  12. import { shallowRef, unref, customRef, triggerRef } from '../src/ref'
  13. describe('reactivity/ref', () => {
  14. it('should hold a value', () => {
  15. const a = ref(1)
  16. expect(a.value).toBe(1)
  17. a.value = 2
  18. expect(a.value).toBe(2)
  19. })
  20. it('should be reactive', () => {
  21. const a = ref(1)
  22. let dummy
  23. let calls = 0
  24. effect(() => {
  25. calls++
  26. dummy = a.value
  27. })
  28. expect(calls).toBe(1)
  29. expect(dummy).toBe(1)
  30. a.value = 2
  31. expect(calls).toBe(2)
  32. expect(dummy).toBe(2)
  33. // same value should not trigger
  34. a.value = 2
  35. expect(calls).toBe(2)
  36. expect(dummy).toBe(2)
  37. })
  38. it('should make nested properties reactive', () => {
  39. const a = ref({
  40. count: 1
  41. })
  42. let dummy
  43. effect(() => {
  44. dummy = a.value.count
  45. })
  46. expect(dummy).toBe(1)
  47. a.value.count = 2
  48. expect(dummy).toBe(2)
  49. })
  50. it('should work without initial value', () => {
  51. const a = ref()
  52. let dummy
  53. effect(() => {
  54. dummy = a.value
  55. })
  56. expect(dummy).toBe(undefined)
  57. a.value = 2
  58. expect(dummy).toBe(2)
  59. })
  60. it('should work like a normal property when nested in a reactive object', () => {
  61. const a = ref(1)
  62. const obj = reactive({
  63. a,
  64. b: {
  65. c: a
  66. }
  67. })
  68. let dummy1: number
  69. let dummy2: number
  70. effect(() => {
  71. dummy1 = obj.a
  72. dummy2 = obj.b.c
  73. })
  74. const assertDummiesEqualTo = (val: number) =>
  75. [dummy1, dummy2].forEach(dummy => expect(dummy).toBe(val))
  76. assertDummiesEqualTo(1)
  77. a.value++
  78. assertDummiesEqualTo(2)
  79. obj.a++
  80. assertDummiesEqualTo(3)
  81. obj.b.c++
  82. assertDummiesEqualTo(4)
  83. })
  84. it('should unwrap nested ref in types', () => {
  85. const a = ref(0)
  86. const b = ref(a)
  87. expect(typeof (b.value + 1)).toBe('number')
  88. })
  89. it('should unwrap nested values in types', () => {
  90. const a = {
  91. b: ref(0)
  92. }
  93. const c = ref(a)
  94. expect(typeof (c.value.b + 1)).toBe('number')
  95. })
  96. it('should NOT unwrap ref types nested inside arrays', () => {
  97. const arr = ref([1, ref(3)]).value
  98. expect(isRef(arr[0])).toBe(false)
  99. expect(isRef(arr[1])).toBe(true)
  100. expect((arr[1] as Ref).value).toBe(3)
  101. })
  102. it('should unwrap ref types as props of arrays', () => {
  103. const arr = [ref(0)]
  104. const symbolKey = Symbol('')
  105. arr['' as any] = ref(1)
  106. arr[symbolKey as any] = ref(2)
  107. const arrRef = ref(arr).value
  108. expect(isRef(arrRef[0])).toBe(true)
  109. expect(isRef(arrRef['' as any])).toBe(false)
  110. expect(isRef(arrRef[symbolKey as any])).toBe(false)
  111. expect(arrRef['' as any]).toBe(1)
  112. expect(arrRef[symbolKey as any]).toBe(2)
  113. })
  114. it('should keep tuple types', () => {
  115. const tuple: [number, string, { a: number }, () => number, Ref<number>] = [
  116. 0,
  117. '1',
  118. { a: 1 },
  119. () => 0,
  120. ref(0)
  121. ]
  122. const tupleRef = ref(tuple)
  123. tupleRef.value[0]++
  124. expect(tupleRef.value[0]).toBe(1)
  125. tupleRef.value[1] += '1'
  126. expect(tupleRef.value[1]).toBe('11')
  127. tupleRef.value[2].a++
  128. expect(tupleRef.value[2].a).toBe(2)
  129. expect(tupleRef.value[3]()).toBe(0)
  130. tupleRef.value[4].value++
  131. expect(tupleRef.value[4].value).toBe(1)
  132. })
  133. it('should keep symbols', () => {
  134. const customSymbol = Symbol()
  135. const obj = {
  136. [Symbol.asyncIterator]: { a: 1 },
  137. [Symbol.unscopables]: { b: '1' },
  138. [customSymbol]: { c: [1, 2, 3] }
  139. }
  140. const objRef = ref(obj)
  141. expect(objRef.value[Symbol.asyncIterator]).toBe(obj[Symbol.asyncIterator])
  142. expect(objRef.value[Symbol.unscopables]).toBe(obj[Symbol.unscopables])
  143. expect(objRef.value[customSymbol]).toStrictEqual(obj[customSymbol])
  144. })
  145. test('unref', () => {
  146. expect(unref(1)).toBe(1)
  147. expect(unref(ref(1))).toBe(1)
  148. })
  149. test('shallowRef', () => {
  150. const sref = shallowRef({ a: 1 })
  151. expect(isReactive(sref.value)).toBe(false)
  152. let dummy
  153. effect(() => {
  154. dummy = sref.value.a
  155. })
  156. expect(dummy).toBe(1)
  157. sref.value = { a: 2 }
  158. expect(isReactive(sref.value)).toBe(false)
  159. expect(dummy).toBe(2)
  160. })
  161. test('shallowRef force trigger', () => {
  162. const sref = shallowRef({ a: 1 })
  163. let dummy
  164. effect(() => {
  165. dummy = sref.value.a
  166. })
  167. expect(dummy).toBe(1)
  168. sref.value.a = 2
  169. expect(dummy).toBe(1) // should not trigger yet
  170. // force trigger
  171. triggerRef(sref)
  172. expect(dummy).toBe(2)
  173. })
  174. test('isRef', () => {
  175. expect(isRef(ref(1))).toBe(true)
  176. expect(isRef(computed(() => 1))).toBe(true)
  177. expect(isRef(0)).toBe(false)
  178. expect(isRef(1)).toBe(false)
  179. // an object that looks like a ref isn't necessarily a ref
  180. expect(isRef({ value: 0 })).toBe(false)
  181. })
  182. test('toRef', () => {
  183. const a = reactive({
  184. x: 1
  185. })
  186. const x = toRef(a, 'x')
  187. expect(isRef(x)).toBe(true)
  188. expect(x.value).toBe(1)
  189. // source -> proxy
  190. a.x = 2
  191. expect(x.value).toBe(2)
  192. // proxy -> source
  193. x.value = 3
  194. expect(a.x).toBe(3)
  195. // reactivity
  196. let dummyX
  197. effect(() => {
  198. dummyX = x.value
  199. })
  200. expect(dummyX).toBe(x.value)
  201. // mutating source should trigger effect using the proxy refs
  202. a.x = 4
  203. expect(dummyX).toBe(4)
  204. // should keep ref
  205. const r = { x: ref(1) }
  206. expect(toRef(r, 'x')).toBe(r.x)
  207. })
  208. test('toRefs', () => {
  209. const a = reactive({
  210. x: 1,
  211. y: 2
  212. })
  213. const { x, y } = toRefs(a)
  214. expect(isRef(x)).toBe(true)
  215. expect(isRef(y)).toBe(true)
  216. expect(x.value).toBe(1)
  217. expect(y.value).toBe(2)
  218. // source -> proxy
  219. a.x = 2
  220. a.y = 3
  221. expect(x.value).toBe(2)
  222. expect(y.value).toBe(3)
  223. // proxy -> source
  224. x.value = 3
  225. y.value = 4
  226. expect(a.x).toBe(3)
  227. expect(a.y).toBe(4)
  228. // reactivity
  229. let dummyX, dummyY
  230. effect(() => {
  231. dummyX = x.value
  232. dummyY = y.value
  233. })
  234. expect(dummyX).toBe(x.value)
  235. expect(dummyY).toBe(y.value)
  236. // mutating source should trigger effect using the proxy refs
  237. a.x = 4
  238. a.y = 5
  239. expect(dummyX).toBe(4)
  240. expect(dummyY).toBe(5)
  241. })
  242. test('toRefs should warn on plain object', () => {
  243. toRefs({})
  244. expect(`toRefs() expects a reactive object`).toHaveBeenWarned()
  245. })
  246. test('toRefs should warn on plain array', () => {
  247. toRefs([])
  248. expect(`toRefs() expects a reactive object`).toHaveBeenWarned()
  249. })
  250. test('toRefs reactive array', () => {
  251. const arr = reactive(['a', 'b', 'c'])
  252. const refs = toRefs(arr)
  253. expect(Array.isArray(refs)).toBe(true)
  254. refs[0].value = '1'
  255. expect(arr[0]).toBe('1')
  256. arr[1] = '2'
  257. expect(refs[1].value).toBe('2')
  258. })
  259. test('customRef', () => {
  260. let value = 1
  261. let _trigger: () => void
  262. const custom = customRef((track, trigger) => ({
  263. get() {
  264. track()
  265. return value
  266. },
  267. set(newValue: number) {
  268. value = newValue
  269. _trigger = trigger
  270. }
  271. }))
  272. expect(isRef(custom)).toBe(true)
  273. let dummy
  274. effect(() => {
  275. dummy = custom.value
  276. })
  277. expect(dummy).toBe(1)
  278. custom.value = 2
  279. // should not trigger yet
  280. expect(dummy).toBe(1)
  281. _trigger!()
  282. expect(dummy).toBe(2)
  283. })
  284. test('should not trigger when setting value to same proxy', () => {
  285. const obj = reactive({ count: 0 })
  286. const a = ref(obj)
  287. const spy1 = jest.fn(() => a.value)
  288. effect(spy1)
  289. a.value = obj
  290. expect(spy1).toBeCalledTimes(1)
  291. const b = shallowRef(obj)
  292. const spy2 = jest.fn(() => b.value)
  293. effect(spy2)
  294. b.value = obj
  295. expect(spy2).toBeCalledTimes(1)
  296. })
  297. })