import type { SVGAttributes } from "../../types.js"; import { overrideSvgAttributes } from "./overrideSvgAttributes.js"; import fc from "fast-check"; import { parse } from "ultrahtml"; import { bench, describe, expect } from "vitest"; const svgArbitrary = fc .record({ start: fc.mixedCase(fc.constantFrom("")), middle: fc.string({ unit: "grapheme-composite" }), end: fc.mixedCase(fc.constantFrom("")), }) .map(({ start, middle, end }) => `${start}${middle}${end}`); const SVGAttributesArbitrary = fc.record({ height: fc.option(fc.integer()), width: fc.option(fc.integer()), "aria-hidden": fc.option(fc.boolean()), "aria-label": fc.option(fc.string()), viewBox: fc.option(fc.string()), fill: fc.option(fc.string()), xmlns: fc.option(fc.string()), }); describe("overrideSvgAtrributes", () => { bench("should return the same output if no overrides are given", async () => { expect(await overrideSvgAttributes("")).toBe(""); expect(await overrideSvgAttributes("")).toBe(""); expect(await overrideSvgAttributes("")).toBe(""); }); bench("should strip leading and following whitespace", async () => { expect(await overrideSvgAttributes(" ")).toBe(""); expect(await overrideSvgAttributes(" ")).toBe(""); expect(await overrideSvgAttributes(" ")).toBe(""); }); bench("should override height and width", async () => { expect( await overrideSvgAttributes(``, { height: 400, width: 20, }), ).toBe(''); }); bench("should not include null and undefined properties", async () => { expect( await overrideSvgAttributes("", { height: null, width: undefined, }), ).toBe(""); expect( await overrideSvgAttributes( ``, { height: null, width: undefined, }, ), ).toBe(``); }); bench("should throw an error if `svgSource` is empty", async () => { await expect( async () => await overrideSvgAttributes(""), ).rejects.toThrowErrorMatchingInlineSnapshot( '"`svgSource` must have content"', ); }); bench( "should throw an error if svgSource doesn't start with ` { await expect( async () => await overrideSvgAttributes("
"), ).rejects.toThrowErrorMatchingInlineSnapshot( '"`svgSource` must begin with ` await overrideSvgAttributes("/images/www/hero.svg"), ).rejects.toThrowErrorMatchingInlineSnapshot( '"`svgSource` must begin with ` { expect( await overrideSvgAttributes("", { height: null, width: null, "aria-hidden": true, "aria-label": null, viewBox: "0 0 2712 894", }), ).toBe(''); await fc.assert( fc.asyncProperty( svgArbitrary, SVGAttributesArbitrary, fc.context(), async (svgSource, overrides, ctx) => { ctx.log(svgSource); const transformedSource = await overrideSvgAttributes( svgSource, overrides, ); ctx.log(transformedSource); expect(transformedSource).toBeTruthy(); // every truthy override should exist in the transformed source Object.entries(overrides) .filter(([, value]) => { ctx.log(`${value}, ${!!value}`); return !!value; }) .forEach(([override]) => { expect(transformedSource).toContain(override); }); }, ), ); }); }); describe("parse()", () => { bench("should never throw", async () => { await fc.assert( fc.asyncProperty( fc.string({ unit: "grapheme-composite" }), async (input) => { expect(await parse(input)).toBeTruthy(); }, ), ); }); });