/
livesearch_input_dropdown_test.js
323 lines (284 loc) · 9.57 KB
/
livesearch_input_dropdown_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
(function($) {
/*
======== A Handy Little QUnit Reference ========
http://api.qunitjs.com/
Test methods:
module(name, {[setup][ ,teardown]})
test(name, callback)
expect(numberOfAssertions)
stop(increment)
start(decrement)
Test assertions:
ok(value, [message])
equal(actual, expected, [message])
notEqual(actual, expected, [message])
deepEqual(actual, expected, [message])
notDeepEqual(actual, expected, [message])
strictEqual(actual, expected, [message])
notStrictEqual(actual, expected, [message])
throws(block, [expected], [message])
*/
module('jQuery#livesearch_input_dropdown', {
// This will run before each test in this module.
setup: function() {
this.$input = $('#qunit-fixture #plain input');
this.delay = 400;
this.makeServer = function () {
this.server = this.sandbox.useFakeServer();
this.server.respondWith(function (req) {
req.respond(
200,
{ "Content-Type": "application/json" },
'["' + req.url + '"]');
});
};
this.applyLivesearchInputDropdown = function (options) {
var callback = this.spy();
this.$input.livesearch_input_dropdown(
$.extend(
{delay: this.delay, minimum_characters: 0}, options
)
);
this.$input.on('livesearch:results', callback);
return callback;
};
this.type = function (val, delay) {
this.$input.val(val);
this.$input.trigger('input');
this.clock.tick(delay || this.delay);
this.server.respond();
};
}
});
test('is chainable', function() {
expect(1);
// Not a bad test to run on collection methods.
strictEqual(
this.$input.livesearch_input_dropdown()[0],
this.$input[0],
'should be chainable');
});
test('correct markup is added when needed', function () {
this.makeServer();
this.applyLivesearchInputDropdown();
strictEqual($('#plain ul.result_list').length, 1);
});
test('correct markup could already exist', function () {
this.$input = $('#qunit-fixture #with_results input');
this.makeServer();
this.applyLivesearchInputDropdown();
strictEqual(
$('#with_results ul.result_list').length, 1,
'no additional results list was added'
);
});
test('the input could be in a .field_with_icon', function () {
this.$input = $('#qunit-fixture #just_field_with_icon input');
this.makeServer();
this.applyLivesearchInputDropdown();
strictEqual($('#just_field_with_icon .field_with_icon').siblings('.results').length, 1);
});
test('results from the server appear', function () {
this.makeServer();
this.applyLivesearchInputDropdown();
this.type();
ok($('#plain .result_list li').length);
});
test('able to specify how to retrieve names from results', function () {
expect(2);
this.makeServer();
this.applyLivesearchInputDropdown({
return_name_from_result: function (item) {
strictEqual(typeof item, 'string');
return 'name';
}});
this.type();
strictEqual($('#plain .result_list li:first').text(), 'name');
});
test('able to manually process entire server response', function () {
expect(2);
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function (server_response) {
strictEqual(server_response.length, 1);
return ['name'];
}});
this.type();
strictEqual($('#plain .result_list li:first').text(), 'name');
});
test('able to set metadata with counts and see \'see all\' link', function () {
expect(1);
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return { results: ['name'], count: 2 };
}});
this.type();
strictEqual($('#plain .result_list a.see_all').text(), 'see all 2 results');
});
test('no results shows no results div', function () {
expect(1);
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return [];
}});
this.type();
ok($('#plain .no_results').is(':visible'));
});
var KEY = {
UP: 38,
DOWN: 40,
DEL: 46,
TAB: 9,
ENTER: 13,
ESC: 27,
COMMA: 188,
PAGEUP: 33,
PAGEDOWN: 34,
BACKSPACE: 8
};
function key(keyCode) {
var event = jQuery.Event('keydown');
event.keyCode = keyCode;
$('#plain input').trigger(event);
return event;
}
test('pressing up before anything is selected, selects the last item', function () {
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return ['first', 'last'];
}});
this.type();
key(KEY.UP);
strictEqual($('#plain .result_list li.selected').text(), 'last');
});
test('pressing up twice selects the second item', function () {
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return ['first', 'second', 'third'];
}});
this.type();
key(KEY.UP);
key(KEY.UP);
strictEqual($('#plain .result_list li.selected').text(), 'second');
});
test('pressing up at the beginning of the list does not change selected item', function () {
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return ['first', 'last'];
}});
this.type();
key(KEY.UP);
key(KEY.UP);
key(KEY.UP);
strictEqual($('#plain .result_list li.selected').text(), 'last');
});
test('pressing down before anything is selected, selects the first item', function () {
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return ['first', 'last'];
}});
this.type();
key(KEY.DOWN);
strictEqual($('#plain .result_list li.selected').text(), 'first');
});
test('pressing down twice selects the second item', function () {
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return ['first', 'second', 'third'];
}});
this.type();
key(KEY.DOWN);
key(KEY.DOWN);
strictEqual($('#plain .result_list li.selected').text(), 'second');
});
test('pressing down at the end of the list does not change selected item', function () {
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return ['first', 'last'];
}});
this.type();
key(KEY.DOWN);
key(KEY.DOWN);
key(KEY.DOWN);
strictEqual($('#plain .result_list li.selected').text(), 'first');
});
test('pressing enter before selecting anything does not trigger selected', function () {
var callback = this.spy();
$('#plain').on('livesearch:selected', callback);
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return ['first', 'last'];
}});
this.type();
var e = key(KEY.ENTER);
strictEqual(callback.callCount, 0, 'does not trigger selected');
ok(e.isDefaultPrevented(), 'default will be prevented');
});
test('pressing enter after selecting something does trigger selected', function () {
var callback = this.spy();
$('#plain').on('livesearch:selected', callback);
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return ['first', 'last'];
}});
this.type();
key(KEY.DOWN);
var e = key(KEY.ENTER);
strictEqual(callback.callCount, 1, 'does trigger selected');
ok(e.isDefaultPrevented(), 'default will be prevented');
});
test('clicking on an item triggers selected', function () {
var e = jQuery.Event('click');
var callback = this.spy();
$('#plain').on('livesearch:selected', callback);
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return ['first', 'last'];
}});
this.type();
$('#plain .results li:first').trigger(e);
strictEqual(callback.callCount, 1, 'does trigger selected');
ok(e.isDefaultPrevented(), 'default will be prevented');
});
test('pressing a random key only executes default', function () {
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return ['first', 'last'];
}});
this.type();
var e = key(KEY.PAGEUP);
ok(!e.isDefaultPrevented(), 'default will not be prevented');
});
test('mouseover gives the .selected class', function () {
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return ['first', 'last'];
}});
this.type();
$('#plain .results li:first').trigger('mouseover');
ok($('#plain li.selected').length);
});
test('mouseout removes the .selected class', function () {
this.makeServer();
this.applyLivesearchInputDropdown({process_results: function () {
return ['first', 'last'];
}});
this.type();
$('#plain .results li:first').trigger('mouseover');
$('#plain .results li:first').trigger('mouseout');
ok(!$('#plain li.selected').length);
});
test('option to allow enter to submit', function () {
// jQuery will use the native focus, which demands the document to be in
// focus. That's not true in a headless browser.
jQuery.find.selectors.filters.focus = function(elem) {
var doc = elem.ownerDocument;
return elem === doc.activeElement && !!(elem.type || elem.href);
};
this.makeServer();
this.applyLivesearchInputDropdown({input_can_submit_on_enter: true});
this.type();
$('#plain input').focus();
var e = key(KEY.ENTER);
ok(!e.isDefaultPrevented(), 'default is allowed');
});
}(jQuery));