reactive.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  1. import { isRef, ref, shallowRef } from '../src/ref'
  2. import {
  3. isProxy,
  4. isReactive,
  5. isReadonly,
  6. isShallow,
  7. markRaw,
  8. reactive,
  9. readonly,
  10. shallowReactive,
  11. shallowReadonly,
  12. toRaw,
  13. } from '../src/reactive'
  14. import { computed } from '../src/computed'
  15. import { effect } from '../src/effect'
  16. import { targetMap } from '../src/dep'
  17. describe('reactivity/reactive', () => {
  18. test('Object', () => {
  19. const original = { foo: 1 }
  20. const observed = reactive(original)
  21. expect(observed).not.toBe(original)
  22. expect(isReactive(observed)).toBe(true)
  23. expect(isReactive(original)).toBe(false)
  24. // get
  25. expect(observed.foo).toBe(1)
  26. // has
  27. expect('foo' in observed).toBe(true)
  28. // ownKeys
  29. expect(Object.keys(observed)).toEqual(['foo'])
  30. })
  31. test('proto', () => {
  32. const obj = {}
  33. const reactiveObj = reactive(obj)
  34. expect(isReactive(reactiveObj)).toBe(true)
  35. // read prop of reactiveObject will cause reactiveObj[prop] to be reactive
  36. // @ts-expect-error
  37. const prototype = reactiveObj['__proto__']
  38. const otherObj = { data: ['a'] }
  39. expect(isReactive(otherObj)).toBe(false)
  40. const reactiveOther = reactive(otherObj)
  41. expect(isReactive(reactiveOther)).toBe(true)
  42. expect(reactiveOther.data[0]).toBe('a')
  43. })
  44. test('nested reactives', () => {
  45. const original = {
  46. nested: {
  47. foo: 1,
  48. },
  49. array: [{ bar: 2 }],
  50. }
  51. const observed = reactive(original)
  52. expect(isReactive(observed.nested)).toBe(true)
  53. expect(isReactive(observed.array)).toBe(true)
  54. expect(isReactive(observed.array[0])).toBe(true)
  55. })
  56. test('observing subtypes of IterableCollections(Map, Set)', () => {
  57. // subtypes of Map
  58. class CustomMap extends Map {}
  59. const cmap = reactive(new CustomMap())
  60. expect(cmap).toBeInstanceOf(Map)
  61. expect(isReactive(cmap)).toBe(true)
  62. cmap.set('key', {})
  63. expect(isReactive(cmap.get('key'))).toBe(true)
  64. // subtypes of Set
  65. class CustomSet extends Set {}
  66. const cset = reactive(new CustomSet())
  67. expect(cset).toBeInstanceOf(Set)
  68. expect(isReactive(cset)).toBe(true)
  69. let dummy
  70. effect(() => (dummy = cset.has('value')))
  71. expect(dummy).toBe(false)
  72. cset.add('value')
  73. expect(dummy).toBe(true)
  74. cset.delete('value')
  75. expect(dummy).toBe(false)
  76. })
  77. test('observing subtypes of WeakCollections(WeakMap, WeakSet)', () => {
  78. // subtypes of WeakMap
  79. class CustomMap extends WeakMap {}
  80. const cmap = reactive(new CustomMap())
  81. expect(cmap).toBeInstanceOf(WeakMap)
  82. expect(isReactive(cmap)).toBe(true)
  83. const key = {}
  84. cmap.set(key, {})
  85. expect(isReactive(cmap.get(key))).toBe(true)
  86. // subtypes of WeakSet
  87. class CustomSet extends WeakSet {}
  88. const cset = reactive(new CustomSet())
  89. expect(cset).toBeInstanceOf(WeakSet)
  90. expect(isReactive(cset)).toBe(true)
  91. let dummy
  92. effect(() => (dummy = cset.has(key)))
  93. expect(dummy).toBe(false)
  94. cset.add(key)
  95. expect(dummy).toBe(true)
  96. cset.delete(key)
  97. expect(dummy).toBe(false)
  98. })
  99. // #8647
  100. test('observing Set with reactive initial value', () => {
  101. const observed = reactive({})
  102. const observedSet = reactive(new Set([observed]))
  103. expect(observedSet.has(observed)).toBe(true)
  104. expect(observedSet.size).toBe(1)
  105. // expect nothing happens
  106. observedSet.add(observed)
  107. expect(observedSet.size).toBe(1)
  108. })
  109. test('observed value should proxy mutations to original (Object)', () => {
  110. const original: any = { foo: 1 }
  111. const observed = reactive(original)
  112. // set
  113. observed.bar = 1
  114. expect(observed.bar).toBe(1)
  115. expect(original.bar).toBe(1)
  116. // delete
  117. delete observed.foo
  118. expect('foo' in observed).toBe(false)
  119. expect('foo' in original).toBe(false)
  120. })
  121. test('original value change should reflect in observed value (Object)', () => {
  122. const original: any = { foo: 1 }
  123. const observed = reactive(original)
  124. // set
  125. original.bar = 1
  126. expect(original.bar).toBe(1)
  127. expect(observed.bar).toBe(1)
  128. // delete
  129. delete original.foo
  130. expect('foo' in original).toBe(false)
  131. expect('foo' in observed).toBe(false)
  132. })
  133. test('setting a property with an unobserved value should wrap with reactive', () => {
  134. const observed = reactive<{ foo?: object }>({})
  135. const raw = {}
  136. observed.foo = raw
  137. expect(observed.foo).not.toBe(raw)
  138. expect(isReactive(observed.foo)).toBe(true)
  139. })
  140. test('observing already observed value should return same Proxy', () => {
  141. const original = { foo: 1 }
  142. const observed = reactive(original)
  143. const observed2 = reactive(observed)
  144. expect(observed2).toBe(observed)
  145. })
  146. test('observing the same value multiple times should return same Proxy', () => {
  147. const original = { foo: 1 }
  148. const observed = reactive(original)
  149. const observed2 = reactive(original)
  150. expect(observed2).toBe(observed)
  151. })
  152. test('should not pollute original object with Proxies', () => {
  153. const original: any = { foo: 1 }
  154. const original2 = { bar: 2 }
  155. const observed = reactive(original)
  156. const observed2 = reactive(original2)
  157. observed.bar = observed2
  158. expect(observed.bar).toBe(observed2)
  159. expect(original.bar).toBe(original2)
  160. })
  161. // #1246
  162. test('mutation on objects using reactive as prototype should not trigger', () => {
  163. const observed = reactive({ foo: 1 })
  164. const original = Object.create(observed)
  165. let dummy
  166. effect(() => (dummy = original.foo))
  167. expect(dummy).toBe(1)
  168. observed.foo = 2
  169. expect(dummy).toBe(2)
  170. original.foo = 3
  171. expect(dummy).toBe(2)
  172. original.foo = 4
  173. expect(dummy).toBe(2)
  174. })
  175. test('toRaw', () => {
  176. const original = { foo: 1 }
  177. const observed = reactive(original)
  178. expect(toRaw(observed)).toBe(original)
  179. expect(toRaw(original)).toBe(original)
  180. })
  181. test('toRaw on object using reactive as prototype', () => {
  182. const original = { foo: 1 }
  183. const observed = reactive(original)
  184. const inherited = Object.create(observed)
  185. expect(toRaw(inherited)).toBe(inherited)
  186. })
  187. test('toRaw on user Proxy wrapping reactive', () => {
  188. const original = {}
  189. const re = reactive(original)
  190. const obj = new Proxy(re, {})
  191. const raw = toRaw(obj)
  192. expect(raw).toBe(original)
  193. })
  194. test('should not unwrap Ref<T>', () => {
  195. const observedNumberRef = reactive(ref(1))
  196. const observedObjectRef = reactive(ref({ foo: 1 }))
  197. expect(isRef(observedNumberRef)).toBe(true)
  198. expect(isRef(observedObjectRef)).toBe(true)
  199. })
  200. test('should unwrap computed refs', () => {
  201. // readonly
  202. const a = computed(() => 1)
  203. // writable
  204. const b = computed({
  205. get: () => 1,
  206. set: () => {},
  207. })
  208. const obj = reactive({ a, b })
  209. // check type
  210. obj.a + 1
  211. obj.b + 1
  212. expect(typeof obj.a).toBe(`number`)
  213. expect(typeof obj.b).toBe(`number`)
  214. })
  215. test('should allow setting property from a ref to another ref', () => {
  216. const foo = ref(0)
  217. const bar = ref(1)
  218. const observed = reactive({ a: foo })
  219. const dummy = computed(() => observed.a)
  220. expect(dummy.value).toBe(0)
  221. // @ts-expect-error
  222. observed.a = bar
  223. expect(dummy.value).toBe(1)
  224. bar.value++
  225. expect(dummy.value).toBe(2)
  226. })
  227. test('non-observable values', () => {
  228. const assertValue = (value: any) => {
  229. reactive(value)
  230. expect(
  231. `value cannot be made reactive: ${String(value)}`,
  232. ).toHaveBeenWarnedLast()
  233. }
  234. // number
  235. assertValue(1)
  236. // string
  237. assertValue('foo')
  238. // boolean
  239. assertValue(false)
  240. // null
  241. assertValue(null)
  242. // undefined
  243. assertValue(undefined)
  244. // symbol
  245. const s = Symbol()
  246. assertValue(s)
  247. // bigint
  248. const bn = BigInt('9007199254740991')
  249. assertValue(bn)
  250. // built-ins should work and return same value
  251. const p = Promise.resolve()
  252. expect(reactive(p)).toBe(p)
  253. const r = new RegExp('')
  254. expect(reactive(r)).toBe(r)
  255. const d = new Date()
  256. expect(reactive(d)).toBe(d)
  257. })
  258. test('markRaw', () => {
  259. const obj = reactive({
  260. foo: { a: 1 },
  261. bar: markRaw({ b: 2 }),
  262. })
  263. expect(isReactive(obj.foo)).toBe(true)
  264. expect(isReactive(obj.bar)).toBe(false)
  265. })
  266. test('markRaw should skip non-extensible objects', () => {
  267. const obj = Object.seal({ foo: 1 })
  268. expect(() => markRaw(obj)).not.toThrowError()
  269. })
  270. test('markRaw should not redefine on an marked object', () => {
  271. const obj = markRaw({ foo: 1 })
  272. const raw = markRaw(obj)
  273. expect(raw).toBe(obj)
  274. expect(() => markRaw(obj)).not.toThrowError()
  275. })
  276. test('should not markRaw object as reactive', () => {
  277. const a = reactive({ a: 1 })
  278. const b = reactive({ b: 2 }) as any
  279. b.a = markRaw(toRaw(a))
  280. expect(b.a === a).toBe(false)
  281. })
  282. test('should not observe non-extensible objects', () => {
  283. const obj = reactive({
  284. foo: Object.preventExtensions({ a: 1 }),
  285. // sealed or frozen objects are considered non-extensible as well
  286. bar: Object.freeze({ a: 1 }),
  287. baz: Object.seal({ a: 1 }),
  288. })
  289. expect(isReactive(obj.foo)).toBe(false)
  290. expect(isReactive(obj.bar)).toBe(false)
  291. expect(isReactive(obj.baz)).toBe(false)
  292. })
  293. test('should not observe objects with __v_skip', () => {
  294. const original = {
  295. foo: 1,
  296. __v_skip: true,
  297. }
  298. const observed = reactive(original)
  299. expect(isReactive(observed)).toBe(false)
  300. })
  301. test('hasOwnProperty edge case: Symbol values', () => {
  302. const key = Symbol()
  303. const obj = reactive({ [key]: 1 }) as { [key]?: 1 }
  304. let dummy
  305. effect(() => {
  306. dummy = obj.hasOwnProperty(key)
  307. })
  308. expect(dummy).toBe(true)
  309. delete obj[key]
  310. expect(dummy).toBe(false)
  311. })
  312. test('hasOwnProperty edge case: non-string values', () => {
  313. const key = {}
  314. const obj = reactive({ '[object Object]': 1 }) as { '[object Object]'?: 1 }
  315. let dummy
  316. effect(() => {
  317. // @ts-expect-error
  318. dummy = obj.hasOwnProperty(key)
  319. })
  320. expect(dummy).toBe(true)
  321. // @ts-expect-error
  322. delete obj[key]
  323. expect(dummy).toBe(false)
  324. })
  325. test('isProxy', () => {
  326. const foo = {}
  327. expect(isProxy(foo)).toBe(false)
  328. const fooRe = reactive(foo)
  329. expect(isProxy(fooRe)).toBe(true)
  330. const fooSRe = shallowReactive(foo)
  331. expect(isProxy(fooSRe)).toBe(true)
  332. const barRl = readonly(foo)
  333. expect(isProxy(barRl)).toBe(true)
  334. const barSRl = shallowReadonly(foo)
  335. expect(isProxy(barSRl)).toBe(true)
  336. const c = computed(() => {})
  337. expect(isProxy(c)).toBe(false)
  338. })
  339. test('The results of the shallow and readonly assignments are the same (Map)', () => {
  340. const map = reactive(new Map())
  341. map.set('foo', shallowReactive({ a: 2 }))
  342. expect(isShallow(map.get('foo'))).toBe(true)
  343. map.set('bar', readonly({ b: 2 }))
  344. expect(isReadonly(map.get('bar'))).toBe(true)
  345. })
  346. test('The results of the shallow and readonly assignments are the same (Set)', () => {
  347. const set = reactive(new Set())
  348. set.add(shallowReactive({ a: 2 }))
  349. set.add(readonly({ b: 2 }))
  350. let count = 0
  351. for (const i of set) {
  352. if (count === 0) expect(isShallow(i)).toBe(true)
  353. else expect(isReadonly(i)).toBe(true)
  354. count++
  355. }
  356. })
  357. // #11696
  358. test('should use correct receiver on set handler for refs', () => {
  359. const a = reactive(ref(1))
  360. effect(() => a.value)
  361. expect(() => {
  362. a.value++
  363. }).not.toThrow()
  364. })
  365. // #11979
  366. test('should release property Dep instance if it no longer has subscribers', () => {
  367. let obj = { x: 1 }
  368. let a = reactive(obj)
  369. const e = effect(() => a.x)
  370. expect(targetMap.get(obj)?.get('x')).toBeTruthy()
  371. e.effect.stop()
  372. expect(targetMap.get(obj)?.get('x')).toBeFalsy()
  373. })
  374. test('should trigger reactivity when Map key is undefined', () => {
  375. const map = reactive(new Map())
  376. const c = computed(() => map.get(void 0))
  377. expect(c.value).toBe(void 0)
  378. map.set(void 0, 1)
  379. expect(c.value).toBe(1)
  380. })
  381. test('should return true for reactive objects', () => {
  382. expect(isReactive(reactive({}))).toBe(true)
  383. expect(isReactive(readonly(reactive({})))).toBe(true)
  384. expect(isReactive(ref({}).value)).toBe(true)
  385. expect(isReactive(readonly(ref({})).value)).toBe(true)
  386. expect(isReactive(shallowReactive({}))).toBe(true)
  387. })
  388. test('should return false for non-reactive objects', () => {
  389. expect(isReactive(ref(true))).toBe(false)
  390. expect(isReactive(shallowRef({}).value)).toBe(false)
  391. })
  392. })