Skip to content

Commit 3ab02a5

Browse files
authored
chore: Fix test cov And Infrastructural tests (#351)
1 parent a0249e2 commit 3ab02a5

File tree

11 files changed

+528
-44
lines changed

11 files changed

+528
-44
lines changed

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,6 @@ tsconfig.json
7777
renovate.json
7878
.travis.yml
7979
static-resources
80-
src
81-
index.ts
8280
.huskyrc.json
8381
.prettierrc
8482
.prettierignore

test/index.test.ts renamed to index.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Command } from 'commander';
22
import * as fs from 'fs';
3-
import { runCLI } from '../index';
4-
import printTableFromInp from '../src/service';
3+
import { runCLI } from './index';
4+
import printTableFromInp from './src/service';
55

66
// Mock dependencies
77
jest.mock('fs');
8-
jest.mock('../src/service');
8+
jest.mock('./src/service');
99
jest.mock('commander', () => {
1010
const mockCommand = {
1111
option: jest.fn().mockReturnThis(),

jestconfig.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{
22
"preset": "ts-jest",
33
"testEnvironment": "node",
4-
"moduleDirectories": ["node_modules", "src"],
54
"moduleFileExtensions": ["ts", "js", "json"],
6-
"roots": ["<rootDir>"],
7-
"testMatch": ["<rootDir>/src/**/*.test.ts", "<rootDir>/test/**/*.test.ts"],
5+
"testMatch": ["<rootDir>/test/**/*.test.ts","<rootDir>/src/**/*.test.ts", "<rootDir>/index.test.ts"],
86
"collectCoverage": true,
97
"collectCoverageFrom": [
108
"src/**/*.ts",

src/inputVerifier.test.ts

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
import { isValidJson, verifyInput, verifyTableOptions } from './inputVerifier';
2+
3+
describe('inputVerifier', () => {
4+
// Mock console.error and console.log
5+
const mockConsoleError = jest.spyOn(console, 'error').mockImplementation(() => {});
6+
const mockConsoleLog = jest.spyOn(console, 'log').mockImplementation(() => {});
7+
8+
beforeEach(() => {
9+
// Clear mock calls before each test
10+
mockConsoleError.mockClear();
11+
mockConsoleLog.mockClear();
12+
});
13+
14+
describe('isValidJson', () => {
15+
describe('valid inputs', () => {
16+
it('should validate empty array', () => {
17+
expect(isValidJson('[]')).toBe(true);
18+
expect(mockConsoleError).not.toHaveBeenCalled();
19+
});
20+
21+
it('should validate array with single object', () => {
22+
expect(isValidJson('[{"test": "value"}]')).toBe(true);
23+
expect(mockConsoleError).not.toHaveBeenCalled();
24+
});
25+
26+
it('should validate array with multiple objects', () => {
27+
expect(
28+
isValidJson(
29+
'[{"id": 1, "name": "test1"}, {"id": 2, "name": "test2"}]'
30+
)
31+
).toBe(true);
32+
expect(mockConsoleError).not.toHaveBeenCalled();
33+
});
34+
35+
it('should validate real-world example', () => {
36+
expect(
37+
isValidJson(
38+
'[{ "index":3, "text":"I would like some gelb bananen bitte", "value":200 }, { "index":4, "text":"I hope batch update is working", "value":300 } ]'
39+
)
40+
).toBe(true);
41+
expect(mockConsoleError).not.toHaveBeenCalled();
42+
});
43+
});
44+
45+
describe('invalid inputs', () => {
46+
it('should reject non-array JSON', () => {
47+
expect(isValidJson('{"key": "value"}')).toBe(false);
48+
expect(mockConsoleError).toHaveBeenCalledWith('"input" is not an array');
49+
});
50+
51+
it('should reject malformed JSON', () => {
52+
expect(isValidJson('[{ "incomplete": "json"')).toBe(false);
53+
expect(mockConsoleLog).toHaveBeenCalledWith('Invalid JSON input');
54+
});
55+
56+
it('should reject real-world invalid example', () => {
57+
expect(
58+
isValidJson(
59+
'[{ "index":3 "text":"I would like some gelb bananen bitte", "value":200 }, { "index":4, "text":"I hope batch update is working", "value":300 } ]'
60+
)
61+
).toBe(false);
62+
expect(mockConsoleLog).toHaveBeenCalledWith('Invalid JSON input');
63+
});
64+
});
65+
});
66+
67+
describe('verifyInput', () => {
68+
describe('valid inputs', () => {
69+
it('should accept empty array', () => {
70+
expect(verifyInput('[]')).toBe(true);
71+
});
72+
73+
it('should accept array with one object', () => {
74+
expect(verifyInput('[{"test": "data"}]')).toBe(true);
75+
});
76+
77+
it('should accept array with multiple objects', () => {
78+
expect(verifyInput('[{"test": "data"}, {"test2": "data2"}]')).toBe(true);
79+
});
80+
81+
it('should accept real-world example', () => {
82+
expect(
83+
verifyInput(
84+
'[{ "index":3, "text":"I would like some gelb bananen bitte", "value":200 }, { "index":4, "text":"I hope batch update is working", "value":300 } ]'
85+
)
86+
).toBe(true);
87+
});
88+
});
89+
90+
describe('invalid inputs', () => {
91+
it('should reject empty string', () => {
92+
expect(verifyInput('')).toBe(false);
93+
expect(mockConsoleError).toHaveBeenCalledWith('Input is required');
94+
});
95+
96+
it('should reject undefined', () => {
97+
expect(verifyInput(undefined as any)).toBe(false);
98+
expect(mockConsoleError).toHaveBeenCalledWith('Input is required');
99+
});
100+
101+
it('should reject real-world invalid example', () => {
102+
expect(verifyInput('{"test": "data"}')).toBe(false);
103+
expect(mockConsoleError).toHaveBeenCalledWith('"input" is not an array');
104+
});
105+
106+
it('should reject malformed JSON', () => {
107+
expect(verifyInput('{"test": "data"')).toBe(false);
108+
expect(mockConsoleLog).toHaveBeenCalledWith('Invalid JSON input');
109+
});
110+
});
111+
});
112+
113+
describe('verifyTableOptions', () => {
114+
describe('valid options', () => {
115+
it('should accept empty object', () => {
116+
expect(verifyTableOptions('{}')).toBe(true);
117+
});
118+
119+
it('should accept style options', () => {
120+
expect(verifyTableOptions('{"style": {"color": "red"}}')).toBe(true);
121+
});
122+
123+
it('should accept column options', () => {
124+
expect(verifyTableOptions('{"columns": [{"name": "test", "color": "blue"}]}')).toBe(true);
125+
});
126+
127+
it('should accept real-world example', () => {
128+
expect(
129+
verifyTableOptions(
130+
'{"style": {"headerColor": "yellow"}, "columns": [{"name": "value", "color": "green"}]}'
131+
)
132+
).toBe(true);
133+
});
134+
});
135+
136+
describe('invalid options', () => {
137+
it('should reject empty string', () => {
138+
expect(verifyTableOptions('')).toBe(false);
139+
expect(mockConsoleError).toHaveBeenCalledWith('Table options are required');
140+
});
141+
142+
it('should reject undefined', () => {
143+
expect(verifyTableOptions(undefined as any)).toBe(false);
144+
expect(mockConsoleError).toHaveBeenCalledWith('Table options are required');
145+
});
146+
147+
it('should reject malformed JSON', () => {
148+
expect(verifyTableOptions('{"test": "data"')).toBe(false);
149+
expect(mockConsoleError).toHaveBeenCalledWith('Invalid tableOptions JSON');
150+
});
151+
});
152+
});
153+
});

src/service.test.ts

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// Mock instance methods
2+
const mockAddRow = jest.fn();
3+
const mockPrintTable = jest.fn();
4+
5+
// Mock Table constructor
6+
const MockTable = jest.fn().mockImplementation(() => ({
7+
addRow: mockAddRow,
8+
printTable: mockPrintTable
9+
}));
10+
11+
// Mock modules
12+
jest.mock('console-table-printer', () => ({
13+
Table: MockTable
14+
}));
15+
16+
jest.mock('./inputVerifier', () => ({
17+
verifyInput: jest.fn(),
18+
verifyTableOptions: jest.fn(),
19+
}));
20+
21+
import printTableFromInp from './service';
22+
import * as inputVerifier from './inputVerifier';
23+
24+
describe('service', () => {
25+
describe('printTableFromInp', () => {
26+
beforeEach(() => {
27+
jest.clearAllMocks();
28+
});
29+
30+
describe('input validation', () => {
31+
it('should handle invalid input', () => {
32+
(inputVerifier.verifyInput as jest.Mock).mockReturnValue(false);
33+
const input = 'invalid input';
34+
printTableFromInp(input);
35+
expect(inputVerifier.verifyInput).toHaveBeenCalledWith(input);
36+
expect(MockTable).not.toHaveBeenCalled();
37+
});
38+
39+
it('should handle invalid table options', () => {
40+
(inputVerifier.verifyInput as jest.Mock).mockReturnValue(true);
41+
(inputVerifier.verifyTableOptions as jest.Mock).mockReturnValue(false);
42+
const input = '[{"test": "data"}]';
43+
const options = 'invalid options';
44+
printTableFromInp(input, options);
45+
expect(inputVerifier.verifyTableOptions).toHaveBeenCalledWith(options);
46+
expect(MockTable).not.toHaveBeenCalled();
47+
});
48+
});
49+
50+
describe('successful printing', () => {
51+
beforeEach(() => {
52+
(inputVerifier.verifyInput as jest.Mock).mockReturnValue(true);
53+
(inputVerifier.verifyTableOptions as jest.Mock).mockReturnValue(true);
54+
});
55+
56+
it('should print table without options', () => {
57+
const input = '[{"id": 1, "name": "test"}]';
58+
printTableFromInp(input);
59+
expect(MockTable).toHaveBeenCalledWith(undefined);
60+
expect(mockAddRow).toHaveBeenCalledWith({ id: 1, name: 'test' });
61+
expect(mockPrintTable).toHaveBeenCalled();
62+
});
63+
64+
it('should print table with options', () => {
65+
const input = '[{"id": 1, "name": "test"}]';
66+
const options = '{"title": "Test Table"}';
67+
printTableFromInp(input, options);
68+
expect(MockTable).toHaveBeenCalledWith({ title: 'Test Table' });
69+
expect(mockAddRow).toHaveBeenCalledWith({ id: 1, name: 'test' });
70+
expect(mockPrintTable).toHaveBeenCalled();
71+
});
72+
73+
it('should handle empty array input', () => {
74+
const input = '[]';
75+
printTableFromInp(input);
76+
expect(MockTable).toHaveBeenCalled();
77+
expect(mockAddRow).not.toHaveBeenCalled();
78+
expect(mockPrintTable).toHaveBeenCalled();
79+
});
80+
81+
it('should handle complex data with options', () => {
82+
const input = '[{"id": 1, "data": {"nested": "value"}}]';
83+
const options = '{"columns": [{"name": "id", "alignment": "right"}]}';
84+
printTableFromInp(input, options);
85+
expect(MockTable).toHaveBeenCalledWith({ columns: [{ name: 'id', alignment: 'right' }] });
86+
expect(mockAddRow).toHaveBeenCalledWith({ id: 1, data: { nested: 'value' } });
87+
expect(mockPrintTable).toHaveBeenCalled();
88+
});
89+
});
90+
91+
describe('edge cases', () => {
92+
beforeEach(() => {
93+
(inputVerifier.verifyInput as jest.Mock).mockReturnValue(true);
94+
(inputVerifier.verifyTableOptions as jest.Mock).mockReturnValue(true);
95+
});
96+
97+
it('should handle undefined input', () => {
98+
printTableFromInp('' as any);
99+
expect(inputVerifier.verifyInput).toHaveBeenCalledWith('');
100+
expect(MockTable).not.toHaveBeenCalled();
101+
});
102+
103+
it('should handle empty string input', () => {
104+
printTableFromInp('');
105+
expect(inputVerifier.verifyInput).toHaveBeenCalledWith('');
106+
expect(MockTable).not.toHaveBeenCalled();
107+
});
108+
109+
it('should handle null options', () => {
110+
const input = '[{"test": "data"}]';
111+
printTableFromInp(input, undefined);
112+
expect(MockTable).toHaveBeenCalled();
113+
expect(mockAddRow).toHaveBeenCalledWith({ test: 'data' });
114+
expect(mockPrintTable).toHaveBeenCalled();
115+
});
116+
117+
it('should handle empty options', () => {
118+
const input = '[{"test": "data"}]';
119+
printTableFromInp(input, '');
120+
expect(MockTable).toHaveBeenCalled();
121+
expect(mockAddRow).toHaveBeenCalledWith({ test: 'data' });
122+
expect(mockPrintTable).toHaveBeenCalled();
123+
});
124+
});
125+
126+
describe('real world examples', () => {
127+
beforeEach(() => {
128+
(inputVerifier.verifyInput as jest.Mock).mockReturnValue(true);
129+
(inputVerifier.verifyTableOptions as jest.Mock).mockReturnValue(true);
130+
});
131+
132+
it('should handle table with multiple columns and styling', () => {
133+
const input = '[{"id": 1, "value": 100}, {"id": 2, "value": 200}]';
134+
const options = '{"style": {"headerColor": "yellow"}, "columns": [{"name": "value", "color": "green"}]}';
135+
const expectedData = [
136+
{ id: 1, value: 100 },
137+
{ id: 2, value: 200 }
138+
];
139+
const expectedOptions = {
140+
style: { headerColor: 'yellow' },
141+
columns: [{ name: 'value', color: 'green' }]
142+
};
143+
144+
printTableFromInp(input, options);
145+
146+
expect(inputVerifier.verifyInput).toHaveBeenCalledWith(input);
147+
expect(inputVerifier.verifyTableOptions).toHaveBeenCalledWith(options);
148+
expect(MockTable).toHaveBeenCalledWith(expectedOptions);
149+
expectedData.forEach(row => {
150+
expect(mockAddRow).toHaveBeenCalledWith(row);
151+
});
152+
expect(mockPrintTable).toHaveBeenCalled();
153+
});
154+
});
155+
});
156+
});

0 commit comments

Comments
 (0)