forked from mtkopone/zelect
-
Notifications
You must be signed in to change notification settings - Fork 2
/
test.js
543 lines (494 loc) · 16.5 KB
/
test.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
describe('zelect', function() {
var keys = { tab:9, enter:13, esc:27, left:37, up:38, right:39, down:40 }
describe('The basics', function() {
beforeEach(function() {
setup('with-two-options')
$('#select').zelect({ throttle:0 })
})
it('renders', function() {
defaultInitialState()
})
it('shows dropdown on click', function() {
$('.zelected').click()
defaultOpenState()
})
it('filters options', function() {
$('.zelected').click()
$('.zearch').val('la').keyup()
items(['Last'])
$('.zearch').val('').keyup()
items(['First', 'Last'])
})
it('shows no results text', function() {
$('.zelected').click()
$('.zearch').val('x').keyup()
items([])
visible('.dropdown .no-results')
txt('.dropdown .no-results', "No results for 'x'")
})
it('selects on click', function(done) {
var expected = { label:'Last', value:'Last'}
$('#select').change(function(evt, item) {
eq(item, expected)
visible('.zelected')
hidden('.dropdown')
val('#select', 'Last')
selectionIs('Last', expected)
done()
})
$('.zelected').click()
$('.dropdown li:last').click()
})
it('closes dropdown on esc', function() {
$('.zelected').click()
keyup(keys.esc)
hidden('.dropdown')
})
it('closes dropdown on tab', function() {
$('.zelected').click()
keydown(keys.tab)
hidden('.dropdown')
})
})
describe('Initially selected item', function() {
it('First loaded item', function() {
setup('empty')
$('#select').zelect({ loader:function(term, page, callback) { return callback(['First','Second']) }})
selectionIs('First', 'First')
})
it('<option> with "selected"="selected"', function() {
setup('with-two-options-with-values')
$('#select option:last').attr('selected', 'selected')
$('#select').zelect()
val('#select', 'second')
selectionIs('Second', { value: 'second', label: 'Second' })
})
it('opts.placeholder', function() {
setup('with-two-options')
$('#select').zelect({ throttle:0, placeholder:$('<h2>').text('Plz zelect...') })
html('.zelected', '<h2>Plz zelect...</h2>')
hasClass('.zelected', 'placeholder')
$('.zelected').click()
$('.dropdown li:first').click()
noClass('.zelected', 'placeholder')
})
it('opts.initial', function() {
var initial = { label:'something completely different', value:'pow'}
var changeChecked = false
setup('with-two-options')
$('#select').on('change', function(e, item) {
eq(item, initial)
selectionIs('something completely different', initial)
changeChecked = true
})
$('#select').zelect({ initial:initial })
selectionIs('something completely different', initial)
// <select> val can't be changed to an option that doesnt exist:
val('#select', 'First')
assert.isTrue(changeChecked)
})
it('noResults fallback', function() {
setup('empty')
$('#select').zelect({
noResults: function() { return 'Sorry...' },
loader: function(term, page, callback) { return callback([]) }
})
html('.zelected', 'Sorry...')
hasClass('.zelected', 'placeholder')
})
})
describe('Loader', function() {
it('loads first results before "ready"', function(done) {
setup('empty')
$('#select').on('ready', function() {
items(['1','2'])
done()
})
$('#select').zelect({ loader:function(term, page, callback) {
if (page == 0) return callback(['1','2'])
return callback([])
}})
})
it('adds loading class', function(done) {
setup('empty')
noClass('.dropdown ol', 'loading')
$('#select').zelect({ loader:function(term, page, callback) {
hasClass('.dropdown ol', 'loading')
callback([])
done()
}})
noClass('.dropdown ol', 'loading')
})
it('clears list on search term change', function() {
setup('empty')
$('#select').zelect({ throttle:0, loader:function(term, page, callback) {
if (page == 0) return callback([term])
return callback([])
}})
items([''])
$('.zelected').click()
$('.zearch').val('x').keyup()
items(['x'])
$('.zearch').val('y').keyup()
items(['y'])
})
it('loads more results when last item is visible until exhausted', function() {
setup('empty')
$('#select').zelect({ throttle:0, loader:function(term, page, callback) {
if (page < 5) return callback([term+':'+page])
return callback([])
}})
$('.zelected').click()
$('.zearch').val('t').keyup()
items(['t:0','t:1','t:2','t:3','t:4'])
})
it('loads more results when list scrolled to bottom', function() {
setup('empty')
$('#select').zelect({ throttle:0, loader:function(term, page, callback) {
if (page < 5) return callback([page])
return callback([])
}})
$('.dropdown ol').css({ height: '50px', 'overflow-y':'scroll' })
$('.dropdown li').css({ height: '20px' })
$('.zelected').click()
items(['0','1','2'])
$('.dropdown ol').scrollTop(1000).trigger('scroll')
items(['0','1','2','3'])
$('.dropdown ol').scrollTop(1000).trigger('scroll')
items(['0','1','2','3','4'])
})
it('loads pages in order', function(done) {
setup('empty')
$('#select').on('ready', function() {
$('.zelected').click()
})
$('#select').zelect({ throttle:0, loader:function(term, page, callback) {
if (page < 3) {
setTimeout(function() {
callback([page])
}, (2 - page) * 5)
} else {
items(['0','1','2'])
callback([])
done()
}
}})
})
})
describe('Customization', function() {
it('noResults', function() {
setup('with-two-options')
$('#select').zelect({ throttle:0, noResults:function(term) { return 'POW['+term+']POW' } })
$('.zelected').click()
$('.zearch').val('xxx').keyup()
visible('.dropdown .no-results')
txt('.dropdown .no-results', 'POW[xxx]POW')
})
it('renderItem', function() {
setup('with-two-options')
$('#select').zelect({ throttle:0, renderItem:function(item) { return $('<pre>').text(item.label) } })
html('.dropdown li:first', '<pre>First</pre>')
html('.zelected', '<pre>First</pre>')
})
it('renderItem can e.g. highlight option matches', function() {
setup('with-several-options')
$('#select').zelect({ throttle:0, renderItem:function(item, term) {
var re = new RegExp('\\b('+term+')', 'i')
return $('<span>').html(item.label.replace(re, '<em>$1</em>'))
}})
$('.zelected').click()
$('.zearch').val('is').keyup()
items(['This is first', 'Here is second', "Isn't fifth"])
html('.dropdown li:first', '<span>This <em>is</em> first</span>')
})
it('regexpMatcher', function() {
setup('with-two-options')
$('#select').zelect({ throttle:0, regexpMatcher:function(term) { return new RegExp(term) } })
$('.zelected').click()
$('.zearch').val('ast').keyup()
items(['Last'])
$('.zearch').val('s').keyup()
items(['First', 'Last'])
})
it('can be set to an arbitrary item at any time', function() {
var item = { label:'Someone Else', data:'secret' }
var changeChecked = false
setup('with-two-options')
$('#select').on('change', function(e, i) {
eq(i, item)
selectionIs('Someone Else', item)
changeChecked = true
})
$('#select').zelect({ placeholder: 'Nothing selected yet'})
$('#select').zelectItem(item)
selectionIs('Someone Else', item)
// <select> val can't be changed to an option that doesnt exist:
val('#select', 'First')
assert.isTrue(changeChecked)
})
it('can zelectItem without firing a change event', function() {
var item = { label:'Someone Else', data:'secret' }
setup('with-two-options')
$('#select').zelect()
$('#select').on('change', function() { assert.fail(1, 2, 'No change event should fire') })
$('#select').zelectItem(item, false)
selectionIs('Someone Else', item)
})
it('can refresh an arbitrary item', function() {
setup('with-two-options')
function valueAsId(x) { return x.value }
var newItem = { value:'First', label:'Updated First' }
$('#select').zelect().refreshZelectItem(newItem, valueAsId)
selectionIs('Updated First', newItem)
items(['Updated First', 'Last'])
})
})
describe('This and that', function() {
it('triggers ready', function(done) {
setup('empty')
$('#select').on('ready', function() {
txt('.dropdown li', "12")
done()
})
$('#select').zelect({ loader:function(term, page, callback) { callback(['1','2']) } })
})
it('allows empty string as an option value', function() {
setup('option-with-empty-string')
$('#select').zelect()
$('.zelected').click()
$('.dropdown li:last').click()
txt('.zelected', 'Has empty string as value')
val('#select', '')
})
it('throttles search input', function(done) {
setup('with-two-options-with-values')
$('#select').zelect({
placeholder: 'Nothing selected yet...',
throttle:1,
loader: function(term, page, callback) {
if (term == '') return callback([])
eq(term, '123')
eq(page, 0)
callback([])
done()
}
})
$('.zelected').click()
$('.zearch')
.val('1').keyup()
.val('12').keyup()
.val('123').keyup()
})
it('can be set up in a detached DOM node', function() {
setup('with-two-options')
$('#select').wrap($('<div>').attr('id', 'parent'))
var $parent = $('#parent').detach()
$parent.find('#select').zelect()
$('#sut').append($parent)
defaultInitialState()
})
it('throws an error if <select> does not have a parent', function(done) {
try {
$('<select>').zelect()
} catch (err) {
eq(err.message, '<select> element must have a parent')
done()
}
})
it('$(select).reset()', function() {
setup('with-two-options')
$('#select').zelect()
$('.zelected').click()
$('.dropdown li:last').click()
val('#select', 'Last')
$('#select').resetZelect()
val('#select', 'First')
selectionIs('First', { label:'First', value:'First' })
})
})
describe('List navigation', function() {
beforeEach(function() {
setup('empty')
$('#select').zelect({ placeholder:'Nothing selected', throttle:0, loader:function(term, page, callback) {
if (term == 'no-results') return callback([])
if (page >= 2) return callback([])
callback(_.range(page*10, page*10+10))
}})
$('.zelect .dropdown ol').css({ height: '100px', 'overflow-y':'auto' })
$('.zelected').click()
})
it('sets first item as current', function() {
hasClass('.dropdown li:first', 'current')
})
it('selects on enter', function() {
txt('.zelected', 'Nothing selected')
keydown(keys.down)
keyup(keys.enter)
txt('.zelected', '1')
})
it('enter is a noop if no selection can be made', function() {
$('.zearch').val('no-results').keyup()
visible('.dropdown .no-results')
keydown(keys.enter)
keyup(keys.enter)
txt('.zelected', 'Nothing selected')
visible('.dropdown .no-results')
visible('.dropdown')
hasClass('.zelect', 'open')
})
it('moves selection on mouse enter', function() {
$('.dropdown li:eq(3)').mouseenter(); eq($('.dropdown li.current').index(), 3)
$('.dropdown li:eq(1)').mouseenter(); eq($('.dropdown li.current').index(), 1)
})
it('moves up and down', function() {
keydown(keys.down); eq($('.dropdown li.current').index(), 1)
keydown(keys.down); eq($('.dropdown li.current').index(), 2)
keydown(keys.up); eq($('.dropdown li.current').index(), 1)
keydown(keys.up); eq($('.dropdown li.current').index(), 0)
})
it("doesn't go up past first", function() {
keydown(keys.up); keydown(keys.up); keydown(keys.up)
eq($('.dropdown li.current').index(), 0)
})
it("doesn't go down past last", function() {
go(keys.down)
eq($('.dropdown li.current').index(), 19)
})
it('scrolls list up and down as necessary', function() {
go(keys.down); ok($('.dropdown ol').scrollTop() > 400)
go(keys.up); eq($('.dropdown ol').scrollTop(), 0)
})
it('skips first mouseenter after scroll', function() {
go(keys.down);
$('.dropdown li:eq(17)').mouseenter()
eq($('.dropdown li.current').index(), 19)
$('.dropdown li:eq(17)').mouseenter()
eq($('.dropdown li.current').index(), 17)
})
it('functions after filtering', function() {
keydown(keys.down);
keydown(keys.down);
eq($('.dropdown li.current').index(), 2)
$('.zearch').val('xxx').keyup()
eq($('.dropdown li.current').index(), 0)
keydown(keys.down);
eq($('.dropdown li.current').index(), 1)
})
function go(key) {
_.range(0, 25).forEach(function() { keydown(key); $('.dropdown ol').scroll() })
}
})
describe('Blur', function() {
beforeEach(function() {
setup('with-two-options')
$('#select').zelect()
})
it('adds hover class when mouse over zelect', function() {
$('.zelect').mouseenter()
hasClass('.zelect', 'hover')
$('.zelected').click()
hasClass('.zelect', 'hover')
$('.dropdown li:first').mouseenter()
hasClass('.zelect', 'hover')
$('.zelect').mouseleave()
noClass('.zelect', 'hover')
})
it('closes dropdown on blur when no hover class', function() {
$('.zelected').click()
$('.zearch').blur()
defaultInitialState()
})
it("doesn't close dropdown on blur when hover class exists", function() {
$('.zelected').click()
$('.zelect').mouseenter()
$('.zearch').blur()
defaultOpenState()
$('.zelect').mouseleave()
$('.zearch').blur()
defaultInitialState()
})
})
describe('XSS payload', function() {
beforeEach(function() {
setup('xss-payload')
$('#select').zelect()
})
it("doesn't execute (throw an error) when opening dropdown", function() {
$('.zelected').click()
})
it("doesn't execute when searching", function() {
$('.zelected').click()
$('.zearch')
.val('value').keyup()
.val('option').keyup()
})
})
function ok(bool, msg) {
assert.isTrue(bool, msg)
}
function eq(a,b, msg) {
assert.deepEqual(a,b, msg)
}
function txt(locator, expected) {
eq($(locator).text(), expected, locator+'.text()')
}
function val(locator, expected) {
eq($(locator).val(), expected, locator+'.val()')
}
function html(locator, expected) {
eq($(locator).html(), expected)
}
function lengthOf(locator, n) {
eq($(locator).size(), n, locator+'.length !== '+n)
}
function visible(locator) {
ok($(locator).is(':visible'), locator+' is hidden')
}
function hidden(locator) {
ok($(locator).is(':hidden'), locator+' is visible')
}
function hasClass(locator, clazz) {
ok($(locator).hasClass(clazz), locator+' doesnt have class '+clazz)
}
function noClass(locator, clazz) {
assert.isFalse($(locator).hasClass(clazz), locator+' has class '+clazz)
}
function items(arr) {
lengthOf('.dropdown li', arr.length)
$.each(arr, function(ii, e) {
txt('.dropdown li:eq('+ii+')', e)
})
}
function selectionIs(string, item) {
txt('.zelected', string)
eq($('#select').data('zelected'), item)
}
function defaultInitialState() {
hidden('#select')
visible('.zelect')
visible('.zelected')
hidden('.dropdown')
hidden('.zearch')
hidden('.dropdown .no-results')
txt('.zelected', 'First')
val('.zearch', '')
items(['First','Last'])
}
function defaultOpenState() {
visible('.zelect')
visible('.zelected')
visible('.dropdown')
visible('.zearch')
visible('.dropdown ol li')
hasClass('.zelect', 'open')
val('.zearch', '')
hidden('.dropdown .no-results')
items(['First','Last'])
}
function keyup(code) {
$('.zearch').trigger($.Event('keyup', { which: code }))
}
function keydown(code) {
$('.zearch').trigger($.Event('keydown', { which: code }))
}
})