workbenchPointerDragDrop.test.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. // @vitest-environment jsdom
  2. import { afterEach, describe, expect, it, vi } from "vitest";
  3. import {
  4. cancelWorkbenchPointerDrag,
  5. finishWorkbenchPointerDrag,
  6. registerWorkbenchPointerDropTarget,
  7. updateWorkbenchPointerDrag,
  8. } from "@/v4/workbenchPointerDragDrop.js";
  9. afterEach(() => {
  10. cancelWorkbenchPointerDrag();
  11. document.body.replaceChildren();
  12. });
  13. describe("workbench pointer drag/drop bridge", () => {
  14. it("previews and drops a grouped task at the matching pane edge", () => {
  15. const element = document.createElement("div");
  16. document.body.append(element);
  17. vi.spyOn(element, "getBoundingClientRect").mockReturnValue({
  18. bottom: 220,
  19. height: 200,
  20. left: 20,
  21. right: 420,
  22. top: 20,
  23. width: 400,
  24. x: 20,
  25. y: 20,
  26. toJSON: () => ({}),
  27. });
  28. const onPreview = vi.fn();
  29. const onDrop = vi.fn();
  30. const payload = {
  31. kind: "zcode/session" as const,
  32. workspacePath: "/repo",
  33. workspaceIdentity: "ssh:host:/repo",
  34. remoteSessionId: "remote-1",
  35. sessionId: "task-1",
  36. };
  37. const unregister = registerWorkbenchPointerDropTarget(element, {
  38. canDrop: () => true,
  39. onDrop,
  40. onPreview,
  41. });
  42. expect(updateWorkbenchPointerDrag(payload, 30, 120)).toBe(true);
  43. expect(onPreview).toHaveBeenLastCalledWith("left");
  44. expect(finishWorkbenchPointerDrag(payload, 30, 120)).toBe(true);
  45. expect(onDrop).toHaveBeenCalledWith("left", payload);
  46. expect(onPreview).toHaveBeenLastCalledWith(null);
  47. unregister();
  48. });
  49. it("keeps the pane inactive in its center and after cancellation", () => {
  50. const element = document.createElement("div");
  51. document.body.append(element);
  52. vi.spyOn(element, "getBoundingClientRect").mockReturnValue({
  53. bottom: 220,
  54. height: 200,
  55. left: 20,
  56. right: 420,
  57. top: 20,
  58. width: 400,
  59. x: 20,
  60. y: 20,
  61. toJSON: () => ({}),
  62. });
  63. const onPreview = vi.fn();
  64. const unregister = registerWorkbenchPointerDropTarget(element, {
  65. onDrop: vi.fn(),
  66. onPreview,
  67. });
  68. const payload = {
  69. kind: "zcode/session" as const,
  70. workspacePath: "/repo",
  71. sessionId: "task-1",
  72. };
  73. expect(updateWorkbenchPointerDrag(payload, 30, 120)).toBe(true);
  74. expect(updateWorkbenchPointerDrag(payload, 220, 120)).toBe(false);
  75. cancelWorkbenchPointerDrag();
  76. expect(onPreview).toHaveBeenLastCalledWith(null);
  77. unregister();
  78. });
  79. });