reactive.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import { ref, isRef } from '../src/ref'
  2. import {
  3. reactive,
  4. isReactive,
  5. toRaw,
  6. markRaw,
  7. shallowReactive
  8. } from '../src/reactive'
  9. import { mockWarn } from '@vue/shared'
  10. import { computed } from '../src/computed'
  11. describe('reactivity/reactive', () => {
  12. mockWarn()
  13. test('Object', () => {
  14. const original = { foo: 1 }
  15. const observed = reactive(original)
  16. expect(observed).not.toBe(original)
  17. expect(isReactive(observed)).toBe(true)
  18. expect(isReactive(original)).toBe(false)
  19. // get
  20. expect(observed.foo).toBe(1)
  21. // has
  22. expect('foo' in observed).toBe(true)
  23. // ownKeys
  24. expect(Object.keys(observed)).toEqual(['foo'])
  25. })
  26. test('proto', () => {
  27. const obj = {}
  28. const reactiveObj = reactive(obj)
  29. expect(isReactive(reactiveObj)).toBe(true)
  30. // read prop of reactiveObject will cause reactiveObj[prop] to be reactive
  31. // @ts-ignore
  32. const prototype = reactiveObj['__proto__']
  33. const otherObj = { data: ['a'] }
  34. expect(isReactive(otherObj)).toBe(false)
  35. const reactiveOther = reactive(otherObj)
  36. expect(isReactive(reactiveOther)).toBe(true)
  37. expect(reactiveOther.data[0]).toBe('a')
  38. })
  39. test('nested reactives', () => {
  40. const original = {
  41. nested: {
  42. foo: 1
  43. },
  44. array: [{ bar: 2 }]
  45. }
  46. const observed = reactive(original)
  47. expect(isReactive(observed.nested)).toBe(true)
  48. expect(isReactive(observed.array)).toBe(true)
  49. expect(isReactive(observed.array[0])).toBe(true)
  50. })
  51. test('observed value should proxy mutations to original (Object)', () => {
  52. const original: any = { foo: 1 }
  53. const observed = reactive(original)
  54. // set
  55. observed.bar = 1
  56. expect(observed.bar).toBe(1)
  57. expect(original.bar).toBe(1)
  58. // delete
  59. delete observed.foo
  60. expect('foo' in observed).toBe(false)
  61. expect('foo' in original).toBe(false)
  62. })
  63. test('setting a property with an unobserved value should wrap with reactive', () => {
  64. const observed = reactive<{ foo?: object }>({})
  65. const raw = {}
  66. observed.foo = raw
  67. expect(observed.foo).not.toBe(raw)
  68. expect(isReactive(observed.foo)).toBe(true)
  69. })
  70. test('observing already observed value should return same Proxy', () => {
  71. const original = { foo: 1 }
  72. const observed = reactive(original)
  73. const observed2 = reactive(observed)
  74. expect(observed2).toBe(observed)
  75. })
  76. test('observing the same value multiple times should return same Proxy', () => {
  77. const original = { foo: 1 }
  78. const observed = reactive(original)
  79. const observed2 = reactive(original)
  80. expect(observed2).toBe(observed)
  81. })
  82. test('should not pollute original object with Proxies', () => {
  83. const original: any = { foo: 1 }
  84. const original2 = { bar: 2 }
  85. const observed = reactive(original)
  86. const observed2 = reactive(original2)
  87. observed.bar = observed2
  88. expect(observed.bar).toBe(observed2)
  89. expect(original.bar).toBe(original2)
  90. })
  91. test('unwrap', () => {
  92. const original = { foo: 1 }
  93. const observed = reactive(original)
  94. expect(toRaw(observed)).toBe(original)
  95. expect(toRaw(original)).toBe(original)
  96. })
  97. test('should not unwrap Ref<T>', () => {
  98. const observedNumberRef = reactive(ref(1))
  99. const observedObjectRef = reactive(ref({ foo: 1 }))
  100. expect(isRef(observedNumberRef)).toBe(true)
  101. expect(isRef(observedObjectRef)).toBe(true)
  102. })
  103. test('should unwrap computed refs', () => {
  104. // readonly
  105. const a = computed(() => 1)
  106. // writable
  107. const b = computed({
  108. get: () => 1,
  109. set: () => {}
  110. })
  111. const obj = reactive({ a, b })
  112. // check type
  113. obj.a + 1
  114. obj.b + 1
  115. expect(typeof obj.a).toBe(`number`)
  116. expect(typeof obj.b).toBe(`number`)
  117. })
  118. test('non-observable values', () => {
  119. const assertValue = (value: any) => {
  120. reactive(value)
  121. expect(
  122. `value cannot be made reactive: ${String(value)}`
  123. ).toHaveBeenWarnedLast()
  124. }
  125. // number
  126. assertValue(1)
  127. // string
  128. assertValue('foo')
  129. // boolean
  130. assertValue(false)
  131. // null
  132. assertValue(null)
  133. // undefined
  134. assertValue(undefined)
  135. // symbol
  136. const s = Symbol()
  137. assertValue(s)
  138. // built-ins should work and return same value
  139. const p = Promise.resolve()
  140. expect(reactive(p)).toBe(p)
  141. const r = new RegExp('')
  142. expect(reactive(r)).toBe(r)
  143. const d = new Date()
  144. expect(reactive(d)).toBe(d)
  145. })
  146. test('markRaw', () => {
  147. const obj = reactive({
  148. foo: { a: 1 },
  149. bar: markRaw({ b: 2 })
  150. })
  151. expect(isReactive(obj.foo)).toBe(true)
  152. expect(isReactive(obj.bar)).toBe(false)
  153. })
  154. test('should not observe frozen objects', () => {
  155. const obj = reactive({
  156. foo: Object.freeze({ a: 1 })
  157. })
  158. expect(isReactive(obj.foo)).toBe(false)
  159. })
  160. describe('shallowReactive', () => {
  161. test('should not make non-reactive properties reactive', () => {
  162. const props = shallowReactive({ n: { foo: 1 } })
  163. expect(isReactive(props.n)).toBe(false)
  164. })
  165. test('should keep reactive properties reactive', () => {
  166. const props: any = shallowReactive({ n: reactive({ foo: 1 }) })
  167. props.n = reactive({ foo: 2 })
  168. expect(isReactive(props.n)).toBe(true)
  169. })
  170. })
  171. })