diffViewerComponent.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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 patch input with simple separators", () => {
  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('data-hunk-separators="simple"');
  62. });
  63. it("keeps worker pool rendering enabled by default", () => {
  64. const html = renderToStaticMarkup(
  65. createElement(DiffViewer, {
  66. patch: "diff --git a/demo.ts b/demo.ts\n@@ -1 +1 @@\n-foo\n+bar",
  67. }),
  68. );
  69. expect(html).not.toContain('data-disable-worker-pool="true"');
  70. });
  71. it("renders old and new files through MultiFileDiff", () => {
  72. const html = renderToStaticMarkup(
  73. createElement(DiffViewer, {
  74. oldFile: {
  75. name: "demo.ts",
  76. contents: "const value = 1;\n",
  77. },
  78. newFile: {
  79. name: "demo.ts",
  80. contents: "const value = 2;\n",
  81. },
  82. }),
  83. );
  84. expect(html).toContain("<diffs-container");
  85. expect(html).toContain("data-diff-viewer");
  86. expect(html).toContain('data-testid="multi-file-diff"');
  87. expect(html).toContain('data-hunk-separators="line-info"');
  88. });
  89. it("forwards the requested current-side line range to both diff renderers", () => {
  90. const selectedLines = { side: "additions" as const, start: 8, end: 10 };
  91. const patchHtml = renderToStaticMarkup(
  92. createElement(DiffViewer, {
  93. patch: "diff --git a/demo.ts b/demo.ts\n@@ -1 +1 @@\n-old\n+new",
  94. selectedLines,
  95. }),
  96. );
  97. const filesHtml = renderToStaticMarkup(
  98. createElement(DiffViewer, {
  99. oldFile: { name: "demo.ts", contents: "old\n" },
  100. newFile: { name: "demo.ts", contents: "new\n" },
  101. selectedLines,
  102. }),
  103. );
  104. expect(patchHtml).toContain('data-selected-lines="additions:8-10"');
  105. expect(filesHtml).toContain('data-selected-lines="additions:8-10"');
  106. });
  107. });