Skip to content

Commit

Permalink
test(index): improve constructor coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Jun 23, 2023
1 parent f9e1b60 commit 155fb3d
Showing 1 changed file with 44 additions and 32 deletions.
76 changes: 44 additions & 32 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,14 @@ const { Poppler } = require("./index");
const testDirectory = `${__dirname}/../test_files/`;
const file = `${testDirectory}pdf_1.3_NHS_Constitution.pdf`;

const windowsPath = path.joinSafe(
__dirname,
"lib",
"win32",
"poppler-23.05.0",
"Library",
"bin"
);
let testBinaryPath;
switch (process.platform) {
// macOS
Expand All @@ -26,14 +34,7 @@ switch (process.platform) {
// Windows OS
case "win32":
default:
testBinaryPath = path.joinSafe(
__dirname,
"lib",
"win32",
"poppler-23.05.0",
"Library",
"bin"
);
testBinaryPath = windowsPath;
break;
}

Expand All @@ -53,34 +54,45 @@ describe("Node-Poppler module", () => {
});

describe("Constructor", () => {
if (process.platform === "win32") {
it("Converts PDF file to SVG file without binary path set on win32, and use included binaries", async () => {
const poppler = new Poppler();
const options = {
svgFile: true,
};
const outputFile = `${testDirectory}pdf_1.3_NHS_Constitution.svg`;
let platform;

const res = await poppler.pdfToCairo(file, outputFile, options);
beforeEach(() => {
// Copy the process platform
({ platform } = process);
});

expect(res).toBe("No Error");
await expect(fs.access(outputFile)).resolves.toBeUndefined();
afterEach(() => {
// Restore the process platform
Object.defineProperty(process, "platform", {
value: platform,
});
});

it("Creates a new Poppler instance without the binary path set on win32", () => {
Object.defineProperty(process, "platform", {
value: "win32",
});
}

if (process.platform !== "win32") {
it(`Rejects with an Error object if binary path unset on ${process.platform}`, async () => {
expect.assertions(1);
try {
// eslint-disable-next-line no-unused-vars
const poppler = new Poppler();
} catch (err) {
expect(err.message).toBe(
`${process.platform} poppler-util binaries are not provided, please pass the installation directory as a parameter to the Poppler instance.`
);
}

// spy on the the Poppler class constructor and check this.popplerPath is set
const poppler = new Poppler();
expect(poppler.popplerPath).toBe(windowsPath);
});

it("Throws an Error if the binary path is not set and the platform is not win32", () => {
Object.defineProperty(process, "platform", {
value: "mockOS",
});
}

expect.assertions(1);
try {
// eslint-disable-next-line no-unused-vars
const poppler = new Poppler();

Check notice

Code scanning / CodeQL

Unused variable, import, function or class Note

Unused variable poppler.
} catch (err) {
expect(err.message).toBe(
`${process.platform} poppler-util binaries are not provided, please pass the installation directory as a parameter to the Poppler instance.`
);
}
});
});

describe("pdfAttach function", () => {
Expand Down

0 comments on commit 155fb3d

Please sign in to comment.