reactive.spec.ts 5.4 KB

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