-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathindex.test.ts
More file actions
98 lines (77 loc) · 3.04 KB
/
index.test.ts
File metadata and controls
98 lines (77 loc) · 3.04 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { Command } from 'commander';
import * as fs from 'fs';
import { runCLI } from './index';
import printTableFromInp from './src/service';
// Mock dependencies
jest.mock('./src/service');
jest.mock('commander', () => {
const mockCommand = {
option: jest.fn().mockReturnThis(),
version: jest.fn().mockReturnThis(),
parse: jest.fn().mockReturnThis(),
opts: jest.fn().mockReturnValue({}),
};
return { Command: jest.fn(() => mockCommand) };
});
describe('CLI', () => {
let mockConsoleLog: jest.SpyInstance;
beforeEach(() => {
// Mock console.log
mockConsoleLog = jest.spyOn(console, 'log').mockImplementation();
// Reset mocks
(printTableFromInp as jest.Mock).mockReset();
// Reset Commander mock
const mockCommand = new Command();
(mockCommand.opts as jest.Mock).mockReturnValue({});
(mockCommand.version as jest.Mock).mockReset();
(mockCommand.option as jest.Mock).mockReset();
(mockCommand.parse as jest.Mock).mockReset();
// Set up default mock returns
(mockCommand.version as jest.Mock).mockReturnValue(mockCommand);
(mockCommand.option as jest.Mock).mockReturnValue(mockCommand);
(mockCommand.parse as jest.Mock).mockReturnValue(mockCommand);
});
afterEach(() => {
jest.restoreAllMocks();
});
it('should configure version command with package version', () => {
const mockCommand = new Command();
runCLI(['node', 'index.js']);
expect(mockCommand.version).toHaveBeenCalledWith(
expect.any(String),
'-v, --version',
'output the current version'
);
});
it('should handle input option', () => {
const input = '[{"id":1,"name":"John"}]';
const mockCommand = new Command();
(mockCommand.opts as jest.Mock).mockReturnValue({ input });
runCLI(['node', 'index.js', '-i', input]);
expect(printTableFromInp).toHaveBeenCalledWith(input, undefined);
});
it('should handle input with table options', () => {
const input = '[{"id":1,"name":"John"}]';
const tableOptions = '{"title":"Test"}';
const mockCommand = new Command();
(mockCommand.opts as jest.Mock).mockReturnValue({ input, tableOptions });
runCLI(['node', 'index.js', '-i', input, '-t', tableOptions]);
expect(printTableFromInp).toHaveBeenCalledWith(input, tableOptions);
});
it('should handle stdin option', () => {
const input = '[{"id":1,"name":"John"}]';
const mockCommand = new Command();
(mockCommand.opts as jest.Mock).mockReturnValue({ stdin: true });
// Mock fs.readFileSync only for this test
const mockReadFileSync = jest.spyOn(fs, 'readFileSync')
.mockReturnValue(Buffer.from(input));
runCLI(['node', 'index.js', '-s']);
expect(mockReadFileSync).toHaveBeenCalledWith(0);
expect(printTableFromInp).toHaveBeenCalledWith(input, undefined);
mockReadFileSync.mockRestore();
});
it('should show error when no input option is provided', () => {
runCLI(['node', 'index.js']);
expect(mockConsoleLog).toHaveBeenCalledWith('Error: Cant detect input option');
});
});