reactiveArray.spec.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import { reactive, isReactive, toRaw } from '../src/reactive'
  2. import { ref, isRef } from '../src/ref'
  3. import { effect } from '../src/effect'
  4. describe('reactivity/reactive/Array', () => {
  5. test('should make Array reactive', () => {
  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. expect(isReactive(observed[0])).toBe(true)
  12. // get
  13. expect(observed[0].foo).toBe(1)
  14. // has
  15. expect(0 in observed).toBe(true)
  16. // ownKeys
  17. expect(Object.keys(observed)).toEqual(['0'])
  18. })
  19. test('cloned reactive Array should point to observed values', () => {
  20. const original = [{ foo: 1 }]
  21. const observed = reactive(original)
  22. const clone = observed.slice()
  23. expect(isReactive(clone[0])).toBe(true)
  24. expect(clone[0]).not.toBe(original[0])
  25. expect(clone[0]).toBe(observed[0])
  26. })
  27. test('observed value should proxy mutations to original (Array)', () => {
  28. const original: any[] = [{ foo: 1 }, { bar: 2 }]
  29. const observed = reactive(original)
  30. // set
  31. const value = { baz: 3 }
  32. const reactiveValue = reactive(value)
  33. observed[0] = value
  34. expect(observed[0]).toBe(reactiveValue)
  35. expect(original[0]).toBe(value)
  36. // delete
  37. delete observed[0]
  38. expect(observed[0]).toBeUndefined()
  39. expect(original[0]).toBeUndefined()
  40. // mutating methods
  41. observed.push(value)
  42. expect(observed[2]).toBe(reactiveValue)
  43. expect(original[2]).toBe(value)
  44. })
  45. test('Array identity methods should work with raw values', () => {
  46. const raw = {}
  47. const arr = reactive([{}, {}])
  48. arr.push(raw)
  49. expect(arr.indexOf(raw)).toBe(2)
  50. expect(arr.indexOf(raw, 3)).toBe(-1)
  51. expect(arr.includes(raw)).toBe(true)
  52. expect(arr.includes(raw, 3)).toBe(false)
  53. expect(arr.lastIndexOf(raw)).toBe(2)
  54. expect(arr.lastIndexOf(raw, 1)).toBe(-1)
  55. // should work also for the observed version
  56. const observed = arr[2]
  57. expect(arr.indexOf(observed)).toBe(2)
  58. expect(arr.indexOf(observed, 3)).toBe(-1)
  59. expect(arr.includes(observed)).toBe(true)
  60. expect(arr.includes(observed, 3)).toBe(false)
  61. expect(arr.lastIndexOf(observed)).toBe(2)
  62. expect(arr.lastIndexOf(observed, 1)).toBe(-1)
  63. })
  64. test('Array identity methods should work if raw value contains reactive objects', () => {
  65. const raw = []
  66. const obj = reactive({})
  67. raw.push(obj)
  68. const arr = reactive(raw)
  69. expect(arr.includes(obj)).toBe(true)
  70. })
  71. test('Array identity methods should be reactive', () => {
  72. const obj = {}
  73. const arr = reactive([obj, {}])
  74. let index: number = -1
  75. effect(() => {
  76. index = arr.indexOf(obj)
  77. })
  78. expect(index).toBe(0)
  79. arr.reverse()
  80. expect(index).toBe(1)
  81. })
  82. test('delete on Array should not trigger length dependency', () => {
  83. const arr = reactive([1, 2, 3])
  84. const fn = vi.fn()
  85. effect(() => {
  86. fn(arr.length)
  87. })
  88. expect(fn).toHaveBeenCalledTimes(1)
  89. delete arr[1]
  90. expect(fn).toHaveBeenCalledTimes(1)
  91. })
  92. test('add existing index on Array should not trigger length dependency', () => {
  93. const array = new Array(3)
  94. const observed = reactive(array)
  95. const fn = vi.fn()
  96. effect(() => {
  97. fn(observed.length)
  98. })
  99. expect(fn).toHaveBeenCalledTimes(1)
  100. observed[1] = 1
  101. expect(fn).toHaveBeenCalledTimes(1)
  102. })
  103. test('add non-integer prop on Array should not trigger length dependency', () => {
  104. const array: any[] & { x?: string } = new Array(3)
  105. const observed = reactive(array)
  106. const fn = vi.fn()
  107. effect(() => {
  108. fn(observed.length)
  109. })
  110. expect(fn).toHaveBeenCalledTimes(1)
  111. observed.x = 'x'
  112. expect(fn).toHaveBeenCalledTimes(1)
  113. observed[-1] = 'x'
  114. expect(fn).toHaveBeenCalledTimes(1)
  115. observed[NaN] = 'x'
  116. expect(fn).toHaveBeenCalledTimes(1)
  117. })
  118. // #2427
  119. test('track length on for ... in iteration', () => {
  120. const array = reactive([1])
  121. let length = ''
  122. effect(() => {
  123. length = ''
  124. for (const key in array) {
  125. length += key
  126. }
  127. })
  128. expect(length).toBe('0')
  129. array.push(1)
  130. expect(length).toBe('01')
  131. })
  132. describe('Array methods w/ refs', () => {
  133. let original: any[]
  134. beforeEach(() => {
  135. original = reactive([1, ref(2)])
  136. })
  137. // read + copy
  138. test('read only copy methods', () => {
  139. const raw = original.concat([3, ref(4)])
  140. expect(isRef(raw[1])).toBe(true)
  141. expect(isRef(raw[3])).toBe(true)
  142. })
  143. // read + write
  144. test('read + write mutating methods', () => {
  145. const res = original.copyWithin(0, 1, 2)
  146. const raw = toRaw(res)
  147. expect(isRef(raw[0])).toBe(true)
  148. expect(isRef(raw[1])).toBe(true)
  149. })
  150. test('read + identity', () => {
  151. const ref = original[1]
  152. expect(ref).toBe(toRaw(original)[1])
  153. expect(original.indexOf(ref)).toBe(1)
  154. })
  155. })
  156. describe('Array subclasses', () => {
  157. class SubArray<T> extends Array<T> {
  158. lastPushed: undefined | T
  159. lastSearched: undefined | T
  160. push(item: T) {
  161. this.lastPushed = item
  162. return super.push(item)
  163. }
  164. indexOf(searchElement: T, fromIndex?: number | undefined): number {
  165. this.lastSearched = searchElement
  166. return super.indexOf(searchElement, fromIndex)
  167. }
  168. }
  169. test('calls correct mutation method on Array subclass', () => {
  170. const subArray = new SubArray(4, 5, 6)
  171. const observed = reactive(subArray)
  172. subArray.push(7)
  173. expect(subArray.lastPushed).toBe(7)
  174. observed.push(9)
  175. expect(observed.lastPushed).toBe(9)
  176. })
  177. test('calls correct identity-sensitive method on Array subclass', () => {
  178. const subArray = new SubArray(4, 5, 6)
  179. const observed = reactive(subArray)
  180. let index
  181. index = subArray.indexOf(4)
  182. expect(index).toBe(0)
  183. expect(subArray.lastSearched).toBe(4)
  184. index = observed.indexOf(6)
  185. expect(index).toBe(2)
  186. expect(observed.lastSearched).toBe(6)
  187. })
  188. })
  189. })