forked from bpampuch/pdfmake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElementWriter.spec.js
241 lines (196 loc) · 5.43 KB
/
ElementWriter.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
'use strict';
var assert = require('assert');
var ElementWriter = require('../../js/ElementWriter').default;
describe('ElementWriter', function () {
var ew, ctx, page, fakePosition;
beforeEach(function () {
fakePosition = { fake: 'position' };
page = { items: [] };
ctx = {
x: 10,
y: 20,
availableWidth: 100,
availableHeight: 100,
getCurrentPage: function () {
return page;
},
getCurrentPosition: function () {
return fakePosition;
},
moveDown: function (offset) {
ctx.y += offset;
ctx.availableHeight -= offset;
}
};
ew = new ElementWriter(ctx);
});
function buildLine(height, alignment, x, y) {
return {
getHeight: function () {
return height;
},
getWidth: function () {
return 60;
},
clone: function () {
let result = {};
for (let key in this) {
if (this.hasOwnProperty(key)) {
result[key] = this[key];
}
}
return result;
},
inlines: [
{
alignment: alignment,
x: 0,
},
{
x: 30,
},
{
x: 50
}
],
x: x,
y: y
};
}
describe('addLine', function () {
it('should add lines to the current page if there\'s enough space', function () {
var line = buildLine(20);
var position = ew.addLine(line);
assert.equal(page.items.length, 1);
assert.equal(position, fakePosition);
});
it('should return position on page', function () {
var line = buildLine(20);
ew.pushContext(50, 50);
ew.pushContext(20, 30);
ew.pushContext(11, 40);
var position = ew.addLine(line);
assert.equal(position, fakePosition);
});
it('should not add line and return false if there\'s not enough space', function () {
var line = buildLine(120);
assert(!ew.addLine(line));
assert.equal(page.items.length, 0);
});
it('should set line.x and line.y to current context\'s values', function () {
var line = buildLine(30);
ew.addLine(line);
assert.equal(line.x, 10);
assert.equal(line.y, 20);
});
it('should update context.y and context.availableHeight', function () {
ew.addLine(buildLine(30));
assert.equal(ctx.y, 20 + 30);
assert.equal(ctx.availableHeight, 100 - 30);
});
describe('should support line alignment', function () {
it('right', function () {
var line = buildLine(30, 'right');
ew.addLine(line);
assert.equal(line.x, 10 + 100 - line.getWidth());
});
it('center', function () {
var line = buildLine(30, 'center');
ew.addLine(line);
assert.equal(line.x, 10 + (100 - line.getWidth()) / 2);
});
it('justify', function () {
var line = buildLine(30, 'justify');
ew.addLine(line);
assert.equal(line.x, 10);
var additionalSpacing = (100 - 60) / 2;
assert.equal(line.inlines[1].x, 30 + additionalSpacing);
assert.equal(line.inlines[2].x, 50 + 2 * additionalSpacing);
});
});
});
describe('addVector', function () {
it('should add vectors to the current page', function () {
ew.addVector({ type: 'rect', x: 10, y: 10 });
assert.equal(page.items.length, 1);
});
it('should offset vectors to the current position', function () {
var rect = { type: 'rect', x: 10, y: 10 };
var ellipse = { type: 'ellipse', x: 10, y: 10 };
var line = { type: 'line', x1: 10, x2: 50, y1: 10, y2: 20 };
var polyline = { type: 'polyline', points: [{ x: 0, y: 0 }, { x: 20, y: 20 }] };
ew.addVector(rect);
ew.addVector(ellipse);
ew.addVector(line);
ew.addVector(polyline);
assert.equal(rect.x, 20);
assert.equal(rect.y, 30);
assert.equal(ellipse.x, 20);
assert.equal(ellipse.y, 30);
assert.equal(line.x1, 20);
assert.equal(line.x2, 60);
assert.equal(line.y1, 30);
assert.equal(line.y2, 40);
assert.equal(polyline.points[0].x, 10);
assert.equal(polyline.points[0].y, 20);
assert.equal(polyline.points[1].x, 30);
assert.equal(polyline.points[1].y, 40);
});
});
describe('addFragment', function () {
var fragment;
beforeEach(function () {
fragment = {
items: [
{
type: 'line',
item: buildLine(30, 'left', 10, 10)
},
{
type: 'line',
item: buildLine(30, 'left', 10, 50)
},
{
type: 'vector',
item: { type: 'rect', x: 10, y: 20 }
},
{
type: 'vector',
item: { type: 'rect', x: 40, y: 60 }
}
]
};
});
it('should add all fragment vectors and lines', function () {
ew.addFragment(fragment);
assert.equal(page.items.length, 4);
});
it('should return false if fragment height is larger than available space', function () {
fragment.height = 120;
assert(!ew.addFragment(fragment));
});
it('should update current position', function () {
fragment.height = 50;
ew.addFragment(fragment);
assert.equal(ctx.y, 20 + 50);
});
it('should offset lines and vectors', function () {
ew.addFragment(fragment);
assert.equal(page.items[0].item.x, 20);
assert.equal(page.items[0].item.y, 30);
assert.equal(page.items[1].item.x, 20);
assert.equal(page.items[1].item.y, 70);
assert.equal(page.items[2].item.x, 20);
assert.equal(page.items[2].item.y, 40);
assert.equal(page.items[3].item.x, 50);
assert.equal(page.items[3].item.y, 80);
});
it('should not modify original line/vector positions', function () {
ew.addFragment(fragment);
assert.equal(fragment.items[0].item.x, 10);
assert.equal(fragment.items[0].item.y, 10);
assert.equal(fragment.items[3].item.x, 40);
assert.equal(fragment.items[3].item.y, 60);
});
});
});