UNPKG

2.64 kBJavaScriptView Raw
1
2describe('ware', function () {
3
4 var assert = require('assert');
5 var noop = function(){};
6 var ware = require('..');
7
8 describe('#use', function () {
9 it('should be chainable', function () {
10 var w = ware();
11 assert(w.use(noop) == w);
12 });
13
14 it('should add a middleware to fns', function () {
15 var w = ware().use(noop);
16 assert(1 == w.fns.length);
17 });
18 });
19
20 describe('#run', function () {
21 it('should receive an error', function (done) {
22 var error = new Error();
23 ware()
24 .use(function (next) { next(error); })
25 .run(function (err) {
26 assert(err == error);
27 done();
28 });
29 });
30
31 it('should receive initial arguments', function (done) {
32 ware()
33 .use(function (req, res, next) { next(); })
34 .run('req', 'res', function (err, req, res) {
35 assert(!err);
36 assert('req' == req);
37 assert('res' == res);
38 done();
39 });
40 });
41
42 it('should take any number of arguments', function (done) {
43 ware()
44 .use(function (a, b, c, next) { next(); })
45 .run('a', 'b', 'c', function (err, a, b, c) {
46 assert(!err);
47 assert('a' == a);
48 assert('b' == b);
49 assert('c' == c);
50 done();
51 });
52 });
53
54 it('should let middleware manipulate the same input objects', function (done) {
55 ware()
56 .use(function (obj, next) {
57 obj.value = obj.value * 2;
58 next();
59 })
60 .use(function (obj, next) {
61 obj.value = obj.value.toString();
62 next();
63 })
64 .run({ value: 21 }, function (err, obj) {
65 assert('42' == obj.value);
66 done();
67 });
68 });
69
70 it('should skip non-error handlers on error', function (done) {
71 ware()
72 .use(function (next) { next(new Error()); })
73 .use(function (next) { assert(false); next(); })
74 .run(function (err) {
75 assert(err);
76 done();
77 });
78 });
79
80 it('should call error middleware on error', function (done) {
81 var errors = 0;
82 ware()
83 .use(function (next) { next(new Error()); })
84 .use(function (err, next) { errors++; next(err); })
85 .use(function (err, next) { errors++; next(err); })
86 .run(function (err) {
87 assert(err);
88 assert(2 == errors);
89 done();
90 });
91 });
92
93 it('should not require a callback', function (done) {
94 ware()
95 .use(function (obj, next) { assert(obj); next(); })
96 .use(function (obj, next) { done(); })
97 .run('obj');
98 });
99 });
100});
\No newline at end of file