1+ import { Table } from 'console-table-printer' ;
2+
3+ // Define interfaces for type checking
4+ interface Person {
5+ name : string ;
6+ age : number ;
7+ country : string ;
8+ salary ?: number ;
9+ }
10+
11+ interface TableWithStyles {
12+ addRow : ( row : Record < string , any > ) => void ;
13+ addRows : ( rows : Record < string , any > [ ] ) => void ;
14+ printTable : ( ) => void ;
15+ addRowIndexColumn : ( ) => void ;
16+ sortBy : ( columnName : string ) => void ;
17+ }
18+
19+ describe ( 'Console Table Printer - TypeScript Advanced Tests' , ( ) => {
20+ test ( 'should be properly typed and importable with advanced features' , ( ) => {
21+ // Verify that the Table class exists and is a constructor
22+ expect ( typeof Table ) . toBe ( 'function' ) ;
23+
24+ // Create a table with TypeScript type checking
25+ const p = new Table ( ) ;
26+ expect ( p ) . toBeInstanceOf ( Table ) ;
27+ } ) ;
28+
29+ test ( 'should create a table with rows' , ( ) => {
30+ const p = new Table ( ) ;
31+
32+ // Add rows
33+ p . addRows ( [
34+ { name : 'Alice' , age : 25 , country : 'USA' } ,
35+ { name : 'Bob' , age : 30 , country : 'Canada' } ,
36+ { name : 'Charlie' , age : 35 , country : 'UK' }
37+ ] ) ;
38+
39+ // The test passes if no TypeScript errors occur
40+ expect ( true ) . toBe ( true ) ;
41+ } ) ;
42+
43+ test ( 'should add rows with TypeScript type checking' , ( ) => {
44+ const p = new Table ( ) ;
45+
46+ // Add rows with TypeScript type checking
47+ const people : Person [ ] = [
48+ { name : 'Charlie' , age : 35 , country : 'UK' } ,
49+ { name : 'Alice' , age : 25 , country : 'USA' } ,
50+ { name : 'Bob' , age : 30 , country : 'Canada' }
51+ ] ;
52+
53+ p . addRows ( people ) ;
54+
55+ // The test passes if no TypeScript errors occur
56+ expect ( true ) . toBe ( true ) ;
57+ } ) ;
58+
59+ test ( 'should create a table with rows containing salary information' , ( ) => {
60+ const p = new Table ( ) ;
61+
62+ // Add rows with TypeScript type checking
63+ const people : Person [ ] = [
64+ { name : 'Alice' , age : 25 , country : 'USA' , salary : 100000 } ,
65+ { name : 'Bob' , age : 30 , country : 'Canada' , salary : 120000 } ,
66+ { name : 'Charlie' , age : 35 , country : 'UK' , salary : 90000 }
67+ ] ;
68+
69+ p . addRows ( people ) ;
70+
71+ // The test passes if no TypeScript errors occur
72+ expect ( true ) . toBe ( true ) ;
73+ } ) ;
74+
75+ test ( 'should create a table with custom columns' , ( ) => {
76+ // Create a table with custom columns
77+ const p = new Table ( {
78+ columns : [
79+ { name : 'name' , alignment : 'left' } ,
80+ { name : 'age' , alignment : 'right' } ,
81+ { name : 'country' , alignment : 'left' }
82+ ]
83+ } ) ;
84+
85+ // Add rows with TypeScript type checking
86+ const people : Person [ ] = [
87+ { name : 'Alice' , age : 25 , country : 'USA' } ,
88+ { name : 'Bob' , age : 30 , country : 'Canada' } ,
89+ { name : 'Charlie' , age : 35 , country : 'UK' }
90+ ] ;
91+
92+ p . addRows ( people ) ;
93+
94+ // The test passes if no TypeScript errors occur
95+ expect ( true ) . toBe ( true ) ;
96+ } ) ;
97+
98+ test ( 'should handle nested objects in rows' , ( ) => {
99+ interface NestedPerson {
100+ name : string ;
101+ details : {
102+ age : number ;
103+ country : string ;
104+ } ;
105+ }
106+
107+ const p = new Table ( ) ;
108+
109+ // Add rows with nested objects
110+ const people : NestedPerson [ ] = [
111+ { name : 'Alice' , details : { age : 25 , country : 'USA' } } ,
112+ { name : 'Bob' , details : { age : 30 , country : 'Canada' } } ,
113+ { name : 'Charlie' , details : { age : 35 , country : 'UK' } }
114+ ] ;
115+
116+ // Add rows using flat structure for display
117+ p . addRows ( [
118+ { name : people [ 0 ] . name , age : people [ 0 ] . details . age , country : people [ 0 ] . details . country } ,
119+ { name : people [ 1 ] . name , age : people [ 1 ] . details . age , country : people [ 1 ] . details . country } ,
120+ { name : people [ 2 ] . name , age : people [ 2 ] . details . age , country : people [ 2 ] . details . country }
121+ ] ) ;
122+
123+ // The test passes if no TypeScript errors occur
124+ expect ( true ) . toBe ( true ) ;
125+ } ) ;
126+ } ) ;
0 commit comments