reactive.spec.ts 8.5 KB

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