Skip to content

Commit 738059b

Browse files
committed
fix: use splice to set values in arrays
1 parent ee79803 commit 738059b

File tree

2 files changed

+59
-1
lines changed

2 files changed

+59
-1
lines changed

src/utils.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,8 @@ export function walkSet (obj, path, value) {
5656
const keys = ('' + path).split('.')
5757
const key = keys.pop()
5858
const target = keys.reduce((target, key) => target[key], obj)
59-
return (target[key] = value)
59+
// global isFinite is different from Number.isFinite
60+
// it convert values to numbers
61+
if (isFinite(key)) target.splice(key, 1, value)
62+
else target[key] = value
6063
}

test/refs-documents.spec.js

+55
Original file line numberDiff line numberDiff line change
@@ -363,3 +363,58 @@ test('properly updates a documen with refs', async () => {
363363
a: { isA: true }
364364
})
365365
})
366+
367+
test('updates values in arrays', async () => {
368+
await item.update({
369+
arr: [a, b]
370+
})
371+
372+
await vm.$bind('item', item)
373+
374+
expect(vm.item).toEqual({
375+
arr: [{ isA: true }, null]
376+
})
377+
378+
await b.update({ isB: true })
379+
380+
expect(vm.item).toEqual({
381+
arr: [{ isA: true }, { isB: true }]
382+
})
383+
384+
await item.update({
385+
arr: [c]
386+
})
387+
388+
// NOTE see (1)
389+
await delay(5)
390+
391+
expect(vm.item).toEqual({
392+
arr: [{ isC: true }]
393+
})
394+
})
395+
396+
test('correctly updates arrays', async () => {
397+
await item.update({
398+
arr: [a, b]
399+
})
400+
401+
await vm.$bind('item', item)
402+
403+
const spy = jest.fn()
404+
vm.$watch('item.arr', spy)
405+
406+
await b.update({ isB: true })
407+
408+
expect(spy).toHaveBeenCalledTimes(1)
409+
410+
await item.update({
411+
arr: [c]
412+
})
413+
414+
expect(spy).toHaveBeenCalledTimes(2)
415+
416+
// NOTE see (1)
417+
await delay(5)
418+
419+
expect(spy).toHaveBeenCalledTimes(3)
420+
})

0 commit comments

Comments
 (0)