reactive.spec.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. import { ref, isRef } from '../src/ref'
  2. import { reactive, isReactive, toRaw, markRaw } from '../src/reactive'
  3. import { mockWarn } from '@vue/shared'
  4. import { computed } from '../src/computed'
  5. describe('reactivity/reactive', () => {
  6. mockWarn()
  7. test('Object', () => {
  8. const original = { foo: 1 }
  9. const observed = reactive(original)
  10. expect(observed).not.toBe(original)
  11. expect(isReactive(observed)).toBe(true)
  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. test('observed value should proxy mutations to original (Object)', () => {
  46. const original: any = { foo: 1 }
  47. const observed = reactive(original)
  48. // set
  49. observed.bar = 1
  50. expect(observed.bar).toBe(1)
  51. expect(original.bar).toBe(1)
  52. // delete
  53. delete observed.foo
  54. expect('foo' in observed).toBe(false)
  55. expect('foo' in original).toBe(false)
  56. })
  57. test('setting a property with an unobserved value should wrap with reactive', () => {
  58. const observed = reactive<{ foo?: object }>({})
  59. const raw = {}
  60. observed.foo = raw
  61. expect(observed.foo).not.toBe(raw)
  62. expect(isReactive(observed.foo)).toBe(true)
  63. })
  64. test('observing already observed value should return same Proxy', () => {
  65. const original = { foo: 1 }
  66. const observed = reactive(original)
  67. const observed2 = reactive(observed)
  68. expect(observed2).toBe(observed)
  69. })
  70. test('observing the same value multiple times should return same Proxy', () => {
  71. const original = { foo: 1 }
  72. const observed = reactive(original)
  73. const observed2 = reactive(original)
  74. expect(observed2).toBe(observed)
  75. })
  76. test('should not pollute original object with Proxies', () => {
  77. const original: any = { foo: 1 }
  78. const original2 = { bar: 2 }
  79. const observed = reactive(original)
  80. const observed2 = reactive(original2)
  81. observed.bar = observed2
  82. expect(observed.bar).toBe(observed2)
  83. expect(original.bar).toBe(original2)
  84. })
  85. test('toRaw', () => {
  86. const original = { foo: 1 }
  87. const observed = reactive(original)
  88. expect(toRaw(observed)).toBe(original)
  89. expect(toRaw(original)).toBe(original)
  90. })
  91. test('toRaw on object using reactive as prototype', () => {
  92. const original = reactive({})
  93. const obj = Object.create(original)
  94. const raw = toRaw(obj)
  95. expect(raw).toBe(obj)
  96. expect(raw).not.toBe(toRaw(original))
  97. })
  98. test('should not unwrap Ref<T>', () => {
  99. const observedNumberRef = reactive(ref(1))
  100. const observedObjectRef = reactive(ref({ foo: 1 }))
  101. expect(isRef(observedNumberRef)).toBe(true)
  102. expect(isRef(observedObjectRef)).toBe(true)
  103. })
  104. test('should unwrap computed refs', () => {
  105. // readonly
  106. const a = computed(() => 1)
  107. // writable
  108. const b = computed({
  109. get: () => 1,
  110. set: () => {}
  111. })
  112. const obj = reactive({ a, b })
  113. // check type
  114. obj.a + 1
  115. obj.b + 1
  116. expect(typeof obj.a).toBe(`number`)
  117. expect(typeof obj.b).toBe(`number`)
  118. })
  119. test('should allow setting property from a ref to another ref', () => {
  120. const foo = ref(0)
  121. const bar = ref(1)
  122. const observed = reactive({ a: foo })
  123. const dummy = computed(() => observed.a)
  124. expect(dummy.value).toBe(0)
  125. // @ts-ignore
  126. observed.a = bar
  127. expect(dummy.value).toBe(1)
  128. bar.value++
  129. expect(dummy.value).toBe(2)
  130. })
  131. test('non-observable values', () => {
  132. const assertValue = (value: any) => {
  133. reactive(value)
  134. expect(
  135. `value cannot be made reactive: ${String(value)}`
  136. ).toHaveBeenWarnedLast()
  137. }
  138. // number
  139. assertValue(1)
  140. // string
  141. assertValue('foo')
  142. // boolean
  143. assertValue(false)
  144. // null
  145. assertValue(null)
  146. // undefined
  147. assertValue(undefined)
  148. // symbol
  149. const s = Symbol()
  150. assertValue(s)
  151. // built-ins should work and return same value
  152. const p = Promise.resolve()
  153. expect(reactive(p)).toBe(p)
  154. const r = new RegExp('')
  155. expect(reactive(r)).toBe(r)
  156. const d = new Date()
  157. expect(reactive(d)).toBe(d)
  158. })
  159. test('markRaw', () => {
  160. const obj = reactive({
  161. foo: { a: 1 },
  162. bar: markRaw({ b: 2 })
  163. })
  164. expect(isReactive(obj.foo)).toBe(true)
  165. expect(isReactive(obj.bar)).toBe(false)
  166. })
  167. test('should not observe frozen objects', () => {
  168. const obj = reactive({
  169. foo: Object.freeze({ a: 1 })
  170. })
  171. expect(isReactive(obj.foo)).toBe(false)
  172. })
  173. test('should not observe objects with __v_skip', () => {
  174. const original = {
  175. foo: 1,
  176. __v_skip: true
  177. }
  178. const observed = reactive(original)
  179. expect(isReactive(observed)).toBe(false)
  180. })
  181. })