Skip to content

The agent that grows with you

test(desktop): pin rafCoalesce push/cancel contract

kshitijk4poor3675d919006a
diff --git a/apps/desktop/src/lib/raf-coalesce.test.ts b/apps/desktop/src/lib/raf-coalesce.test.ts
new file mode 100644
index 00000000000..e91221a5f8d
--- /dev/null
+++ b/apps/desktop/src/lib/raf-coalesce.test.ts
@@ -0,0 +1,34 @@
+import { afterEach, describe, expect, it, vi } from 'vitest'
+
+import { rafCoalesce } from './raf-coalesce'
+
+afterEach(() => {
+ vi.useRealTimers()
+})
+
+describe('rafCoalesce', () => {
+ it('applies only the last pushed value, once per frame', () => {
+ vi.useFakeTimers({ toFake: ['requestAnimationFrame', 'cancelAnimationFrame'] })
+ const apply = vi.fn()
+ const coalesced = rafCoalesce<number>(apply)
+
+ coalesced.push(1)
+ coalesced.push(2)
+ vi.advanceTimersToNextFrame()
+
+ expect(apply).toHaveBeenCalledTimes(1)
+ expect(apply).toHaveBeenCalledWith(2)
+ })
+
+ it('cancel drops the pending value without applying it', () => {
+ vi.useFakeTimers({ toFake: ['requestAnimationFrame', 'cancelAnimationFrame'] })
+ const apply = vi.fn()
+ const coalesced = rafCoalesce<number>(apply)
+
+ coalesced.push(1)
+ coalesced.cancel()
+ vi.advanceTimersToNextFrame()
+
+ expect(apply).not.toHaveBeenCalled()
+ })
+})