reactive.spec.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { ref, isRef } from '../src/ref'
  2. import { reactive, isReactive, toRaw, markNonReactive } from '../src/reactive'
  3. import { mockWarn } from '@vue/runtime-test'
  4. describe('reactivity/reactive', () => {
  5. mockWarn()
  6. test('Object', () => {
  7. const original = { foo: 1 }
  8. const observed = reactive(original)
  9. expect(observed).not.toBe(original)
  10. expect(isReactive(observed)).toBe(true)
  11. expect(isReactive(original)).toBe(false)
  12. // get
  13. expect(observed.foo).toBe(1)
  14. // has
  15. expect('foo' in observed).toBe(true)
  16. // ownKeys
  17. expect(Object.keys(observed)).toEqual(['foo'])
  18. })
  19. test('Array', () => {
  20. const original = [{ foo: 1 }]
  21. const observed = reactive(original)
  22. expect(observed).not.toBe(original)
  23. expect(isReactive(observed)).toBe(true)
  24. expect(isReactive(original)).toBe(false)
  25. expect(isReactive(observed[0])).toBe(true)
  26. // get
  27. expect(observed[0].foo).toBe(1)
  28. // has
  29. expect(0 in observed).toBe(true)
  30. // ownKeys
  31. expect(Object.keys(observed)).toEqual(['0'])
  32. })
  33. test('cloned reactive Array should point to observed values', () => {
  34. const original = [{ foo: 1 }]
  35. const observed = reactive(original)
  36. const clone = observed.slice()
  37. expect(isReactive(clone[0])).toBe(true)
  38. expect(clone[0]).not.toBe(original[0])
  39. expect(clone[0]).toBe(observed[0])
  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('observed value should proxy mutations to original (Object)', () => {
  54. const original: any = { foo: 1 }
  55. const observed = reactive(original)
  56. // set
  57. observed.bar = 1
  58. expect(observed.bar).toBe(1)
  59. expect(original.bar).toBe(1)
  60. // delete
  61. delete observed.foo
  62. expect('foo' in observed).toBe(false)
  63. expect('foo' in original).toBe(false)
  64. })
  65. test('observed value should proxy mutations to original (Array)', () => {
  66. const original: any[] = [{ foo: 1 }, { bar: 2 }]
  67. const observed = reactive(original)
  68. // set
  69. const value = { baz: 3 }
  70. const reactiveValue = reactive(value)
  71. observed[0] = value
  72. expect(observed[0]).toBe(reactiveValue)
  73. expect(original[0]).toBe(value)
  74. // delete
  75. delete observed[0]
  76. expect(observed[0]).toBeUndefined()
  77. expect(original[0]).toBeUndefined()
  78. // mutating methods
  79. observed.push(value)
  80. expect(observed[2]).toBe(reactiveValue)
  81. expect(original[2]).toBe(value)
  82. })
  83. test('setting a property with an unobserved value should wrap with reactive', () => {
  84. const observed = reactive<{ foo?: object }>({})
  85. const raw = {}
  86. observed.foo = raw
  87. expect(observed.foo).not.toBe(raw)
  88. expect(isReactive(observed.foo)).toBe(true)
  89. })
  90. test('observing already observed value should return same Proxy', () => {
  91. const original = { foo: 1 }
  92. const observed = reactive(original)
  93. const observed2 = reactive(observed)
  94. expect(observed2).toBe(observed)
  95. })
  96. test('observing the same value multiple times should return same Proxy', () => {
  97. const original = { foo: 1 }
  98. const observed = reactive(original)
  99. const observed2 = reactive(original)
  100. expect(observed2).toBe(observed)
  101. })
  102. test('should not pollute original object with Proxies', () => {
  103. const original: any = { foo: 1 }
  104. const original2 = { bar: 2 }
  105. const observed = reactive(original)
  106. const observed2 = reactive(original2)
  107. observed.bar = observed2
  108. expect(observed.bar).toBe(observed2)
  109. expect(original.bar).toBe(original2)
  110. })
  111. test('unwrap', () => {
  112. const original = { foo: 1 }
  113. const observed = reactive(original)
  114. expect(toRaw(observed)).toBe(original)
  115. expect(toRaw(original)).toBe(original)
  116. })
  117. test('should not unwrap Ref<T>', () => {
  118. const observedNumberRef = reactive(ref(1))
  119. const observedObjectRef = reactive(ref({ foo: 1 }))
  120. expect(isRef(observedNumberRef)).toBe(true)
  121. expect(isRef(observedObjectRef)).toBe(true)
  122. })
  123. test('non-observable values', () => {
  124. const assertValue = (value: any) => {
  125. reactive(value)
  126. expect(
  127. `value cannot be made reactive: ${String(value)}`
  128. ).toHaveBeenWarnedLast()
  129. }
  130. // number
  131. assertValue(1)
  132. // string
  133. assertValue('foo')
  134. // boolean
  135. assertValue(false)
  136. // null
  137. assertValue(null)
  138. // undefined
  139. assertValue(undefined)
  140. // symbol
  141. const s = Symbol()
  142. assertValue(s)
  143. // built-ins should work and return same value
  144. const p = Promise.resolve()
  145. expect(reactive(p)).toBe(p)
  146. const r = new RegExp('')
  147. expect(reactive(r)).toBe(r)
  148. const d = new Date()
  149. expect(reactive(d)).toBe(d)
  150. })
  151. test('markNonReactive', () => {
  152. const obj = reactive({
  153. foo: { a: 1 },
  154. bar: markNonReactive({ b: 2 })
  155. })
  156. expect(isReactive(obj.foo)).toBe(true)
  157. expect(isReactive(obj.bar)).toBe(false)
  158. })
  159. })