39 lines
1.2 KiB
TypeScript
39 lines
1.2 KiB
TypeScript
import { describe, expect, test } from "bun:test";
|
|
import { auditImage, createResponsiveImage } from "../src/index.ts";
|
|
|
|
describe("@wrnexus/image", () => {
|
|
test("builds deterministic responsive image attributes", () => {
|
|
const image = createResponsiveImage({
|
|
src: "/hero.jpg",
|
|
alt: "Hero",
|
|
width: 1200,
|
|
height: 600,
|
|
widths: [400, 800, 1200, 800],
|
|
sizes: "100vw",
|
|
format: "webp",
|
|
fetchPriority: "high",
|
|
});
|
|
expect(image.srcset).toContain("400w");
|
|
expect(image.srcset).toContain("1200w");
|
|
expect(image.loading).toBe("eager");
|
|
expect(image.width).toBe("1200");
|
|
});
|
|
|
|
test("reports layout shift and LCP mistakes", () => {
|
|
const issues = auditImage({ src: "hero.jpg", isLcp: true, loading: "lazy" });
|
|
expect(issues.map((issue) => issue.code)).toContain("WRN-IMAGE-DIMENSIONS");
|
|
expect(issues.map((issue) => issue.code)).toContain("WRN-IMAGE-LCP-LAZY");
|
|
});
|
|
test("rejects insecure remote images unless explicitly allowed", () => {
|
|
expect(() =>
|
|
createResponsiveImage({
|
|
src: "http://images.example.com/hero.jpg",
|
|
alt: "Hero",
|
|
width: 800,
|
|
height: 400,
|
|
remoteHosts: ["images.example.com"],
|
|
}),
|
|
).toThrow();
|
|
});
|
|
});
|