This repository was archived by the owner on Apr 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathtest-main.js
More file actions
207 lines (185 loc) · 6.26 KB
/
test-main.js
File metadata and controls
207 lines (185 loc) · 6.26 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
let expect = require("chai").expect;
let fs = require("fs");
let PDFImage = require("../").PDFImage;
describe("PDFImage", function () {
let pdfPath = "/tmp/test.pdf";
let pdfImage;
let generatedFiles = [];
this.timeout(7000);
before(function(done){
fs.createReadStream('tests/test.pdf').pipe(fs.createWriteStream(pdfPath));
if (fs.existsSync(pdfPath)){
done();
} else {
throw new Error({
message: 'File missing at: '+ pdfPath + '. Copy task was not a success'
});
}
});
beforeEach(function() {
pdfImage = new PDFImage(pdfPath)
});
it("should have correct basename", function () {
expect(pdfImage.pdfFileBaseName).equal("test");
});
it("should set custom basename", function() {
pdfImage.setPdfFileBaseName('custom-basename');
expect(pdfImage.pdfFileBaseName).equal("custom-basename");
});
it("should return correct page path", function () {
expect(pdfImage.getOutputImagePathForPage(1))
.equal("/tmp/test-1.png");
expect(pdfImage.getOutputImagePathForPage(2))
.equal("/tmp/test-2.png");
expect(pdfImage.getOutputImagePathForPage(1000))
.equal("/tmp/test-1000.png");
expect(pdfImage.getOutputImagePathForFile())
.equal("/tmp/test.png");
});
it("should return correct convert command", function () {
var convertCommand = pdfImage.constructConvertCommandForPage(1);
expect(convertCommand.cmd).equal("convert");
expect(convertCommand.args.length).equal(2);
});
it("should return correct convert command to combine images", function () {
var cmdConfig = pdfImage.constructCombineCommandForFile(['/tmp/test-0.png', '/tmp/test-1.png']);
expect(cmdConfig.cmd).equal('convert');
expect(cmdConfig.args.length).equal(4);
});
it("should use gm when you ask it to", function () {
pdfImage = new PDFImage(pdfPath, {graphicsMagick: true});
var cmdConfig = pdfImage.constructConvertCommandForPage(1);
expect(cmdConfig.cmd).equal('gm convert');
expect(cmdConfig.args.length).equal(2);
});
// TODO: Do page updating test
it("should convert PDF's page to a file with the default extension", function () {
return new Promise(function(resolve, reject) {
pdfImage.convertPage(1).then(function (imagePath) {
expect(imagePath).equal("/tmp/test-1.png");
expect(fs.existsSync(imagePath)).to.be.true;
generatedFiles.push(imagePath);
resolve();
}).catch(function(err){
reject(error.message + " " + error.stderr);
});
});
});
it("should convert PDF's page 10 to a file with the default extension", function () {
return new Promise(function(resolve, reject){
pdfImage.convertPage(9).then(function (imagePath) {
expect(imagePath).equal("/tmp/test-9.png");
expect(fs.existsSync(imagePath)).to.be.true;
generatedFiles.push(imagePath);
resolve();
}).catch(function(err){
reject(error.message + " " + error.stderr);
});
})
});
it("should convert PDF's page to file with a specified extension", function () {
return new Promise(function(resolve, reject) {
pdfImage.setConvertExtension("jpeg");
pdfImage.convertPage(1).then(function (imagePath) {
expect(imagePath).equal("/tmp/test-1.jpeg");
expect(fs.existsSync(imagePath)).to.be.true;
generatedFiles.push(imagePath);
resolve();
}).catch(function(err){
reject(error.message + " " + error.stderr);
});
});
});
it("should convert all PDF's pages to files", function () {
return new Promise(function(resolve, reject) {
pdfImage.convertFile().then(function (imagePaths) {
imagePaths.forEach(function(imagePath){
expect(fs.existsSync(imagePath)).to.be.true;
generatedFiles.push(imagePath);
});
resolve();
}).catch(function(err){
reject(error.message + " " + error.stderr);
});
});
});
it("should convert all PDF's pages to single image", function () {
return new Promise(function(resolve, reject){
let pdfImageCombined = new PDFImage(pdfPath, {
combinedImage: true,
});
pdfImageCombined.convertFile().then(function (imagePath) {
expect(imagePath).to.equal("/tmp/test.png");
expect(fs.existsSync(imagePath)).to.be.true;
generatedFiles.push(imagePath);
resolve();
}).catch(function (error) {
reject(error.message + " " + error.stderr);
});
})
});
it("should return # of pages", function () {
return new Promise(function(resolve, reject) {
pdfImage.numberOfPages().then(function (numberOfPages) {
expect(parseInt(numberOfPages)).to.be.equal(10);
resolve();
}).catch(function(err){
reject(error.message + " " + error.stderr);
});
});
});
it("should construct convert options correctly", function () {
pdfImage.setConvertOptions({
"-density": 300,
"-trim": null
});
expect(pdfImage.constructConvertOptions()[0]).equal("-density");
expect(pdfImage.constructConvertOptions()[1]).equal(300);
expect(pdfImage.constructConvertOptions()[2]).equal("-trim");
});
it("should convert all PDF's pages with convertOptions", function () {
return new Promise(function(resolve, reject){
pdfImage.setConvertOptions({
"-quality": 100,
"-trim": null
});
pdfImage.convertFile().then(function (images) {
images.forEach(function(image){
expect(fs.existsSync(image)).to.be.true;
});
generatedFiles = images;
resolve();
}).catch(function (error) {
reject(error.message + " " + error.stderr);
});
})
});
afterEach(function(done){
//cleanUp files generated during test
let i = generatedFiles.length;
if (i > 0 ){
generatedFiles.forEach(function(filepath, index){
fs.unlink(filepath, function(err) {
i--;
if (err) {
done(err);
} else if (i <= 0) {
done();
}
});
});
generatedFiles = []; //clear after delete
} else {
done();
}
});
after(function(done){
//finaly - remove test.pdf from /tmp/
fs.unlink(pdfPath, function(err) {
if (err) {
done(err);
}
done();
});
});
});