forked from twitter/typeahead.js
/
dataset_spec.js
496 lines (376 loc) · 13.3 KB
/
dataset_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
describe('Dataset', function() {
var www = WWW(), mockSuggestions, mockSuggestionsDisplayFn;
mockSuggestions = [
{ value: 'one', raw: { value: 'one' } },
{ value: 'two', raw: { value: 'two' } },
{ value: 'html', raw: { value: '<b>html</b>' } }
];
mockSuggestionsDisplayFn = [
{ display: '4' },
{ display: '5' },
{ display: '6' }
];
beforeEach(function() {
this.dataset = new Dataset({
name: 'test',
node: $('<div>'),
source: this.source = jasmine.createSpy('source')
}, www);
});
it('should throw an error if source is missing', function() {
expect(noSource).toThrow();
function noSource() { new Dataset({}, www); }
});
it('should throw an error if the name is not a valid class name', function() {
expect(fn).toThrow();
function fn() {
var d = new Dataset({
name: 'a space',
node: $('<div>'),
source: $.noop
}, www);
}
});
describe('#getRoot', function() {
it('should return the root element', function() {
var sel = 'div' + www.selectors.dataset + www.selectors.dataset + '-test';
expect(this.dataset.$el).toBe(sel);
});
});
describe('#update', function() {
it('should render suggestions', function() {
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
expect(this.dataset.$el).toContainText('one');
expect(this.dataset.$el).toContainText('two');
expect(this.dataset.$el).toContainText('html');
});
it('should escape html chars from display value when using default template', function() {
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
expect(this.dataset.$el).toContainText('<b>html</b>');
});
it('should respect limit option', function() {
this.dataset.limit = 2;
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
expect(this.dataset.$el).toContainText('one');
expect(this.dataset.$el).toContainText('two');
expect(this.dataset.$el).not.toContainText('three');
});
it('should allow custom display functions', function() {
this.dataset = new Dataset({
name: 'test',
node: $('<div>'),
display: function(o) { return o.display; },
source: this.source = jasmine.createSpy('source')
}, www);
this.source.andCallFake(syncMockSuggestionsDisplayFn);
this.dataset.update('woah');
expect(this.dataset.$el).toContainText('4');
expect(this.dataset.$el).toContainText('5');
expect(this.dataset.$el).toContainText('6');
});
it('should ignore async invocations of sync', function() {
this.source.andCallFake(asyncSync);
this.dataset.update('woah');
expect(this.dataset.$el).not.toContainText('one');
});
it('should ignore subesequent invocations of sync', function() {
this.source.andCallFake(multipleSync);
this.dataset.update('woah');
expect(this.dataset.$el.find('.tt-suggestion')).toHaveLength(3);
});
it('should trigger asyncRequested when needing/expecting backfill', function() {
var spy = jasmine.createSpy();
this.dataset.async = true;
this.dataset.onSync('asyncRequested', spy);
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
expect(spy).toHaveBeenCalled();
});
it('should not trigger asyncRequested when not expecting backfill', function() {
var spy = jasmine.createSpy();
this.dataset.async = false;
this.dataset.onSync('asyncRequested', spy);
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
expect(spy).not.toHaveBeenCalled();
});
it('should not trigger asyncRequested when not expecting backfill', function() {
var spy = jasmine.createSpy();
this.dataset.limit = 2;
this.dataset.async = true;
this.dataset.onSync('asyncRequested', spy);
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
expect(spy).not.toHaveBeenCalled();
});
it('should trigger asyncCanceled when pending aysnc is canceled', function() {
var spy = jasmine.createSpy();
this.dataset.async = true;
this.dataset.onSync('asyncCanceled', spy);
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
this.dataset.cancel();
waits(100);
runs(function() {
expect(spy).toHaveBeenCalled();
});
});
it('should not trigger asyncCanceled when cancel happens after update', function() {
var spy = jasmine.createSpy();
this.dataset.async = true;
this.dataset.onSync('asyncCanceled', spy);
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
waits(100);
runs(function() {
this.dataset.cancel();
expect(spy).not.toHaveBeenCalled();
});
});
it('should trigger asyncReceived when aysnc is received', function() {
var spy = jasmine.createSpy();
this.dataset.async = true;
this.dataset.onSync('asyncReceived', spy);
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
waits(100);
runs(function() {
expect(spy).toHaveBeenCalled();
});
});
it('should not trigger asyncReceived if canceled', function() {
var spy = jasmine.createSpy();
this.dataset.async = true;
this.dataset.onSync('asyncReceived', spy);
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
this.dataset.cancel();
waits(100);
runs(function() {
expect(spy).not.toHaveBeenCalled();
});
});
it('should not modify sync when async is added', function() {
var $test;
this.dataset.async = true;
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
$test = this.dataset.$el.find('.tt-suggestion').first();
$test.addClass('test');
waits(100);
runs(function() {
expect($test).toHaveClass('test');
});
});
it('should respect limit option in regard to async', function() {
this.dataset.async = true;
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
waits(100);
runs(function() {
expect(this.dataset.$el.find('.tt-suggestion')).toHaveLength(5);
});
});
it('should cancel pending async', function() {
var spy1 = jasmine.createSpy(), spy2 = jasmine.createSpy();
this.dataset.async = true;
this.dataset.onSync('asyncCanceled', spy1);
this.dataset.onSync('asyncReceived', spy2);
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
this.dataset.update('woah again');
waits(100);
runs(function() {
expect(spy1.callCount).toBe(1);
expect(spy2.callCount).toBe(1);
});
});
it('should render notFound when no suggestions are available', function() {
this.dataset = new Dataset({
source: this.source,
node: $('<div>'),
templates: {
notFound: '<h2>empty</h2>'
}
}, www);
this.source.andCallFake(syncEmptySuggestions);
this.dataset.update('woah');
expect(this.dataset.$el).toContainText('empty');
});
it('should render pending when no suggestions are available but async is pending', function() {
this.dataset = new Dataset({
source: this.source,
node: $('<div>'),
async: true,
templates: {
pending: '<h2>pending</h2>'
}
}, www);
this.source.andCallFake(syncEmptySuggestions);
this.dataset.update('woah');
expect(this.dataset.$el).toContainText('pending');
});
it('should render header when suggestions are rendered', function() {
this.dataset = new Dataset({
source: this.source,
node: $('<div>'),
templates: {
header: '<h2>header</h2>'
}
}, www);
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
expect(this.dataset.$el).toContainText('header');
});
it('should render footer when suggestions are rendered', function() {
this.dataset = new Dataset({
source: this.source,
node: $('<div>'),
templates: {
footer: function(c) { return '<p>' + c.query + '</p>'; }
}
}, www);
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
expect(this.dataset.$el).toContainText('woah');
});
it('should not render header/footer if there is no content', function() {
this.dataset = new Dataset({
source: this.source,
node: $('<div>'),
templates: {
header: '<h2>header</h2>',
footer: '<h2>footer</h2>'
}
}, www);
this.source.andCallFake(syncEmptySuggestions);
this.dataset.update('woah');
expect(this.dataset.$el).not.toContainText('header');
expect(this.dataset.$el).not.toContainText('footer');
});
it('should not render stale suggestions', function() {
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('nelly');
waits(100);
runs(function() {
expect(this.dataset.$el).toContainText('one');
expect(this.dataset.$el).toContainText('two');
expect(this.dataset.$el).toContainText('html');
expect(this.dataset.$el).not.toContainText('four');
expect(this.dataset.$el).not.toContainText('five');
});
});
it('should not render async suggestions if update was canceled', function() {
this.source.andCallFake(fakeGetWithAsyncSuggestions);
this.dataset.update('woah');
this.dataset.cancel();
waits(100);
runs(function() {
var rendered = this.dataset.$el.find('.tt-suggestion');
expect(rendered).toHaveLength(3);
});
});
it('should render all async suggestions if sync had no content', function() {
this.source.andCallFake(fakeGetWithEmptySyncAndAsyncSuggestions);
this.dataset.update('woah');
waits(100);
runs(function() {
var rendered = this.dataset.$el.find('.tt-suggestion');
expect(rendered).toHaveLength(5);
});
});
it('should trigger rendered after suggestions are rendered', function() {
var spy;
this.dataset.onSync('rendered', spy = jasmine.createSpy());
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
waitsFor(function() { return spy.callCount; });
});
});
describe('#clear', function() {
it('should clear suggestions', function() {
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
this.dataset.clear();
expect(this.dataset.$el).toBeEmpty();
});
it('should cancel pending updates', function() {
var spy;
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
spy = spyOn(this.dataset, 'cancel');
this.dataset.clear();
expect(spy).toHaveBeenCalled();
});
it('should trigger cleared', function() {
var spy;
this.dataset.onSync('cleared', spy = jasmine.createSpy());
this.dataset.clear();
expect(spy).toHaveBeenCalled();
});
});
describe('#isEmpty', function() {
it('should return true when empty', function() {
expect(this.dataset.isEmpty()).toBe(true);
});
it('should return false when not empty', function() {
this.source.andCallFake(syncMockSuggestions);
this.dataset.update('woah');
expect(this.dataset.isEmpty()).toBe(false);
});
});
describe('#destroy', function() {
it('should set dataset element to dummy element', function() {
var $prevEl = this.dataset.$el;
this.dataset.destroy();
expect(this.dataset.$el).not.toBe($prevEl);
});
});
// helper functions
// ----------------
function syncEmptySuggestions(q, sync, async) {
sync([]);
}
function syncMockSuggestions(q, sync, async) {
sync(mockSuggestions);
}
function syncMockSuggestionsDisplayFn(q, sync, async) {
sync(mockSuggestionsDisplayFn);
}
function asyncSync(q, sync, async) {
setTimeout(function() { sync(mockSuggestions); }, 0);
}
function multipleSync(q, sync, async) {
sync(mockSuggestions);
sync(mockSuggestions);
}
function fakeGetWithAsyncSuggestions(query, sync, async) {
sync(mockSuggestions);
setTimeout(function() {
async([
{ value: 'four', raw: { value: 'four' } },
{ value: 'five', raw: { value: 'five' } },
{ value: 'six', raw: { value: 'six' } },
{ value: 'seven', raw: { value: 'seven' } },
{ value: 'eight', raw: { value: 'eight' } },
]);
}, 0);
}
function fakeGetWithEmptySyncAndAsyncSuggestions(query, sync, async) {
sync([]);
setTimeout(function() {
async([
{ value: 'four', raw: { value: 'four' } },
{ value: 'five', raw: { value: 'five' } },
{ value: 'six', raw: { value: 'six' } },
{ value: 'seven', raw: { value: 'seven' } },
{ value: 'eight', raw: { value: 'eight' } },
]);
}, 0);
}
});