Skip to content

Commit be94dd3

Browse files
committed
Add more tests
1 parent f448af1 commit be94dd3

5 files changed

Lines changed: 39 additions & 25 deletions

File tree

Gruntfile.coffee

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
module.exports = (grunt) ->
22

33
grunt.initConfig
4-
mochaTest:
4+
mochacli:
55
test:
66
options:
77
ui: 'bdd'
88
reporter: 'spec'
9-
require: ['coffee-script']
9+
compilers: ['coffee:coffee-script/register']
1010
slow: '1ms'
11-
12-
src: ['test/**/*.coffee']
11+
timeout: '10s'
12+
files: 'test/**/*'
1313

1414
watch:
1515
tests:
1616
files: ['test/**/*.coffee', 'lib/**/*.coffee']
17-
tasks: ['mochaTest']
17+
tasks: ['mochacli']
1818

1919

20-
grunt.loadNpmTasks('grunt-mocha-test')
20+
grunt.loadNpmTasks('grunt-mocha-cli')
2121
grunt.loadNpmTasks('grunt-contrib-watch')
2222

23-
grunt.registerTask('test', ['mochaTest', 'watch'])
23+
grunt.registerTask('test', ['mochacli', 'watch'])

lib/index.coffee

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ phantomjs = require('phantomjs')
1515
# - Page Footer -> document.getElementById('pageFooter')
1616
#
1717
# When no #pageContent is available, phantomjs will use document.body as pdf content
18-
script = path.join(__dirname, 'scripts/pdf_a4_portrait.coffee')
18+
script = path.join(__dirname, 'scripts', 'pdf_a4_portrait.coffee')
1919

2020
exports.create = (string, options, callback) ->
2121
if arguments.length == 2
@@ -45,7 +45,7 @@ exports.create = (string, options, callback) ->
4545
# Clean up the timeout cause the process ended anyways
4646
clearTimeout(timeout)
4747
if (stderr.length || code) > 0
48-
error = new Error(Buffer.concat(stderr).toString() || 'Unknown Error')
48+
error = new Error(Buffer.concat(stderr).toString() || 'html-pdf: Unknown Error')
4949
return callback(error)
5050

5151
file = Buffer.concat(stdout)

lib/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ path = require('path');
88

99
phantomjs = require('phantomjs');
1010

11-
script = path.join(__dirname, 'scripts/pdf_a4_portrait.coffee');
11+
script = path.join(__dirname, 'scripts', 'pdf_a4_portrait.coffee');
1212

1313
exports.create = function(string, options, callback) {
1414
var child, content, stderr, stdout, timeout;
@@ -41,7 +41,7 @@ exports.create = function(string, options, callback) {
4141
var error, file, filename, isFileBuffer;
4242
clearTimeout(timeout);
4343
if ((stderr.length || code) > 0) {
44-
error = new Error(Buffer.concat(stderr).toString() || 'Unknown Error');
44+
error = new Error(Buffer.concat(stderr).toString() || 'html-pdf: Unknown Error');
4545
return callback(error);
4646
}
4747
file = Buffer.concat(stdout);

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,16 @@
1212
"author": "Marc Bachmann",
1313
"license": "MIT",
1414
"devDependencies": {
15-
"coffee-script": "^1.7.1",
1615
"chai": "^1.9.1",
17-
"grunt-mocha-test": "^0.10.2",
18-
"grunt": "^0.4.4",
19-
"mocha": "^1.18.2",
20-
"grunt-contrib-watch": "^0.6.1"
16+
"coffee-script": "^1.7.1",
17+
"grunt": "^0.4.5",
18+
"grunt-cli": "^0.1.13",
19+
"grunt-contrib-watch": "^0.6.1",
20+
"grunt-mocha-cli": "^1.11.0",
21+
"mocha": "^1.18.2"
2122
},
2223
"dependencies": {
23-
"phantomjs": "^1.9.7-8"
24+
"phantomjs": "^1.9.8"
2425
},
2526
"repository": {
2627
"type": "git",

test/create.coffee

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe 'html-pdf', ->
1717
<div id="pageContent">Content</div>
1818
<div id="pageFooter">Footer</div>
1919
</body>
20-
</html>
20+
</html>
2121
"""
2222

2323
it 'throws error when passing null', (done) ->
@@ -40,17 +40,29 @@ describe 'html-pdf', ->
4040

4141
it 'does not throw an error when succeeding', (done) ->
4242
pdf.create @html, (error, pdf) =>
43-
@pdf = pdf
4443
expect(error).to.be.null
45-
fs.writeFile(path.join(__dirname,'simple.pdf'), pdf)
4644
done()
4745

48-
it 'returns a buffer', ->
49-
expect(Buffer.isBuffer(@pdf), 'Should be a buffer').to.be.equal(true)
5046

47+
it 'buffer must be returned when no filename specified', (done) ->
48+
pdf.create @html, (error, pdf) =>
49+
expect(pdf).to.be.defined
50+
done()
51+
52+
53+
it 'returns a pdf buffer', (done) ->
54+
pdf.create @html, (error, pdf) =>
55+
expect(Buffer.isBuffer(pdf), 'Expect to be a pdf Buffer').to.be.equal(true)
56+
expect(/^\%PDF-1.4/.test(pdf.toString()), 'Has a PDF header').to.be.equal(true)
57+
done()
58+
59+
60+
it 'saves the pdf to a destination', (done) ->
61+
file = path.join(__dirname,'simple.pdf')
62+
pdf.create @html, filename: file, (error, pdf) =>
63+
expect(fs.existsSync(path.join(__dirname,'simple.pdf'))).to.equal(true)
64+
done()
5165

52-
it 'returns a pdf buffer', ->
53-
expect(/^\%PDF-1.4/.test(@pdf.toString()), 'Has a PDF header').to.be.equal(true)
5466

5567
it 'works with a custom page size and footer', (done) ->
5668
options =
@@ -71,8 +83,9 @@ describe 'html-pdf', ->
7183
options =
7284
width: '50mm'
7385
height: '90mm'
86+
filename: path.join(__dirname,'businesscard.pdf')
7487

7588
pdf.create html, options,(error, pdf) =>
76-
fs.writeFile(path.join(__dirname,'businesscard.pdf'), pdf)
7789
expect(error).to.be.null
90+
expect(fs.existsSync(options.filename)).to.equal(true)
7891
done()

0 commit comments

Comments
 (0)