chromiumly 3.3.0 → 3.3.1

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 (48) hide show
  1. package/README.md +1 -0
  2. package/dist/pdf-engines/utils/pdf-engines.utils.js +5 -5
  3. package/dist/pdf-engines/utils/pdf-engines.utils.js.map +1 -1
  4. package/package.json +11 -8
  5. package/src/.prettierrc.yml +0 -4
  6. package/src/chromium/converters/converter.ts +0 -22
  7. package/src/chromium/converters/html.converter.ts +0 -134
  8. package/src/chromium/converters/markdown.converter.ts +0 -129
  9. package/src/chromium/converters/tests/html.converter.test.ts +0 -190
  10. package/src/chromium/converters/tests/markdown.converter.test.ts +0 -187
  11. package/src/chromium/converters/tests/url.converter.test.ts +0 -164
  12. package/src/chromium/converters/url.converter.ts +0 -125
  13. package/src/chromium/index.ts +0 -6
  14. package/src/chromium/interfaces/common.types.ts +0 -15
  15. package/src/chromium/interfaces/converter.types.ts +0 -45
  16. package/src/chromium/interfaces/screenshot.types.ts +0 -15
  17. package/src/chromium/screenshots/html.screenshot.ts +0 -105
  18. package/src/chromium/screenshots/markdown.screenshot.ts +0 -100
  19. package/src/chromium/screenshots/screenshot.ts +0 -22
  20. package/src/chromium/screenshots/tests/html.screenshot.test.ts +0 -192
  21. package/src/chromium/screenshots/tests/markdown.screenshot.test.ts +0 -176
  22. package/src/chromium/screenshots/tests/url.screenshot.test.ts +0 -166
  23. package/src/chromium/screenshots/url.screenshot.ts +0 -96
  24. package/src/chromium/utils/converter.utils.ts +0 -187
  25. package/src/chromium/utils/screenshot.utils.ts +0 -127
  26. package/src/chromium/utils/tests/converter.utils.test.ts +0 -496
  27. package/src/chromium/utils/tests/screenshot.utils.test.ts +0 -338
  28. package/src/common/constants.ts +0 -9
  29. package/src/common/gotenberg.utils.ts +0 -86
  30. package/src/common/index.ts +0 -3
  31. package/src/common/tests/gotenberg.utils.test.ts +0 -141
  32. package/src/common/types.ts +0 -7
  33. package/src/gotenberg.ts +0 -54
  34. package/src/libre-office/index.ts +0 -1
  35. package/src/libre-office/interfaces/libre-office.types.ts +0 -156
  36. package/src/libre-office/libre-office.ts +0 -61
  37. package/src/libre-office/tests/libre-office.test.ts +0 -56
  38. package/src/libre-office/utils/constants.ts +0 -132
  39. package/src/libre-office/utils/libre-office.utils.ts +0 -128
  40. package/src/libre-office/utils/tests/libre-office.utils.test.ts +0 -156
  41. package/src/main.config.ts +0 -157
  42. package/src/main.ts +0 -13
  43. package/src/pdf-engines/index.ts +0 -1
  44. package/src/pdf-engines/interfaces/pdf-engines.types.ts +0 -10
  45. package/src/pdf-engines/pdf-engines.ts +0 -156
  46. package/src/pdf-engines/tests/pdf.engine.test.ts +0 -163
  47. package/src/pdf-engines/utils/pdf-engines.utils.ts +0 -68
  48. package/src/pdf-engines/utils/tests/pdf-engines.utils.test.ts +0 -71
