forked from bpampuch/pdfmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDocumentContext.spec.js
352 lines (276 loc) · 8.64 KB
/
DocumentContext.spec.js
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
'use strict';
var assert = require('assert');
var rewire = require("rewire");
var DocumentContext = rewire('../../js/DocumentContext');
describe('DocumentContext', function () {
var pc;
beforeEach(function () {
pc = new DocumentContext.default({ width: 400, height: 800, orientation: 'portrait' }, { left: 40, right: 40, top: 60, bottom: 60 });
// pc.addPage();
});
it('should set initial values based on pageSize and pageMargins', function () {
assert.equal(pc.x, 40);
assert.equal(pc.y, 60);
assert.equal(pc.availableWidth, 400 - 40 - 40);
assert.equal(pc.availableHeight, 800 - 60 - 60);
});
describe('beginColumnGroup', function () {
it('should save current settings', function () {
pc.beginColumnGroup();
pc.x = 80;
pc.page = 3;
assert.equal(pc.snapshots.length, 1);
assert.equal(pc.snapshots[0].x, 40);
assert.equal(pc.snapshots[0].page, 0);
});
});
describe('beginColumn', function () {
it('should set y, page and availableHeight back to the values stored in beginColumnGroup', function () {
pc.beginColumnGroup();
pc.y = 150;
pc.page = 5;
pc.availableHeight = 123;
pc.beginColumn();
assert.equal(pc.y, 60);
assert.equal(pc.page, 0);
assert.equal(pc.availableHeight, 800 - 60 - 60);
});
it('should add offset to current x', function () {
pc.beginColumnGroup();
pc.beginColumn(50, 30);
assert.equal(pc.x, 40 + 30);
});
it('should add previous column widths to x when starting a new column', function () {
pc.beginColumnGroup();
pc.beginColumn(30);
assert.equal(pc.x, 40);
pc.beginColumn(20);
assert.equal(pc.x, 40 + 30);
});
it('should set availableWidth to the specified column width', function () {
pc.beginColumnGroup();
pc.beginColumn(30);
assert.equal(pc.availableWidth, 30);
});
it('should save context in endingCell if provided', function () {
var endingCell = {};
pc.beginColumnGroup();
pc.beginColumn(30, 0, endingCell);
pc.y = 150;
pc.page = 3;
pc.availableHeight = 123;
pc.beginColumn(30, 0);
assert.equal(endingCell._columnEndingContext.y, 150);
assert.equal(endingCell._columnEndingContext.page, 3);
assert.equal(endingCell._columnEndingContext.availableHeight, 123);
});
});
describe('completeColumnGroup', function () {
it('should set x to the value stored in beginColumnGroup', function () {
pc.beginColumnGroup();
pc.x = 150;
pc.completeColumnGroup();
assert.equal(pc.x, 40);
});
it('should set page to the value pointing to the end of the longest column', function () {
pc.beginColumnGroup();
pc.beginColumn(30);
pc.page = 3;
pc.beginColumn(30);
pc.page = 7;
pc.beginColumn(30);
pc.page = 4;
pc.completeColumnGroup();
assert.equal(pc.page, 7);
});
it('should skip non-ending-cells (spanning over multiple rows) during vsync', function () {
var endingCell = {};
pc.beginColumnGroup();
pc.beginColumn(30, 0, endingCell);
pc.y = 150;
pc.page = 3;
pc.availableHeight = 123;
pc.beginColumn(30, 0);
pc.y = 100;
pc.page = 3;
pc.completeColumnGroup();
assert.equal(pc.page, 3);
assert.equal(pc.y, 100);
});
it('non-ending-cells (spanning over multiple rows) should also work with nested columns', function () {
var endingCell = {};
var endingCell2 = {};
// external table
pc.beginColumnGroup();
// col1 spans over 2 rows
pc.beginColumn(30, 0, endingCell);
pc.y = 350;
pc.beginColumn(40);
pc.y = 100;
// column3 contains a nested table
pc.beginColumn(100);
pc.beginColumnGroup();
pc.beginColumn(20);
pc.y = 100;
pc.beginColumn(20);
pc.y = 120;
// col3.3 spans over 2 rows
pc.beginColumn(40, 0, endingCell2);
pc.y = 180;
pc.completeColumnGroup();
//// bottom of all non-spanned columns
assert.equal(pc.y, 120);
// second row (of nested table)
pc.beginColumnGroup();
pc.beginColumn(20);
pc.y = 10;
pc.beginColumn(20);
pc.y = 20;
// col3.3 spans over 2 rows
pc.beginColumn(40, 0);
pc.markEnding(endingCell2);
pc.completeColumnGroup();
//// spanned column was large enough to influence bottom
assert.equal(pc.y, 180);
pc.completeColumnGroup();
//// bottom of all non-spanned columns
assert.equal(pc.y, 180);
// second row
pc.beginColumnGroup();
pc.beginColumn(30);
pc.markEnding(endingCell);
pc.beginColumn(40);
pc.y = 50;
pc.beginColumn(100);
pc.y = 10;
pc.completeColumnGroup();
assert.equal(pc.y, 350);
});
});
describe('addMargin', function () {
it('should change both x and availableWidth', function () {
var x = pc.x;
var aWidth = pc.availableWidth;
pc.addMargin(10);
assert.equal(pc.x, x + 10);
assert.equal(pc.availableWidth, aWidth - 10);
});
it('should support left and right margins', function () {
var x = pc.x;
var aWidth = pc.availableWidth;
pc.addMargin(10, 15);
assert.equal(pc.x, x + 10);
assert.equal(pc.availableWidth, aWidth - 10 - 15);
});
});
describe('moveDown', function () {
it('should change both y and availableHeight', function () {
var y = pc.y;
var ah = pc.availableHeight;
pc.moveDown(123);
assert.equal(pc.y, y + 123);
assert.equal(pc.availableHeight, ah - 123);
});
it('should return true if there is still some space left on the page', function () {
assert(pc.moveDown(123));
});
it('should return false if there\'s no space left after the operation', function () {
assert(!pc.moveDown(1200));
});
});
describe('moveToNext page', function () {
it('should add new page in portrait', function () {
pc.moveToNextPage();
assert.equal(pc.pages[0].pageSize.orientation, 'portrait');
assert.equal(pc.pages[1].pageSize.orientation, 'portrait');
});
it('should add a new page in the same orientation as the previous one', function () {
pc.moveToNextPage('landscape');
pc.moveToNextPage();
pc.moveToNextPage('portrait');
pc.moveToNextPage();
assert.equal(pc.pages[0].pageSize.orientation, 'portrait');
assert.equal(pc.pages[1].pageSize.orientation, 'landscape');
assert.equal(pc.pages[2].pageSize.orientation, 'landscape');
assert.equal(pc.pages[3].pageSize.orientation, 'portrait');
assert.equal(pc.pages[4].pageSize.orientation, 'portrait');
});
});
describe('moveToRelative', function () {
it('should change coordinates', function () {
pc.x = 100;
pc.y = 200;
pc.moveToRelative(50, 100);
assert.equal(pc.x, 150);
assert.equal(pc.y, 300);
});
});
describe('addPage', function () {
var pageSize;
beforeEach(function () {
pageSize = { width: 200, height: 400, orientation: 'landscape' };
});
it('should add a new page', function () {
pc.addPage(pageSize);
assert.equal(pc.pages.length, 2);
});
it('should return added page', function () {
var page = pc.addPage(pageSize);
assert.equal(page, pc.pages[pc.pages.length - 1]);
});
it('should set y, availableHeight and availableWidth on page to initial values', function () {
pc.y = 123;
pc.availableHeight = 123;
pc.initializePage();
assert.equal(pc.y, 60);
assert.equal(pc.availableHeight, 800 - 60 - 60);
assert.equal(pc.availableWidth, 400 - 40 - 40);
});
it('should keep column width when in column group, but set page width', function () {
pc.beginColumnGroup();
pc.beginColumn(100, 0, {});
pc.initializePage();
assert.equal(pc.availableWidth, 100);
pc.completeColumnGroup();
assert.equal(pc.availableWidth, 400 - 40 - 40);
});
});
describe('bottomMostContext', function () {
it('should return context with larger page if pages are different', function () {
var result = DocumentContext.__get__('bottomMostContext')({ page: 2, y: 10 }, { page: 3, y: 5 });
assert.equal(result.page, 3);
assert.equal(result.y, 5);
});
it('should return context with larger y if both contexts have the same page', function () {
var result = DocumentContext.__get__('bottomMostContext')({ page: 3, y: 100 }, { page: 3, y: 50 });
assert.equal(result.page, 3);
assert.equal(result.y, 100);
});
});
it('should support nesting', function () {
pc.beginColumnGroup();
pc.beginColumn(50);
pc.y = 200;
pc.beginColumn(40);
pc.y = 150;
pc.beginColumn(80);
pc.beginColumnGroup();
assert.equal(pc.snapshots.length, 2);
assert.equal(pc.snapshots[1].x, 40 + 50 + 40);
pc.beginColumn(20);
pc.y = 240;
pc.page = 2;
pc.beginColumn(20);
pc.y = 260;
pc.completeColumnGroup();
assert.equal(pc.snapshots.length, 1);
assert.equal(pc.x, 40 + 50 + 40);
assert.equal(pc.page, 2);
assert.equal(pc.y, 240);
pc.completeColumnGroup();
assert.equal(pc.snapshots.length, 0);
assert.equal(pc.x, 40);
assert.equal(pc.page, 2);
assert.equal(pc.y, 240);
});
});