template-project/node_modules/@jimp/plugin-contain/src/index.test.ts
2025-05-30 18:13:30 +08:00

76 lines
2.3 KiB
TypeScript

import { expect, test, describe } from "vitest";
import { makeTestImage } from "@jimp/test-utils";
import { HorizontalAlign, VerticalAlign, createJimp } from "@jimp/core";
import { methods } from "./index.js";
const jimp = createJimp({ plugins: [methods] });
describe("All align combinations for contain", () => {
const vertical = jimp.fromBitmap(
makeTestImage(
"▴▴▴▴▸▸▸▸",
"▴▴▴▴▸▸▸▸",
"▴▴▴▴▸▸▸▸",
"▴▴▴▴▸▸▸▸",
"▴▴▴▴▸▸▸▸",
"▴▴▴▴▸▸▸▸",
"▾▾▾▾◆◆◆◆",
"▾▾▾▾◆◆◆◆",
"▾▾▾▾◆◆◆◆",
"▾▾▾▾◆◆◆◆",
"▾▾▾▾◆◆◆◆",
"▾▾▾▾◆◆◆◆",
),
);
// stores the Jimp instances of the JGD images above.
const horizontal = jimp.fromBitmap(
makeTestImage(
"▴▴▴▴▴▴▸▸▸▸▸▸",
"▴▴▴▴▴▴▸▸▸▸▸▸",
"▴▴▴▴▴▴▸▸▸▸▸▸",
"▴▴▴▴▴▴▸▸▸▸▸▸",
"▾▾▾▾▾▾◆◆◆◆◆◆",
"▾▾▾▾▾▾◆◆◆◆◆◆",
"▾▾▾▾▾▾◆◆◆◆◆◆",
"▾▾▾▾▾▾◆◆◆◆◆◆",
),
);
const tests: Array<
[keyof typeof HorizontalAlign, keyof typeof VerticalAlign]
> = [
["LEFT", "TOP"],
["CENTER", "TOP"],
["RIGHT", "TOP"],
["LEFT", "MIDDLE"],
["CENTER", "MIDDLE"],
["RIGHT", "MIDDLE"],
["LEFT", "BOTTOM"],
["CENTER", "BOTTOM"],
["RIGHT", "BOTTOM"],
];
tests.forEach(([horizontalAlign, verticalAlign]) => {
const align = horizontalAlign + " " + verticalAlign;
const alignValue =
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(HorizontalAlign as any)[horizontalAlign] |
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(VerticalAlign as any)[verticalAlign];
test("vertical contain aligned to " + align, () => {
expect(
vertical.clone().contain({ w: 6, h: 6, align: alignValue }),
).toMatchSnapshot();
});
test("horizontal contain aligned to " + align, () => {
expect(
horizontal.clone().contain({ w: 6, h: 6, align: alignValue }),
).toMatchSnapshot();
});
});
});