reactiveArray.spec.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. // #9742
  133. test('mutation on user proxy of reactive Array', () => {
  134. const array = reactive<number[]>([])
  135. const proxy = new Proxy(array, {})
  136. proxy.push(1)
  137. expect(array).toHaveLength(1)
  138. expect(proxy).toHaveLength(1)
  139. })
  140. describe('Array methods w/ refs', () => {
  141. let original: any[]
  142. beforeEach(() => {
  143. original = reactive([1, ref(2)])
  144. })
  145. // read + copy
  146. test('read only copy methods', () => {
  147. const raw = original.concat([3, ref(4)])
  148. expect(isRef(raw[1])).toBe(true)
  149. expect(isRef(raw[3])).toBe(true)
  150. })
  151. // read + write
  152. test('read + write mutating methods', () => {
  153. const res = original.copyWithin(0, 1, 2)
  154. const raw = toRaw(res)
  155. expect(isRef(raw[0])).toBe(true)
  156. expect(isRef(raw[1])).toBe(true)
  157. })
  158. test('read + identity', () => {
  159. const ref = original[1]
  160. expect(ref).toBe(toRaw(original)[1])
  161. expect(original.indexOf(ref)).toBe(1)
  162. })
  163. })
  164. describe('Array subclasses', () => {
  165. class SubArray<T> extends Array<T> {
  166. lastPushed: undefined | T
  167. lastSearched: undefined | T
  168. push(item: T) {
  169. this.lastPushed = item
  170. return super.push(item)
  171. }
  172. indexOf(searchElement: T, fromIndex?: number | undefined): number {
  173. this.lastSearched = searchElement
  174. return super.indexOf(searchElement, fromIndex)
  175. }
  176. }
  177. test('calls correct mutation method on Array subclass', () => {
  178. const subArray = new SubArray(4, 5, 6)
  179. const observed = reactive(subArray)
  180. subArray.push(7)
  181. expect(subArray.lastPushed).toBe(7)
  182. observed.push(9)
  183. expect(observed.lastPushed).toBe(9)
  184. })
  185. test('calls correct identity-sensitive method on Array subclass', () => {
  186. const subArray = new SubArray(4, 5, 6)
  187. const observed = reactive(subArray)
  188. let index
  189. index = subArray.indexOf(4)
  190. expect(index).toBe(0)
  191. expect(subArray.lastSearched).toBe(4)
  192. index = observed.indexOf(6)
  193. expect(index).toBe(2)
  194. expect(observed.lastSearched).toBe(6)
  195. })
  196. })
  197. })