fix(reactivity): avoid length mutating array methods causing infinite updates (#2138)
fix #2137 Co-authored-by: Evan You <yyx990803@gmail.com>
This commit is contained in:
@@ -358,6 +358,29 @@ describe('reactivity/effect', () => {
|
||||
expect(counterSpy).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it('should avoid infinite recursive loops when use Array.prototype.push/unshift/pop/shift', () => {
|
||||
;(['push', 'unshift'] as const).forEach(key => {
|
||||
const arr = reactive<number[]>([])
|
||||
const counterSpy1 = jest.fn(() => (arr[key] as any)(1))
|
||||
const counterSpy2 = jest.fn(() => (arr[key] as any)(2))
|
||||
effect(counterSpy1)
|
||||
effect(counterSpy2)
|
||||
expect(arr.length).toBe(2)
|
||||
expect(counterSpy1).toHaveBeenCalledTimes(1)
|
||||
expect(counterSpy2).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
;(['pop', 'shift'] as const).forEach(key => {
|
||||
const arr = reactive<number[]>([1, 2, 3, 4])
|
||||
const counterSpy1 = jest.fn(() => (arr[key] as any)())
|
||||
const counterSpy2 = jest.fn(() => (arr[key] as any)())
|
||||
effect(counterSpy1)
|
||||
effect(counterSpy2)
|
||||
expect(arr.length).toBe(2)
|
||||
expect(counterSpy1).toHaveBeenCalledTimes(1)
|
||||
expect(counterSpy2).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
})
|
||||
|
||||
it('should allow explicitly recursive raw function loops', () => {
|
||||
const counter = reactive({ num: 0 })
|
||||
const numSpy = jest.fn(() => {
|
||||
|
||||
Reference in New Issue
Block a user