Skip to content

Commit

Permalink
chore: add tests for readBarcodes input
Browse files Browse the repository at this point in the history
  • Loading branch information
Sec-ant committed Feb 23, 2025
1 parent 395cd7b commit 5990d6f
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 2 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@
"@babel/types": "^7.26.9",
"@biomejs/biome": "1.9.4",
"@changesets/cli": "^2.28.1",
"@napi-rs/canvas": "^0.1.67",
"@types/babel__core": "^7.20.5",
"@types/node": "^22.13.5",
"@vitest/ui": "^3.0.6",
Expand Down
110 changes: 110 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added tests/samples/qrcode/wikipedia.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
70 changes: 68 additions & 2 deletions tests/unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
import { afterAll, describe, expect, test, vi } from "vitest";
import { readFile } from "node:fs/promises";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { createCanvas, loadImage } from "@napi-rs/canvas";
import { afterAll, beforeAll, describe, expect, test, vi } from "vitest";
import {
prepareZXingModule as prepareZXingFullModule,
purgeZXingModule as purgeZXingFullModule,
} from "../src/full/index.js";
import { prepareZXingModule as prepareZXingReaderModule } from "../src/reader/index.js";
import {
prepareZXingModule as prepareZXingReaderModule,
readBarcodes,
} from "../src/reader/index.js";

describe("prepare zxing module", () => {
const consoleMock = vi.spyOn(console, "error").mockImplementation(() => {});
Expand Down Expand Up @@ -178,3 +185,62 @@ describe("prepare zxing module", () => {
expect(modulePromise2).toBe(modulePromise3);
});
});

describe("readBarcodes input", async () => {
const arrayBuffer = await readFile(
fileURLToPath(new URL("./samples/qrcode/wikipedia.png", import.meta.url)),
);

beforeAll(async () => {
await prepareZXingReaderModule({
overrides: {
wasmBinary: (
await readFile(
resolve(import.meta.dirname, "../src/reader/zxing_reader.wasm"),
)
).buffer as ArrayBuffer,
},
fireImmediately: true,
});
});

test("readBarcodes accepts ArrayBuffer as input", async () => {
const readResult = await readBarcodes(arrayBuffer);
expect(readResult).length(1);
expect(readResult[0].text).toBe("http://en.m.wikipedia.org");
});

test("readBarcodes accepts Blob as input", async () => {
const blob = new Blob([arrayBuffer], { type: "image/png" });
const readResult = await readBarcodes(blob);
expect(readResult).length(1);
expect(readResult[0].text).toBe("http://en.m.wikipedia.org");
});

test("readBarcodes accepts File as input", async () => {
const file = new File([arrayBuffer], "wikipedia.png", {
type: "image/png",
});
const readResult = await readBarcodes(file);
expect(readResult).length(1);
expect(readResult[0].text).toBe("http://en.m.wikipedia.org");
});

test("readBarcodes accepts Uint8Array as input", async () => {
const uint8Array = new Uint8Array(arrayBuffer);
const readResult = await readBarcodes(uint8Array);
expect(readResult).length(1);
expect(readResult[0].text).toBe("http://en.m.wikipedia.org");
});

test("readBarcodes accepts ImageData as input", async () => {
const image = await loadImage(arrayBuffer);
const canvas = createCanvas(image.width, image.height);
const context = canvas.getContext("2d");
context.drawImage(image, 0, 0, image.width, image.height);
const imageData = context.getImageData(0, 0, image.width, image.height);
const readResult = await readBarcodes(imageData);
expect(readResult).length(1);
expect(readResult[0].text).toBe("http://en.m.wikipedia.org");
});
});

0 comments on commit 5990d6f

Please sign in to comment.