Skip to content

Commit c0c9432

Browse files
test(watch): watching a reactive array (#10848)
1 parent 0e3d617 commit c0c9432

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

packages/runtime-core/__tests__/apiWatch.spec.ts

+42
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,30 @@ describe('api: watch', () => {
9696
expect(spy).toBeCalledWith([1], [1], expect.anything())
9797
})
9898

99+
it('should not call functions inside a reactive source array', () => {
100+
const spy1 = vi.fn()
101+
const array = reactive([spy1])
102+
const spy2 = vi.fn()
103+
watch(array, spy2, { immediate: true })
104+
expect(spy1).toBeCalledTimes(0)
105+
expect(spy2).toBeCalledWith([spy1], undefined, expect.anything())
106+
})
107+
108+
it('should not unwrap refs in a reactive source array', async () => {
109+
const val = ref({ foo: 1 })
110+
const array = reactive([val])
111+
const spy = vi.fn()
112+
watch(array, spy, { immediate: true })
113+
expect(spy).toBeCalledTimes(1)
114+
expect(spy).toBeCalledWith([val], undefined, expect.anything())
115+
116+
// deep by default
117+
val.value.foo++
118+
await nextTick()
119+
expect(spy).toBeCalledTimes(2)
120+
expect(spy).toBeCalledWith([val], [val], expect.anything())
121+
})
122+
99123
it('should not fire if watched getter result did not change', async () => {
100124
const spy = vi.fn()
101125
const n = ref(0)
@@ -186,6 +210,24 @@ describe('api: watch', () => {
186210
expect(dummy).toBe(1)
187211
})
188212

213+
it('directly watching reactive array with explicit deep: false', async () => {
214+
const val = ref(1)
215+
const array: any[] = reactive([val])
216+
const spy = vi.fn()
217+
watch(array, spy, { immediate: true, deep: false })
218+
expect(spy).toBeCalledTimes(1)
219+
expect(spy).toBeCalledWith([val], undefined, expect.anything())
220+
221+
val.value++
222+
await nextTick()
223+
expect(spy).toBeCalledTimes(1)
224+
225+
array[1] = 2
226+
await nextTick()
227+
expect(spy).toBeCalledTimes(2)
228+
expect(spy).toBeCalledWith([val, 2], [val, 2], expect.anything())
229+
})
230+
189231
// #9916
190232
it('watching shallow reactive array with deep: false', async () => {
191233
class foo {

0 commit comments

Comments
 (0)