reactive.spec.ts 10 KB

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