UNPKG

6.46 kBJavaScriptView Raw
1var Table = require('../src/table');
2var colors = require('colors/safe');
3
4module.exports = function(runTest) {
5
6 function it(name, fn) {
7 var result = fn();
8 runTest(name, result[0], result[1], result[2]);
9 }
10
11 it('Basic Usage',function(){
12 function makeTable(){
13 // By default, headers will be red, and borders will be grey
14 // Those defaults are overwritten in a lot of these examples and within the tests.
15 // It makes unit tests easier and visually cleaner, and does not require a screenshot image.
16 var table = new Table({head:['a','b']});
17
18 table.push(['c','d']);
19
20 return table;
21 }
22
23 var expected = [
24 colors.gray('┌───') + colors.gray('┬───┐')
25 , colors.gray('│') + colors.red(' a ') + colors.gray('│') + colors.red(' b ') + colors.gray('│')
26 , colors.gray('├───') + colors.gray('┼───┤')
27 , colors.gray('│') + (' c ') + colors.gray('│') + (' d ') + colors.gray('│')
28 , colors.gray('└───') + colors.gray('┴───┘')
29 ];
30
31 return [makeTable,expected,'basic-usage-with-colors'];
32 });
33
34 it('Basic Usage - No color styles', function (){
35 function makeTable(){
36 var table = new Table({
37 head: ['Rel', 'Change', 'By', 'When']
38 , style: {
39 'padding-left': 1
40 , 'padding-right': 1
41 , head: [] //overriding header style to not use colors
42 , border: [] //overriding border style to not use colors
43 }
44 , colWidths: [6, 21, 25, 17]
45 });
46
47 table.push(
48 ['v0.1', 'Testing something cool', 'rauchg@gmail.com', '7 minutes ago']
49 , ['v0.1', 'Testing something cool', 'rauchg@gmail.com', '8 minutes ago']
50 );
51
52 return table;
53 }
54
55 var expected = [
56 '┌──────┬─────────────────────┬─────────────────────────┬─────────────────┐'
57 , '│ Rel │ Change │ By │ When │'
58 , '├──────┼─────────────────────┼─────────────────────────┼─────────────────┤'
59 , '│ v0.1 │ Testing something … │ rauchg@gmail.com │ 7 minutes ago │'
60 , '├──────┼─────────────────────┼─────────────────────────┼─────────────────┤'
61 , '│ v0.1 │ Testing something … │ rauchg@gmail.com │ 8 minutes ago │'
62 , '└──────┴─────────────────────┴─────────────────────────┴─────────────────┘'
63 ];
64
65 return [makeTable,expected];
66 });
67
68
69 it('Create vertical tables by adding objects a that specify key-value pairs', function() {
70 function makeTable(){
71 var table = new Table({ style: {'padding-left':0, 'padding-right':0, head:[], border:[]} });
72
73 table.push(
74 {'v0.1': 'Testing something cool'}
75 , {'v0.1': 'Testing something cool'}
76 );
77
78 return table;
79 }
80
81 var expected = [
82 '┌────┬──────────────────────┐'
83 , '│v0.1│Testing something cool│'
84 , '├────┼──────────────────────┤'
85 , '│v0.1│Testing something cool│'
86 , '└────┴──────────────────────┘'
87 ];
88
89 return [makeTable,expected];
90 });
91
92 it('Cross tables are similar to vertical tables, but include an empty string for the first header', function() {
93 function makeTable(){
94 var table = new Table({ head: ["", "Header 1", "Header 2"], style: {'padding-left':0, 'padding-right':0, head:[], border:[]} }); // clear styles to prevent color output
95
96 table.push(
97 {"Header 3": ['v0.1', 'Testing something cool'] }
98 , {"Header 4": ['v0.1', 'Testing something cool'] }
99 );
100
101 return table;
102 }
103
104 var expected = [
105 '┌────────┬────────┬──────────────────────┐'
106 , '│ │Header 1│Header 2 │'
107 , '├────────┼────────┼──────────────────────┤'
108 , '│Header 3│v0.1 │Testing something cool│'
109 , '├────────┼────────┼──────────────────────┤'
110 , '│Header 4│v0.1 │Testing something cool│'
111 , '└────────┴────────┴──────────────────────┘'
112 ];
113
114 return [makeTable,expected];
115 });
116
117 it('Stylize the table with custom border characters', function (){
118 function makeTable(){
119 var table = new Table({
120 chars: {
121 'top': '═'
122 , 'top-mid': '╤'
123 , 'top-left': '╔'
124 , 'top-right': '╗'
125 , 'bottom': '═'
126 , 'bottom-mid': '╧'
127 , 'bottom-left': '╚'
128 , 'bottom-right': '╝'
129 , 'left': '║'
130 , 'left-mid': '╟'
131 , 'right': '║'
132 , 'right-mid': '╢'
133 },
134 style: {
135 head: []
136 , border: []
137 }
138 });
139
140 table.push(
141 ['foo', 'bar', 'baz']
142 , ['frob', 'bar', 'quuz']
143 );
144
145 return table;
146 }
147
148 var expected = [
149 '╔══════╤═════╤══════╗'
150 , '║ foo │ bar │ baz ║'
151 , '╟──────┼─────┼──────╢'
152 , '║ frob │ bar │ quuz ║'
153 , '╚══════╧═════╧══════╝'
154 ];
155
156 return [makeTable,expected];
157 });
158
159};
\No newline at end of file