Skip to content

Commit

Permalink
fix spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Dakuan committed Feb 15, 2015
1 parent b46be3f commit 01618b5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 18 deletions.
12 changes: 5 additions & 7 deletions src/handlers/error.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,18 @@ var parseOpt = R.cond(
[R.is(Array), randomElement], // if array pick from those
[R.is(RegExp), codeMatchingRegex], // if regex then code matching that regex
[R.alwaysTrue, function() {
return randomElement.call(null, all);
return randomElement.call(null, all);
}] // random error code
);

// sends an error code
function _error(code) {
var hander = function _errorHandler(req, res, next) {
var code = parseOpt(code);
console.log('CHAOS: throwing ' + code);
res.status(code);
return function(req, res, next) {
var toThrow = parseOpt(code);
console.log('CHAOS: throwing ' + toThrow);
res.status(toThrow);
res.end();
}
hander.code = code;
return hander;
}

module.exports = {
Expand Down
1 change: 1 addition & 0 deletions src/util/run-on-prop.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ function _runOnProp(fn, prop) {
return R.compose(
fn, // apply the function
R.prop('value'), // unwrap the monad
// R.map(R.I),
// R.map(R.ifElse(R.is(Number), R.I, R.always())), // if the prop isn't a number use undefined
R.map(R.prop(prop)), // get the prop if the argument is truthy
Maybe // Maybe null
Expand Down
40 changes: 29 additions & 11 deletions tests/handlers/errors-spec.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,55 @@
var subject = require('../../src/handlers/error'),
R = require('ramda'),
R = require('ramda'),
assert = require('assert');

function assertStatus(handler, expectedCode) {
handler(null, {
status: function(code) {
assert(code === expectedCode);
},
end: function() {}
});
}

describe("Error handler", function() {
describe("when a valid number is provided ", function() {
it("should use that error code", function() {
assert(subject.factory({
var handler = subject.factory({
error: 420
}).code === 420);
});
assertStatus(handler, 420);
});
});

describe("when an array is provided", function() {
it("should use a value from the array", function() {
assert(subject.factory({
var handler = subject.factory({
error: [123]
}).code === 123);
});
assertStatus(handler, 123);
});
});

describe("when a regex is provided", function() {
it("should use a value that matches the regex", function() {
assert(subject.factory({
var handler = subject.factory({
error: /^400/
}).code === 400);
});
assertStatus(handler, 400);
});
});

describe("when nothing is provided", function() {
it("should use a valid error code", function() {
var code = subject.factory().code;
assert(code >= 400);
assert(code <= 506);
assert(R.is(Number, code));
var handler = subject.factory();
handler(null, {
status: function(code) {
assert(code >= 400);
assert(code <= 506);
assert(R.is(Number, code));
},
end: function() {}
});
});
});
});

0 comments on commit 01618b5

Please sign in to comment.