Skip to content

Commit

Permalink
Merge pull request #511 from Fdawgs/test/coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Fdawgs committed Jun 24, 2023
2 parents f9e1b60 + b21db44 commit abfe676
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 32 deletions.
8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@
"text",
"lcovonly"
],
"coverageThreshold": {
"global": {
"branches": 90,
"functions": 100,
"lines": 90,
"statements": 90
}
},
"testEnvironment": "node",
"testTimeout": 60000
},
Expand Down
94 changes: 62 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,44 @@ 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.`
);
}

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();
} 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 Expand Up @@ -905,6 +916,25 @@ describe("Node-Poppler module", () => {
).resolves.toBeUndefined();
});

it.each([true, false])(
"Converts PDF file to HTML file with ignoreImages option set to %s",
async (ignoreImages) => {
const poppler = new Poppler(testBinaryPath);
const options = {
firstPageToConvert: 1,
lastPageToConvert: 2,
ignoreImages,
};

const res = await poppler.pdfToHtml(file, undefined, options);

expect(res).toMatch("Page-2");
await expect(
fs.access(`${testDirectory}pdf_1.3_NHS_Constitution.html`)
).resolves.toBeUndefined();
}
);

it("Accepts options and only process 2 pages of PDF file", async () => {
const poppler = new Poppler(testBinaryPath);
const options = {
Expand Down

0 comments on commit abfe676

Please sign in to comment.