previewPaneImageContent.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import { createElement } from "react";
  2. import { renderToStaticMarkup } from "react-dom/server";
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. getImagePreviewPixelRatio,
  6. ImagePreviewContent,
  7. SvgPreviewContent,
  8. } from "@/previewPaneImageContent.js";
  9. describe("ImagePreviewContent", () => {
  10. it("centers the image and preserves natural size until it reaches the pane bounds", () => {
  11. const html = renderToStaticMarkup(
  12. createElement(ImagePreviewContent, {
  13. title: "logo.png",
  14. imageSource: "data:image/png;base64,",
  15. }),
  16. );
  17. expect(html).toContain("flex h-full min-h-0 items-center justify-center");
  18. expect(html).toContain("p-10");
  19. expect(html).toContain("[background-color:var(--color-background)]");
  20. expect(html).toContain("[background-image:linear-gradient(45deg,var(--color-surface)_25%,transparent_25%)");
  21. expect(html).toContain("[background-position:0_0,0_4px,4px_-4px,-4px_0]");
  22. expect(html).toContain("[background-size:8px_8px]");
  23. expect(html).toContain("h-auto max-h-full w-auto max-w-full");
  24. expect(html).not.toContain("rounded");
  25. expect(html).not.toMatch(/\sw-full(\s|")/);
  26. });
  27. it("detects retina pixel ratios from image file names", () => {
  28. expect(getImagePreviewPixelRatio("/tmp/logo@2x.png")).toBe(2);
  29. expect(getImagePreviewPixelRatio("icon@3x.webp")).toBe(3);
  30. expect(getImagePreviewPixelRatio("asset@1.5x.avif")).toBe(1.5);
  31. expect(getImagePreviewPixelRatio("/tmp/@2x/logo.png")).toBe(1);
  32. expect(getImagePreviewPixelRatio("logo@0x.png")).toBe(1);
  33. expect(getImagePreviewPixelRatio("logo@2x.backup.png")).toBe(1);
  34. });
  35. it("fits svg previews to the container instead of using bitmap natural sizing", () => {
  36. const html = renderToStaticMarkup(
  37. createElement(SvgPreviewContent, {
  38. title: "logo@2x.svg",
  39. svgContent:
  40. '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M0 0h24v24H0z"/></svg>',
  41. }),
  42. );
  43. expect(html).toContain("h-full max-h-full w-full max-w-full");
  44. expect(html).not.toContain("h-auto max-h-full w-auto max-w-full");
  45. });
  46. });