reactive.spec.ts 9.5 KB

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