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:
ᴜɴвʏтᴇ
2020-09-18 13:01:36 +08:00
committed by GitHub
parent 422f05e085
commit f316a332b0
2 changed files with 46 additions and 3 deletions

View File

@@ -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(() => {