Skip to content

Commit

Permalink
feat: update unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cherfia committed Nov 4, 2023
1 parent db8cbf2 commit cf9336e
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 20 deletions.
49 changes: 46 additions & 3 deletions src/chromium/converters/tests/html.converter.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { promises, createReadStream } from "fs";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { createReadStream, promises } from "fs";

import fetch from "node-fetch";
import FormData from "form-data";
Expand Down Expand Up @@ -51,7 +52,7 @@ describe("HtmlConverter", () => {
html: "path/to/index.html",
pdfFormat: PdfFormat.A_1a,
});
expect(mockFormDataAppend).toBeCalledTimes(2);
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
expect(buffer).toEqual(Buffer.from("content"));
});
});
Expand All @@ -64,7 +65,49 @@ describe("HtmlConverter", () => {
html: "path/to/index.html",
properties: { size: { width: 8.3, height: 11.7 } },
});
expect(mockFormDataAppend).toBeCalledTimes(3);
expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
expect(buffer).toEqual(Buffer.from("content"));
});
});

describe("when header parameter is passed", () => {
it("should return a buffer", async () => {
mockPromisesAccess.mockResolvedValue();
mockFetch.mockResolvedValue(new Response("content"));
const buffer = await converter.convert({
html: "path/to/index.html",
header: "path/to/header.html",
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
expect(buffer).toEqual(Buffer.from("content"));
});
});

describe("when footer parameter is passed", () => {
it("should return a buffer", async () => {
mockPromisesAccess.mockResolvedValue();
mockFetch.mockResolvedValue(new Response("content"));
const buffer = await converter.convert({
html: "path/to/index.html",
footer: "path/to/footer.html",
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
expect(buffer).toEqual(Buffer.from("content"));
});
});

describe("when all parameters are passed", () => {
it("should return a buffer", async () => {
mockPromisesAccess.mockResolvedValue();
mockFetch.mockResolvedValue(new Response("content"));
const buffer = await converter.convert({
html: "path/to/index.html",
header: "path/to/header.html",
footer: "path/to/footer.html",
pdfFormat: PdfFormat.A_1a,
properties: { size: { width: 8.3, height: 11.7 } },
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(6);
expect(buffer).toEqual(Buffer.from("content"));
});
});
Expand Down
53 changes: 50 additions & 3 deletions src/chromium/converters/tests/markdown.converter.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import FormData from "form-data";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { createReadStream, promises } from "fs";

import FormData from "form-data";
import fetch from "node-fetch";

import { PdfFormat } from "../../../common";
Expand Down Expand Up @@ -54,7 +56,7 @@ describe("MarkdownConverter", () => {
markdown: "path/to/file.md",
pdfFormat: PdfFormat.A_2b,
});
expect(mockFormDataAppend).toBeCalledTimes(3);
expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
expect(buffer).toEqual(Buffer.from("content"));
});
});
Expand All @@ -68,7 +70,52 @@ describe("MarkdownConverter", () => {
markdown: "path/to/file.md",
properties: { size: { width: 8.3, height: 11.7 } },
});
expect(mockFormDataAppend).toBeCalledTimes(4);
expect(mockFormDataAppend).toHaveBeenCalledTimes(4);
expect(buffer).toEqual(Buffer.from("content"));
});
});

