projectPointerDrag.test.ts 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { runInNewContext } from "node:vm";
  2. import { afterEach, expect, it, vi } from "vitest";
  3. import { startProjectPointerDrag } from "#e2e/helpers/project-sidebar";
  4. vi.mock("#e2e/helpers/desktop-app", () => ({}));
  5. afterEach(() => vi.unstubAllGlobals());
  6. it("合并后的单次位移只激活 sensor;手势仍产生真实拖拽位移", async () => {
  7. let y = 0;
  8. let origin = 0;
  9. let down = false;
  10. let active = false;
  11. let offset = 0;
  12. const document = {
  13. querySelector: () => ({
  14. getBoundingClientRect: () => ({ left: 20, top: 100 + offset, width: 80, height: 32 }),
  15. }),
  16. };
  17. vi.stubGlobal("$", () => ({ getAttribute: async () => (active ? "true" : null) }));
  18. vi.stubGlobal("browser", {
  19. execute: async (fn: (...args: unknown[]) => unknown, ...args: unknown[]) =>
  20. runInNewContext(`(${fn.toString()})(...args)`, { args, document }),
  21. releaseActions: async () => {
  22. down = false;
  23. active = false;
  24. },
  25. performActions: async (sources: Array<{ actions: Array<{ type: string; y?: number }> }>) => {
  26. for (const source of sources)
  27. for (const action of source.actions) {
  28. if (action.type === "pointerDown") {
  29. down = true;
  30. origin = y;
  31. }
  32. if (action.type !== "pointerMove") continue;
  33. y = action.y ?? y;
  34. if (!down) continue;
  35. // 模拟 PointerSensor:跨阈值的事件激活,后续事件才更新位移。
  36. if (!active && Math.abs(y - origin) > 6) active = true;
  37. else if (active) offset = y - origin;
  38. }
  39. },
  40. waitUntil: async (predicate: () => Promise<boolean>) => {
  41. if (!(await predicate())) throw new Error("拖拽未激活");
  42. },
  43. });
  44. const initial = await startProjectPointerDrag("button");
  45. expect(active).toBe(true);
  46. expect(document.querySelector().getBoundingClientRect().top - initial.top).toBeGreaterThanOrEqual(
  47. 6,
  48. );
  49. });