chromiumly 2.4.0 → 2.5.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (66) hide show
  1. package/dist/chromium/converters/converter.js.map +1 -1
  2. package/dist/chromium/converters/html.converter.d.ts +4 -6
  3. package/dist/chromium/converters/html.converter.js +3 -9
  4. package/dist/chromium/converters/html.converter.js.map +1 -1
  5. package/dist/chromium/converters/markdown.converter.d.ts +5 -7
  6. package/dist/chromium/converters/markdown.converter.js +3 -16
  7. package/dist/chromium/converters/markdown.converter.js.map +1 -1
  8. package/dist/chromium/converters/url.converter.d.ts +3 -5
  9. package/dist/chromium/converters/url.converter.js +1 -1
  10. package/dist/chromium/converters/url.converter.js.map +1 -1
  11. package/dist/chromium/index.js.map +1 -1
  12. package/dist/chromium/interfaces/converter.types.d.ts +3 -6
  13. package/dist/chromium/utils/converter.utils.d.ts +3 -1
  14. package/dist/chromium/utils/converter.utils.js +24 -21
  15. package/dist/chromium/utils/converter.utils.js.map +1 -1
  16. package/dist/common/constants.js.map +1 -1
  17. package/dist/common/gotenberg.utils.js.map +1 -1
  18. package/dist/common/index.d.ts +1 -0
  19. package/dist/common/index.js.map +1 -1
  20. package/dist/common/types.d.ts +3 -0
  21. package/dist/common/types.js +3 -0
  22. package/dist/common/types.js.map +1 -0
  23. package/dist/gotenberg.js.map +1 -1
  24. package/dist/libre-office/index.js.map +1 -1
  25. package/dist/libre-office/utils/constants.js.map +1 -1
  26. package/dist/libre-office/utils/libre-office.utils.d.ts +3 -4
  27. package/dist/libre-office/utils/libre-office.utils.js +18 -9
  28. package/dist/libre-office/utils/libre-office.utils.js.map +1 -1
  29. package/dist/main.config.js.map +1 -1
  30. package/dist/main.js.map +1 -1
  31. package/dist/pdf-engines/index.js.map +1 -1
  32. package/dist/pdf-engines/pdf.engine.js +3 -3
  33. package/dist/pdf-engines/pdf.engine.js.map +1 -1
  34. package/dist/pdf-engines/utils/engine.utils.d.ts +2 -3
  35. package/dist/pdf-engines/utils/engine.utils.js +17 -8
  36. package/dist/pdf-engines/utils/engine.utils.js.map +1 -1
  37. package/package.json +7 -7
  38. package/src/chromium/converters/converter.ts +5 -5
  39. package/src/chromium/converters/html.converter.ts +54 -60
  40. package/src/chromium/converters/markdown.converter.ts +56 -68
  41. package/src/chromium/converters/tests/html.converter.test.ts +119 -135
  42. package/src/chromium/converters/tests/markdown.converter.test.ts +131 -150
  43. package/src/chromium/converters/tests/url.converter.test.ts +114 -123
  44. package/src/chromium/converters/url.converter.ts +55 -57
  45. package/src/chromium/index.ts +3 -3
  46. package/src/chromium/interfaces/converter.types.ts +27 -27
  47. package/src/chromium/utils/converter.utils.ts +141 -139
  48. package/src/chromium/utils/tests/converter.utils.test.ts +312 -311
  49. package/src/common/constants.ts +3 -3
  50. package/src/common/gotenberg.utils.ts +16 -16
  51. package/src/common/index.ts +3 -2
  52. package/src/common/tests/gotenberg.utils.test.ts +54 -54
  53. package/src/common/types.ts +3 -0
  54. package/src/gotenberg.ts +4 -4
  55. package/src/libre-office/index.ts +2 -2
  56. package/src/libre-office/interfaces/libre-office.types.ts +2 -2
  57. package/src/libre-office/utils/constants.ts +76 -76
  58. package/src/libre-office/utils/libre-office.utils.ts +42 -35
  59. package/src/libre-office/utils/tests/libre-office.utils.test.ts +81 -69
  60. package/src/main.config.ts +22 -21
  61. package/src/main.ts +3 -3
  62. package/src/pdf-engines/index.ts +1 -1
  63. package/src/pdf-engines/pdf.engine.ts +49 -49
  64. package/src/pdf-engines/tests/pdf.engine.test.ts +94 -94
  65. package/src/pdf-engines/utils/engine.utils.ts +20 -12
  66. package/src/pdf-engines/utils/tests/engine.utils.test.ts +60 -48
