forked from twitter/typeahead.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransport_spec.js
197 lines (139 loc) · 5.52 KB
/
transport_spec.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
describe('Transport', function() {
beforeEach(function() {
jasmine.Ajax.useMock();
jasmine.Clock.useMock();
this.transport = new Transport();
});
afterEach(function() {
// run twice to flush out on-deck requests
$.each(ajaxRequests, drop);
$.each(ajaxRequests, drop);
clearAjaxRequests();
Transport.resetCache();
function drop(i, req) {
req.readyState !== 4 && req.response(fixtures.ajaxResps.ok);
}
});
it('should use jQuery.ajax as the default transport mechanism', function() {
var req, resp = fixtures.ajaxResps.ok, spy = jasmine.createSpy();
this.transport.get('/test', spy);
req = mostRecentAjaxRequest();
req.response(resp);
expect(req.url).toBe('/test');
expect(spy).toHaveBeenCalledWith(null, resp.parsed);
});
it('should allow the transport mechanism to be configured', function() {
var resp = fixtures.ajaxResps.ok,
cbSpy = jasmine.createSpy(),
sendSpy = jasmine.createSpy().andCallFake(send);
this.transport = new Transport({ transport: sendSpy });
this.transport.get('/test', cbSpy);
jasmine.Clock.tick(0);
expect(cbSpy).toHaveBeenCalledWith(null, resp.parsed);
expect(sendSpy).toHaveBeenCalledWith(
'/test',
{},
jasmine.any(Function),
jasmine.any(Function)
);
// send must be async
function send(url, o, onSuccess, onError) { onSuccess(resp.parsed); }
});
it('should respect maxPendingRequests configuration', function() {
for (var i = 0; i < 10; i++) {
this.transport.get('/test' + i, $.noop);
}
expect(ajaxRequests.length).toBe(6);
});
it('should support rate limiting', function() {
this.transport = new Transport({ rateLimiter: rateLimiter });
for (var i = 0; i < 5; i++) {
this.transport.get('/test' + i, $.noop);
}
jasmine.Clock.tick(100);
expect(ajaxRequests.length).toBe(1);
function rateLimiter(fn) { return _.debounce(fn, 20); }
});
it('should cache most recent requests', function() {
var spy1 = jasmine.createSpy(), spy2 = jasmine.createSpy();
this.transport.get('/test1', $.noop);
mostRecentAjaxRequest().response(fixtures.ajaxResps.ok);
this.transport.get('/test2', $.noop);
mostRecentAjaxRequest().response(fixtures.ajaxResps.ok1);
expect(ajaxRequests.length).toBe(2);
this.transport.get('/test1', spy1);
this.transport.get('/test2', spy2);
jasmine.Clock.tick(0);
// no ajax requests were made on subsequent requests
expect(ajaxRequests.length).toBe(2);
expect(spy1).toHaveBeenCalledWith(null, fixtures.ajaxResps.ok.parsed);
expect(spy2).toHaveBeenCalledWith(null, fixtures.ajaxResps.ok1.parsed);
});
it('should not cache requests if cache option is false', function() {
this.transport = new Transport({ cache: false });
this.transport.get('/test1', $.noop);
mostRecentAjaxRequest().response(fixtures.ajaxResps.ok);
this.transport.get('/test1', $.noop);
mostRecentAjaxRequest().response(fixtures.ajaxResps.ok);
expect(ajaxRequests.length).toBe(2);
});
it('should prevent dog pile', function() {
var spy1 = jasmine.createSpy(), spy2 = jasmine.createSpy();
this.transport.get('/test1', spy1);
this.transport.get('/test1', spy2);
mostRecentAjaxRequest().response(fixtures.ajaxResps.ok);
expect(ajaxRequests.length).toBe(1);
waitsFor(function() { return spy1.callCount && spy2.callCount; });
runs(function() {
expect(spy1).toHaveBeenCalledWith(null, fixtures.ajaxResps.ok.parsed);
expect(spy2).toHaveBeenCalledWith(null, fixtures.ajaxResps.ok.parsed);
});
});
it('should always make a request for the last call to #get', function() {
var spy = jasmine.createSpy();
for (var i = 0; i < 6; i++) {
this.transport.get('/test' + i, $.noop);
}
this.transport.get('/test' + i, spy);
expect(ajaxRequests.length).toBe(6);
_.each(ajaxRequests, function(req) {
req.response(fixtures.ajaxResps.ok);
});
expect(ajaxRequests.length).toBe(7);
mostRecentAjaxRequest().response(fixtures.ajaxResps.ok);
expect(spy).toHaveBeenCalled();
});
it('should invoke the callback with err set to true on failure', function() {
var req, resp = fixtures.ajaxResps.err, spy = jasmine.createSpy();
this.transport.get('/test', spy);
req = mostRecentAjaxRequest();
req.response(resp);
expect(req.url).toBe('/test');
expect(spy).toHaveBeenCalledWith(true);
});
it('should not send cancelled requests', function() {
this.transport = new Transport({ rateLimiter: rateLimiter });
this.transport.get('/test', $.noop);
this.transport.cancel();
jasmine.Clock.tick(100);
expect(ajaxRequests.length).toBe(0);
function rateLimiter(fn) { return _.debounce(fn, 20); }
});
it('should not send outdated requests', function() {
this.transport = new Transport({ rateLimiter: rateLimiter });
// warm cache
this.transport.get('/test1', $.noop);
jasmine.Clock.tick(100);
mostRecentAjaxRequest().response(fixtures.ajaxResps.ok);
expect(mostRecentAjaxRequest().url).toBe('/test1');
expect(ajaxRequests.length).toBe(1);
// within the same rate-limit cycle, request test2 and test1. test2 becomes
// outdated after test1 is requested and no request is sent for test1
// because it's a cache hit
this.transport.get('/test2', $.noop);
this.transport.get('/test1', $.noop);
jasmine.Clock.tick(100);
expect(ajaxRequests.length).toBe(1);
function rateLimiter(fn) { return _.debounce(fn, 20); }
});
});