-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathtable.spec.js
More file actions
449 lines (437 loc) · 14.1 KB
/
table.spec.js
File metadata and controls
449 lines (437 loc) · 14.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
import { runDocTest } from './helpers';
describe('table', function () {
test('simple table', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Italic.ttf');
doc.table({
data: [
['Column 1', 'Column 2', 'Column 3'],
['One value goes here', 'Another one here', 'OK?'],
],
});
});
});
test('defining column widths', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Italic.ttf');
doc.table({
columnStyles: [100, '*', 200, '*'],
data: [
['width=100', 'star-sized', 'width=200', 'star-sized'],
[
'fixed-width cells have exactly the specified width',
{ text: 'nothing interesting here', textColor: 'grey' },
{ text: 'nothing interesting here', textColor: 'grey' },
{ text: 'nothing interesting here', textColor: 'grey' },
],
],
});
});
});
test('defining row heights', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Italic.ttf');
doc.table({
rowStyles: [20, 50, 70],
data: [
['row 1 with height 20', 'column B'],
['row 2 with height 50', 'column B'],
['row 3 with height 70', 'column B'],
],
});
doc.moveDown();
doc.text('With same height:');
doc.table({
rowStyles: 40,
data: [
['row 1', 'column B'],
['row 2', 'column B'],
['row 3', 'column B'],
],
});
doc.moveDown();
doc.text('With height from function:');
doc.table({
rowStyles: (row) => (row + 1) * 25,
data: [
['row 1', 'column B'],
['row 2', 'column B'],
['row 3', 'column B'],
],
});
});
});
test('column/row spans', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Italic.ttf');
doc.table({
columnStyles: [200, '*', '*'],
rowStyles: (i) => {
return i < 2
? {
font: { src: 'tests/fonts/Roboto-MediumItalic.ttf' },
align: { x: 'center' },
}
: {
textColor: 'grey',
};
},
data: [
[{ colSpan: 2, text: 'Header with Colspan = 2' }, 'Header 3'],
['Header 1', 'Header 2', 'Header 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
[
{
rowSpan: 3,
text: 'rowspan set to 3\nLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor',
},
'Sample value 2',
'Sample value 3',
],
['Sample value 2', 'Sample value 3'],
['Sample value 2', 'Sample value 3'],
[
'Sample value 1',
{
colSpan: 2,
rowSpan: 2,
text: 'Both:\nrowspan and colspan\ncan be defined at the same time',
},
],
['Sample value 1'],
],
});
});
});
test('styling tables', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Italic.ttf');
doc.text('noBorders:').moveDown(1);
doc
.table({
rowStyles: { border: false },
data: [
['Header 1', 'Header 2', 'Header 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
],
})
.moveDown(2)
.text('headerLineOnly:')
.moveDown(1)
.table({
rowStyles: (i) => {
return i < 1 ? { border: [0, 0, 1, 0] } : { border: false };
},
data: [
['Header 1', 'Header 2', 'Header 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
],
})
.moveDown(2)
.text('lightHorizontalLines:')
.moveDown(1)
.table({
rowStyles: (i) => {
return i < 1
? {
border: [0, 0, 2, 0],
borderColor: 'black',
}
: {
border: [0, 0, 1, 0],
borderColor: '#aaa',
};
},
data: [
['Header 1', 'Header 2', 'Header 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
],
})
.moveDown(2)
.text('but you can provide a custom styler as well')
.moveDown(1)
.table({
defaultStyle: { border: 1, borderColor: 'gray' },
columnStyles: (i) => {
if (i === 0) {
return { border: { left: 2 }, borderColor: { left: 'black' } };
}
if (i === 2) {
return { border: { right: 2 }, borderColor: { right: 'black' } };
}
},
rowStyles: (i) => {
if (i === 0) {
return { border: { top: 2 }, borderColor: { top: 'black' } };
}
if (i === 3) {
return {
border: { bottom: 2 },
borderColor: { bottom: 'black' },
};
}
},
data: [
['Header 1', 'Header 2', 'Header 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
],
})
.moveDown(2)
.text('zebra style')
.moveDown(1)
.table({
rowStyles: (i) => {
if (i % 2 === 0) return { backgroundColor: '#ccc' };
},
data: [
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
['Sample value 1', 'Sample value 2', 'Sample value 3'],
],
});
});
});
test('optional border', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Italic.ttf');
doc.table({
data: [
[
{
border: [true, false, false, false],
backgroundColor: '#eee',
text: 'border:\n[true, false, false, false]',
},
{
border: false,
backgroundColor: '#ddd',
text: 'border:\nfalse',
},
{
border: true,
backgroundColor: '#eee',
text: 'border:\ntrue',
},
],
[
{
rowSpan: 3,
border: true,
backgroundColor: '#eef',
text: 'rowSpan: 3\n\nborder:\ntrue',
},
{
border: undefined,
backgroundColor: '#eee',
text: 'border:\nundefined (default)',
},
{
border: [false, false, false, true],
backgroundColor: '#ddd',
text: 'border:\n[false, false, false, true]',
},
],
[
{
colSpan: 2,
border: true,
backgroundColor: '#efe',
text: 'colSpan: 2\n\nborder:\ntrue',
},
],
[
{
border: 0,
backgroundColor: '#eee',
text: 'border:\n0 (same as false)',
},
{
border: [false, true, true, false],
backgroundColor: '#ddd',
text: 'border:\n[false, true, true, false]',
},
],
],
});
doc.moveDown(2);
doc.table({
defaultStyle: { border: false, width: 60 },
data: [
['', 'column 1', 'column 2', 'column 3'],
[
'row 1',
{
rowSpan: 3,
colSpan: 3,
border: true,
backgroundColor: '#ccc',
text: 'rowSpan: 3\ncolSpan: 3\n\nborder:\n[true, true, true, true]',
},
],
['row 2'],
['row 3'],
],
});
});
});
test('iterables', function () {
return runDocTest(async function (doc) {
doc.font('tests/fonts/Roboto-Italic.ttf');
const syncIterator = (function* () {
yield ['1', '2'];
yield ['3', '4'];
})();
doc.table({ data: syncIterator });
});
});
test('rotated text', function () {
return runDocTest(function (doc) {
doc.font('tests/fonts/Roboto-Italic.ttf');
doc.fontSize(7);
doc.table({
debug: true,
defaultStyle: { height: 50, width: 50, padding: 4 },
columnStyles: (i) => ({ textOptions: { rotation: i * 45 } }),
rowStyles: [
{ align: { x: 'left', y: 'top' } },
{ align: { x: 'left', y: 'center' } },
{ align: { x: 'left', y: 'bottom' } },
{ align: { x: 'center', y: 'top' } },
{ align: { x: 'center', y: 'center' } },
{ align: { x: 'center', y: 'bottom' } },
{ align: { x: 'right', y: 'top' } },
{ align: { x: 'right', y: 'center' } },
{ align: { x: 'right', y: 'bottom' } },
{ align: { x: 'justify', y: 'top' } },
{ align: { x: 'justify', y: 'center' } },
{ align: { x: 'justify', y: 'bottom' } },
],
data: [
Array(9)
.fill(null)
.map((_, i) => `L,T @${(i * 45).toString().padStart(3, '0')}`),
Array(9)
.fill(null)
.map((_, i) => `L,C @${(i * 45).toString().padStart(3, '0')}`),
Array(9)
.fill(null)
.map((_, i) => `L,B @${(i * 45).toString().padStart(3, '0')}`),
Array(9)
.fill(null)
.map((_, i) => `C,T @${(i * 45).toString().padStart(3, '0')}`),
Array(9)
.fill(null)
.map((_, i) => `C,C @${(i * 45).toString().padStart(3, '0')}`),
Array(9)
.fill(null)
.map((_, i) => `C,B @${(i * 45).toString().padStart(3, '0')}`),
Array(9)
.fill(null)
.map((_, i) => `R,T @${(i * 45).toString().padStart(3, '0')}`),
Array(9)
.fill(null)
.map((_, i) => `R,C @${(i * 45).toString().padStart(3, '0')}`),
Array(9)
.fill(null)
.map((_, i) => `R,B @${(i * 45).toString().padStart(3, '0')}`),
Array(9)
.fill(null)
.map((_, i) => `J,T @${(i * 45).toString().padStart(3, '0')}`),
Array(9)
.fill(null)
.map((_, i) => `J,C @${(i * 45).toString().padStart(3, '0')}`),
Array(9)
.fill(null)
.map((_, i) => `J,B @${(i * 45).toString().padStart(3, '0')}`),
],
});
});
});
test('line flowing rotated text', function () {
return runDocTest(
{ layout: 'landscape', margin: 19 },
async function (doc) {
doc.font('tests/fonts/Roboto-Italic.ttf');
doc.table({
debug: true,
defaultStyle: { align: 'center' },
columnStyles: [{ width: 200, textOptions: { rotation: 45 } }],
data: [
[
'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Praesent laoreet pulvinar velit, at interdum velit ullamcorper at. Sed a velit vulputate, tristique metus eu, hendrerit nisi. Ut vitae nisl in sapien ultricies commodo et consectetur. nibh. Etiam tempor in orci quis viverra. Ut commodo, purus ac elementum ultricies, diam risus ultricies turpis, ac accumsan orci turpis a libero. Curabitur convallis nisi sed nisi elementum sollicitudin. Aenean eget urna luctus, blandit nulla eget, dapibus neque. Aliquam ut arcu erat.',
],
],
});
},
);
});
test('multi line rotated text', function () {
return runDocTest(
{ layout: 'landscape', margin: 19 },
async function (doc) {
doc.font('tests/fonts/Roboto-Italic.ttf');
doc.table({
debug: true,
defaultStyle: { align: 'center', width: 200, height: 200 },
columnStyles: [{ textOptions: { rotation: 90 } }],
data: [['Hello\nWorld']],
});
},
);
});
test('multi page table', function () {
return runDocTest({ size: [500, 300] }, function (doc) {
doc.font('tests/fonts/Roboto-Italic.ttf');
doc.table({
debug: true,
data: [
[
{
rowSpan: 3,
text: 'LEFT_COLUMN',
},
'RIGHT_COLUM_ROW_1\n'.repeat(5),
],
['RIGHT_COLUM_ROW_2\n'.repeat(5)],
['RIGHT_COLUM_ROW_3'],
],
});
});
});
test('ignore trailing line break - issue #1620', function () {
return runDocTest(
{
systemFonts: true,
failureThreshold: 0.002,
failureThresholdType: 'percent',
},
function (doc) {
doc.table({
debug: true,
data: [['trailingLineBreak\ndefault (true)']],
});
doc.table({
debug: true,
defaultStyle: { textOptions: { trailingLineBreak: false } },
data: [['trailingLineBreak\nfalse']],
});
},
);
});
});