-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathcell.spec.js
171 lines (160 loc) · 5.1 KB
/
cell.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
/* global describe, it, expect, comparePdf jsPDF */
describe("Module: Cell", () => {
beforeAll(loadGlobals);
it("getTextDimensions", () => {
var doc = new jsPDF("p", "pt", "a4");
expect(
doc.getTextDimensions(doc.splitTextToSize("Octocat loves jsPDF", 50)).w
).toEqual(43.35999999999999);
expect(
doc.getTextDimensions(doc.splitTextToSize("Octocat loves jsPDF", 50)).h
).toEqual(71.19999999999999);
expect(doc.getTextDimensions("Octocat loves jsPDF").w).toEqual(
144.48000000000002
);
expect(doc.getTextDimensions("Octocat loves jsPDF").h).toEqual(16);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 150 }).w
).toEqual(144.48000000000002);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 150 }).h
).toEqual(16);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 150, lineHeightFactor: 2 }).h
).toEqual(16);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100 }).h
).toEqual(34.4);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100, lineHeightFactor: undefined }).h
).toEqual(34.4);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100, lineHeightFactor: 2 }).h
).toEqual(48);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100 }).w
).toEqual(96.64000000000001);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100, lineHeightFactor: 2 }).w
).toEqual(96.64000000000001);
expect(
doc.getTextDimensions("Octocat loves jsPDF\njsPDF loves Octocat", { maxWidth: 100 }).h
).toEqual(71.19999999999999);
expect(
doc.getTextDimensions("Octocat loves jsPDF\njsPDF loves Octocat", { maxWidth: 100 }).w
).toEqual(96.64000000000001);
expect(doc.getTextDimensions("").w).toEqual(0);
expect(doc.getTextDimensions("").h).toEqual(0);
expect(doc.getTextDimensions([""]).w).toEqual(0);
expect(doc.getTextDimensions([""]).h).toEqual(0);
expect(function() {
doc.getTextDimensions();
}).toThrow(
new Error(
"getTextDimensions expects text-parameter to be of type String or type Number or an Array of Strings."
)
);
});
var generateData = function(amount) {
var result = [];
var data = {
coin: "100",
game_group: "GameGroup",
game_name: "XPTO2",
game_version: "25",
machine: "20485861",
vlt: "0"
};
for (var i = 0; i < amount; i += 1) {
result.push(data);
}
return result;
};
function createHeaders(keys) {
return keys.map(key => ({
name: key,
prompt: key,
width: 65,
align: "center",
padding: 0
}));
}
var headerNames = [
"coin",
"game_group",
"game_name",
"game_version",
"machine",
"vlt"
];
var header = createHeaders(headerNames);
it("table with CellConfig[]", () => {
var doc = new jsPDF({
putOnlyUsedFonts: true,
orientation: "landscape",
floatPrecision: 2
});
doc.table(1, 1, generateData(100), header);
comparePdf(doc.output(), "table.pdf");
});
it("table with string[] and without autoSize", () => {
var doc = new jsPDF({
putOnlyUsedFonts: true,
orientation: "landscape",
floatPrecision: 2
});
doc.table(1, 1, generateData(100), headerNames);
comparePdf(doc.output(), "table-autoSize-headerNames.pdf");
});
it("table with string[] and autoSize", () => {
var doc = new jsPDF({
putOnlyUsedFonts: true,
orientation: "landscape",
floatPrecision: 2
});
doc.table(1, 1, generateData(100), headerNames, { autoSize: true });
comparePdf(doc.output(), "table-autoSize-headerNames.pdf");
});
it("table-autoSize", () => {
var doc = new jsPDF({
putOnlyUsedFonts: true,
orientation: "landscape",
floatPrecision: 2
});
doc.table(1, 1, generateData(100), header, { autoSize: true });
comparePdf(doc.output(), "table-autoSize.pdf");
});
it("table-formatted", () => {
var doc = new jsPDF({
putOnlyUsedFonts: true,
orientation: "landscape",
floatPrecision: 2
});
doc.table(1, 1, generateData(100), header, {
rowStart: function(e, docInstance) {
// docInstance equal to doc
if (17 < e.row && e.row < 36)
docInstance.setTextColor(255,0,0);
else
docInstance.setTextColor(0,0,0);
},
cellStart: function(e, docInstance) {
// docInstance equal to doc
if (e.row === 27 && e.col === 3)
docInstance.setFont(undefined, "bold");
else
docInstance.setFont(undefined, "normal");
}
});
comparePdf(doc.output(), "table-formatted.pdf");
});
it("table error handling", () => {
var doc = new jsPDF({ putOnlyUsedFonts: true, orientation: "landscape" });
expect(function() {
doc.table(1, 1, undefined, header, { autoSize: true });
}).toThrow(new Error("No data for PDF table."));
expect(function() {
doc.printHeaderRow(1, false);
}).toThrow(new Error("Property tableHeaderRow does not exist."));
});
});