@@ -1,36 +1,37 @@
1
- import { Gotenberg } from "./gotenberg";
1
+ import {Gotenberg} from "./gotenberg";
2
2
 
3
3
  export enum ChromiumRoute {
4
- URL = "url",
5
- HTML = "html",
6
- MARKDOWN = "markdown",
4
+ URL = "url",
5
+ HTML = "html",
6
+ MARKDOWN = "markdown",
7
7
  }
8
8
 
9
9
  enum PdfEngineRoute {
10
- MERGE = "merge",
10
+ MERGE = "merge",
11
11
  }
12
12
 
13
13
  enum LibreOfficeRoute {
14
- CONVERT = "convert",
14
+ CONVERT = "convert",
15
15
  }
16
+
16
17
  export class Chromiumly {
17
- public static readonly GOTENBERG_ENDPOINT = Gotenberg.endpoint;
18
+ public static readonly GOTENBERG_ENDPOINT = Gotenberg.endpoint;
18
19
 
19
- public static readonly CHROMIUM_PATH = "forms/chromium/convert";
20
- public static readonly PDF_ENGINES_PATH = "forms/pdfengines";
21
- public static readonly LIBRE_OFFICE_PATH = "forms/libreoffice";
20
+ public static readonly CHROMIUM_PATH = "forms/chromium/convert";
21
+ public static readonly PDF_ENGINES_PATH = "forms/pdfengines";
22
+ public static readonly LIBRE_OFFICE_PATH = "forms/libreoffice";
22
23
 
23
- public static readonly CHROMIUM_ROUTES = {
24
- url: ChromiumRoute.URL,
25
- html: ChromiumRoute.HTML,
26
- markdown: ChromiumRoute.MARKDOWN,
27
- };
24
+ public static readonly CHROMIUM_ROUTES = {
25
+ url: ChromiumRoute.URL,
26
+ html: ChromiumRoute.HTML,
27
+ markdown: ChromiumRoute.MARKDOWN,
28
+ };
28
29
 
29
- public static readonly PDF_ENGINE_ROUTES = {
30
- merge: PdfEngineRoute.MERGE,
31
- };
30
+ public static readonly PDF_ENGINE_ROUTES = {
31
+ merge: PdfEngineRoute.MERGE,
32
+ };
32
33
 
33
- public static readonly LIBRE_OFFICE_ROUTES = {
34
- convert: LibreOfficeRoute.CONVERT,
35
- };
34
+ public static readonly LIBRE_OFFICE_ROUTES = {
35
+ convert: LibreOfficeRoute.CONVERT,
36
+ };
36
37
  }
package/src/main.ts CHANGED
@@ -1,3 +1,3 @@
1
- export { PdfFormat } from "./common/constants";
2
- export { HtmlConverter, MarkdownConverter, UrlConverter } from "./chromium";
3
- export { PDFEngine } from "./pdf-engines";
1
+ export {PdfFormat} from "./common/constants";
2
+ export {HtmlConverter, MarkdownConverter, UrlConverter} from "./chromium";
3
+ export {PDFEngine} from "./pdf-engines";
@@ -1 +1 @@
1
- export { PDFEngine } from "./pdf.engine";
1
+ export {PDFEngine} from "./pdf.engine";
@@ -1,65 +1,65 @@
1
- import { PathLike, promises } from "fs";
1
+ import {PathLike, promises} from "fs";
2
2
  import path from "path";
