diff options
Diffstat (limited to 'test/test.js')
| -rw-r--r-- | test/test.js | 139 |
1 files changed, 139 insertions, 0 deletions
diff --git a/test/test.js b/test/test.js new file mode 100644 index 0000000..e6b92cc --- /dev/null +++ b/test/test.js @@ -0,0 +1,139 @@ +import { describe, it } from "node:test"; +import assert from "node:assert"; +import { createCanvas } from "canvas"; +import { parse } from "wmap-parser"; +import { renderToCanvas, renderToImage } from "../src/index.js"; +import { StageType } from "../src/stage_type.js"; +import fs from "fs"; + +const wmapSource = fs.readFileSync("./test/example.wmap", "utf-8"); + +describe("wmap-renderer-canvas", () => { + describe("renderToCanvas", () => { + it("should render a map to canvas", async () => { + const map = parse(wmapSource); + const canvas = createCanvas(1384, 1084); + + await renderToCanvas(map, canvas); + + assert.strictEqual(canvas.width, 1384); + assert.strictEqual(canvas.height, 1084); + }); + + it("should throw error without map", async () => { + const canvas = createCanvas(1384, 1084); + + await assert.rejects( + async () => await renderToCanvas(null, canvas), + { message: "Map object is required" }, + ); + }); + + it("should throw error without canvas", async () => { + const map = parse(wmapSource); + + await assert.rejects(async () => await renderToCanvas(map, null), { + message: "Canvas is required", + }); + }); + + it("should render without background when option is set", async () => { + const map = parse(wmapSource); + const canvas = createCanvas(1384, 1084); + + await renderToCanvas(map, canvas, StageType.ACTIVITIES, { + showBackground: false, + }); + + assert.strictEqual(canvas.width, 1384); + assert.strictEqual(canvas.height, 1084); + }); + }); + + describe("renderToImage", () => { + it("should render a map to PNG stream", async () => { + const map = parse(wmapSource); + const stream = await renderToImage(map, "png"); + + assert.ok(stream); + + const out = fs.createWriteStream("test-output.png"); + stream.pipe(out); + + await new Promise((resolve, reject) => { + out.on("finish", resolve); + out.on("error", reject); + }); + }); + + it("should render a map to JPEG stream", async () => { + const map = parse(wmapSource); + const stream = await renderToImage(map, "jpeg"); + + assert.ok(stream); + + const out = fs.createWriteStream("test-output.jpeg"); + stream.pipe(out); + + await new Promise((resolve, reject) => { + out.on("finish", resolve); + out.on("error", reject); + }); + }); + + it("should render a map to PDF stream", async () => { + const map = parse(wmapSource); + const stream = await renderToImage(map, "pdf"); + + assert.ok(stream); + + const out = fs.createWriteStream("test-output.pdf"); + stream.pipe(out); + + await new Promise((resolve, reject) => { + out.on("finish", resolve); + out.on("error", reject); + }); + }); + + it("should render without background when option is set", async () => { + const map = parse(wmapSource); + const stream = await renderToImage( + map, + "png", + StageType.ACTIVITIES, + { + showBackground: false, + }, + ); + + assert.ok(stream); + + const out = fs.createWriteStream("test-output-no-bg.png"); + stream.pipe(out); + + await new Promise((resolve, reject) => { + out.on("finish", resolve); + out.on("error", reject); + }); + }); + + it("should throw error for invalid image type", async () => { + const map = parse(wmapSource); + + await assert.rejects( + async () => await renderToImage(map, "invalid"), + { + message: + "Invalid image type. Must be one of: png, jpeg, pdf", + }, + ); + }); + + it("should throw error without map", async () => { + await assert.rejects(async () => await renderToImage(null, "png"), { + message: "Map object is required", + }); + }); + }); +}); |