browserDrag.test.ts 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import {afterEach,expect,it,vi} from 'vitest'
  2. import {flushPromises} from '@vue/test-utils'
  3. import {BrowserDragSession} from '@/utils/browserDrag'
  4. import type {BrowserPointerAction} from '@shared/managedBrowser'
  5. afterEach(()=>vi.useRealTimers())
  6. it('serializes input and coalesces pending movement without losing the final point',async()=>{
  7. vi.useFakeTimers()
  8. let release!:()=>void
  9. const send=vi.fn(async(_action:BrowserPointerAction)=>{})
  10. send.mockImplementationOnce(()=>new Promise<void>(resolve=>{release=resolve}))
  11. const failed=vi.fn(),session=new BrowserDragSession({x:1,y:2},send,failed)
  12. session.move({x:10,y:20});session.move({x:30,y:40});session.end({x:50,y:60})
  13. expect(send).toHaveBeenCalledTimes(1)
  14. release();await session.done
  15. expect(send.mock.calls.map(([action])=>[action.phase,action.x,action.y])).toEqual([['start',1,2],['move',30,40],['end',50,60]])
  16. expect(new Set(send.mock.calls.map(([action])=>action.gestureId)).size).toBe(1)
  17. await vi.advanceTimersByTimeAsync(10000);expect(send).toHaveBeenCalledTimes(3);expect(failed).not.toHaveBeenCalled()
  18. })
  19. it('keeps a stationary hold alive and discards queued moves on cancellation',async()=>{
  20. vi.useFakeTimers()
  21. const send=vi.fn(async(_action:BrowserPointerAction)=>{}),session=new BrowserDragSession({x:1,y:2},send,vi.fn())
  22. await flushPromises();await vi.advanceTimersByTimeAsync(1000)
  23. expect(send.mock.calls.map(([action])=>action.phase)).toEqual(['start','move'])
  24. let release!:()=>void
  25. send.mockImplementationOnce(()=>new Promise<void>(resolve=>{release=resolve}))
  26. session.move({x:10,y:20});session.move({x:30,y:40})
  27. const done=session.cancel();release();await done
  28. expect(send.mock.calls.map(([action])=>action.phase)).toEqual(['start','move','move','cancel'])
  29. await vi.advanceTimersByTimeAsync(10000);expect(send).toHaveBeenCalledTimes(4)
  30. })
  31. it('releases an uncertain input after failure without replaying it',async()=>{
  32. vi.useFakeTimers()
  33. const send=vi.fn(async(_action:BrowserPointerAction)=>{}).mockRejectedValueOnce(new Error('timeout')),failed=vi.fn()
  34. const session=new BrowserDragSession({x:1,y:2},send,failed)
  35. session.move({x:30,y:40});await session.done
  36. expect(send.mock.calls.map(([action])=>action.phase)).toEqual(['start','cancel'])
  37. expect(failed).toHaveBeenCalledWith(expect.objectContaining({message:'timeout'}))
  38. await vi.advanceTimersByTimeAsync(10000);expect(send).toHaveBeenCalledTimes(2)
  39. })