Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { afterEach, expect, it, vi } from "vitest";
import { cleanup, render } from "@testing-library/react";
import { TemplateCard } from "./template-card";
afterEach(cleanup);
it.each([1, 6, 40, 300])("keeps %i bars visible within the SVG viewport", (count) => {
const data = Array.from({ length: count }, (_, i) => ({ label: String(i), value: i + 1 }));
const { container } = render(<TemplateCard id="chart" name="Revenue" description="" html=""
componentType="barChart" componentData={{ data }} dataDescription="" version={1} onApply={vi.fn()} />);
const svg = container.querySelector("svg")!;
const [, , viewportWidth, viewportHeight] = svg.getAttribute("viewBox")!.split(" ").map(Number);
const bars = Array.from(svg.querySelectorAll("rect"));
expect(bars).toHaveLength(count);
let previousRight = -1;
for (const bar of bars) {
const x = Number(bar.getAttribute("x"));
const y = Number(bar.getAttribute("y"));
const width = Number(bar.getAttribute("width"));
const height = Number(bar.getAttribute("height"));
expect(width).toBeGreaterThan(0);
expect(height).toBeGreaterThan(0);
expect(x).toBeGreaterThanOrEqual(previousRight);
expect(x + width).toBeLessThanOrEqual(viewportWidth);
expect(y).toBeGreaterThanOrEqual(0);
expect(y + height).toBeLessThanOrEqual(viewportHeight);
previousRight = x + width;
}
});
8 changes: 5 additions & 3 deletions apps/app/src/components/template-library/template-card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ interface TemplateCardProps {
function BarChartPreview({ data }: { data: { label: string; value: number }[] }) {
if (!data?.length) return null;
const max = Math.max(...data.map((d) => d.value));
const barWidth = Math.min(40, Math.floor(280 / data.length) - 8);
const chartWidth = data.length * (barWidth + 8);
const slotWidth = Math.min(48, 280 / data.length);
const gap = Math.min(8, slotWidth / 4);
const barWidth = slotWidth - gap;
const chartWidth = data.length * slotWidth;
const chartHeight = 100;

return (
Expand All @@ -42,7 +44,7 @@ function BarChartPreview({ data }: { data: { label: string; value: number }[] })
return (
<rect
key={i}
x={i * (barWidth + 8)}
x={i * slotWidth}
y={chartHeight - h - 10}
width={barWidth}
height={h}
Expand Down