UNPKG

10.5 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('use colSpan to span columns - (colSpan above normal cell)',function(){
12 function makeTable(){
13 var table = new Table({style:{head:[],border:[]}});
14
15 table.push(
16 [{colSpan:2,content:'greetings'}],
17 [{colSpan:2,content:'greetings'}],
18 ['hello','howdy']
19 );
20
21 return table;
22 }
23
24 var expected = [
25 '┌───────────────┐'
26 , '│ greetings │'
27 , '├───────────────┤'
28 , '│ greetings │'
29 , '├───────┬───────┤'
30 , '│ hello │ howdy │'
31 , '└───────┴───────┘'
32 ];
33
34 return [makeTable,expected];
35 });
36
37 it('use colSpan to span columns - (colSpan below normal cell)',function(){
38 function makeTable(){
39 var table = new Table({style:{head:[],border:[]}});
40
41 table.push(
42 ['hello','howdy'],
43 [{colSpan:2,content:'greetings'}],
44 [{colSpan:2,content:'greetings'}]
45 );
46
47 return table;
48 }
49
50 var expected = [
51 '┌───────┬───────┐'
52 , '│ hello │ howdy │'
53 , '├───────┴───────┤'
54 , '│ greetings │'
55 , '├───────────────┤'
56 , '│ greetings │'
57 , '└───────────────┘'
58 ];
59
60 return [makeTable,expected];
61 });
62
63 it('use rowSpan to span rows - (rowSpan on the left side)',function(){
64 function makeTable(){
65 var table = new Table({style:{head:[],border:[]}});
66
67 table.push(
68 [{rowSpan:2,content:'greetings'},{rowSpan:2,content:'greetings',vAlign:'center'},'hello'],
69 ['howdy']
70 );
71
72 return table;
73 }
74
75 var expected = [
76 '┌───────────┬───────────┬───────┐'
77 , '│ greetings │ │ hello │'
78 , '│ │ greetings ├───────┤'
79 , '│ │ │ howdy │'
80 , '└───────────┴───────────┴───────┘'
81 ];
82
83 return [makeTable,expected];
84 });
85
86
87 it('use rowSpan to span rows - (rowSpan on the right side)',function(){
88 function makeTable(){
89 var table = new Table({style:{head:[],border:[]}});
90
91 table.push(
92 ['hello',{rowSpan:2,content:'greetings'},{rowSpan:2,content:'greetings',vAlign:'bottom'}],
93 ['howdy']
94 );
95
96 return table;
97 }
98
99 var expected = [
100 '┌───────┬───────────┬───────────┐'
101 , '│ hello │ greetings │ │'
102 , '├───────┤ │ │'
103 , '│ howdy │ │ greetings │'
104 , '└───────┴───────────┴───────────┘'
105 ];
106
107 return[makeTable,expected];
108 });
109
110 it('mix rowSpan and colSpan together for complex table layouts',function(){
111 function makeTable(){
112 var table = new Table({style:{head:[],border:[]}});
113
114 table.push(
115 [{content:'hello',colSpan:2},{rowSpan:2, colSpan:2,content:'sup'},{rowSpan:3,content:'hi'}],
116 [{content:'howdy',colSpan:2}],
117 ['o','k','','']
118 );
119
120 return table;
121 }
122
123 var expected = [
124 '┌───────┬─────┬────┐'
125 , '│ hello │ sup │ hi │'
126 , '├───────┤ │ │'
127 , '│ howdy │ │ │'
128 , '├───┬───┼──┬──┤ │'
129 , '│ o │ k │ │ │ │'
130 , '└───┴───┴──┴──┴────┘'
131 ];
132
133 return [makeTable,expected];
134 });
135
136 it('multi-line content will flow across rows in rowSpan cells',function(){
137 function makeTable(){
138 var table = new Table({style:{head:[],border:[]}});
139
140 table.push(
141 ['hello',{rowSpan:2,content:'greetings\nfriends'},{rowSpan:2,content:'greetings\nfriends'}],
142 ['howdy']
143 );
144
145 return table;
146 }
147
148 var expected = [
149 '┌───────┬───────────┬───────────┐'
150 , '│ hello │ greetings │ greetings │'
151 , '├───────┤ friends │ friends │'
152 , '│ howdy │ │ │'
153 , '└───────┴───────────┴───────────┘'
154 ];
155
156 return [makeTable, expected];
157 });
158
159 it('multi-line content will flow across rows in rowSpan cells - (complex layout)',function(){
160 function makeTable(){
161 var table = new Table({style:{head:[],border:[]}});
162
163 table.push(
164 [{content:'hello',colSpan:2},{rowSpan:2, colSpan:2,content:'sup\nman\nhey'},{rowSpan:3,content:'hi\nyo'}],
165 [{content:'howdy',colSpan:2}],
166 ['o','k','','']
167 );
168
169 return table;
170 }
171
172 var expected = [
173 '┌───────┬─────┬────┐'
174 , '│ hello │ sup │ hi │'
175 , '├───────┤ man │ yo │'
176 , '│ howdy │ hey │ │'
177 , '├───┬───┼──┬──┤ │'
178 , '│ o │ k │ │ │ │'
179 , '└───┴───┴──┴──┴────┘'
180 ];
181
182 return [makeTable,expected];
183 });
184
185 it('rowSpan cells can have a staggered layout',function(){
186 function makeTable(){
187 var table = new Table({style:{head:[],border:[]}});
188
189 table.push(
190 [{content:'a',rowSpan:2},'b'],
191 [{content:'c',rowSpan:2}],
192 ['d']
193 );
194
195 return table;
196 }
197
198 var expected = [
199 '┌───┬───┐'
200 , '│ a │ b │'
201 , '│ ├───┤'
202 , '│ │ c │'
203 , '├───┤ │'
204 , '│ d │ │'
205 , '└───┴───┘'
206 ];
207
208 return [makeTable,expected];
209 });
210
211 it('the layout manager automatically create empty cells to fill in the table',function(){
212 function makeTable(){
213 var table = new Table({style:{head:[],border:[]}});
214
215 //notice we only create 3 cells here, but the table ends up having 6.
216 table.push(
217 [{content:'a',rowSpan:3,colSpan:2},'b'],
218 [],
219 [{content:'c',rowSpan:2,colSpan:2}],
220 []
221 );
222 return table;
223 }
224
225 var expected = [
226 '┌───┬───┬──┐'
227 , '│ a │ b │ │' // top-right and bottom-left cells are automatically created to fill the empty space
228 , '│ ├───┤ │'
229 , '│ │ │ │'
230 , '│ ├───┴──┤'
231 , '│ │ c │'
232 , '├───┤ │'
233 , '│ │ │'
234 , '└───┴──────┘'
235 ];
236
237 return [makeTable,expected];
238 });
239
240 it('use table `rowHeights` option to fix row height. The truncation symbol will be shown on the last line.',function(){
241 function makeTable(){
242 var table = new Table({rowHeights:[2],style:{head:[],border:[]}});
243
244 table.push(['hello\nhi\nsup']);
245
246 return table;
247 }
248
249 var expected = [
250 '┌───────┐'
251 , '│ hello │'
252 , '│ hi… │'
253 , '└───────┘'
254 ];
255
256 return [makeTable,expected];
257 });
258
259 it('if `colWidths` is not specified, the layout manager will automatically widen rows to fit the content',function(){
260 function makeTable(){
261 var table = new Table({style:{head:[],border:[]}});
262
263 table.push(
264 [{colSpan:2,content:'hello there'}],
265 ['hi', 'hi']
266 );
267
268 return table;
269 }
270
271 var expected = [
272 '┌─────────────┐'
273 , '│ hello there │'
274 , '├──────┬──────┤'
275 , '│ hi │ hi │'
276 , '└──────┴──────┘'
277 ];
278
279 return [makeTable,expected];
280 });
281
282 it('you can specify a column width for only the first row, other rows will be automatically widened to fit content',function(){
283 function makeTable(){
284 var table = new Table({colWidths:[4],style:{head:[],border:[]}});
285
286 table.push(
287 [{colSpan:2,content:'hello there'}],
288 ['hi',{hAlign:'center',content:'hi'}]
289 );
290
291 return table;
292 }
293
294 var expected = [
295 '┌─────────────┐'
296 , '│ hello there │'
297 , '├────┬────────┤'
298 , '│ hi │ hi │'
299 , '└────┴────────┘'
300 ];
301
302 return [makeTable, expected];
303 });
304
305 it('a column with a null column width will be automatically widened to fit content',function(){
306 function makeTable(){
307 var table = new Table({colWidths:[null, 4],style:{head:[],border:[]}});
308
309 table.push(
310 [{colSpan:2,content:'hello there'}],
311 [{hAlign:'right',content:'hi'}, 'hi']
312 );
313
314 return table;
315 }
316
317 var expected = [
318 '┌─────────────┐'
319 , '│ hello there │'
320 , '├────────┬────┤'
321 , '│ hi │ hi │'
322 , '└────────┴────┘'
323 ];
324
325 return [makeTable,expected];
326 });
327
328 it('feel free to use colors in your content strings, column widths will be calculated correctly',function(){
329 function makeTable(){
330 var table = new Table({colWidths:[5],style:{head:[],border:[]}});
331
332 table.push([colors.red('hello')]);
333
334 return table;
335 }
336
337 var expected = [
338 '┌─────┐'
339 , '│ ' + colors.red('he') + '… │'
340 , '└─────┘'
341 ];
342
343 return [makeTable,expected,'truncation-with-colors'];
344 });
345};
346
347/*
348
349 var expected = [
350 '┌──┬───┬──┬──┐'
351 , '│ │ │ │ │'
352 , '├──┼───┼──┼──┤'
353 , '│ │ … │ │ │'
354 , '├──┼───┼──┼──┤'
355 , '│ │ … │ │ │'
356 , '└──┴───┴──┴──┘'
357 ];
358 */
\No newline at end of file