/
tests.js
136 lines (132 loc) · 3.71 KB
/
tests.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* eslint-env node, mocha */
/* eslint camelcase:0, handle-callback-err: 0 */
var demand = require('must'),
di = require('./index'),
_ = require('lodash');
var thrownErr = new Error("Should've been caught by asyncdi");
var fn_basic = function() { return true; };
var fn_async = function(callback) { setTimeout(callback, 0, null, true); };
var fn_next = function(next){ return next(); };
var fn_one = function(one) { one(); return true; };
var fn_scope = function(){ return this; };
var fn_error = function() { throw thrownErr; };
var fn_reflect_args = function() { return _.toArray(arguments); };
var fn_overwritten_toString = function(){ return true; };
fn_overwritten_toString.toString = function() {
return 'toString overwritten';
};
var scope = {};
var notAFunction = {};
describe('AsyncDI', function() {
describe('new Wrapper', function() {
it('must be an instance of Wrapper', function() {
new di.Wrapper(fn_basic).must.be.an.instanceof(di.Wrapper);
});
});
describe('()', function() {
it('must be an instance of Wrapper', function() {
di(fn_basic).must.be.an.instanceof(di.Wrapper);
});
});
describe('(notAFunction)', function() {
it('must throw an error', function() {
demand(function(){
di(notAFunction);
}).to.throw(/function/i);
});
});
describe('(fn_overwritten_toString)', function() {
it('should not throw an error', function() {
demand(function(){
di(fn_overwritten_toString);
}).to.not.throw();
});
});
describe('fn_basic.isAsync', function() {
it('must not be async', function() {
demand(di(fn_basic).isAsync).not.be.true();
});
});
describe('fn_async.isAsync', function() {
it('must be async', function() {
demand(di(fn_async).isAsync).be.true();
});
});
describe('fn_next.isAsync', function() {
it('must be async, if so configured', function() {
demand(di(fn_next, null, { callback: 'next' }).isAsync).be.true();
});
});
describe('fn_basic.deps', function() {
it('must be empty', function() {
demand(di(fn_async).deps).eql([]);
});
});
describe('fn_basic.call(callback)', function() {
it('must callback(null, true)', function(done) {
di(fn_basic).call(function(err, val) {
demand(err).be.null();
demand(val).be.true();
done();
});
});
});
describe('fn_basic.call()', function() {
it('must return true', function() {
di(fn_basic).call().must.be.true();
});
});
describe('fn_async.call(callback)', function() {
it('must callback(null, true)', function(done) {
di(fn_async).call(function(err, val) {
demand(err).be.null();
demand(val).be.true();
done();
});
});
});
describe('fn_one.requires', function() {
it('must require `one`', function() {
demand(di(fn_one).requires.one).be.true();
});
});
describe('fn_one.requires', function() {
it('must not require `two`', function() {
demand(di(fn_one).requires.two).be.undefined();
});
});
describe('(fn_reflect_args).provides', function() {
it('accept an array of values', function(done) {
var a = {},
b = {};
di(fn_reflect_args).provides([a, b]).call(function(err, val) {
demand(val).eql([a, b]);
done();
});
});
});
describe('(fn_scope).call(scope, callback)', function() {
it('must return scope', function(done) {
di(fn_scope).call(scope, function(err, val) {
demand(val).equal(scope);
done();
});
});
});
describe('(fn_scope, scope).call(callback)', function() {
it('must return scope', function(done){
di(fn_scope, scope).call(function(err, val) {
demand(val).equal(scope);
done();
});
});
});
describe('(fn_error).call(callback)', function() {
it('must return thrownErr', function(done) {
di(fn_error).call(function(err){
demand(err).equal(thrownErr);
done();
});
});
});
});