-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.js
180 lines (145 loc) · 5.11 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
MockGapi = function(){
var functions = [];
this.add = function(f){
functions.push(f)
}
this.execute = function(){
for (var i=0; i<functions.length; i++){
functions[i]();
}
}
}
setupGapi = function(){
gapi = {};
gapi.client = {};
gapi.client.newBatch = sinon.spy(function(){
return new MockGapi()
});
initGapi();
}
describe('GapiAutoBatcher', function(){
describe('constructor', function(){
it('should return a new instance when called without "new".', function(){
expect(GapiAutoBatcher()).to.be.instanceof(GapiAutoBatcher);
});
it("should use the default option when an option is not specified", function(){
expect(GapiAutoBatcher().config.batchInterval).to.equal(GapiAutoBatcher.defaults.batchInterval)
});
it("should use the options provided to the constructor.", function(){
var batchInterval = 42;
expect(GapiAutoBatcher({batchInterval: batchInterval}).config.batchInterval)
.to.equal(batchInterval);
});
});
describe("execute", function(){
it("should execute a request passed to call() after config.batchInterval, if " +
"no other request is passed in within that time.", function(){
setupGapi();
var interval = 30;
var batcher = new GapiAutoBatcher({batchInterval: interval});
var clock = sinon.useFakeTimers();
var request = sinon.spy();
batcher.execute(request);
clock.tick(interval);
expect(request.callCount).to.equal(1);
clock.restore();
});
it("should return the original request, so request.then() can be chained to it.", function(){
setupGapi();
var interval = 30;
var batcher = new GapiAutoBatcher({batchInterval: interval});
var request = sinon.spy();
var returned = batcher.execute(request);
expect(request).to.equal(returned);
});
it("should not execute a request passed in to call() before the necessary time has elapsed", function(){
setupGapi();
var interval = 30;
var batcher = new GapiAutoBatcher({batchInterval: interval});
var clock = sinon.useFakeTimers();
var request = sinon.spy();
batcher.execute(request);
clock.restore();
expect(request.callCount).to.equal(0);
});
it("should extend the interval by batchInterval if a new request is added.", function(){
setupGapi();
var interval = 30;
var batcher = new GapiAutoBatcher({batchInterval: interval});
var clock = sinon.useFakeTimers();
var request1 = sinon.spy();
var request2 = sinon.spy();
batcher.execute(request1);
clock.tick(29);
batcher.execute(request2);
clock.tick(31);
expect(request1.callCount).to.equal(1);
expect(request2.callCount).to.equal(1);
clock.restore();
});
it("should not execute the batch before the extended timer has run out", function(){
setupGapi();
var interval = 30;
var batcher = new GapiAutoBatcher({batchInterval: interval});
var clock = sinon.useFakeTimers();
var request1 = sinon.spy();
var request2 = sinon.spy();
batcher.execute(request1);
clock.tick(29);
batcher.execute(request2);
clock.tick(5);
expect(request1.callCount).to.equal(0);
expect(request2.callCount).to.equal(0);
clock.restore();
});
it("should only extend the timeout upto confix.maxWait", function(){
setupGapi();
var interval = 30, maxWait = 50;
var batcher = new GapiAutoBatcher({batchInterval: interval, maxWait: maxWait});
var clock = sinon.useFakeTimers();
var request1 = sinon.spy();
var request2 = sinon.spy();
batcher.execute(request1);
clock.tick(29);
batcher.execute(request2);
clock.tick(21);
expect(request1.callCount).to.equal(1);
expect(request2.callCount).to.equal(1);
clock.restore();
})
it("should start a new batch after the previous batch has been executed", function(){
setupGapi();
var interval = 30, maxWait = 50;
var batcher = new GapiAutoBatcher({batchInterval: interval, maxWait: maxWait});
var clock = sinon.useFakeTimers();
var request1 = sinon.spy();
var request2 = sinon.spy();
batcher.execute(request1);
clock.tick(31);
batcher.execute(request2);
clock.tick(31);
expect(request1.callCount).to.equal(1);
expect(request2.callCount).to.equal(1);
clock.restore();
})
});
describe("gapi", function(){
before(function(){
GapiAutoBatcher.gapi.listeners = [];
GapiAutoBatcher.gapi.resolved = false;
});
it("should resolve GapiAutoBatcher.gapi when initGapi is called.", function(){
var callback = sinon.spy();
GapiAutoBatcher.gapi.then(callback);
expect(callback.callCount).to.equal(0);
setupGapi();
expect(callback.callCount).to.equal(1);
});
it("should call the function passed to 'then' immediately when gapi is already loaded.", function(){
setupGapi();
var callback = sinon.spy();
GapiAutoBatcher.gapi.then(callback);
expect(callback.callCount).to.equal(1);
});
})
})