diffViewerComponent.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it, vi } from "vitest";
  4. import { DiffViewer } from "../src/components/ui/diff-viewer.js";
  5. vi.mock("@pierre/diffs/react", () => ({
  6. MultiFileDiff: ({
  7. className,
  8. options,
  9. selectedLines,
  10. }: {
  11. className?: string;
  12. options?: Record<string, unknown>;
  13. selectedLines?: { start: number; end: number; side?: string } | null;
  14. }) =>
  15. createElement("diffs-container", {
  16. class: className,
  17. "data-diff-indicators": String(options?.diffIndicators),
  18. "data-disable-file-header": String(Boolean(options?.disableFileHeader)),
  19. "data-hunk-separators": String(options?.hunkSeparators),
  20. "data-line-diff-type": String(options?.lineDiffType),
  21. "data-unsafe-css": String(options?.unsafeCSS ?? ""),
  22. "data-selected-lines": selectedLines
  23. ? `${selectedLines.side}:${selectedLines.start}-${selectedLines.end}`
  24. : "",
  25. "data-testid": "multi-file-diff",
  26. }),
  27. PatchDiff: ({
  28. className,
  29. disableWorkerPool,
  30. options,
  31. selectedLines,
  32. }: {
  33. className?: string;
  34. disableWorkerPool?: boolean;
  35. options?: Record<string, unknown>;
  36. selectedLines?: { start: number; end: number; side?: string } | null;
  37. }) =>
  38. createElement("diffs-container", {
  39. class: className,
  40. "data-diff-indicators": String(options?.diffIndicators),
  41. "data-disable-file-header": String(Boolean(options?.disableFileHeader)),
  42. "data-disable-worker-pool": String(Boolean(disableWorkerPool)),
  43. "data-hunk-separators": String(options?.hunkSeparators),
  44. "data-line-diff-type": String(options?.lineDiffType),
  45. "data-unsafe-css": String(options?.unsafeCSS ?? ""),
  46. "data-selected-lines": selectedLines
  47. ? `${selectedLines.side}:${selectedLines.start}-${selectedLines.end}`
  48. : "",
  49. "data-testid": "patch-diff",
  50. }),
  51. }));
  52. describe("DiffViewer", () => {
  53. it("renders the @pierre/diffs patch container without legacy fallback markup", () => {
  54. const html = renderToStaticMarkup(
  55. createElement(DiffViewer, {
  56. patch: "diff --git a/demo.ts b/demo.ts\n@@ -1 +1 @@\n-foo\n+bar",
  57. }),
  58. );
  59. expect(html).toContain("<diffs-container");
  60. expect(html).toContain("data-diff-viewer");
  61. expect(html).toContain("overflow-auto");
  62. expect(html).toContain("min-h-full");
  63. expect(html).toContain("--diffs-bg:var(--color-background)");
  64. expect(html).toContain('data-diff-indicators="bars"');
  65. expect(html).toContain('data-disable-file-header="true"');
  66. expect(html).toContain('data-hunk-separators="simple"');
  67. expect(html).toContain('data-line-diff-type="word-alt"');
  68. expect(html).toContain('data-unsafe-css=""');
  69. expect(html).not.toContain("whitespace-pre");
  70. });
  71. it("keeps worker pool rendering enabled by default", () => {
  72. const html = renderToStaticMarkup(
  73. createElement(DiffViewer, {
  74. patch: "diff --git a/demo.ts b/demo.ts\n@@ -1 +1 @@\n-foo\n+bar",
  75. }),
  76. );
  77. expect(html).not.toContain('data-disable-worker-pool="true"');
  78. });
  79. it("renders old and new files through MultiFileDiff", () => {
  80. const html = renderToStaticMarkup(
  81. createElement(DiffViewer, {
  82. oldFile: {
  83. name: "demo.ts",
  84. contents: "const value = 1;\n",
  85. },
  86. newFile: {
  87. name: "demo.ts",
  88. contents: "const value = 2;\n",
  89. },
  90. }),
  91. );
  92. expect(html).toContain("<diffs-container");
  93. expect(html).toContain("data-diff-viewer");
  94. expect(html).toContain('data-testid="multi-file-diff"');
  95. expect(html).toContain('data-diff-indicators="bars"');
  96. expect(html).toContain('data-disable-file-header="true"');
  97. expect(html).toContain('data-hunk-separators="line-info"');
  98. expect(html).toContain('data-line-diff-type="word-alt"');
  99. expect(html).toContain('data-unsafe-css=""');
  100. expect(html).toContain("min-h-full");
  101. });
  102. it("forwards the requested current-side line range to both diff renderers", () => {
  103. const selectedLines = { side: "additions" as const, start: 8, end: 10 };
  104. const patchHtml = renderToStaticMarkup(
  105. createElement(DiffViewer, {
  106. patch: "diff --git a/demo.ts b/demo.ts\n@@ -1 +1 @@\n-old\n+new",
  107. selectedLines,
  108. }),
  109. );
  110. const filesHtml = renderToStaticMarkup(
  111. createElement(DiffViewer, {
  112. oldFile: { name: "demo.ts", contents: "old\n" },
  113. newFile: { name: "demo.ts", contents: "new\n" },
  114. selectedLines,
  115. }),
  116. );
  117. expect(patchHtml).toContain('data-selected-lines="additions:8-10"');
  118. expect(filesHtml).toContain('data-selected-lines="additions:8-10"');
  119. });
  120. });