Skip to content

Commit

Permalink
Add tests to reach 100 percent test coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
sjones6 committed Oct 12, 2017
1 parent c72bf1f commit 3957148
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 5 deletions.
4 changes: 0 additions & 4 deletions example/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,5 @@ module.exports = makeAuth([
res.end("Logged in!")
}
)
},
{
path: '/logout',
handler: ({req, res}) => res.end('Logged out!')
}
])
1 change: 0 additions & 1 deletion src/defaults/error.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module.exports = ({err, req, res}) => {
console.log(e.stack)
res.statusCode = 500;
res.end("Internal server error")
}
23 changes: 23 additions & 0 deletions tests/defaults/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const assert = require('assert')

const errorHandler = require('./../../src/defaults/error')

describe("default error handler", function() {

it('should set the error status code', () => {
let mockRes = {
end: function() {}
}
errorHandler({res: mockRes})
assert.strictEqual(mockRes.statusCode, 500)
})

it('should call the end method with a message', () => {
let mockRes = {
end: function(msg) {
assert.strictEqual("Internal server error", msg)
}
}
errorHandler({res: mockRes})
})
})
12 changes: 12 additions & 0 deletions tests/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@ describe('group', function() {
assert.strictEqual(typeof group({}) === 'function', true)
})

it('should handle a falsey path', () => {
const makeGroup = group({
path: false
});
const fakePath = "/somepath"
const routes = makeGroup([{
path: fakePath,
handler: () => {}
}])
assert.strictEqual(routes[0].path, fakePath)
})

it('should return a new routes array given a route array', () => {
assert.strictEqual(Array.isArray(group({})([])), true)
})
Expand Down
25 changes: 25 additions & 0 deletions tests/http/make-create-server.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const assert = require('assert')

const { Server } = require('http')
const { Server: HttpsServer } = require('tls')
const makeCreateServer = require('../../src/http/make-create-server')

describe('make create server', function() {

it("should return a function when passed an http option", () => {
assert.strictEqual(typeof makeCreateServer(false), 'function')
assert.strictEqual(typeof makeCreateServer({}), 'function')
assert.strictEqual(typeof makeCreateServer(true), 'function')
})

it("should make an HTTP server (not HTTPs) when HTTPS is disabled", () => {
const makeserver = makeCreateServer(false)
assert.strictEqual(makeserver(() => {}) instanceof Server, true)
})

it("should make an HTTPs server (not HTTP) when HTTPS is enabled", () => {
const makeserver = makeCreateServer({})
assert.strictEqual(makeserver(() => {}) instanceof HttpsServer, true)
})

})
29 changes: 29 additions & 0 deletions tests/routes/make-final.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const assert = require('assert')

const makeFinal = require('./../../src/routes/make-final')

describe('routes/make-final', function() {

it('should return a routeable object when passed a configuration object', () => {
assert.strictEqual(typeof makeFinal({}), 'object')
assert.strictEqual(typeof makeFinal({
lost: () => {}
}).handler, 'function')
})

it('should bypass the static service when a static path is not provided', () => {
const lost = () => {};
assert.strictEqual(makeFinal({lost}).handler, lost)
})

it('should bypass the static service when the static option is false', () => {
const lost = () => {};
assert.strictEqual(makeFinal({lost, static: false}).handler, lost)
})

it('should attempt to serve static files when the static path is not false', () => {
const lost = () => {};
assert.notStrictEqual(makeFinal({lost, static: "/"}).handler, lost)
})

})
63 changes: 63 additions & 0 deletions tests/routes/make-matcher.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
const assert = require('assert')

const makeMatcher = require('./../../src/routes/make-matcher')


describe('routes/make-matcher', function() {

const testPath = "/some-path";
const matcher = makeMatcher(path => {
assert.strictEqual(path, testPath)
return new RegExp(path)
})

it("should take a function that constructs a regular expression to match the path", () => {
matcher({
route: {
path: testPath
},
url: testPath
})
})

it("should match the request method if given even if cases mismatch", () => {
assert.strictEqual(matcher({
route: {
path: testPath,
method: "GET"
},
url: testPath,
method: "get"
}), true)
})

it("should fail to match if the methods are different", () => {
assert.strictEqual(matcher({
route: {
path: testPath,
method: "GET"
},
url: testPath,
method: "post"
}), false)
})

it("should match all methods if no method is specified", () => {
assert.strictEqual(matcher({
route: { path: testPath },
url: testPath,
method: "post"
}), true)
assert.strictEqual(matcher({
route: { path: testPath },
url: testPath,
method: "get"
}), true)
assert.strictEqual(matcher({
route: { path: testPath },
url: testPath,
method: "head"
}), true)
})

})

0 comments on commit 3957148

Please sign in to comment.