forked from parallax/jsPDF
/
cell.spec.js
104 lines (97 loc) · 2.94 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
/* global describe, it, expect, comparePdf jsPDF */
describe("Module: Cell", () => {
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(doc.splitTextToSize("Octocat loves jsPDF", 150)).w
).toEqual(144.48000000000002);
expect(
doc.getTextDimensions(doc.splitTextToSize("Octocat loves jsPDF", 150)).h
).toEqual(16);
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", {maxTextLength: 100 }).h,).toEqual(32);
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 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 header = createHeaders([
"coin",
"game_group",
"game_name",
"game_version",
"machine",
"vlt"
]);
it("cellInitialize", () => {
var doc = new jsPDF({ putOnlyUsedFonts: true, orientation: "landscape" });
doc.cellInitialize();
});
it("table", () => {
var doc = new jsPDF({
putOnlyUsedFonts: true,
orientation: "landscape",
floatPrecision: 2
});
doc.table(1, 1, generateData(100), header);
comparePdf(doc.output(), "table.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 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."));
});
});