thoughts/node_modules/jsonwebtoken/node_modules/cb/test/tests.js
node_modules/jsonwebtoken/node_modules/cb/test/tests.jsRaw
1var assert = require('assert'),
2 cb = require('../');
3
4function invokeAsync(callback) {
5 setTimeout(function() {
6 callback(null, 'foo');
7 }, 100);
8}
9
10function invokeAsyncError(callback) {
11 setTimeout(function() {
12 callback(new Error());
13 }, 100);
14}
15
16function invokeAsyncTwice(callback) {
17 setTimeout(function() {
18 callback(null, 'foo');
19 callback(null, 'foo');
20 }, 100);
21}
22
23describe('cb(callback)', function() {
24
25 it('should invoke the provided callback', function(done) {
26 invokeAsync(cb(function(err, res) {
27 assert.strictEqual(res, 'foo');
28 done();
29 }));
30 });
31
32 it('shouldn\'t mess with errors', function(done) {
33 invokeAsyncError(cb(function(err, res) {
34 assert(err);
35 done();
36 }));
37 });
38
39 it('should allow multiple executions', function(done) {
40 var count = 0;
41 invokeAsyncTwice(cb(function(err, res) {
42 count++;
43 if (count === 2) done();
44 }));
45 });
46
47});
48
49describe('cb(callback).timeout(ms)', function() {
50
51 it('should complete successfully within timeout period', function(done) {
52 invokeAsync(cb(function(err, res) {
53 assert.strictEqual(res, 'foo');
54 done();
55 }).timeout(200));
56 });
57
58 it('should complete with an error after timeout period', function(done) {
59 invokeAsync(cb(function(err, res) {
60 assert(err);
61 done();
62 }).timeout(50));
63 });
64
65 it('error resulting from a timeout should be instanceof cb.TimeoutError', function(done) {
66 invokeAsync(cb(function(err, res) {
67 assert(err instanceof cb.TimeoutError);
68 done();
69 }).timeout(50));
70 });
71
72});
73
74describe('cb(callback).error(errback)', function() {
75
76 it('should skip the err argument when invoking callback', function(done) {
77 invokeAsync(cb(function(res) {
78 assert.strictEqual(res, 'foo');
79 done();
80 }).error(assert.ifError));
81 });
82
83 it('should pass errors to provided errback', function(done) {
84 invokeAsyncError(cb(function(res) {
85 throw new Error('should not be invoked');
86 }).error(function(err) {
87 assert(err);
88 done();
89 }));
90 });
91
92});
93
94describe('cb(callback).error(errback).timeout(ms)', function() {
95
96 it('should skip the err argument when invoking callback', function(done) {
97 invokeAsync(cb(function(res) {
98 assert.strictEqual(res, 'foo');
99 done();
100 }).error(assert.ifError).timeout(200));
101 });
102
103 it('should pass timeout error to provided errback', function(done) {
104 invokeAsyncError(cb(function(res) {
105 throw new Error('should not be invoked');
106 }).error(function(err) {
107 assert(err);
108 done();
109 }).timeout(50));
110 });
111
112});
113
114describe('cb(callback).once()', function() {
115
116 it('should allow multiple executions', function(done) {
117 var count = 0;
118 invokeAsyncTwice(cb(function(err, res) {
119 count++;
120 assert.notEqual(count, 2);
121 setTimeout(done, 100);
122 }).once());
123 });
124
125});