Skip to content

Commit fb5f399

Browse files
committed
test(watch): sync with vue
vuejs/core#13434
1 parent 657e6f1 commit fb5f399

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

packages/core/__tests__/watch.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,57 @@ describe('watch', () => {
994994
expect(cb).toHaveBeenCalledTimes(4)
995995
})
996996

997+
test('watching the same object at different depths', async () => {
998+
const arr1: any[] = reactive([[[{ foo: {} }]]])
999+
const arr2 = arr1[0]
1000+
const arr3 = arr2[0]
1001+
const obj = arr3[0]
1002+
arr1.push(arr3)
1003+
1004+
const cb1 = vi.fn()
1005+
const cb2 = vi.fn()
1006+
const cb3 = vi.fn()
1007+
const cb4 = vi.fn()
1008+
watch(arr1, cb1, { deep: 1 })
1009+
watch(arr1, cb2, { deep: 2 })
1010+
watch(arr1, cb3, { deep: 3 })
1011+
watch(arr1, cb4, { deep: 4 })
1012+
1013+
await nextTick()
1014+
expect(cb1).toHaveBeenCalledTimes(0)
1015+
expect(cb2).toHaveBeenCalledTimes(0)
1016+
expect(cb3).toHaveBeenCalledTimes(0)
1017+
expect(cb4).toHaveBeenCalledTimes(0)
1018+
1019+
obj.foo = {}
1020+
await nextTick()
1021+
expect(cb1).toHaveBeenCalledTimes(0)
1022+
expect(cb2).toHaveBeenCalledTimes(0)
1023+
expect(cb3).toHaveBeenCalledTimes(1)
1024+
expect(cb4).toHaveBeenCalledTimes(1)
1025+
1026+
obj.foo.bar = 1
1027+
await nextTick()
1028+
expect(cb1).toHaveBeenCalledTimes(0)
1029+
expect(cb2).toHaveBeenCalledTimes(0)
1030+
expect(cb3).toHaveBeenCalledTimes(1)
1031+
expect(cb4).toHaveBeenCalledTimes(2)
1032+
1033+
arr3.push(obj.foo)
1034+
await nextTick()
1035+
expect(cb1).toHaveBeenCalledTimes(0)
1036+
expect(cb2).toHaveBeenCalledTimes(1)
1037+
expect(cb3).toHaveBeenCalledTimes(2)
1038+
expect(cb4).toHaveBeenCalledTimes(3)
1039+
1040+
obj.foo.bar = 2
1041+
await nextTick()
1042+
expect(cb1).toHaveBeenCalledTimes(0)
1043+
expect(cb2).toHaveBeenCalledTimes(1)
1044+
expect(cb3).toHaveBeenCalledTimes(3)
1045+
expect(cb4).toHaveBeenCalledTimes(4)
1046+
})
1047+
9971048
test('pause / resume', async () => {
9981049
const count = ref(0)
9991050
const cb = vi.fn()

0 commit comments

Comments
 (0)