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