/
main.js
667 lines (623 loc) · 20.7 KB
/
main.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
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
describe('chosen.ajaxaddition', function(){
var select = $('#test-select'),
multiSelect = $('#test-multi-select'),
space = $('#test-space'),
emptySelect = $('#empty-select');
beforeEach(function(){
space.append(select.clone());
});
afterEach(function(){
space.html('');
});
it("should initial chosen", function(){
var mock = sinon.mock($.fn);
mock.expects('chosen').once();
$('select', space).ajaxChosen({},{});
expect(mock.verify()).to.be.true;
});
it("should not add an extra empty option if one is present", function(){
expect($('select option', space)).to.have.length(1);
$('select', space).ajaxChosen({},{});
expect($('select option', space)).to.have.length(1);
});
it("should proxy chosen options", function(){
var mock = sinon.mock($.fn),
args = {no_results_text: "No results matched"};
mock.expects('chosen').once().withExactArgs(args);
$('select', space).ajaxChosen({},{},args);
expect(mock.verify()).to.be.true;
});
it("should not fire of ajax for non-essential keys", function(){
var keyCodes = [9,13,16,17,18,19,20,27,33,34,35,36,37,38,39,40,44,45,144,145,91,93,224,112,113,114,115,116,117,118,119,120,121,122,123],
key,
chosen,
input,
server = sinon.fakeServer.create(),
clock = sinon.useFakeTimers();
server.respondWith(
'/search',
[200, { 'Content-Type': 'application/json' }, '{ q: "", results: []}']
);
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url:'/search'
},{}).next();
chosen.trigger('click');
input = $('input', chosen).val('word');
for(var i=0, len=keyCodes.length; i < len; i++){
key = $.Event('keyup');
key.which = keyCodes[i];
input.trigger(key);
};
clock.tick(750);
server.restore();
clock.restore();
expect(server.requests).to.have.length(0);
});
describe('options', function(){
beforeEach(function(){
this.server = sinon.fakeServer.create();
this.clock = sinon.useFakeTimers();
this.response = { q: "banana", complex: { results: [{id:1, text:"Chiquita"},{id:2, text:"Dole"}] }};
this.server.respondWith(
'/search',
[200, { 'Content-Type': 'application/json' },
JSON.stringify(this.response)]
);
});
afterEach(function(){
this.server.restore();
this.clock.restore();
});
it('should use loadingImg while processing', function(){
var chosen,
input,
key;
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url:'/search'
},{
loadingImg: 'img/l04der.gif'
}).next();
chosen.trigger('click');
input = $('input', chosen).val('banan');
expect(input.css('background-image')).to.match(/chosen-sprite\.png/i);
key = $.Event('keyup');
key.which = 65;
input.trigger(key);
expect(input.css('background-image')).to.match(/l04der\.gif/i);
});
it('should apply the processItems function to response data', function(){
var chosen,
input,
key,
ajaxChosenOptions = {
processItems: function(data){ return data.complex.results; }
};
sinon.spy(ajaxChosenOptions, "processItems");
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url:'/search'
},ajaxChosenOptions).next();
chosen.trigger('click');
input = $('input', chosen).val('banan');
key = $.Event('keyup');
key.which = 65;
input.trigger(key);
this.clock.tick(750);
this.server.respond();
expect(ajaxChosenOptions.processItems.calledWithExactly(this.response)).to.be.true;
expect($('select option', space)).to.have.length(3);
});
it('should use return value of useAjax to determine if ajax should be fired after keyups', function(){
var chosen,
input,
key,
ajaxChosenOptions = {
useAjax: function(e){ return false; }
};
sinon.spy(ajaxChosenOptions, "useAjax");
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url:'/search'
},ajaxChosenOptions).next();
chosen.trigger('click');
input = $('input', chosen).val('banan');
key = $.Event('keyup');
key.which = 65;
input.trigger(key);
this.clock.tick(750);
expect(ajaxChosenOptions.useAjax.calledWithExactly(key)).to.be.true;
expect(this.server.requests).to.have.length(0);
});
it("should use generateUrl to create dynmic url", function(){
var chosen,
input,
key,
ajaxChosenOptions = {
generateUrl: function(q){ return '/search'; }
};
sinon.spy(ajaxChosenOptions, "generateUrl");
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url:'/abc123'
},ajaxChosenOptions).next();
chosen.trigger('click');
input = $('input', chosen).val('banan');
key = $.Event('keyup');
key.which = 65;
input.trigger(key);
this.clock.tick(750);
expect(ajaxChosenOptions.generateUrl.calledOnce).to.be.true;
expect(ajaxChosenOptions.generateUrl.calledWithExactly('banan')).to.be.true;
expect(this.server.requests).to.have.length(1);
});
});
describe('ajaxOptions', function(){
beforeEach(function(){
this.server = sinon.fakeServer.create();
this.clock = sinon.useFakeTimers();
this.server.respondWith(
'/search',
[200, { 'Content-Type': 'application/json' },
'{ "q": "banana", "results": []}']
);
});
afterEach(function(){
this.server.restore();
this.clock.restore();
});
it('should fire ajax options callback on completion', function(){
var chosen,
input,
key,
successFn = sinon.mock();
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search',
success: successFn
},{}).next();
chosen.trigger('click');
input = $('input', chosen).val('banan');
key = $.Event('keyup');
key.which = 65;
input.trigger(key);
this.clock.tick(750);
this.server.respond();
successFn.once().withArgs({ q: "", results: []});
expect(successFn.verify()).to.be.true;
});
it('should handle and preserve ajax.data in hash format', function(){
var chosen,
input,
key,
aData,
expectedData;
sinon.spy(jQuery, "ajax");
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search',
data: {'somekey':'somevalue'}
},{}).next();
chosen.trigger('click');
input = $('input', chosen).val('banana');
key = $.Event('keyup');
key.which = 65;
input.trigger(key);
this.clock.tick(750);
expect(jQuery.ajax.calledOnce).to.be.true;
aData = jQuery.ajax.getCall(0).args[0].data;
expectedData = {'somekey':'somevalue', 'data' : { 'q': 'banana' }};
expect(aData.somekey).to.equal(expectedData.somekey);
expect(aData.data.q).to.equal(expectedData.data.q);
jQuery.ajax.restore();
});
it('should handle and preserve ajax.data in array format', function(){
var chosen,
input,
key,
aData,
expectedData;
sinon.spy(jQuery, "ajax");
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search',
data: [{'name':'keyboard', 'value':'cat'}]
},{}).next();
chosen.trigger('click');
input = $('input', chosen).val('banana');
key = $.Event('keyup');
key.which = 65;
input.trigger(key);
this.clock.tick(750);
expect(jQuery.ajax.calledOnce).to.be.true;
aData = jQuery.ajax.getCall(0).args[0].data;
expectedData = [{'name':'keyboard', 'value':'cat'},{'name':'q', 'value':'banana'}]
$.each(aData, function(i, v){
expect(expectedData[i].name).to.equal(v.name);
expect(expectedData[i].value).to.equal(v.value);
})
jQuery.ajax.restore();
});
//in array format it shouldn't re-add/duplicate the q - value pair in the array
});
describe('results element', function(){
beforeEach(function(){
this.server = sinon.fakeServer.create();
this.clock = sinon.useFakeTimers();
});
afterEach(function(){
this.server.restore();
this.clock.restore();
});
it('should show no results if response has no items', function(){
var chosen,
input,
key;
this.server.respondWith(
'/search',
[200, { 'Content-Type': 'application/json' },
'{ "q": "banana", "results": []}']
);
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search'
},{}).next();
chosen.trigger('click');
input = $('input', chosen).val('banan');
key = $.Event('keyup');
key.which = 65;
input.trigger(key);
this.clock.tick(750);
this.server.respond();
expect($('.no-results', chosen).is(':visible')).to.be.true;
});
it('should hide no results if response has at least 1 item', function(){
var chosen,
input,
key;
this.server.respondWith(
'/search',
[200, { 'Content-Type': 'application/json' },
'{ "q": "banana", "results": [{"id":1, "text":"Chiquita"}]}']
);
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search'
},{}).next();
chosen.trigger('click');
input = $('input', chosen).val('banan');
key = $.Event('keyup');
key.which = 65;
input.trigger(key);
this.clock.tick(750);
this.server.respond();
expect($('.no-results', chosen).is(':visible')).to.be.false;
});
it('should display error if no processItems option is supplied and there is no results key in data returned', function(){
var chosen,
input,
key;
sinon.spy(console, "log");
this.server.respondWith(
'/search',
[200, { 'Content-Type': 'application/json' },
'{ "q": "banana", "dataz": [{"id":1, "text":"Chiquita"}]}']
);
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search'
},{}).next();
chosen.trigger('click');
input = $('input', chosen).val('banan');
key = $.Event('keyup');
key.which = 65;
input.trigger(key);
this.clock.tick(750);
this.server.respond();
expect(console.log.calledOnce).to.be.true;
console.log.restore();
});
});
it('should not fire off ajax if trim(q) is of 0 length', function(){
var chosen,
input,
key;
this.server = sinon.fakeServer.create();
this.clock = sinon.useFakeTimers();
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search'
},{}).next();
chosen.trigger('click');
input = $('input', chosen).val('');
key = $.Event('keyup');
key.which = 32;
input.trigger(key);
this.clock.tick(750);
this.server.respond();
expect(this.server.requests).to.have.length(0);
this.server.restore();
this.clock.restore();
});
it('should not trim the space at the end of a users input - it might be what they actually meant', function(){
var chosen,
input,
key;
this.server = sinon.fakeServer.create();
this.clock = sinon.useFakeTimers();
this.server.respondWith(
'/search',
[200, { 'Content-Type': 'application/json' },
'{ "q": "Trailing Spaces are cool yo ", "results": [{"id":1, "text":"I hate trailing spaces"}]}']
);
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search'
},{}).next();
chosen.trigger('click');
input = $('input', chosen).val('Trailing Spaces are cool yo ');
key = $.Event('keyup');
key.which = 32;
input.trigger(key);
this.clock.tick(750);
this.server.respond();
expect(this.server.requests[0].requestBody).to.equal("data%5Bq%5D=Trailing+Spaces+are+cool+yo+");
});
describe('multi-select', function(){
beforeEach(function(){
space.html('');
space.append(multiSelect.clone());
})
it('should bind the keyup event to the input', function(){
var chosen,
input;
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search'
},{}).next();
chosen.trigger('click');
input = $('input', chosen).val('');
expect($._data( input[0], "events" )['keyup']).to.have.length(2);
});
it('should keep the options previously selected', function(){
var select,
chosen,
input,
key;
this.server = sinon.fakeServer.create();
this.clock = sinon.useFakeTimers();
this.server.respondWith(
'/search',
[200, { 'Content-Type': 'application/json' },
'{ "q": "banana", "results": [{"id":1, "text":"Chiquita"}]}']
);
select = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search'
},{});
chosen = select.next();
chosen.trigger('click');
input = $('input', chosen).val('banan');
key = $.Event('keyup');
key.which = 32;
input.trigger(key);
this.clock.tick(750);
this.server.respond();
expect($('option',select)).to.have.length(2);//empty + 1 result
//not results in selected
expect($('.chzn-choices li.search-choice',chosen)).to.have.length(0);
//have the chiquita result
expect($('.chzn-results li', chosen).not('.no-results')).to.have.length(1);
expect($('.chzn-results li', chosen).not('.no-results').text()).to.equal('Chiquita');
//click on result to add to selected items
$('.chzn-results li',chosen).not('.no-results').eq(0).addClass('active-result');
$('.chzn-results li',chosen).not('.no-results').eq(0).trigger('mouseup');
//verify that it has been added to the selected list
expect($('option:selected',select)).to.have.length(1);
expect($('option:selected',select).text()).to.equal('Chiquita');
expect($('.chzn-choices li.search-choice',chosen)).to.have.length(1);
expect($('.chzn-choices li.search-choice',chosen).text()).to.equal('Chiquita');
this.server.responses.length = 0;
this.server.respondWith(
'/search',
[200, { 'Content-Type': 'application/json' },
'{ "q": "ferr", "results": [{"id":2, "text":"Ferrari"}]}']
);
chosen.trigger('click');
input = $('input', chosen).val('fer');
key = $.Event('keyup');
key.which = 82;
input.trigger(key);
this.clock.tick(750);
this.server.respond();
//select box still has chiquita selected
expect($('option:selected',select)).to.have.length(1);
expect($('option:selected',select).text()).to.equal('Chiquita');
//have the original result
expect($('.chzn-choices li.search-choice',chosen)).to.have.length(1);
expect($('.chzn-choices li.search-choice',chosen).text()).to.equal('Chiquita');
//notice how we have two results and one is 'active-result' and the other is 'result-selected'
expect($('.chzn-results li.result-selected', chosen)).to.have.length(1);
expect($('.chzn-results li.result-selected', chosen).text()).to.equal('Chiquita');
expect($('.chzn-results li.active-result', chosen).not('.no-results')).to.have.length(1);
expect($('.chzn-results li.active-result', chosen).not('.no-results').text()).to.equal('Ferrari');
//click on result to add to selected items
$('.chzn-results li.active-result', chosen).not('.no-results').eq(0).addClass('active-result');
$('.chzn-results li.active-result', chosen).not('.no-results').eq(0).trigger('mouseup');
//verify selected results
expect($('option:selected',select)).to.have.length(2);
expect($('.chzn-choices li.search-choice',chosen)).to.have.length(2);
var expectedResults = ['Chiquita', 'Ferrari'];
$('.chzn-choices li.search-choice', chosen).each(function(i, elem){
expect($(elem).text()).to.equal(expectedResults[i]);
});
$('option:selected',select).each(function(i, elem){
expect($(elem).text()).to.equal(expectedResults[i]);
});
});
});
describe('empty-select', function(){
beforeEach(function(){
space.html('');
space.append(emptySelect.clone());
});
it("should append an empty option if one is not present", function(){
expect($('select option', space)).to.have.length(0);
$('select', space).ajaxChosen({},{});
expect($('select option', space)).to.have.length(1);
});
it("should not have the nosearch class", function(){
var select, chosen;
select = $('select', space).ajaxChosen({},{});
chosen = select.next();
expect(chosen.hasClass('chzn-container-single-nosearch')).to.be.false;
});
});
describe('a request queue to ensure the user only sees the last response', function(){
beforeEach(function(){
this.clock = sinon.useFakeTimers();
this.xhr = sinon.useFakeXMLHttpRequest();
var requests = this.requests = [];
this.xhr.onCreate = function (xhr) { requests.push(xhr) };
});
afterEach(function(){
this.clock.restore();
this.xhr.restore();
});
it('during typing', function(){
var chosen,
input,
key;
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search'
},{}).next();
chosen.trigger('click');
//first request
input = $('input', chosen).val('monkey');
key = $.Event('keyup');
key.which = 32;
input.trigger(key);
this.clock.tick(750);
//server begins processing request 1
expect(input.val()).to.equal('monkey');
expect(this.requests).to.have.length(1);
//second request
input.val('banana');
key = $.Event('keyup');
key.which = 32;
input.trigger(key);
this.clock.tick(350);//not enough time to start processing the request
expect(input.val()).to.equal('banana');
expect(this.requests).to.have.length(1);//see still one request
//request 1 comes back while we're still typing! Dam the server is fast
this.requests[0].respond(200, { "Content-Type": "application/json" }, '{ "q": "monkey", "results": [{"id":1, "text":"first monkey"}]}');
//discard first response keep newly typed word and ajax chosen should still look like it's still processing
expect(input.val()).to.equal('banana');
expect(input.css('background-image')).to.match(/loading\.gif/i);
//the rest of the clock ticks down so we should now fire off the second request
this.clock.tick(400);
expect(this.requests).to.have.length(2);
expect(input.val()).to.equal('banana');
//yup still waiting for the server to respond
expect(input.css('background-image')).to.match(/loading\.gif/i);
//response 2 comes back and banana is selected
this.requests[1].respond(200, { "Content-Type": "application/json" }, '{ "q": "banana", "results": [{"id":1, "text":"banana bunch"}]}');
expect(input.val()).to.equal('banana');
expect(input.css('background-image')).to.not.match(/loading\.gif/i);
});
it('in order', function(){
var chosen,
input,
key;
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search'
},{}).next();
chosen.trigger('click');
//first request
input = $('input', chosen).val('monkey');
key = $.Event('keyup');
key.which = 32;
input.trigger(key);
this.clock.tick(750);
//server begins processing request 1
expect(input.val()).to.equal('monkey');
expect(this.requests).to.have.length(1);
//second request
input.val('banana');
key = $.Event('keyup');
key.which = 32;
input.trigger(key);
this.clock.tick(750);
//server begins processing request 2
expect(input.val()).to.equal('banana');
expect(this.requests).to.have.length(2);
//response 1 comes back
this.requests[0].respond(200, { "Content-Type": "application/json" }, '{ "q": "monkey", "results": [{"id":1, "text":"first monkey"}]}');
//discard first response keep newly typed word and ajax chosen should still look like it's still processing
expect(input.val()).to.equal('banana');
expect(input.css('background-image')).to.match(/loading\.gif/i);
//response 2 comes back and banana is selected
this.requests[1].respond(200, { "Content-Type": "application/json" }, '{ "q": "banana", "results": [{"id":1, "text":"banana bunch"}]}');
expect(input.val()).to.equal('banana');
expect(input.css('background-image')).to.not.match(/loading\.gif/i);
});
it('out of order', function(){
var chosen,
input,
key;
chosen = $('select', space).ajaxChosen({
dataType: 'json',
type: 'POST',
url: '/search'
},{}).next();
chosen.trigger('click');
//first request
input = $('input', chosen).val('monkey');
key = $.Event('keyup');
key.which = 32;
input.trigger(key);
this.clock.tick(750);
//server begins processing request 1
expect(input.val()).to.equal('monkey');
expect(this.requests).to.have.length(1);
//second request
input.val('banana');
key = $.Event('keyup');
key.which = 32;
input.trigger(key);
this.clock.tick(750);
//server begins processing request 2
expect(input.val()).to.equal('banana');
expect(this.requests).to.have.length(2);
//response 2 comes back first and banana is selected
this.requests[1].respond(200, { "Content-Type": "application/json" }, '{ "q": "banana", "results": [{"id":1, "text":"banana bunch"}]}');
expect(input.val()).to.equal('banana');
expect(input.css('background-image')).to.not.match(/loading\.gif/i);
//response 1 comes back out of order
this.requests[0].respond(200, { "Content-Type": "application/json" }, '{ "q": "monkey", "results": [{"id":1, "text":"first monkey"}]}');
//discard first response keep newly typed word and result set
expect(input.val()).to.equal('banana');
expect(input.css('background-image')).to.not.match(/loading\.gif/i);
});
});
});