-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.js
140 lines (116 loc) · 3.88 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
var chai = require('chai'),
sinon = require('sinon');
var assert = chai.assert,
Job = require('./job.js'),
queue = require('./queue.js');
describe('Job Unit Tests', function() {
it('create a basic job', function() {
var spy = sinon.spy();
var job = new Job([spy]);
assert(!spy.called, 'Job should not run until we tell it to.');
});
it('fail to create a basic job', function(done) {
try {
var job = new Job([]);
done(new Error('Should have failed to create job with no function'));
} catch(e) {
done();
};
});
it('run a job', function(done) {
var spy = sinon.spy();
var job = new Job([spy]);
job.init(sinon.stub(), sinon.stub());
job.runJob(function() {
assert(spy.calledOnce, 'Job should have been called one time.');
done();
});
});
it('run a job with Number delay', function(done) {
this.timeout(1000);
var spy = sinon.spy();
// Allow for max 10 ms overhead
var job = new Job([990, spy]);
job.init(sinon.stub(), sinon.stub());
job.runJob(function() {
assert(spy.calledOnce, 'Delayed job should have been called one time.');
done();
});
});
it('run a job with empty options', function(done) {
var spy = sinon.spy();
var job = new Job([{}, spy]);
job.init(sinon.stub(), sinon.stub());
job.runJob(function() {
assert(spy.calledOnce, 'Delayed job should have been called one time.');
done();
});
});
it('run a job with delay in options', function(done) {
this.timeout(1000);
var spy = sinon.spy();
// Allow for max 10 ms overhead
var job = new Job([{delay: 990}, spy]);
job.init(sinon.stub(), sinon.stub());
job.runJob(function() {
assert(spy.calledOnce, 'Delayed job should have been called one time.');
done();
});
});
it('run a job with a different context', function(done) {
function contextFunc() {
this.t.test;
done();
}
var context = {t: {test: 'success'}};
var job = new Job([{context: context}, contextFunc]);
job.init(sinon.stub(), sinon.stub());
job.runJob(sinon.stub());
});
it('run a job that returns a successful promise', function(done) {
function promiseFunc() {
return Promise.resolve();
}
var called = sinon.spy();
var notCalled = sinon.spy();
var job = new Job([promiseFunc]);
job.init(done);
job.runJob(sinon.stub());
});
it('run a job that returns a rejected promise', function(done) {
function promiseFunc() {
return Promise.reject();
}
var called = sinon.spy();
var notCalled = sinon.spy();
var job = new Job([promiseFunc]);
job.init(null, done);
job.runJob(sinon.stub());
});
});
describe('Queue Unit Tests', function() {
it('runs a function in the queue', function(done) {
queue.addJob(done);
});
it('ensure the returned promise finishes', function(done) {
queue.addJob(sinon.stub()).then(done);
});
it('runs a function in the queue with delay', function(done) {
this.timeout(1000);
queue.addJob(990, done);
});
it('runs multiple functions in the queue', function(done) {
this.timeout(1000);
queue.addJob(980, sinon.stub());
queue.addJob(done);
});
it('run a function with a different promise lib', function(done) {
var nativePromise = Promise,
bbPromise = require('bluebird');
queue.Promise = bbPromise;
queue.addJob(sinon.stub()).then(function() {
queue.Promise = nativePromise;
done();
});
});
});