shallowReactive.spec.ts 3.3 KB

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