3
3
 
4
4
  import FormData from "form-data";
5
5
 
6
- import { Chromiumly } from "../main.config";
7
- import { GotenbergUtils, PdfFormat } from "../common";
8
- import { LibreOfficeUtils, PageProperties } from "../libre-office";
9
- import { PDFEngineUtils } from "./utils/engine.utils";
6
+ import {Chromiumly} from "../main.config";
7
+ import {GotenbergUtils, PdfFormat} from "../common";
8
+ import {LibreOfficeUtils, PageProperties} from "../libre-office";
9
+ import {PDFEngineUtils} from "./utils/engine.utils";
10
10
 
11
11
  export class PDFEngine {
12
- public static async merge({ files }: { files: PathLike[] }): Promise<Buffer> {
13
- const data = new FormData();
14
- await PDFEngineUtils.injectFiles(files, data);
15
- const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.PDF_ENGINES_PATH}/${Chromiumly.PDF_ENGINE_ROUTES.merge}`;
16
- return GotenbergUtils.fetch(endpoint, data);
17
- }
12
+ public static async merge({files}: { files: PathLike[] }): Promise<Buffer> {
13
+ const data = new FormData();
14
+ await PDFEngineUtils.addFiles(files, data);
15
+ const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.PDF_ENGINES_PATH}/${Chromiumly.PDF_ENGINE_ROUTES.merge}`;
16
+ return GotenbergUtils.fetch(endpoint, data);
17
+ }
18
18
 