describe("when header parameter is passed", () => {
it("should return a buffer", async () => {
mockPromisesAccess.mockResolvedValue();
mockFetch.mockResolvedValue(new Response("content"));
const buffer = await converter.convert({
html: "path/to/index.html",
markdown: "path/to/file.md",
header: "path/to/header.html",
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
expect(buffer).toEqual(Buffer.from("content"));
});
});

describe("when footer parameter is passed", () => {
it("should return a buffer", async () => {
mockPromisesAccess.mockResolvedValue();
mockFetch.mockResolvedValue(new Response("content"));
const buffer = await converter.convert({
html: "path/to/index.html",
markdown: "path/to/file.md",
footer: "path/to/footer.html",
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
expect(buffer).toEqual(Buffer.from("content"));
});
});

describe("when all parameters are passed", () => {
it("should return a buffer", async () => {
mockPromisesAccess.mockResolvedValue();
mockFetch.mockResolvedValue(new Response("content"));
const buffer = await converter.convert({
html: "path/to/index.html",
markdown: "path/to/file.md",
header: "path/to/header.html",
footer: "path/to/footer.html",
pdfFormat: PdfFormat.A_1a,
properties: { size: { width: 8.3, height: 11.7 } },
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(7);
expect(buffer).toEqual(Buffer.from("content"));
});
});
Expand Down
57 changes: 55 additions & 2 deletions src/chromium/converters/tests/url.converter.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { createReadStream, promises } from "fs";
import FormData from "form-data";
import fetch from "node-fetch";

Expand All @@ -10,9 +12,16 @@ jest.mock("node-fetch", () => jest.fn());
describe("HtmlConverter", () => {
const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
const mockPromisesAccess = jest.spyOn(promises, "access");

const converter = new UrlConverter();

beforeEach(() => {
(createReadStream as jest.Mock) = jest
.fn()
.mockImplementation((file) => file);
});

afterEach(() => {
jest.resetAllMocks();
});
Expand All @@ -36,14 +45,42 @@ describe("HtmlConverter", () => {
});
});

describe("when header parameter is passed", () => {
it("should return a buffer", async () => {
mockFetch.mockResolvedValueOnce(new Response("content"));
mockPromisesAccess.mockResolvedValue();
mockFetch.mockResolvedValue(new Response("content"));
const buffer = await converter.convert({
url: "http://www.example.com/",
header: "path/to/header.html",
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
expect(buffer).toEqual(Buffer.from("content"));
});
});

describe("when footer parameter is passed", () => {
it("should return a buffer", async () => {
mockFetch.mockResolvedValueOnce(new Response("content"));
mockPromisesAccess.mockResolvedValue();
mockFetch.mockResolvedValue(new Response("content"));
const buffer = await converter.convert({
url: "http://www.example.com/",
footer: "path/to/footer.html",
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
expect(buffer).toEqual(Buffer.from("content"));
});
});

describe("when pdf format parameter is passed", () => {
it("should return a buffer", async () => {
mockFetch.mockResolvedValueOnce(new Response("content"));
const buffer = await converter.convert({
url: "http://www.example.com/",
pdfFormat: PdfFormat.A_3b,
});
expect(mockFormDataAppend).toBeCalledTimes(2);
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
expect(buffer).toEqual(Buffer.from("content"));
});
});
Expand All @@ -55,7 +92,23 @@ describe("HtmlConverter", () => {
url: "http://www.example.com/",
properties: { size: { width: 8.3, height: 11.7 } },
});
expect(mockFormDataAppend).toBeCalledTimes(3);
expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
expect(buffer).toEqual(Buffer.from("content"));
});
});

describe("when all parameters are passed", () => {
it("should return a buffer", async () => {
mockPromisesAccess.mockResolvedValue();
mockFetch.mockResolvedValue(new Response("content"));
const buffer = await converter.convert({
url: "http://www.example.com/",
header: "path/to/header.html",
footer: "path/to/footer.html",
pdfFormat: PdfFormat.A_1a,
properties: { size: { width: 8.3, height: 11.7 } },
});
expect(mockFormDataAppend).toHaveBeenCalledTimes(6);
expect(buffer).toEqual(Buffer.from("content"));
});
});
Expand Down
9 changes: 5 additions & 4 deletions src/libre-office/utils/tests/libre-office.utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LibreOfficeUtils } from "../libre-office.utils";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { promises, createReadStream } from "fs";
import { LibreOfficeUtils } from "../libre-office.utils";

import FormData from "form-data";

Expand All @@ -26,7 +27,7 @@ describe("LibreOfficeUtils", () => {
["path/to/file.docx", "path/to/file.bib"],
data
);
expect(mockFormDataAppend).toBeCalledTimes(2);
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
});
});

Expand Down Expand Up @@ -64,7 +65,7 @@ describe("LibreOfficeUtils", () => {
LibreOfficeUtils.injectPageProperties(data, {
landscape: true,
});
expect(mockFormDataAppend).toBeCalledTimes(1);
expect(mockFormDataAppend).toHaveBeenCalledTimes(1);
expect(data.append).toHaveBeenCalledWith("landscape", "true");
});
});
Expand All @@ -76,7 +77,7 @@ describe("LibreOfficeUtils", () => {
LibreOfficeUtils.injectPageProperties(data, {
nativePageRanges: { from: 1, to: 6 },
});
expect(mockFormDataAppend).toBeCalledTimes(1);
expect(mockFormDataAppend).toHaveBeenCalledTimes(1);
expect(data.append).toHaveBeenCalledWith("nativePageRanges", "1-6");
});
});
Expand Down
9 changes: 5 additions & 4 deletions src/pdf-engines/tests/pdf.engine.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import FormData from "form-data";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { createReadStream, promises } from "fs";
import FormData from "form-data";
import fs from "fs/promises";
import fetch from "node-fetch";
import path from "path";
Expand Down Expand Up @@ -34,7 +35,7 @@ describe("PDFEngine", () => {
files: ["path/to/file.docx", "path/to/file.bib"],
});
expect(buffer).toEqual(Buffer.from("content"));
expect(mockFormDataAppend).toBeCalledTimes(2);
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
});
});

Expand All @@ -49,7 +50,7 @@ describe("PDFEngine", () => {
merge: true,
});
expect(buffer).toEqual(Buffer.from("content"));
expect(mockFormDataAppend).toBeCalledTimes(5);
expect(mockFormDataAppend).toHaveBeenCalledTimes(5);
});
});
});
Expand All @@ -62,7 +63,7 @@ describe("PDFEngine", () => {
files: ["path/to/file.pdf", "path/to/another-file.pdf"],
});
expect(buffer).toEqual(Buffer.from("content"));
expect(mockFormDataAppend).toBeCalledTimes(2);
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
});
});

Expand Down
9 changes: 5 additions & 4 deletions src/pdf-engines/utils/tests/engine.utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { PDFEngineUtils } from "./../engine.utils";
import { promises, createReadStream } from "fs";

// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { createReadStream, promises } from "fs";
import FormData from "form-data";

import { PDFEngineUtils } from "./../engine.utils";

describe("PDFEngineUtils", () => {
const mockPromisesAccess = jest.spyOn(promises, "access");
const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
Expand All @@ -26,7 +27,7 @@ describe("PDFEngineUtils", () => {
["path/to/file.pdf", "path/to/another-file.pdf"],
data
);
expect(mockFormDataAppend).toBeCalledTimes(2);
expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
});
});

Expand Down

0 comments on commit cf9336e

Please sign in to comment.