Skip to content

Commit a77016b

Browse files
committed
started working on tests
1 parent 11c096a commit a77016b

File tree

4 files changed

+88
-2
lines changed

4 files changed

+88
-2
lines changed

index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict'
2+
3+
const HapiFileUploader = require('./lib')
4+
5+
module.exports = HapiFileUploader

lib/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ function fileUploader (server, options, next) {
2828

2929
function errorValidation (cb) {
3030
let methods = {}
31-
if (uploadOptions.generateName && typeof uploadOptions.generateName !== 'function') {
31+
if (uploadOptions.generateName) {
3232
if (typeof uploadOptions.generateName !== 'function') {
3333
return cb(new Error('Generate name must be a function'))
3434
}

package.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Hapi plugin allowing easy and configurable file uploads",
55
"main": "index.js",
66
"scripts": {
7-
"test": "echo \"Error: no test specified\" && exit 1",
7+
"test": "./node_modules/lab/bin/lab --verbose",
8+
"test-cov": "./node_modules/lab/bin/lab --verbose -c",
89
"lint": "standard --verbose | snazzy"
910
},
1011
"pre-commit": [
@@ -30,7 +31,9 @@
3031
"node": ">=4.0.0"
3132
},
3233
"devDependencies": {
34+
"code": "^2.1.1",
3335
"hapi": "^13.2.1",
36+
"lab": "^10.3.0",
3437
"pre-commit": "^1.1.2",
3538
"snazzy": "^3.0.0",
3639
"standard": "^6.0.8"

test/init.js

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
'use strict'
2+
3+
const Lab = require('lab')
4+
const Code = require('code')
5+
const pkg = require('../package.json')
6+
const lab = exports.lab = Lab.script()
7+
const Plugin = require('../lib/index')
8+
9+
let mockServer = {
10+
log: console.log
11+
}
12+
13+
lab.experiment('Plugin - init', () => {
14+
lab.test('Correct attributes', (done) => {
15+
let attributes = Plugin.attributes
16+
Code.expect(attributes.name).to.be.equal(pkg.name)
17+
Code.expect(attributes.version).to.be.equal(pkg.version)
18+
Code.expect(attributes.multiple).to.be.true()
19+
done()
20+
})
21+
22+
lab.test('No upload path', (done) => {
23+
Plugin(mockServer, {}, (err) => {
24+
Code.expect(err).to.be.instanceof(Error)
25+
Code.expect(err.message).to.be.equal('Must define a path to upload files')
26+
done()
27+
})
28+
})
29+
30+
lab.test('Invalid upload path', (done) => {
31+
Plugin(mockServer, {upload: {path: './invalid/path'}}, (err) => {
32+
Code.expect(err).to.be.instanceof(Error)
33+
Code.expect(err.message).to.be.startWith(
34+
'Must define a valid accessible path to upload files - '
35+
)
36+
done()
37+
})
38+
})
39+
40+
lab.test('Invalid upload path', (done) => {
41+
Plugin(mockServer, {upload: {path: './invalid/path'}}, (err) => {
42+
Code.expect(err).to.be.instanceof(Error)
43+
Code.expect(err.message).to.be.startWith(
44+
'Must define a valid accessible path to upload files - '
45+
)
46+
done()
47+
})
48+
})
49+
50+
lab.test('Define route with default options', (done) => {
51+
mockServer.route = function (route) {
52+
const config = route.config
53+
const payload = config.payload
54+
Code.expect(route.method).to.be.equal('POST')
55+
Code.expect(route.path).to.be.equal('/files')
56+
Code.expect(config.tags).to.not.exist()
57+
Code.expect(config.auth).to.be.false()
58+
Code.expect(payload.output).to.be.equal('stream')
59+
Code.expect(payload.parse).to.be.true()
60+
Code.expect(payload.allow).to.be.equal('multipart/form-data')
61+
Code.expect(payload.maxBytes).to.not.exist()
62+
Code.expect(config.pre).to.be.instanceof(Array)
63+
Code.expect(config.pre).to.have.length(1)
64+
Code.expect(config.pre[0].assign).to.be.equal('file')
65+
Code.expect(config.validate.query).to.not.exist()
66+
Code.expect(config.validate.params).to.not.exist()
67+
Code.expect(config.validate.headers).to.not.exist()
68+
Code.expect(config.validate.auth).to.not.exist()
69+
Code.expect(config.validate.payload).to.exist()
70+
Code.expect(config.validate.payload.isJoi).to.be.true()
71+
Code.expect(config.description).to.be.equal('Uploads a file')
72+
}
73+
Plugin(mockServer, {upload: {path: './'}}, (err) => {
74+
Code.expect(err).to.not.exist()
75+
done()
76+
})
77+
})
78+
})

0 commit comments

Comments
 (0)