forked from twitter/typeahead.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbloodhound_spec.js
592 lines (462 loc) · 16 KB
/
bloodhound_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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
describe('Bloodhound', function() {
beforeEach(function() {
jasmine.Ajax.useMock();
jasmine.Transport.useMock();
jasmine.PersistentStorage.useMock();
});
afterEach(function() {
clearAjaxRequests();
});
describe('#initialize', function() {
beforeEach(function() {
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
local: fixtures.data.simple
});
spyOn(this.bloodhound, '_initialize').andCallThrough();
});
it('should not support reinitialization by default', function() {
var p1, p2;
p1 = this.bloodhound.initialize();
p2 = this.bloodhound.initialize();
expect(p1).toBe(p2);
expect(this.bloodhound._initialize.callCount).toBe(1);
});
it('should reinitialize if reintialize flag is true', function() {
var p1, p2;
p1 = this.bloodhound.initialize();
p2 = this.bloodhound.initialize(true);
expect(p1).not.toBe(p2);
expect(this.bloodhound._initialize.callCount).toBe(2);
});
});
describe('#add', function() {
it('should add datums to search index', function() {
var spy = jasmine.createSpy();
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
local: []
});
this.bloodhound.initialize();
this.bloodhound.add(fixtures.data.simple);
this.bloodhound.get('big', spy);
expect(spy).toHaveBeenCalledWith([
{ value: 'big' },
{ value: 'bigger' },
{ value: 'biggest' }
]);
});
});
describe('#clear', function() {
it('should remove all datums to search index', function() {
var spy = jasmine.createSpy();
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
local: fixtures.data.simple
});
this.bloodhound.initialize();
this.bloodhound.clear();
this.bloodhound.get('big', spy);
expect(spy).toHaveBeenCalledWith([]);
});
});
describe('#clearPrefetchCache', function() {
it('should clear persistent storage', function() {
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
prefetch: '/test'
});
this.bloodhound.initialize();
this.bloodhound.clearPrefetchCache();
expect(this.bloodhound.storage.clear).toHaveBeenCalled();
});
});
describe('#clearRemoteCache', function() {
it('should clear remote request cache', function() {
spyOn(Transport, 'resetCache');
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
remote: '/test'
});
this.bloodhound.initialize();
this.bloodhound.clearRemoteCache();
expect(Transport.resetCache).toHaveBeenCalled();
});
});
describe('local', function() {
describe('when local is an array', function() {
beforeEach(function() {
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
local: fixtures.data.simple
});
this.bloodhound.initialize();
});
it('should hydrate the bloodhound', function() {
var spy = jasmine.createSpy();
this.bloodhound.get('big', spy);
expect(spy).toHaveBeenCalledWith([
{ value: 'big' },
{ value: 'bigger' },
{ value: 'biggest' }
]);
});
});
describe('when local is a function that returns an array', function() {
beforeEach(function() {
var localFn = function() {
return fixtures.data.simple;
};
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
local: localFn
});
this.bloodhound.initialize();
});
it('should hydrate the bloodhound', function() {
var spy = jasmine.createSpy();
this.bloodhound.get('big', spy);
expect(spy).toHaveBeenCalledWith([
{ value: 'big' },
{ value: 'bigger' },
{ value: 'biggest' }
]);
});
});
});
describe('prefetch', function() {
it('should throw error if url is not set', function() {
expect(test).toThrow();
function test() { var d = new Bloodhound({ prefetch: {} }); }
});
it('should use url or cacheKey to store data locally', function() {
var ttl = 100;
this.bloodhound1 = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
prefetch: { url: '/test1', cacheKey: 'woah' }
});
expect(PersistentStorage).toHaveBeenCalledWith('woah');
this.bloodhound2 = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
prefetch: { url: '/test2', ttl: ttl, thumbprint: '!' }
});
expect(PersistentStorage).toHaveBeenCalledWith('/test2');
this.bloodhound2.initialize();
ajaxRequests[0].response(fixtures.ajaxResps.ok);
expect(this.bloodhound2.storage.set)
.toHaveBeenCalledWith('data', fixtures.serialized.simple, ttl);
expect(this.bloodhound2.storage.set)
.toHaveBeenCalledWith('protocol', 'http:', ttl);
expect(this.bloodhound2.storage.set)
.toHaveBeenCalledWith('thumbprint', '%VERSION%!', ttl);
});
it('should load data from provided url', function() {
var spy1, spy2;
spy1 = jasmine.createSpy();
spy2 = jasmine.createSpy();
this.bloodhound1 = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
prefetch: '/test1'
});
this.bloodhound2 = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
prefetch: { url: '/test2' }
});
this.bloodhound1.initialize();
this.bloodhound2.initialize();
ajaxRequests[0].response(fixtures.ajaxResps.ok);
ajaxRequests[1].response(fixtures.ajaxResps.ok);
expect(ajaxRequests[0].url).toBe('/test1');
expect(ajaxRequests[1].url).toBe('/test2');
this.bloodhound1.get('big', spy1);
this.bloodhound2.get('big', spy2);
expect(spy1).toHaveBeenCalledWith([
{ value: 'big' },
{ value: 'bigger' },
{ value: 'biggest' }
]);
expect(spy2).toHaveBeenCalledWith([
{ value: 'big' },
{ value: 'bigger' },
{ value: 'biggest' }
]);
});
it('should clear preexisting data', function() {
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
prefetch: '/test'
});
spyOn(this.bloodhound, 'clear');
this.bloodhound.initialize();
mostRecentAjaxRequest().response(fixtures.ajaxResps.ok);
expect(this.bloodhound.clear).toHaveBeenCalled();
});
it('should filter data if filter is provided', function() {
var filterSpy, spy;
filterSpy = jasmine.createSpy().andCallFake(fakeFilter);
spy = jasmine.createSpy();
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
prefetch: { url: '/test', filter: filterSpy }
});
this.bloodhound.initialize();
mostRecentAjaxRequest().response(fixtures.ajaxResps.ok);
expect(filterSpy).toHaveBeenCalled();
this.bloodhound.get('big', spy);
expect(spy).toHaveBeenCalledWith([
{ value: 'BIG' },
{ value: 'BIGGER' },
{ value: 'BIGGEST' }
]);
function fakeFilter(resp) {
return [{ value: 'BIG' }, { value: 'BIGGER' }, { value: 'BIGGEST' }];
}
});
it('should not make a request if data is available in storage', function() {
var that = this, spy = jasmine.createSpy();
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
prefetch: '/test'
});
this.bloodhound.storage.get.andCallFake(fakeGet);
this.bloodhound.initialize();
expect(mostRecentAjaxRequest()).toBeNull();
this.bloodhound.get('big', spy);
expect(spy).toHaveBeenCalledWith([
{ value: 'big' },
{ value: 'bigger' },
{ value: 'biggest' }
]);
function fakeGet(key) {
var val;
switch (key) {
case 'data':
val = fixtures.serialized.simple;
break;
case 'protocol':
val = 'http:';
break;
case 'thumbprint':
val = that.bloodhound.prefetch.thumbprint;
break;
}
return val;
}
});
});
describe('remote', function() {
it('should perform query substitution on the provided url', function() {
this.bloodhound1 = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
remote: { url: '/test?q=$$', wildcard: '$$' }
});
this.bloodhound2 = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
remote: {
url: '/test?q=%QUERY',
replace: function(str, query) {return str.replace('%QUERY', query); }
}
});
this.bloodhound1.initialize();
this.bloodhound2.initialize();
this.bloodhound1.get('one two', $.noop);
this.bloodhound2.get('one two', $.noop);
expect(this.bloodhound1.transport.get).toHaveBeenCalledWith(
'/test?q=one%20two',
{ type: 'GET', dataType: 'json' },
jasmine.any(Function)
);
expect(this.bloodhound2.transport.get).toHaveBeenCalledWith(
'/test?q=one two',
{ type: 'GET', dataType: 'json' },
jasmine.any(Function)
);
});
it('should filter the response if a filter is provided', function() {
var filterSpy, spy;
spy = jasmine.createSpy();
filterSpy = jasmine.createSpy().andCallFake(fakeFilter);
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
remote: { url: '/test', filter: filterSpy }
});
this.bloodhound.initialize();
this.bloodhound.transport.get.andCallFake(fakeGet);
this.bloodhound.get('big', spy);
waitsFor(function() { return spy.callCount; });
runs(function() {
expect(filterSpy).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith([
{ value: 'BIG' },
{ value: 'BIGGER' },
{ value: 'BIGGEST' }
]);
});
function fakeFilter(resp) {
return [{ value: 'BIG' }, { value: 'BIGGER' }, { value: 'BIGGEST' }];
}
function fakeGet(url, o, cb) {
cb(null, fixtures.data.simple);
}
});
describe('when there is not matching data in the search index', function() {
beforeEach(function() {
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
remote: '/test?q=%QUERY',
local: { value: 'not an animal' }
});
this.bloodhound.initialize();
});
it('should call #get callback once if there is a cache hit', function() {
var spy = jasmine.createSpy();
this.bloodhound.transport.get.andCallFake(fakeGetWithCacheHit);
this.bloodhound.get('dog', spy);
expect(spy.callCount).toBe(1);
function fakeGetWithCacheHit(url, o, cb) {
cb(null, fixtures.data.animals);
return true;
}
});
it('should call #get callback once if there is a cache miss', function() {
var spy = jasmine.createSpy();
this.bloodhound.transport.get.andCallFake(fakeGetWithCacheMiss);
this.bloodhound.get('dog', spy);
expect(spy.callCount).toBe(1);
function fakeGetWithCacheMiss(url, o, cb) {
cb(null, fixtures.data.animals);
return false;
}
});
});
describe('when there is matching data in the search index', function() {
beforeEach(function() {
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
remote: '/test?q=%QUERY',
local: { value: 'dog' }
});
this.bloodhound.initialize();
});
it('should call the #get callback twice if there is a cache miss', function() {
var spy = jasmine.createSpy();
this.bloodhound.transport.get.andCallFake(fakeGetWithCacheMiss);
this.bloodhound.get('dog', spy);
expect(spy.callCount).toBe(2);
function fakeGetWithCacheMiss(url, o, cb) {
cb(null, fixtures.data.animals);
return false;
}
});
it('should call the #get callback once if there is a cache hit', function() {
var spy = jasmine.createSpy();
this.bloodhound.transport.get.andCallFake(fakeGetWithCacheHit);
this.bloodhound.get('dog', spy);
expect(spy.callCount).toBe(1);
function fakeGetWithCacheHit(url, o, cb) {
cb(null, fixtures.data.animals);
return true;
}
});
});
it('should should treat failures as empty suggestion sets', function() {
var spy = jasmine.createSpy();
this.bloodhound = new Bloodhound({
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
remote: '/test?q=%QUERY'
});
this.bloodhound.initialize();
this.bloodhound.transport.get.andCallFake(fakeGet);
this.bloodhound.get('dog', spy);
expect(spy).toHaveBeenCalledWith([]);
function fakeGet(url, o, cb) { cb(true); }
});
});
describe('local/prefetch/remote integration', function() {
it('duplicates should be removed if dupDetector is provided', function() {
var spy;
spy = jasmine.createSpy();
this.bloodhound = new Bloodhound({
limit: 6,
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
dupDetector: function(d1, d2) { return d1.value === d2.value; },
local: fixtures.data.animals,
remote: { url: '/test?q=%QUERY' }
});
this.bloodhound.initialize();
this.bloodhound.transport.get.andCallFake(fakeGet);
this.bloodhound.get('dog', spy);
expect(spy).toHaveBeenCalledWith([{ value: 'dog' }]);
waitsFor(function() { return spy.callCount === 2; });
runs(function() {
expect(spy).toHaveBeenCalledWith(fixtures.data.animals);
});
function fakeGet(url, o, cb) {
setTimeout(function() { cb(null, fixtures.data.animals); }, 0);
}
});
it('remote should backfill local/prefetch', function() {
var spy1, spy2;
spy1 = jasmine.createSpy();
spy2 = jasmine.createSpy();
this.bloodhound = new Bloodhound({
limit: 3,
datumTokenizer: datumTokenizer,
queryTokenizer: queryTokenizer,
local: fixtures.data.simple,
remote: { url: '/test?q=%QUERY' }
});
this.bloodhound.initialize();
this.bloodhound.transport.get.andCallFake(fakeGet);
this.bloodhound.get('big', spy1);
this.bloodhound.get('bigg', spy2);
expect(spy1.callCount).toBe(1);
expect(spy2.callCount).toBe(1);
expect(spy1).toHaveBeenCalledWith([
{ value: 'big' },
{ value: 'bigger' },
{ value: 'biggest' }
]);
expect(spy2).toHaveBeenCalledWith([
{ value: 'bigger' },
{ value: 'biggest' }
]);
waitsFor(function() { return spy2.callCount === 2; });
runs(function() {
expect(spy2).toHaveBeenCalledWith([
{ value: 'bigger' },
{ value: 'biggest' },
{ value: 'dog' }
]);
});
function fakeGet(url, o, cb) {
setTimeout(function() { cb(null, fixtures.data.animals); }, 0);
}
});
});
// helper functions
// ----------------
function datumTokenizer(d) { return $.trim(d.value).split(/\s+/); }
function queryTokenizer(s) { return $.trim(s).split(/\s+/); }
});