/
get-default-printer.spec.ts
58 lines (48 loc) · 1.6 KB
/
get-default-printer.spec.ts
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
import getDefaultPrinter from "./get-default-printer";
import execAsync from "../utils/exec-async";
import { Printer } from "../types";
jest.mock("../utils/exec-async");
const defaultPrinterStdout = `system default destination: Virtual_PDF_Printer
`;
const defaultPrinterDetailsStdout = `printer Virtual_PDF_Printer is idle. enabled since Mon 12 Apr 2021 01:35:20 PM EEST
Form mounted:
Content types: any
Printer types: unknown
Description: Virtual PDF Printer
Alerts: none
Location:
Connection: direct
Interface: /etc/cups/ppd/Virtual_PDF_Printer.ppd
`;
afterEach(() => {
// restore the original implementation.
execAsync.mockRestore();
});
it("returns the system default printer", async () => {
execAsync
.mockImplementationOnce(() =>
Promise.resolve({ stdout: defaultPrinterStdout })
)
.mockImplementationOnce(() =>
Promise.resolve({ stdout: defaultPrinterDetailsStdout })
);
const expected: Printer = {
printer: "Virtual_PDF_Printer",
description: "Virtual PDF Printer",
status: "idle",
connection: "direct",
alerts: "none",
};
await expect(getDefaultPrinter()).resolves.toEqual(expected);
await expect(execAsync).toBeCalledWith("lpstat -d");
});
it("returns null when the default printer is not defined", async () => {
execAsync.mockImplementation(() =>
Promise.resolve({ stdout: "no system default destination" })
);
await expect(getDefaultPrinter()).resolves.toBeNull();
});
it("fails with an error", async () => {
execAsync.mockImplementation(() => Promise.reject("error"));
await expect(getDefaultPrinter()).rejects.toMatch("error");
});