pptxElementReferenceChip.test.ts 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // @vitest-environment jsdom
  2. import { fireEvent, render, screen } from "@testing-library/react";
  3. import { createElement } from "react";
  4. import { afterEach, describe, expect, it, vi } from "vitest";
  5. import { ZCodeIntlProvider } from "@/i18n/IntlProvider.js";
  6. import type { PptxElementReference } from "@/lib/pptxElementReference.js";
  7. import { PptxElementReferenceChip } from "@/v4/composer/PptxElementReferenceChip.js";
  8. function makeReference(): PptxElementReference {
  9. return {
  10. bounds: { x: 10, y: 20, width: 300, height: 80 },
  11. capturedAt: 1,
  12. id: "reference-1",
  13. nodeId: "7",
  14. nodeName: "Title",
  15. nodeType: "shape",
  16. slideIndex: 2,
  17. slidePart: "ppt/slides/slide3.xml",
  18. sourceFingerprint: `sha256:${"a".repeat(64)}`,
  19. sourcePath: "/workspace/deck.pptx",
  20. sourceTitle: "deck.pptx",
  21. text: "Quarterly plan",
  22. selectedText: "Quarterly",
  23. comment: "Make the title more concise",
  24. workspacePath: "/workspace",
  25. zIndex: 1,
  26. };
  27. }
  28. afterEach(() => document.body.replaceChildren());
  29. describe("PptxElementReferenceChip", () => {
  30. it("opens a concrete reference while keeping delete as an isolated sibling action", async () => {
  31. const reference = makeReference();
  32. const onOpen = vi.fn();
  33. const onRemove = vi.fn();
  34. render(
  35. createElement(
  36. ZCodeIntlProvider,
  37. { initialLocale: "en-US" },
  38. createElement(PptxElementReferenceChip, {
  39. references: [reference],
  40. onOpen,
  41. onRemove,
  42. }),
  43. ),
  44. );
  45. fireEvent.pointerEnter(
  46. screen.getByRole("button", { name: "1 slide element" }),
  47. );
  48. const referenceLabel = await screen.findByText("Quarterly");
  49. expect(screen.getByText("Make the title more concise")).toBeTruthy();
  50. const referenceButton = referenceLabel.closest("button");
  51. expect(referenceButton).not.toBeNull();
  52. fireEvent.click(referenceButton as HTMLButtonElement);
  53. expect(onOpen).toHaveBeenCalledWith(reference);
  54. fireEvent.click(screen.getByRole("button", { name: "Remove slide element" }));
  55. expect(onRemove).toHaveBeenCalledWith(reference.id);
  56. expect(onOpen).toHaveBeenCalledTimes(1);
  57. });
  58. });