19
- public static async convert({
20
- files,
21
- properties,
22
- pdfFormat,
23
- pdfUA,
24
- merge,
25
- }: {
26
- files: PathLike[];
27
- properties?: PageProperties;
28
- pdfFormat?: PdfFormat;
29
- pdfUA?: boolean;
30
- merge?: boolean;
31
- }): Promise<Buffer> {
32
- const data = new FormData();
19
+ public static async convert({
20
+ files,
21
+ properties,
22
+ pdfFormat,
23
+ pdfUA,
24
+ merge,
25
+ }: {
26
+ files: PathLike[];
27
+ properties?: PageProperties;
28
+ pdfFormat?: PdfFormat;
29
+ pdfUA?: boolean;
30
+ merge?: boolean;
31
+ }): Promise<Buffer> {
32
+ const data = new FormData();
33
33
 
34
- if (pdfFormat) {
35
- data.append("pdfa", pdfFormat);
36
- }
34
+ if (pdfFormat) {
35
+ data.append("pdfa", pdfFormat);
36
+ }
37
37
 
38
- if (pdfUA) {
39
- data.append("pdfUA", String(pdfUA));
40
- }
38
+ if (pdfUA) {
39
+ data.append("pdfUA", String(pdfUA));
40
+ }
41
41
 
42
- if (merge) {
43
- data.append("merge", String(merge));
44
- }
42
+ if (merge) {
43
+ data.append("merge", String(merge));
44
+ }
45
45
 
46
- if (properties) {
47
- LibreOfficeUtils.injectPageProperties(data, properties);
48
- }
46
+ if (properties) {
47
+ LibreOfficeUtils.addPageProperties(data, properties);
48
+ }
49
49
 
50
- await LibreOfficeUtils.injectFiles(files, data);
50
+ await LibreOfficeUtils.addFiles(files, data);
51
51
 
52
- const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.LIBRE_OFFICE_PATH}/${Chromiumly.LIBRE_OFFICE_ROUTES.convert}`;
52
+ const endpoint = `${Chromiumly.GOTENBERG_ENDPOINT}/${Chromiumly.LIBRE_OFFICE_PATH}/${Chromiumly.LIBRE_OFFICE_ROUTES.convert}`;
53
53
 
54
- return GotenbergUtils.fetch(endpoint, data);
55
- }
54
+ return GotenbergUtils.fetch(endpoint, data);
55
+ }
56
56
 
57
- public static async generate(
58
- filename: string,
59
- buffer: Buffer
60
- ): Promise<void> {
61
- const __generated__ = path.resolve(process.cwd(), "__generated__");
62
- await promises.mkdir(path.resolve(__generated__), { recursive: true });
63
- await promises.writeFile(path.resolve(__generated__, filename), buffer);
64
- }
57
+ public static async generate(
58
+ filename: string,
59
+ buffer: Buffer
60
+ ): Promise<void> {
61
+ const __generated__ = path.resolve(process.cwd(), "__generated__");
62
+ await promises.mkdir(path.resolve(__generated__), {recursive: true});
63
+ await promises.writeFile(path.resolve(__generated__, filename), buffer);
64
+ }
65
65
  }
@@ -1,123 +1,123 @@
1
1
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
- import { createReadStream, promises } from "fs";
2
+ import {createReadStream, promises} from "fs";
3
3
  import FormData from "form-data";
4
4
  import fs from "fs/promises";
5
5
  import fetch from "node-fetch";
6
6
  import path from "path";
7
7
 
8
- import { PdfFormat } from "../../common";
9
- import { PDFEngine } from "../pdf.engine";
8
+ import {PdfFormat} from "../../common";
9
+ import {PDFEngine} from "../pdf.engine";
10
10
 
11
- const { Response } = jest.requireActual("node-fetch");
11
+ const {Response} = jest.requireActual("node-fetch");
12
12
  jest.mock("node-fetch", () => jest.fn());
13
13
 
14
14
  describe("PDFEngine", () => {
15
- const mockPromisesAccess = jest.spyOn(promises, "access");
16
- const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
17
- const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
18
-
19
- beforeEach(() => {
20
- (createReadStream as jest.Mock) = jest
21
- .fn()
22
- .mockImplementation((file) => file);
23
- });
24
-
25
- afterEach(() => {
26
- jest.resetAllMocks();
27
- });
28
-
29
- describe("convert", () => {
30
- describe("when no properties are passed", () => {
31
- it("should return a buffer", async () => {
32
- mockPromisesAccess.mockResolvedValue();
33
- mockFetch.mockResolvedValue(new Response("content"));
34
- const buffer = await PDFEngine.convert({
35
- files: ["path/to/file.docx", "path/to/file.bib"],
36
- });
37
- expect(buffer).toEqual(Buffer.from("content"));
38
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
39
- });
15
+ const mockPromisesAccess = jest.spyOn(promises, "access");
16
+ const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
17
+ const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
18
+
19
+ beforeEach(() => {
20
+ (createReadStream as jest.Mock) = jest
21
+ .fn()
22
+ .mockImplementation((file) => file);
23
+ });
24
+
25
+ afterEach(() => {
26
+ jest.resetAllMocks();
40
27
  });
41
28
 
42
- describe("when properties are passed", () => {
43
- it("should return a buffer", async () => {
44
- mockPromisesAccess.mockResolvedValue();
45
- mockFetch.mockResolvedValue(new Response("content"));
46
- const buffer = await PDFEngine.convert({
47
- files: ["path/to/file.docx", "path/to/file.bib"],
48
- properties: { landscape: true },
49
- pdfFormat: PdfFormat.A_1a,
50
- pdfUA: true,
51
- merge: true,
29
+ describe("convert", () => {
30
+ describe("when no properties are passed", () => {
31
+ it("should return a buffer", async () => {
32
+ mockPromisesAccess.mockResolvedValue();
33
+ mockFetch.mockResolvedValue(new Response("content"));
34
+ const buffer = await PDFEngine.convert({
35
+ files: ["path/to/file.docx", "path/to/file.bib"],
36
+ });
37
+ expect(buffer).toEqual(Buffer.from("content"));
38
+ expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
39
+ });
40
+ });
41
+
42
+ describe("when properties are passed", () => {
43
+ it("should return a buffer", async () => {
44
+ mockPromisesAccess.mockResolvedValue();
45
+ mockFetch.mockResolvedValue(new Response("content"));
46
+ const buffer = await PDFEngine.convert({
47
+ files: ["path/to/file.docx", "path/to/file.bib"],
48
+ properties: {landscape: true},
49
+ pdfFormat: PdfFormat.A_1a,
50
+ pdfUA: true,
51
+ merge: true,
52
+ });
53
+ expect(buffer).toEqual(Buffer.from("content"));
54
+ expect(mockFormDataAppend).toHaveBeenCalledTimes(6);
55
+ });
52
56
  });
53
- expect(buffer).toEqual(Buffer.from("content"));
54
- expect(mockFormDataAppend).toHaveBeenCalledTimes(6);
55
- });
56
57
  });
57
- });
58
-
59
- describe("merge", () => {
60
- it("should return a buffer", async () => {
61
- mockPromisesAccess.mockResolvedValue();
62
- mockFetch.mockResolvedValue(new Response("content"));
63
- const buffer = await PDFEngine.merge({
64
- files: ["path/to/file.pdf", "path/to/another-file.pdf"],
65
- });
66
- expect(buffer).toEqual(Buffer.from("content"));
67
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
58
+
59
+ describe("merge", () => {
60
+ it("should return a buffer", async () => {
61
+ mockPromisesAccess.mockResolvedValue();
62
+ mockFetch.mockResolvedValue(new Response("content"));
63
+ const buffer = await PDFEngine.merge({
64
+ files: ["path/to/file.pdf", "path/to/another-file.pdf"],
65
+ });
66
+ expect(buffer).toEqual(Buffer.from("content"));
67
+ expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
68
+ });
68
69
  });
69
- });
70
70
 
71
- describe("generate", () => {
72
- const mockFilename = "test.pdf";
73
- const mockBuffer = Buffer.from("mock pdf content");
71
+ describe("generate", () => {
72
+ const mockFilename = "test.pdf";
73
+ const mockBuffer = Buffer.from("mock pdf content");
74
74
 
75
- afterAll(() => {
76
- jest.restoreAllMocks();
77
- });
75
+ afterAll(() => {
76
+ jest.restoreAllMocks();
77
+ });
78
78
 
79
- it("should generate a PDF file", async () => {
80
- const mockGeneratedDir = path.join(process.cwd(), "__generated__");
81
- const mockGeneratedFilePath = path.join(mockGeneratedDir, mockFilename);
79
+ it("should generate a PDF file", async () => {
80
+ const mockGeneratedDir = path.join(process.cwd(), "__generated__");
81
+ const mockGeneratedFilePath = path.join(mockGeneratedDir, mockFilename);
82
82
 
83
- const mockPromisesMkDir = jest
84
- .spyOn(fs, "mkdir")
85
- .mockResolvedValueOnce(mockGeneratedDir);
83
+ const mockPromisesMkDir = jest
84
+ .spyOn(fs, "mkdir")
85
+ .mockResolvedValueOnce(mockGeneratedDir);
86
86
 
87
- const mockPromisesWriteFile = jest
88
- .spyOn(fs, "writeFile")
89
- .mockResolvedValueOnce();
87
+ const mockPromisesWriteFile = jest
88
+ .spyOn(fs, "writeFile")
89
+ .mockResolvedValueOnce();
90
90
 
91
- await PDFEngine.generate(mockFilename, mockBuffer);
91
+ await PDFEngine.generate(mockFilename, mockBuffer);
92
92
 
93
- expect(mockPromisesMkDir).toHaveBeenCalledWith(mockGeneratedDir, {
94
- recursive: true,
95
- });
93
+ expect(mockPromisesMkDir).toHaveBeenCalledWith(mockGeneratedDir, {
94
+ recursive: true,
95
+ });
96
96
 
97
- expect(mockPromisesWriteFile).toHaveBeenCalledWith(
98
- mockGeneratedFilePath,
99
- mockBuffer
100
- );
101
- });
97
+ expect(mockPromisesWriteFile).toHaveBeenCalledWith(
98
+ mockGeneratedFilePath,
99
+ mockBuffer
100
+ );
101
+ });
102
102
 
103
- it("should handle errors during file generation", async () => {
104
- jest
105
- .spyOn(fs, "mkdir")
106
- .mockRejectedValueOnce(new Error("Cannot create directory"));
103
+ it("should handle errors during file generation", async () => {
104
+ jest
105
+ .spyOn(fs, "mkdir")
106
+ .mockRejectedValueOnce(new Error("Cannot create directory"));
107
107
 
108
- await expect(
109
- PDFEngine.generate(mockFilename, mockBuffer)
110
- ).rejects.toThrow("Cannot create directory");
111
- });
108
+ await expect(
109
+ PDFEngine.generate(mockFilename, mockBuffer)
110
+ ).rejects.toThrow("Cannot create directory");
111
+ });
112
112
 
113
- it("should handle errors during file writing", async () => {
114
- jest
115
- .spyOn(fs, "writeFile")
116
- .mockRejectedValueOnce(new Error("Failed to write to file"));
113
+ it("should handle errors during file writing", async () => {
114
+ jest
115
+ .spyOn(fs, "writeFile")
116
+ .mockRejectedValueOnce(new Error("Failed to write to file"));
117
117
 
118
- await expect(
119
- PDFEngine.generate(mockFilename, mockBuffer)
120
- ).rejects.toThrow("Failed to write to file");
118
+ await expect(
119
+ PDFEngine.generate(mockFilename, mockBuffer)
120
+ ).rejects.toThrow("Failed to write to file");
121
+ });
121
122
  });
122
- });
123
123
  });
@@ -1,19 +1,27 @@
1
- import { constants, createReadStream, PathLike, promises } from "fs";
1
+ import {constants, createReadStream, promises, ReadStream} from "fs";
2
2
  import path from "path";
3
3
 
4
4
  import FormData from "form-data";
5
+ import {PathLikeOrReadStream} from "../../common";
5
6
 
6
7
  export class PDFEngineUtils {
7
- public static async injectFiles(files: PathLike[], data: FormData) {
8
- for (const file of files) {
9
- await promises.access(file, constants.R_OK);
10
- const filename = path.basename(file.toString());
11
- const extension = path.extname(filename);
12
- if (extension === ".pdf") {
13
- data.append(filename, createReadStream(file));
14
- } else {
15
- throw new Error(`${extension} is not supported`);
16
- }
8
+ public static async addFiles(files: PathLikeOrReadStream[], data: FormData) {
9
+ for (const [key, file] of files.entries()) {
10
+ const filename = `file${key}`
11
+ if (Buffer.isBuffer(file)) {
12
+ data.append("files", file, filename);
13
+ } else if (file instanceof ReadStream) {
14
+ data.append("files", file, filename);
15
+ } else {
16
+ await promises.access(file, constants.R_OK);
17
+ const filename = path.basename(file.toString());
18
+ const extension = path.extname(filename);
19
+ if (extension === ".pdf") {
20
+ data.append(filename, createReadStream(file));
21
+ } else {
22
+ throw new Error(`${extension} is not supported`);
23
+ }
24
+ }
25
+ }
17
26
  }
18
- }
19
27
  }
@@ -1,60 +1,72 @@
1
1
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
- import { createReadStream, promises } from "fs";
2
+ import {createReadStream, promises} from "fs";
3
3
  import FormData from "form-data";
4
4
 
5
- import { PDFEngineUtils } from "./../engine.utils";
5
+ import {PDFEngineUtils} from "../engine.utils";
6
6
 
7
7
  describe("PDFEngineUtils", () => {
8
- const mockPromisesAccess = jest.spyOn(promises, "access");
9
- const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
10
- const data = new FormData();
8
+ const mockPromisesAccess = jest.spyOn(promises, "access");
9
+ const mockFormDataAppend = jest.spyOn(FormData.prototype, "append");
10
+ const data = new FormData();
11
11
 
12
- beforeEach(() => {
13
- (createReadStream as jest.Mock) = jest
14
- .fn()
15
- .mockImplementation((file) => file);
16
- });
17
-
18
- afterEach(() => {
19
- jest.resetAllMocks();
20
- });
21
-
22
- describe("injectFiles", () => {
23
- describe("when files exist", () => {
24
- it("should append each file to data", async () => {
25
- mockPromisesAccess.mockResolvedValue();
26
- await PDFEngineUtils.injectFiles(
27
- ["path/to/file.pdf", "path/to/another-file.pdf"],
28
- data
29
- );
30
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
31
- });
12
+ beforeEach(() => {
13
+ (createReadStream as jest.Mock) = jest
14
+ .fn()
15
+ .mockImplementation((file) => file);
32
16
  });
33
17
 
34
- describe("when one of the files is not PDF", () => {
35
- it("should throw an error", async () => {
36
- mockPromisesAccess.mockResolvedValue();
37
- await expect(() =>
38
- PDFEngineUtils.injectFiles(
39
- ["path/to/file.docx", "path/to/file.pdf"],
40
- data
41
- )
42
- ).rejects.toThrow(".docx is not supported");
43
- });
18
+ afterEach(() => {
19
+ jest.resetAllMocks();
44
20
  });
45
21
 
46
- describe("when one of the files does not exist", () => {
47
- it("should throw an error", async () => {
48
- const errorMessage =
49
- "ENOENT: no such file or directory, access 'path/to/index.html'";
50
- mockPromisesAccess.mockRejectedValue(new Error(errorMessage));
51
- await expect(() =>
52
- PDFEngineUtils.injectFiles(
53
- ["path/to/file.pdf", "path/to/another-file.pdf"],
54
- data
55
- )
56
- ).rejects.toThrow(errorMessage);
57
- });
22
+ describe("addFiles", () => {
23
+ describe("when files exist", () => {
24
+ describe("when files parameter contains paths", () => {
25
+ it("should append each file to data", async () => {
26
+ mockPromisesAccess.mockResolvedValue();
27
+ await PDFEngineUtils.addFiles(
28
+ ["path/to/file.pdf", "path/to/another-file.pdf"],
29
+ data
30
+ );
31
+ expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
32
+ });
33
+ })
34
+ describe("when files parameter contains a buffer", () => {
35
+ it("should append each file to data", async () => {
36
+ mockPromisesAccess.mockResolvedValue();
37
+ await PDFEngineUtils.addFiles(
38
+ [Buffer.from("data"), "path/to/another-file.pdf"],
39
+ data
40
+ );
41
+ expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
42
+ });
43
+ })
44
+ });
45
+
46
+ describe("when one of the files is not PDF", () => {
47
+ it("should throw an error", async () => {
48
+ mockPromisesAccess.mockResolvedValue();
49
+ await expect(() =>
50
+ PDFEngineUtils.addFiles(
51
+ ["path/to/file.docx", "path/to/file.pdf"],
52
+ data
53
+ )
54
+ ).rejects.toThrow(".docx is not supported");
55
+ });
56
+ });
57
+
58
+ describe("when one of the files does not exist", () => {
59
+ it("should throw an error", async () => {
60
+ const errorMessage =
61
+ "ENOENT: no such file or directory, access 'path/to/index.html'";
62
+ mockPromisesAccess.mockRejectedValue(new Error(errorMessage));
63
+ await expect(() =>
64
+ PDFEngineUtils.addFiles(
65
+ ["path/to/file.pdf", "path/to/another-file.pdf"],
66
+ data
67
+ )
68
+ ).rejects.toThrow(errorMessage);
69
+ });
70
+ });
58
71
  });
59
- });
60
72
  });