|
1 | | -import { IParseFlagsOptions } from "./../src/lib/parse-flags"; |
2 | | -import * as writer from "../src/lib/write-out"; |
3 | | -import clipboardy from "clipboardy"; |
4 | | -import { parseFlags } from "../src/lib/parse-flags"; |
5 | 1 | import fs from "fs"; |
| 2 | +import * as clipboardy from "clipboardy"; |
6 | 3 |
|
7 | | -jest.spyOn(process.stdout, "write").mockImplementation(); |
8 | | -afterAll(() => { |
9 | | - jest.restoreAllMocks(); |
10 | | -}); |
11 | | -describe("testing parse flags function", () => { |
12 | | - test("write out call", () => { |
13 | | - const writeOutMock = jest.spyOn(writer, "writeOut"); |
14 | | - parseFlags({ |
15 | | - data: "<h1>foo</h1>", |
16 | | - inPath: undefined, |
17 | | - outPath: undefined, |
18 | | - }); |
19 | | - expect(writeOutMock).toHaveBeenCalledTimes(1); |
20 | | - writeOutMock.mockRestore(); |
| 4 | +import { IParseFlagsOptions, parseFlags } from "../src/lib/parse-flags"; |
| 5 | + |
| 6 | +jest.mock("fs"); |
| 7 | + |
| 8 | +describe("parseFlags", () => { |
| 9 | + const originalIsTTY = process.stdin.isTTY; |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + jest.resetAllMocks(); |
| 13 | + jest.restoreAllMocks(); |
| 14 | + process.stdin.isTTY = originalIsTTY; |
| 15 | + }); |
| 16 | + |
| 17 | + it("should return the same data when no inPath or toClipboard options are set", () => { |
| 18 | + const options: IParseFlagsOptions = { |
| 19 | + data: "test data", |
| 20 | + }; |
| 21 | + |
| 22 | + const result = parseFlags(options); |
| 23 | + expect(result.data).toEqual(options.data); |
21 | 24 | }); |
22 | 25 |
|
23 | | - test("input from file", () => { |
24 | | - const mockOut = jest.spyOn(process.stdout, "write"); |
| 26 | + it("should read data from inPath when inPath is set", () => { |
25 | 27 | const options: IParseFlagsOptions = { |
26 | 28 | data: "", |
27 | | - inPath: "__tests__/files/index.html", |
| 29 | + inPath: "input.txt", |
| 30 | + }; |
| 31 | + |
| 32 | + const fileContent = "file content"; |
| 33 | + (fs.readFileSync as jest.Mock).mockReturnValue(fileContent); |
| 34 | + |
| 35 | + const result = parseFlags(options); |
| 36 | + expect(fs.readFileSync).toHaveBeenCalledWith(options.inPath, "utf8"); |
| 37 | + expect(result.data).toEqual(fileContent); |
| 38 | + }); |
| 39 | + |
| 40 | + it("should return useGfm as true when useGfm option is set", () => { |
| 41 | + const options: IParseFlagsOptions = { |
| 42 | + data: "test data", |
| 43 | + useGfm: true, |
| 44 | + }; |
| 45 | + |
| 46 | + const result = parseFlags(options); |
| 47 | + expect(result.useGfm).toBe(true); |
| 48 | + }); |
| 49 | + |
| 50 | + it("should return useGfm as false when useGfm option is not set", () => { |
| 51 | + const options: IParseFlagsOptions = { |
| 52 | + data: "test data", |
28 | 53 | }; |
29 | | - parseFlags(options); |
30 | | - expect(mockOut.mock.calls[0][0]).toBe("# foo\n"); |
31 | | - mockOut.mockRestore(); |
| 54 | + |
| 55 | + const result = parseFlags(options); |
| 56 | + expect(result.useGfm).toBeUndefined(); |
32 | 57 | }); |
33 | 58 |
|
34 | | - test("input from file fs read", () => { |
35 | | - const orig = process.stdin.isTTY; |
| 59 | + it("should read data from clipboard when toClipboard is set and process.stdin.isTTY is true", async () => { |
| 60 | + const options: IParseFlagsOptions = { |
| 61 | + data: "", |
| 62 | + toClipboard: true, |
| 63 | + }; |
| 64 | + |
| 65 | + const clipboardContent = "clipboard content"; |
| 66 | + const clipboardyMock = jest.fn().mockReturnValue(clipboardContent); |
| 67 | + |
36 | 68 | process.stdin.isTTY = true; |
37 | | - const mockFsReadSync = jest.spyOn(fs, "readFileSync"); |
| 69 | + jest.mock("clipboardy", () => { |
| 70 | + return { |
| 71 | + __esModule: true, |
| 72 | + default: { |
| 73 | + ...clipboardy, |
| 74 | + readSync: clipboardyMock, |
| 75 | + }, |
| 76 | + }; |
| 77 | + }); |
| 78 | + |
| 79 | + let parseFlags; |
| 80 | + jest.isolateModules(async () => { |
| 81 | + parseFlags = require("../src/lib/parse-flags").parseFlags; |
| 82 | + await parseFlags(options); |
| 83 | + }); |
| 84 | + |
| 85 | + expect(clipboardyMock).toHaveBeenCalled(); |
| 86 | + expect(options.data).toEqual(clipboardContent); |
| 87 | + }); |
| 88 | + |
| 89 | + it("should not read data from clipboard when process.stdin.isTTY is false", async () => { |
38 | 90 | const options: IParseFlagsOptions = { |
39 | 91 | data: "", |
40 | | - inPath: "__tests__/files/index.html", |
| 92 | + toClipboard: true, |
41 | 93 | }; |
42 | | - parseFlags(options); |
43 | | - expect(mockFsReadSync).toHaveBeenCalled(); |
44 | | - mockFsReadSync.mockRestore(); |
45 | | - process.stdin.isTTY = orig; |
| 94 | + process.stdin.isTTY = false; |
| 95 | + |
| 96 | + const clipboardyMock = { |
| 97 | + readSync: jest.fn(), |
| 98 | + }; |
| 99 | + jest.mock("clipboardy", () => clipboardyMock); |
| 100 | + |
| 101 | + let parseFlags; |
| 102 | + await jest.isolateModules(async () => { |
| 103 | + parseFlags = require("../src/lib/parse-flags").parseFlags; |
| 104 | + await parseFlags(options); |
| 105 | + }); |
| 106 | + |
| 107 | + process.stdin.isTTY = originalIsTTY; |
| 108 | + expect(clipboardyMock.readSync).not.toHaveBeenCalled(); |
46 | 109 | }); |
47 | 110 |
|
48 | | - test("set and write to clipboard", () => { |
49 | | - const orig = process.stdin.isTTY; |
50 | | - process.stdin.isTTY = true; |
51 | | - jest.spyOn(process, "exit").mockImplementation(); |
52 | | - const mockClippy = jest.spyOn(clipboardy, "writeSync"); |
53 | | - const data = "<h1>foo</h1>"; |
54 | | - clipboardy.writeSync(data); |
| 111 | + it("should handle clipboardy.readSync() error and output error message", async () => { |
55 | 112 | const options: IParseFlagsOptions = { |
56 | 113 | data: "", |
57 | 114 | toClipboard: true, |
58 | 115 | }; |
59 | | - parseFlags(options); |
60 | | - expect(mockClippy).toHaveBeenCalled(); |
61 | | - expect(clipboardy.readSync()).toBe("# foo\n"); |
62 | | - process.stdin.isTTY = orig; |
| 116 | + process.stdin.isTTY = true; |
| 117 | + const stdoutSpy = jest.spyOn(process.stdout, "write").mockImplementation(); |
| 118 | + |
| 119 | + const errorMessage = "Clipboard read error"; |
| 120 | + |
| 121 | + await jest.isolateModules(async () => { |
| 122 | + jest.mock("clipboardy", () => { |
| 123 | + return { |
| 124 | + __esModule: true, |
| 125 | + default: { |
| 126 | + ...clipboardy, |
| 127 | + readSync: jest.fn().mockImplementation(() => { |
| 128 | + throw new Error(errorMessage); |
| 129 | + }), |
| 130 | + }, |
| 131 | + }; |
| 132 | + }); |
| 133 | + |
| 134 | + const { parseFlags } = require("../src/lib/parse-flags"); |
| 135 | + |
| 136 | + parseFlags(options); |
| 137 | + expect(stdoutSpy).toHaveBeenCalledWith(`${errorMessage}\n`); |
| 138 | + }); |
63 | 139 | }); |
64 | 140 | }); |
0 commit comments