-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.js
121 lines (111 loc) · 3.47 KB
/
test.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
var LazyPromise = require('./'),
chai = require('chai'),
expect = chai.expect;
chai.use(require('chai-spies'));
describe('a lazy promise', function() {
it('should inherit from normal Promise', function() {
var promise = new LazyPromise(function() {});
expect(promise).to.be.an['instanceof'](Promise);
});
it('should not do anything when left unused', function(cb) {
var factory = chai.spy(),
promise = new LazyPromise(factory);
setTimeout(function() {
expect(factory).to.not.have.been.called();
cb();
}, 20);
});
it('should call the factory when .then is called', function(cb) {
var factory = chai.spy(),
promise = new LazyPromise(factory);
promise.then(function() {}, function() {});
setTimeout(function() {
expect(factory).to.have.been.called();
cb();
}, 20);
});
it('should return a promise from .then', function() {
var promise = new LazyPromise(function() {}),
ret = promise.then(function() {}, function() {});
expect(ret)
.to.have.property('then')
.and.to.be.a('function');
});
it('should call the factory asynchronously', function() {
var factory = chai.spy(),
promise = new LazyPromise(factory);
promise.then(function() {}, function() {});
expect(factory).to.not.have.been.called();
});
it('should only call the success callback on success', function(cb) {
var factory = chai.spy(function(resolve, reject) {
resolve({});
}),
promise = new LazyPromise(factory),
success = chai.spy(),
failure = chai.spy();
promise.then(success, failure);
setTimeout(function() {
expect(success).to.have.been.called();
expect(failure).to.not.have.been.called();
cb();
}, 20);
});
it('should pass resolution values', function(cb) {
var sentinel = {},
factory = chai.spy(function(resolve, reject) {
resolve(sentinel);
}),
promise = new LazyPromise(factory),
success = chai.spy(function(value) {
expect(value).to.equal(sentinel);
}),
failure = chai.spy();
promise.then(success, failure);
setTimeout(function() {
expect(success).to.have.been.called();
cb();
}, 20);
});
it('should only call the failure callback on failure', function(cb) {
var factory = chai.spy(function(resolve, reject) {
reject({});
}),
promise = new LazyPromise(factory),
success = chai.spy(),
failure = chai.spy();
promise.then(success, failure);
setTimeout(function() {
expect(success).to.not.have.been.called();
expect(failure).to.have.been.called();
cb();
}, 20);
});
it('should pass failure reasons', function(cb) {
var sentinel = {},
factory = chai.spy(function(resolve, reject) {
reject(sentinel);
}),
promise = new LazyPromise(factory),
success = chai.spy(),
failure = chai.spy(function(reason) {
expect(reason).to.equal(sentinel);
});
promise.then(success, failure);
setTimeout(function() {
expect(failure).to.have.been.called();
cb();
}, 20);
});
it('should only call the factory once', function(cb) {
var factory = chai.spy(),
promise = new LazyPromise(factory);
promise.then(function() {}, function() {});
promise.then(function() {}, function() {});
setTimeout(function() {
promise.then(function() {}, function() {});
expect(factory).to.have.been.called.once;
cb();
}, 20);
});
});