Skip to content

Commit

Permalink
fix(reactivity): track length on for in iteration on Array
Browse files Browse the repository at this point in the history
fix #2427
  • Loading branch information
yyx990803 committed Oct 19, 2020
1 parent d9ad45a commit 0e5a3c4
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packages/reactivity/__tests__/reactiveArray.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,21 @@ describe('reactivity/reactive/Array', () => {
expect(fn).toHaveBeenCalledTimes(1)
})

// #2427
test('track length on for ... in iteration', () => {
const array = reactive([1])
let length = ''
effect(() => {
length = ''
for (const key in array) {
length += key
}
})
expect(length).toBe('0')
array.push(1)
expect(length).toBe('01')
})

describe('Array methods w/ refs', () => {
let original: any[]
beforeEach(() => {
Expand Down
2 changes: 1 addition & 1 deletion packages/reactivity/src/baseHandlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ function has(target: object, key: string | symbol): boolean {
}

function ownKeys(target: object): (string | number | symbol)[] {
track(target, TrackOpTypes.ITERATE, ITERATE_KEY)
track(target, TrackOpTypes.ITERATE, isArray(target) ? 'length' : ITERATE_KEY)
return Reflect.ownKeys(target)
}

Expand Down

0 comments on commit 0e5a3c4

Please sign in to comment.