Skip to content
Source
Sign inSign up

word-images.browser.test.ts

24 lines983 Bytes

1import { expect, it } from "vite-plus/test";
2import { createBitmapImageResolver } from "./word-images";
3​
4it("asks the window to rasterize SVG pictures at their drawn size, and draws the bitmaps it returns", async () => {
5 const svg = new TextEncoder().encode(
6 '<svg xmlns="http://www.w3.org/2000/svg" width="10" height="10"><rect width="10" height="10"/></svg>',
7 );
8 const images = createBitmapImageResolver(() => ({ bytes: svg, mimeType: "image/svg+xml" }));
9 const sizes = new Map([["media:1", { width: 40, height: 20 }]]);
10​
11 expect(await images.resolve("media:1")).toBeNull();
12​
13 const [request, ...rest] = images.vectors(sizes);
14​
15 expect(rest).toEqual([]);
16 expect(request).toMatchObject({ asset: "media:1", size: { width: 40, height: 20 } });
17​
18 const bitmap = await createImageBitmap(new ImageData(40, 20));
19​
20 images.provide([{ asset: "media:1", bitmap }]);
21​
22 expect(await images.resolve("media:1")).toBe(bitmap);
23 expect(images.vectors(sizes)).toEqual([]);
24});