@@ -1,100 +0,0 @@
1
- import FormData from 'form-data';
2
-
3
- import { GotenbergUtils, PathLikeOrReadStream } from '../../common';
4
- import { ImageProperties } from '../interfaces/screenshot.types';
5
- import { ScreenshotUtils } from '../utils/screenshot.utils';
6
- import { Screenshot } from './screenshot';
7
- import { ChromiumRoute, Chromiumly } from '../../main.config';
8
- import { EmulatedMediaType } from '../interfaces/common.types';
9
-
10
- /**
11
- * Class representing a Markdown screenshot that extends the base Screenshot class.
12
- * This class is used to screenshots HTML with markdown content using Gotenberg service.
13
- *
14
- * @extends Screenshot
15
- */
16
- export class MarkdownScreenshot extends Screenshot {
17
- /**
18
- * Creates an instance of MarkdownScreenshot.
19
- * Initializes the Screenshot with the Markdown screenshot route.
20
- */
21
- constructor() {
22
- super(ChromiumRoute.MARKDOWN);
23
- }
24
-
25
- /**
26
- * Screenshots HTML with markdown.
27
- *
28
- * @param {Object} options - Screenshot options.
29
- * @param {PathLikeOrReadStream} options.html - PathLike or ReadStream of the HTML content to be screenshoted.
30
- * @param {PathLikeOrReadStream} options.markdown - PathLike or ReadStream of the Markdown content to be screenshoted.
31
- * @param {PathLikeOrReadStream} [options.header] - PathLike or ReadStream of the header HTML content.
32
- * @param {PathLikeOrReadStream} [options.footer] - PathLike or ReadStream of the footer HTML content.
33
- * @param {ImageProperties} [options.properties] - Image properties for the screenshot.
34
- * @param {EmulatedMediaType} [options.emulatedMediaType] - Emulated media type for the screenshot.
35
- * @param {string} [options.waitDelay] - Delay before the screenshot process starts.
36
- * @param {string} [options.waitForExpression] - JavaScript expression to wait for before completing the screenshot.
37
- * @param {Record<string, string>} [options.extraHttpHeaders] - Additional HTTP headers for the screenshot.
38
- * @param {number []} [options.failOnHttpStatusCodes] - Whether to fail on HTTP status code.
39
- * @param {boolean} [options.failOnConsoleExceptions] - Whether to fail on console exceptions during screenshot.
40
- * @param {boolean} [options.skipNetworkIdleEvent] - Whether to skip network idle event.
41
- * @param {boolean} [options.optimizeForSpeed] - Whether to optimize for speed.
42
- * @returns {Promise<Buffer>} A Promise resolving to the image buffer.
43
- */
44
- async capture({
45
- html,
46
- markdown,
47
- header,
48
- footer,
49
- properties,
50
- emulatedMediaType,
51
- waitDelay,
52
- waitForExpression,
53
- extraHttpHeaders,
54
- failOnHttpStatusCodes,
55
- failOnConsoleExceptions,
56
- skipNetworkIdleEvent,
57
- optimizeForSpeed
58
- }: {
59
- html: PathLikeOrReadStream;
60
- markdown: PathLikeOrReadStream;
61
- header?: PathLikeOrReadStream;
62
- footer?: PathLikeOrReadStream;
63
- properties?: ImageProperties;
64
- emulatedMediaType?: EmulatedMediaType;
65
- waitDelay?: string;
66
- waitForExpression?: string;
67
- extraHttpHeaders?: Record<string, string>;
68
- failOnHttpStatusCodes?: number[];
69
- failOnConsoleExceptions?: boolean;
70
- skipNetworkIdleEvent?: boolean;
71
- optimizeForSpeed?: boolean;
72
- }): Promise<Buffer> {
73
- const data = new FormData();
74
-
75
- await GotenbergUtils.addFile(data, html, 'index.html');
76
-
77
- await GotenbergUtils.addFile(data, markdown, 'file.md');
78
-
79
- await ScreenshotUtils.customize(data, {
80
- header,
81
- footer,
82
- properties,
83
- emulatedMediaType,
84
- waitDelay,
85
- waitForExpression,
86
- extraHttpHeaders,
87
- failOnHttpStatusCodes,
88
- failOnConsoleExceptions,
89
- skipNetworkIdleEvent,
90
- optimizeForSpeed
91
- });
92
-
93
- return GotenbergUtils.fetch(
94
- this.endpoint,
95
- data,
96
- Chromiumly.getGotenbergApiBasicAuthUsername(),
97
- Chromiumly.getGotenbergApiBasicAuthPassword()
98
- );
99
- }
100
- }
@@ -1,22 +0,0 @@
1
- import { Chromiumly, ChromiumRoute } from '../../main.config';
2
-
3
- /**
4
- * Abstract class representing a generic screenshot.
5
- * Concrete screenshot classes should extend this class and implement specific screenshot logic.
6
- */
7
- export abstract class Screenshot {
8
- /**
9
- * The endpoint URL for the screenshot.
10
- */
11
- readonly endpoint: string;
12
-
13
- /**
14
- * Creates an instance of the screenshot class.
15
- * Initializes the endpoint URL based on the provided ChromiumRoute.
16
- *
17
- * @param {ChromiumRoute} route - The ChromiumRoute enum value representing the screenshot route.
18
- */
19
- constructor(route: ChromiumRoute) {
20
- this.endpoint = `${Chromiumly.getGotenbergEndpoint()}/${Chromiumly.CHROMIUM_SCREENSHOT_PATH}/${Chromiumly.CHROMIUM_ROUTES[route]}`;
21
- }
22
- }
@@ -1,192 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
- import { createReadStream, promises } from 'fs';
3
-
4
- import fetch from 'node-fetch';
5
- import FormData from 'form-data';
6
-
7
- import { HtmlScreenshot } from '../html.screenshot';
8
-
9
- const { Response } = jest.requireActual('node-fetch');
10
- jest.mock('node-fetch', () => jest.fn());
11
-
12
- describe('HtmlScreenshot', () => {
13
- const screenshot = new HtmlScreenshot();
14
-
15
- describe('endpoint', () => {
16
- it('should route to Chromium HTML route', () => {
17
- expect(screenshot.endpoint).toEqual(
18
- 'http://localhost:3000/forms/chromium/screenshot/html'
19
- );
20
- });
21
- });
22
-
23
- describe('capture', () => {
24
- const mockPromisesAccess = jest.spyOn(promises, 'access');
25
- const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
26
- const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');
27
-
28
- const assets = [
29
- { file: Buffer.from('asset1'), name: 'asset1' },
30
- { file: Buffer.from('asset2'), name: 'asset2' }
31
- ];
32
-
33
- beforeEach(() => {
34
- (createReadStream as jest.Mock) = jest.fn();
35
- });
36
-
37
- afterEach(() => {
38
- jest.resetAllMocks();
39
- });
40
-
41
- describe('when html parameter is passed', () => {
42
- it('should return a buffer', async () => {
43
- mockFetch.mockResolvedValue(new Response('content'));
44
- const buffer = await screenshot.capture({
45
- html: Buffer.from('data')
46
- });
47
- expect(buffer).toEqual(Buffer.from('content'));
48
- });
49
- });
50
-
51
- describe('when image properties parameter is passed', () => {
52
- it('should return a buffer', async () => {
53
- mockFetch.mockResolvedValue(new Response('content'));
54
- const buffer = await screenshot.capture({
55
- html: Buffer.from('data'),
56
- properties: { format: 'jpeg', quality: 50 }
57
- });
58
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
59
- expect(buffer).toEqual(Buffer.from('content'));
60
- });
61
- });
62
-
63
- describe('when header parameter is passed', () => {
64
- it('should return a buffer', async () => {
65
- mockFetch.mockResolvedValue(new Response('content'));
66
- const buffer = await screenshot.capture({
67
- html: Buffer.from('data'),
68
- header: Buffer.from('header')
69
- });
70
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
71
- expect(buffer).toEqual(Buffer.from('content'));
72
- });
73
- });
74
-
75
- describe('when footer parameter is passed', () => {
76
- it('should return a buffer', async () => {
77
- mockFetch.mockResolvedValue(new Response('content'));
78
- const buffer = await screenshot.capture({
79
- html: Buffer.from('data'),
80
- footer: Buffer.from('footer')
81
- });
82
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
83
- expect(buffer).toEqual(Buffer.from('content'));
84
- });
85
- });
86
-
87
- describe('when assets parameter is passed', () => {
88
- it('should return a buffer', async () => {
89
- mockFetch.mockResolvedValue(new Response('content'));
90
- const buffer = await screenshot.capture({
91
- html: Buffer.from('data'),
92
- assets
93
- });
94
-
95
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
96
- expect(buffer).toEqual(Buffer.from('content'));
97
- });
98
- });
99
-
100
- describe('when emulatedMediaType parameter is passed', () => {
101
- it('should return a buffer', async () => {
102
- mockFetch.mockResolvedValue(new Response('content'));
103
- const buffer = await screenshot.capture({
104
- html: Buffer.from('data'),
105
- emulatedMediaType: 'screen'
106
- });
107
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
108
- expect(buffer).toEqual(Buffer.from('content'));
109
- });
110
- });
111
-
112
- describe('when failOnHttpStatusCodes parameter is passed', () => {
113
- it('should return a buffer', async () => {
114
- mockFetch.mockResolvedValue(new Response('content'));
115
- const buffer = await screenshot.capture({
116
- html: Buffer.from('data'),
117
- failOnHttpStatusCodes: [499, 599]
118
- });
119
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
120
- expect(buffer).toEqual(Buffer.from('content'));
121
- });
122
- });
123
-
124
- describe('when skipNetworkIdleEvent parameter is passed', () => {
125
- it('should return a buffer', async () => {
126
- mockFetch.mockResolvedValue(new Response('content'));
127
- const buffer = await screenshot.capture({
128
- html: Buffer.from('data'),
129
- skipNetworkIdleEvent: true
130
- });
131
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
132
- expect(buffer).toEqual(Buffer.from('content'));
133
- });
134
- });
135
-
136
- describe('when optimizeForSpeed parameter is passed', () => {
137
- it('should return a buffer', async () => {
138
- mockFetch.mockResolvedValue(new Response('content'));
139
- const buffer = await screenshot.capture({
140
- html: Buffer.from('data'),
141
- optimizeForSpeed: true
142
- });
143
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
144
- expect(buffer).toEqual(Buffer.from('content'));
145
- });
146
- });
147
-
148
- describe('when all parameters are passed', () => {
149
- it('should return a buffer', async () => {
150
- mockPromisesAccess.mockResolvedValue();
151
- mockFetch.mockResolvedValue(new Response('content'));
152
- const buffer = await screenshot.capture({
153
- html: Buffer.from('data'),
154
- assets,
155
- header: Buffer.from('header'),
156
- footer: Buffer.from('footer'),
157
- emulatedMediaType: 'screen',
158
- failOnHttpStatusCodes: [499, 599],
159
- skipNetworkIdleEvent: true,
160
- failOnConsoleExceptions: true,
161
- properties: { format: 'jpeg', quality: 50 },
162
- optimizeForSpeed: true
163
- });
164
- expect(mockFormDataAppend).toHaveBeenCalledTimes(12);
165
- expect(buffer).toEqual(Buffer.from('content'));
166
- });
167
- });
168
-
169
- describe('when file does not exist', () => {
170
- it('should throw an error', async () => {
171
- const errorMessage =
172
- "ENOENT: no such file or directory, access 'path/to/index.html'";
173
- mockPromisesAccess.mockRejectedValue(new Error(errorMessage));
174
- await expect(() =>
175
- screenshot.capture({ html: 'path/to/index.html' })
176
- ).rejects.toThrow(errorMessage);
177
- });
178
- });
179
-
180
- describe('when fetch request fails', () => {
181
- it('should throw an error', async () => {
182
- const errorMessage =
183
- 'FetchError: request to http://localhost:3000/forms/chromium/screenshot/html failed';
184
- mockPromisesAccess.mockResolvedValue();
185
- mockFetch.mockRejectedValue(new Error(errorMessage));
186
- await expect(() =>
187
- screenshot.capture({ html: 'path/to/index.html' })
188
- ).rejects.toThrow(errorMessage);
189
- });
190
- });
191
- });
192
- });
@@ -1,176 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
- import { createReadStream, promises } from 'fs';
3
-
4
- import FormData from 'form-data';
5
- import fetch from 'node-fetch';
6
-
7
- import { MarkdownScreenshot } from '../markdown.screenshot';
8
-
9
- const { Response } = jest.requireActual('node-fetch');
10
- jest.mock('node-fetch', () => jest.fn());
11
-
12
- describe('MarkdownScreenshot', () => {
13
- const screenshot = new MarkdownScreenshot();
14
-
15
- describe('endpoint', () => {
16
- it('should route to Chromium Markdown route', () => {
17
- expect(screenshot.endpoint).toEqual(
18
- 'http://localhost:3000/forms/chromium/screenshot/markdown'
19
- );
20
- });
21
- });
22
-
23
- describe('capture', () => {
24
- const mockPromisesAccess = jest.spyOn(promises, 'access');
25
- const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
26
- const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');
27
-
28
- beforeEach(() => {
29
- (createReadStream as jest.Mock) = jest.fn();
30
- });
31
-
32
- afterEach(() => {
33
- jest.resetAllMocks();
34
- });
35
-
36
- describe('when html and markdown parameters are passed', () => {
37
- it('should return a buffer', async () => {
38
- mockFetch.mockResolvedValue(new Response('content'));
39
- const buffer = await screenshot.capture({
40
- html: Buffer.from('data'),
41
- markdown: Buffer.from('markdown')
42
- });
43
- expect(buffer).toEqual(Buffer.from('content'));
44
- });
45
- });
46
-
47
- describe('when image properties parameter is passed', () => {
48
- it('should return a buffer', async () => {
49
- mockFetch.mockResolvedValue(new Response('content'));
50
- const buffer = await screenshot.capture({
51
- html: Buffer.from('data'),
52
- markdown: Buffer.from('markdown'),
53
- properties: { format: 'jpeg', quality: 50 }
54
- });
55
- expect(mockFormDataAppend).toHaveBeenCalledTimes(4);
56
- expect(buffer).toEqual(Buffer.from('content'));
57
- });
58
- });
59
-
60
- describe('when header parameter is passed', () => {
61
- it('should return a buffer', async () => {
62
- mockFetch.mockResolvedValue(new Response('content'));
63
- const buffer = await screenshot.capture({
64
- html: Buffer.from('data'),
65
- markdown: Buffer.from('markdown'),
66
- header: Buffer.from('header')
67
- });
68
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
69
- expect(buffer).toEqual(Buffer.from('content'));
70
- });
71
- });
72
-
73
- describe('when footer parameter is passed', () => {
74
- it('should return a buffer', async () => {
75
- mockFetch.mockResolvedValue(new Response('content'));
76
- const buffer = await screenshot.capture({
77
- html: Buffer.from('data'),
78
- markdown: Buffer.from('markdown'),
79
- footer: Buffer.from('footer')
80
- });
81
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
82
- expect(buffer).toEqual(Buffer.from('content'));
83
- });
84
- });
85
-
86
- describe('when emulatedMediaType parameter is passed', () => {
87
- it('should return a buffer', async () => {
88
- mockFetch.mockResolvedValue(new Response('content'));
89
- const buffer = await screenshot.capture({
90
- html: Buffer.from('data'),
91
- markdown: Buffer.from('markdown'),
92
- emulatedMediaType: 'screen'
93
- });
94
-
95
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
96
- expect(buffer).toEqual(Buffer.from('content'));
97
- });
98
- });
99
-
100
- describe('when failOnHttpStatusCodes parameter is passed', () => {
101
- it('should return a buffer', async () => {
102
- mockFetch.mockResolvedValue(new Response('content'));
103
- const buffer = await screenshot.capture({
104
- html: Buffer.from('data'),
105
- markdown: Buffer.from('markdown'),
106
- failOnHttpStatusCodes: [499, 599]
107
- });
108
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
109
- expect(buffer).toEqual(Buffer.from('content'));
110
- });
111
- });
112
-
113
- describe('when skipNetworkIdleEvent parameter is passed', () => {
114
- it('should return a buffer', async () => {
115
- mockFetch.mockResolvedValue(new Response('content'));
116
- const buffer = await screenshot.capture({
117
- html: Buffer.from('data'),
118
- markdown: Buffer.from('markdown'),
119
- skipNetworkIdleEvent: true
120
- });
121
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
122
- expect(buffer).toEqual(Buffer.from('content'));
123
- });
124
- });
125
-
126
- describe('when all parameters are passed', () => {
127
- it('should return a buffer', async () => {
128
- mockFetch.mockResolvedValue(new Response('content'));
129
- const buffer = await screenshot.capture({
130
- html: Buffer.from('data'),
131
- markdown: Buffer.from('markdown'),
132
- header: Buffer.from('header'),
133
- footer: Buffer.from('footer'),
134
- emulatedMediaType: 'screen',
135
- failOnHttpStatusCodes: [499, 599],
136
- skipNetworkIdleEvent: true,
137
- failOnConsoleExceptions: true,
138
- properties: { format: 'jpeg', quality: 50 },
139
- optimizeForSpeed: true
140
- });
141
- expect(mockFormDataAppend).toHaveBeenCalledTimes(11);
142
- expect(buffer).toEqual(Buffer.from('content'));
143
- });
144
- });
145
-
146
- describe('when file does not exist', () => {
147
- it('should throw an error', async () => {
148
- const errorMessage =
149
- "ENOENT: no such file or directory, access 'path/to/index.html'";
150
- mockPromisesAccess.mockRejectedValue(new Error(errorMessage));
151
-
152
- await expect(() =>
153
- screenshot.capture({
154
- html: 'path/to/index.html',
155
- markdown: 'path/to/file.md'
156
- })
157
- ).rejects.toThrow(errorMessage);
158
- });
159
- });
160
-
161
- describe('when fetch request fails', () => {
162
- it('should throw an error', async () => {
163
- const errorMessage =
164
- 'FetchError: request to http://localhost:3000/forms/chromium/screenshot/html failed';
165
- mockPromisesAccess.mockResolvedValue();
166
- mockFetch.mockRejectedValue(new Error(errorMessage));
167
- await expect(() =>
168
- screenshot.capture({
169
- html: 'path/to/index.html',
170
- markdown: 'path/to/file.md'
171
- })
172
- ).rejects.toThrow(errorMessage);
173
- });
174
- });
175
- });
176
- });
@@ -1,166 +0,0 @@
1
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
2
- import { createReadStream } from 'fs';
3
- import FormData from 'form-data';
4
- import fetch from 'node-fetch';
5
-
6
- import { UrlScreenshot } from './../url.screenshot';
7
-
8
- const { Response } = jest.requireActual('node-fetch');
9
- jest.mock('node-fetch', () => jest.fn());
10
-
11
- describe('URLScreenshot', () => {
12
- const screenshot = new UrlScreenshot();
13
-
14
- describe('endpoint', () => {
15
- it('should route to Chromium HTML route', () => {
16
- expect(screenshot.endpoint).toEqual(
17
- 'http://localhost:3000/forms/chromium/screenshot/url'
18
- );
19
- });
20
- });
21
-
22
- describe('capture', () => {
23
- const mockFetch = fetch as jest.MockedFunction<typeof fetch>;
24
- const mockFormDataAppend = jest.spyOn(FormData.prototype, 'append');
25
-
26
- beforeEach(() => {
27
- (createReadStream as jest.Mock) = jest.fn();
28
- });
29
-
30
- afterEach(() => {
31
- jest.resetAllMocks();
32
- });
33
-
34
- describe('when URL is valid', () => {
35
- it('should return a buffer', async () => {
36
- mockFetch.mockResolvedValueOnce(new Response('content'));
37
- const buffer = await screenshot.capture({
38
- url: 'http://www.example.com/'
39
- });
40
- expect(buffer).toEqual(Buffer.from('content'));
41
- });
42
- });
43
-
44
- describe('when header parameter is passed', () => {
45
- it('should return a buffer', async () => {
46
- mockFetch.mockResolvedValueOnce(new Response('content'));
47
- const buffer = await screenshot.capture({
48
- url: 'http://www.example.com/',
49
- header: Buffer.from('header')
50
- });
51
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
52
- expect(buffer).toEqual(Buffer.from('content'));
53
- });
54
- });
55
-
56
- describe('when footer parameter is passed', () => {
57
- it('should return a buffer', async () => {
58
- mockFetch.mockResolvedValueOnce(new Response('content'));
59
- const buffer = await screenshot.capture({
60
- url: 'http://www.example.com/',
61
- footer: Buffer.from('footer')
62
- });
63
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
64
- expect(buffer).toEqual(Buffer.from('content'));
65
- });
66
- });
67
-
68
- describe('when image properties parameter is passed', () => {
69
- it('should return a buffer', async () => {
70
- mockFetch.mockResolvedValueOnce(new Response('content'));
71
- const buffer = await screenshot.capture({
72
- url: 'http://www.example.com/',
73
- properties: { format: 'jpeg', quality: 50 }
74
- });
75
- expect(mockFormDataAppend).toHaveBeenCalledTimes(3);
76
- expect(buffer).toEqual(Buffer.from('content'));
77
- });
78
- });
79
-
80
- describe('when emulatedMediaType parameter is passed', () => {
81
- it('should return a buffer', async () => {
82
- mockFetch.mockResolvedValue(new Response('content'));
83
- const buffer = await screenshot.capture({
84
- url: 'http://www.example.com/',
85
- emulatedMediaType: 'screen'
86
- });
87
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
88
- expect(buffer).toEqual(Buffer.from('content'));
89
- });
90
- });
91
-
92
- describe('when failOnHttpStatusCodes parameter is passed', () => {
93
- it('should return a buffer', async () => {
94
- mockFetch.mockResolvedValue(new Response('content'));
95
- const buffer = await screenshot.capture({
96
- url: 'http://www.example.com/',
97
- failOnHttpStatusCodes: [499, 599]
98
- });
99
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
100
- expect(buffer).toEqual(Buffer.from('content'));
101
- });
102
- });
103
-
104
- describe('when skipNetworkIdleEvent parameter is passed', () => {
105
- it('should return a buffer', async () => {
106
- mockFetch.mockResolvedValue(new Response('content'));
107
- const buffer = await screenshot.capture({
108
- url: 'http://www.example.com/',
109
- skipNetworkIdleEvent: true
110
- });
111
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
112
- expect(buffer).toEqual(Buffer.from('content'));
113
- });
114
- });
115
-
116
- describe('when optimizeForSpeed parameter is passed', () => {
117
- it('should return a buffer', async () => {
118
- mockFetch.mockResolvedValue(new Response('content'));
119
- const buffer = await screenshot.capture({
120
- url: 'http://www.example.com/',
121
- optimizeForSpeed: true
122
- });
123
- expect(mockFormDataAppend).toHaveBeenCalledTimes(2);
124
- expect(buffer).toEqual(Buffer.from('content'));
125
- });
126
- });
127
-
128
- describe('when all parameters are passed', () => {
129
- it('should return a buffer', async () => {
130
- mockFetch.mockResolvedValue(new Response('content'));
131
- const buffer = await screenshot.capture({
132
- url: 'http://www.example.com/',
133
- header: Buffer.from('header'),
134
- footer: Buffer.from('footer'),
135
- emulatedMediaType: 'screen',
136
- failOnHttpStatusCodes: [499, 599],
137
- skipNetworkIdleEvent: true,
138
- failOnConsoleExceptions: true,
139
- properties: { format: 'jpeg', quality: 50 },
140
- optimizeForSpeed: true
141
- });
142
- expect(mockFormDataAppend).toHaveBeenCalledTimes(10);
143
- expect(buffer).toEqual(Buffer.from('content'));
144
- });
145
- });
146
-
147
- describe('when URL is invalid', () => {
148
- it('should throw an error', async () => {
149
- await expect(() =>
150
- screenshot.capture({ url: 'invalid url' })
151
- ).rejects.toThrow('Invalid URL');
152
- });
153
- });
154
-
155
- describe('when fetch request fails', () => {
156
- it('should throw an error', async () => {
157
- const errorMessage =
158
- 'FetchError: request to http://localhost:3000/forms/chromium/convert/html failed';
159
- mockFetch.mockRejectedValueOnce(new Error(errorMessage));
160
- await expect(() =>
161
- screenshot.capture({ url: 'http://www.example.com/' })
162
- ).rejects.toThrow(errorMessage);
163
- });
164
- });
165
- });
166
- });