reactive.spec.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. test('observed value should proxy mutations to original (Object)', () => {
  100. const original: any = { foo: 1 }
  101. const observed = reactive(original)
  102. // set
  103. observed.bar = 1
  104. expect(observed.bar).toBe(1)
  105. expect(original.bar).toBe(1)
  106. // delete
  107. delete observed.foo
  108. expect('foo' in observed).toBe(false)
  109. expect('foo' in original).toBe(false)
  110. })
  111. test('original value change should reflect in observed value (Object)', () => {
  112. const original: any = { foo: 1 }
  113. const observed = reactive(original)
  114. // set
  115. original.bar = 1
  116. expect(original.bar).toBe(1)
  117. expect(observed.bar).toBe(1)
  118. // delete
  119. delete original.foo
  120. expect('foo' in original).toBe(false)
  121. expect('foo' in observed).toBe(false)
  122. })
  123. test('setting a property with an unobserved value should wrap with reactive', () => {
  124. const observed = reactive<{ foo?: object }>({})
  125. const raw = {}
  126. observed.foo = raw
  127. expect(observed.foo).not.toBe(raw)
  128. expect(isReactive(observed.foo)).toBe(true)
  129. })
  130. test('observing already observed value should return same Proxy', () => {
  131. const original = { foo: 1 }
  132. const observed = reactive(original)
  133. const observed2 = reactive(observed)
  134. expect(observed2).toBe(observed)
  135. })
  136. test('observing the same value multiple times should return same Proxy', () => {
  137. const original = { foo: 1 }
  138. const observed = reactive(original)
  139. const observed2 = reactive(original)
  140. expect(observed2).toBe(observed)
  141. })
  142. test('should not pollute original object with Proxies', () => {
  143. const original: any = { foo: 1 }
  144. const original2 = { bar: 2 }
  145. const observed = reactive(original)
  146. const observed2 = reactive(original2)
  147. observed.bar = observed2
  148. expect(observed.bar).toBe(observed2)
  149. expect(original.bar).toBe(original2)
  150. })
  151. // #1246
  152. test('mutation on objects using reactive as prototype should not trigger', () => {
  153. const observed = reactive({ foo: 1 })
  154. const original = Object.create(observed)
  155. let dummy
  156. effect(() => (dummy = original.foo))
  157. expect(dummy).toBe(1)
  158. observed.foo = 2
  159. expect(dummy).toBe(2)
  160. original.foo = 3
  161. expect(dummy).toBe(2)
  162. original.foo = 4
  163. expect(dummy).toBe(2)
  164. })
  165. test('toRaw', () => {
  166. const original = { foo: 1 }
  167. const observed = reactive(original)
  168. expect(toRaw(observed)).toBe(original)
  169. expect(toRaw(original)).toBe(original)
  170. })
  171. test('toRaw on object using reactive as prototype', () => {
  172. const original = { foo: 1 }
  173. const observed = reactive(original)
  174. const inherited = Object.create(observed)
  175. expect(toRaw(inherited)).toBe(inherited)
  176. })
  177. test('toRaw on user Proxy wrapping reactive', () => {
  178. const original = {}
  179. const re = reactive(original)
  180. const obj = new Proxy(re, {})
  181. const raw = toRaw(obj)
  182. expect(raw).toBe(original)
  183. })
  184. test('should not unwrap Ref<T>', () => {
  185. const observedNumberRef = reactive(ref(1))
  186. const observedObjectRef = reactive(ref({ foo: 1 }))
  187. expect(isRef(observedNumberRef)).toBe(true)
  188. expect(isRef(observedObjectRef)).toBe(true)
  189. })
  190. test('should unwrap computed refs', () => {
  191. // readonly
  192. const a = computed(() => 1)
  193. // writable
  194. const b = computed({
  195. get: () => 1,
  196. set: () => {},
  197. })
  198. const obj = reactive({ a, b })
  199. // check type
  200. obj.a + 1
  201. obj.b + 1
  202. expect(typeof obj.a).toBe(`number`)
  203. expect(typeof obj.b).toBe(`number`)
  204. })
  205. test('should allow setting property from a ref to another ref', () => {
  206. const foo = ref(0)
  207. const bar = ref(1)
  208. const observed = reactive({ a: foo })
  209. const dummy = computed(() => observed.a)
  210. expect(dummy.value).toBe(0)
  211. // @ts-expect-error
  212. observed.a = bar
  213. expect(dummy.value).toBe(1)
  214. bar.value++
  215. expect(dummy.value).toBe(2)
  216. })
  217. test('non-observable values', () => {
  218. const assertValue = (value: any) => {
  219. reactive(value)
  220. expect(
  221. `value cannot be made reactive: ${String(value)}`,
  222. ).toHaveBeenWarnedLast()
  223. }
  224. // number
  225. assertValue(1)
  226. // string
  227. assertValue('foo')
  228. // boolean
  229. assertValue(false)
  230. // null
  231. assertValue(null)
  232. // undefined
  233. assertValue(undefined)
  234. // symbol
  235. const s = Symbol()
  236. assertValue(s)
  237. // bigint
  238. const bn = BigInt('9007199254740991')
  239. assertValue(bn)
  240. // built-ins should work and return same value
  241. const p = Promise.resolve()
  242. expect(reactive(p)).toBe(p)
  243. const r = new RegExp('')
  244. expect(reactive(r)).toBe(r)
  245. const d = new Date()
  246. expect(reactive(d)).toBe(d)
  247. })
  248. test('markRaw', () => {
  249. const obj = reactive({
  250. foo: { a: 1 },
  251. bar: markRaw({ b: 2 }),
  252. })
  253. expect(isReactive(obj.foo)).toBe(true)
  254. expect(isReactive(obj.bar)).toBe(false)
  255. })
  256. test('markRaw should skip non-extensible objects', () => {
  257. const obj = Object.seal({ foo: 1 })
  258. expect(() => markRaw(obj)).not.toThrowError()
  259. })
  260. test('markRaw should not redefine on an marked object', () => {
  261. const obj = markRaw({ foo: 1 })
  262. const raw = markRaw(obj)
  263. expect(raw).toBe(obj)
  264. expect(() => markRaw(obj)).not.toThrowError()
  265. })
  266. test('should not markRaw object as reactive', () => {
  267. const a = reactive({ a: 1 })
  268. const b = reactive({ b: 2 }) as any
  269. b.a = markRaw(toRaw(a))
  270. expect(b.a === a).toBe(false)
  271. })
  272. test('should not observe non-extensible objects', () => {
  273. const obj = reactive({
  274. foo: Object.preventExtensions({ a: 1 }),
  275. // sealed or frozen objects are considered non-extensible as well
  276. bar: Object.freeze({ a: 1 }),
  277. baz: Object.seal({ a: 1 }),
  278. })
  279. expect(isReactive(obj.foo)).toBe(false)
  280. expect(isReactive(obj.bar)).toBe(false)
  281. expect(isReactive(obj.baz)).toBe(false)
  282. })
  283. test('should not observe objects with __v_skip', () => {
  284. const original = {
  285. foo: 1,
  286. __v_skip: true,
  287. }
  288. const observed = reactive(original)
  289. expect(isReactive(observed)).toBe(false)
  290. })
  291. test('hasOwnProperty edge case: Symbol values', () => {
  292. const key = Symbol()
  293. const obj = reactive({ [key]: 1 }) as { [key]?: 1 }
  294. let dummy
  295. effect(() => {
  296. dummy = obj.hasOwnProperty(key)
  297. })
  298. expect(dummy).toBe(true)
  299. delete obj[key]
  300. expect(dummy).toBe(false)
  301. })
  302. test('hasOwnProperty edge case: non-string values', () => {
  303. const key = {}
  304. const obj = reactive({ '[object Object]': 1 }) as { '[object Object]'?: 1 }
  305. let dummy
  306. effect(() => {
  307. // @ts-expect-error
  308. dummy = obj.hasOwnProperty(key)
  309. })
  310. expect(dummy).toBe(true)
  311. // @ts-expect-error
  312. delete obj[key]
  313. expect(dummy).toBe(false)
  314. })
  315. test('isProxy', () => {
  316. const foo = {}
  317. expect(isProxy(foo)).toBe(false)
  318. const fooRe = reactive(foo)
  319. expect(isProxy(fooRe)).toBe(true)
  320. const fooSRe = shallowReactive(foo)
  321. expect(isProxy(fooSRe)).toBe(true)
  322. const barRl = readonly(foo)
  323. expect(isProxy(barRl)).toBe(true)
  324. const barSRl = shallowReadonly(foo)
  325. expect(isProxy(barSRl)).toBe(true)
  326. const c = computed(() => {})
  327. expect(isProxy(c)).toBe(false)
  328. })
  329. test('The results of the shallow and readonly assignments are the same (Map)', () => {
  330. const map = reactive(new Map())
  331. map.set('foo', shallowReactive({ a: 2 }))
  332. expect(isShallow(map.get('foo'))).toBe(true)
  333. map.set('bar', readonly({ b: 2 }))
  334. expect(isReadonly(map.get('bar'))).toBe(true)
  335. })
  336. test('The results of the shallow and readonly assignments are the same (Set)', () => {
  337. const set = reactive(new Set())
  338. set.add(shallowReactive({ a: 2 }))
  339. set.add(readonly({ b: 2 }))
  340. let count = 0
  341. for (const i of set) {
  342. if (count === 0) expect(isShallow(i)).toBe(true)
  343. else expect(isReadonly(i)).toBe(true)
  344. count++
  345. }
  346. })
  347. // #11696
  348. test('should use correct receiver on set handler for refs', () => {
  349. const a = reactive(ref(1))
  350. effect(() => a.value)
  351. expect(() => {
  352. a.value++
  353. }).not.toThrow()
  354. })
  355. // #11979
  356. test('should release property Dep instance if it no longer has subscribers', () => {
  357. let obj = { x: 1 }
  358. let a = reactive(obj)
  359. const e = effect(() => a.x)
  360. expect(targetMap.get(obj)?.get('x')).toBeTruthy()
  361. e.effect.stop()
  362. expect(targetMap.get(obj)?.get('x')).toBeFalsy()
  363. })
  364. test('should trigger reactivity when Map key is undefined', () => {
  365. const map = reactive(new Map())
  366. const c = computed(() => map.get(void 0))
  367. expect(c.value).toBe(void 0)
  368. map.set(void 0, 1)
  369. expect(c.value).toBe(1)
  370. })
  371. test('should return true for reactive objects', () => {
  372. expect(isReactive(reactive({}))).toBe(true)
  373. expect(isReactive(readonly(reactive({})))).toBe(true)
  374. expect(isReactive(ref({}).value)).toBe(true)
  375. expect(isReactive(readonly(ref({})).value)).toBe(true)
  376. expect(isReactive(shallowReactive({}))).toBe(true)
  377. })
  378. test('should return false for non-reactive objects', () => {
  379. expect(isReactive(ref(true))).toBe(false)
  380. expect(isReactive(shallowRef({}).value)).toBe(false)
  381. })
  382. })