shallowReactive.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import {
  2. isReactive,
  3. isShallow,
  4. reactive,
  5. shallowReactive,
  6. shallowReadonly
  7. } from '../src/reactive'
  8. import { effect } from '../src/effect'
  9. describe('shallowReactive', () => {
  10. test('should not make non-reactive properties reactive', () => {
  11. const props = shallowReactive({ n: { foo: 1 } })
  12. expect(isReactive(props.n)).toBe(false)
  13. })
  14. test('should keep reactive properties reactive', () => {
  15. const props: any = shallowReactive({ n: reactive({ foo: 1 }) })
  16. props.n = reactive({ foo: 2 })
  17. expect(isReactive(props.n)).toBe(true)
  18. })
  19. // #2843
  20. test('should allow shallow and normal reactive for same target', () => {
  21. const original = { foo: {} }
  22. const shallowProxy = shallowReactive(original)
  23. const reactiveProxy = reactive(original)
  24. expect(shallowProxy).not.toBe(reactiveProxy)
  25. expect(isReactive(shallowProxy.foo)).toBe(false)
  26. expect(isReactive(reactiveProxy.foo)).toBe(true)
  27. })
  28. test('isShallow', () => {
  29. expect(isShallow(shallowReactive({}))).toBe(true)
  30. expect(isShallow(shallowReadonly({}))).toBe(true)
  31. })
  32. // #5271
  33. test('should respect shallow reactive nested inside reactive on reset', () => {
  34. const r = reactive({ foo: shallowReactive({ bar: {} }) })
  35. expect(isShallow(r.foo)).toBe(true)
  36. expect(isReactive(r.foo.bar)).toBe(false)
  37. r.foo = shallowReactive({ bar: {} })
  38. expect(isShallow(r.foo)).toBe(true)
  39. expect(isReactive(r.foo.bar)).toBe(false)
  40. })
  41. test('should respect shallow/deep versions of same target on access', () => {
  42. const original = {}
  43. const shallow = shallowReactive(original)
  44. const deep = reactive(original)
  45. const r = reactive({ shallow, deep })
  46. expect(r.shallow).toBe(shallow)
  47. expect(r.deep).toBe(deep)
  48. })
  49. describe('collections', () => {
  50. test('should be reactive', () => {
  51. const shallowSet = shallowReactive(new Set())
  52. const a = {}
  53. let size
  54. effect(() => {
  55. size = shallowSet.size
  56. })
  57. expect(size).toBe(0)
  58. shallowSet.add(a)
  59. expect(size).toBe(1)
  60. shallowSet.delete(a)
  61. expect(size).toBe(0)
  62. })
  63. test('should not observe when iterating', () => {
  64. const shallowSet = shallowReactive(new Set())
  65. const a = {}
  66. shallowSet.add(a)
  67. const spreadA = [...shallowSet][0]
  68. expect(isReactive(spreadA)).toBe(false)
  69. })
  70. test('should not get reactive entry', () => {
  71. const shallowMap = shallowReactive(new Map())
  72. const a = {}
  73. const key = 'a'
  74. shallowMap.set(key, a)
  75. expect(isReactive(shallowMap.get(key))).toBe(false)
  76. })
  77. test('should not get reactive on foreach', () => {
  78. const shallowSet = shallowReactive(new Set())
  79. const a = {}
  80. shallowSet.add(a)
  81. shallowSet.forEach(x => expect(isReactive(x)).toBe(false))
  82. })
  83. // #1210
  84. test('onTrack on called on objectSpread', () => {
  85. const onTrackFn = jest.fn()
  86. const shallowSet = shallowReactive(new Set())
  87. let a
  88. effect(
  89. () => {
  90. a = Array.from(shallowSet)
  91. },
  92. {
  93. onTrack: onTrackFn
  94. }
  95. )
  96. expect(a).toMatchObject([])
  97. expect(onTrackFn).toHaveBeenCalled()
  98. })
  99. })
  100. describe('array', () => {
  101. test('should be reactive', () => {
  102. const shallowArray = shallowReactive<unknown[]>([])
  103. const a = {}
  104. let size
  105. effect(() => {
  106. size = shallowArray.length
  107. })
  108. expect(size).toBe(0)
  109. shallowArray.push(a)
  110. expect(size).toBe(1)
  111. shallowArray.pop()
  112. expect(size).toBe(0)
  113. })
  114. test('should not observe when iterating', () => {
  115. const shallowArray = shallowReactive<object[]>([])
  116. const a = {}
  117. shallowArray.push(a)
  118. const spreadA = [...shallowArray][0]
  119. expect(isReactive(spreadA)).toBe(false)
  120. })
  121. test('onTrack on called on objectSpread', () => {
  122. const onTrackFn = jest.fn()
  123. const shallowArray = shallowReactive([])
  124. let a
  125. effect(
  126. () => {
  127. a = Array.from(shallowArray)
  128. },
  129. {
  130. onTrack: onTrackFn
  131. }
  132. )
  133. expect(a).toMatchObject([])
  134. expect(onTrackFn).toHaveBeenCalled()
  135. })
  136. })
  137. })