-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.test.js
68 lines (51 loc) · 1.69 KB
/
index.test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
const fs = require("fs");
const path = require("path");
const puppeteer = require("puppeteer");
const { main } = require("./index.js");
jest.mock("puppeteer");
jest.mock("fs");
jest.mock("path");
describe("main function", () => {
let browser, page;
beforeEach(() => {
browser = {
newPage: jest.fn().mockResolvedValue({
setDefaultTimeout: jest.fn(),
goto: jest.fn(),
$: jest.fn(),
click: jest.fn(),
pdf: jest.fn(),
}),
close: jest.fn(),
};
page = browser.newPage();
puppeteer.launch.mockResolvedValue(browser);
fs.existsSync.mockReturnValue(true);
fs.mkdirSync.mockImplementation(() => {});
path.normalize.mockImplementation((p) => p);
});
afterEach(() => {
jest.resetAllMocks();
});
test("should throw error if URL is not provided", async () => {
process.argv[2] = undefined;
await expect(main()).rejects.toThrow("URL is required");
});
test("should generate PDF file", async () => {
process.argv[2] = "http://example.com";
await main();
expect(puppeteer.launch).toHaveBeenCalled();
expect(page.setDefaultTimeout).toHaveBeenCalledWith(60000);
expect(page.goto).toHaveBeenCalledWith("http://example.com", { waitUntil: "load" });
expect(page.pdf).toHaveBeenCalled();
expect(browser.close).toHaveBeenCalled();
});
test("should handle modal if present", async () => {
process.argv[2] = "http://example.com";
page.$.mockResolvedValueOnce(true).mockResolvedValueOnce({ click: jest.fn() });
await main();
expect(page.$).toHaveBeenCalledWith("div.modal-mask");
expect(page.$).toHaveBeenCalledWith("button");
expect(page.click).toHaveBeenCalled();
});
});