expect(r.value).toBe(1)
expect(c.value).toBe(1)
})
+
+ // edge case where a nested endBatch() causes an effect to be batched in a
+ // nested batch loop with its .next mutated, causing the outer loop to end
+ // early
+ test('nested batch edge case', () => {
+ // useClamp from VueUse
+ const clamp = (n: number, min: number, max: number) =>
+ Math.min(max, Math.max(min, n))
+ function useClamp(src: Ref<number>, min: number, max: number) {
+ return computed({
+ get() {
+ return (src.value = clamp(src.value, min, max))
+ },
+ set(val) {
+ src.value = clamp(val, min, max)
+ },
+ })
+ }
+
+ const src = ref(1)
+ const clamped = useClamp(src, 1, 5)
+ watch(src, val => (clamped.value = val))
+
+ const spy = vi.fn()
+ watch(clamped, spy)
+
+ src.value = 2
+ expect(spy).toHaveBeenCalledTimes(1)
+ src.value = 10
+ expect(spy).toHaveBeenCalledTimes(2)
+ })
})