line-interaction.spec.ts 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import { describe, expect, it, vi } from 'vitest'
  2. import {
  3. completeLineEdit,
  4. hasLineDragStarted,
  5. normalizeLineGeometry
  6. } from '@/components/mt-edit/utils/line-interaction'
  7. describe('自由连线拖动', () => {
  8. it('鼠标轻微抖动时不开始拖动', () => {
  9. expect(hasLineDragStarted(1, 1)).toBe(false)
  10. expect(hasLineDragStarted(2, 2)).toBe(false)
  11. expect(hasLineDragStarted(3, 0)).toBe(true)
  12. })
  13. it('只在真实拖动结束并写回最终数据后记录历史', () => {
  14. const calls: string[] = []
  15. const update = vi.fn(() => calls.push('update'))
  16. const complete = vi.fn(() => calls.push('history'))
  17. completeLineEdit({ id: 'line-1' }, false, update, complete)
  18. expect(calls).toEqual([])
  19. completeLineEdit({ id: 'line-1' }, true, update, complete)
  20. expect(calls).toEqual(['update', 'history'])
  21. })
  22. it('将 DOM 和缩放计算产生的坐标统一保留两位小数', () => {
  23. expect(
  24. normalizeLineGeometry(
  25. {
  26. left: 1480.034912109375,
  27. top: Number('669.9048461914062'),
  28. width: 100.00000000003,
  29. height: Number('50.99999999997')
  30. },
  31. [
  32. { x: 0.00000000003, y: 0 },
  33. { x: 99.99999999997, y: 50.123456 }
  34. ]
  35. )
  36. ).toEqual({
  37. binfo: { left: 1480.03, top: 669.9, width: 100, height: 51 },
  38. points: [
  39. { x: 0, y: 0 },
  40. { x: 100, y: 50.12 }
  41. ]
  42. })
  43. })
  44. })