This repository was archived by the owner on Apr 8, 2022. It is now read-only.
forked from Pixabay/JavaScript-autoComplete
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto-complete.js
373 lines (318 loc) · 13 KB
/
auto-complete.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
var autoComplete = require('../auto-complete');
var JSDOM = require('jsdom').JSDOM
function removeSpaces(text) {
return text.trim().replace(/\s/g, '');
}
function buildTerm(term) {
return {
target: term
}
}
function renderItem(item, prefix, index) {
return '<div class="autocomplete-suggestion" data-val="' + item.target + '" data-index="' + index + '">' + item.target + '</div>';
}
describe('Autocomplete Instance', function () {
jest.useFakeTimers();
jest.spyOn(window, 'getComputedStyle').mockImplementation(function () {
return { 'position': 'fixed' }
});
beforeEach(function () {
document.body.innerHTML = `
<form class="TestForm">
<fieldset>
<div class="input"><input type="search" class="Test">
</div>
</fieldset>
</form>`.trim();
window.localStorage.removeItem('localTest');
});
it('should add class "autocomplete-suggestion"', function () {
// WHEN
autoComplete({
selector: '.Test'
});
// THEN
expect(removeSpaces(document.body.innerHTML)).toBe(removeSpaces(`
<form class="TestForm">
<fieldset>
<div class="input"><input type="search" class="Test" autocomplete="off">
<div class="autocomplete-suggestions"></div>
</div>
</fieldset>
</form>
`));
});
it('should set browser default autocomplete off', function () {
// GIVEN
autoComplete({
selector: '.Test'
});
var inputBox = document.querySelector('.Test');
// THEN
expect(inputBox.getAttribute('autocomplete')).toBe('off')
});
it('should show all local storage queries when queryHistoryStorageName is not null and input is empty', function () {
// GIVEN
autoComplete({
selector: '.Test',
source: function (a, callback) { callback([]) },
queryHistoryStorageName: 'localTest',
buildTerm: buildTerm,
renderItem: renderItem,
target: 'target'
});
window.localStorage.setItem('localTest', JSON.stringify([{ target: '1' }, { target: '2' }]))
var clickEvent = new Event('click');
var inputBox = document.querySelector('.Test');
var suggestions = document.querySelector('.autocomplete-suggestions');
// WHEN
inputBox.dispatchEvent(clickEvent);
// THEN
expect(removeSpaces(suggestions.innerHTML)).toBe(removeSpaces(`
<div class="autocomplete-suggestion" data-val="<b></b>2" data-index="0"><b></b>2</div>
<div class="autocomplete-suggestion" data-val="<b></b>1" data-index="1"><b></b>1</div>
`));
});
it('should not show local storage queries when queryHistoryStorageName is null and input is empty', function () {
// GIVEN
autoComplete({
selector: '.Test',
source: function (a, callback) { callback([]) },
})
window.localStorage.setItem('localTest', JSON.stringify(['1', '2']))
var clickEvent = new MouseEvent('click');
var inputBox = document.querySelector('.Test');
var suggestions = document.querySelector('.autocomplete-suggestions');
// WHEN
inputBox.dispatchEvent(clickEvent);
// THEN
expect(suggestions.innerHTML).toBe("");
});
it('should show local queries suggestion while value of selector not reach minimum char if queryHistoryStorageName is not null', function () {
// GIVEN
autoComplete({
delay: 0,
selector: '.Test',
source: function (a, callback) {
callback(['suggestion 1', 'suggestion 2']);
},
queryHistoryStorageName: 'localTest',
renderItem: renderItem,
buildTerm: buildTerm,
target: 'target',
minChars: 3
});
window.localStorage.setItem('localTest', JSON.stringify([{ target: 'suggestion local 1' }, { target: 'suggestion local 2' }]))
var keyPressEvent = new KeyboardEvent('keyup');
var inputBox = document.querySelector('.Test');
inputBox.value = 'su';
var suggestions = document.querySelector('.autocomplete-suggestions')
// WHEN
inputBox.dispatchEvent(keyPressEvent);
jest.advanceTimersByTime(1);
// THEN
expect(removeSpaces(suggestions.innerHTML)).toBe(removeSpaces(`
<div class="autocomplete-suggestion" data-val="<b>su</b>ggestion local 2" data-index="0">
<b>su</b>ggestion local 2
</div>
<div class="autocomplete-suggestion" data-val="<b>su</b>ggestion local 1" data-index="1">
<b>su</b>ggestion local 1
</div>
`));
});
it('should show nothing while value of selector not reach minimum char and queryHistoryStorageName is not null', function () {
// GIVEN
autoComplete({
delay: 0,
selector: '.Test',
source: function (a, callback) {
callback(['suggestion 1', 'suggestion 2']);
},
minChars: 3
});
window.localStorage.setItem('localTest', JSON.stringify(['suggestion local 1', 'suggestion local 2']))
var keyPressEvent = new KeyboardEvent('keyup');
var inputBox = document.querySelector('.Test');
inputBox.value = 'su';
var suggestions = document.querySelector('.autocomplete-suggestions')
// WHEN
inputBox.dispatchEvent(keyPressEvent);
jest.advanceTimersByTime(1);
// THEN
expect(removeSpaces(suggestions.innerHTML)).toBe('');
})
it('should show suggestions when value of selector reach minimum char', function () {
// GIVEN
autoComplete({
delay: 0,
selector: '.Test',
source: function (a, callback) {
callback(['suggestion 1', 'suggestion 2']);
},
minChars: 3
});
var keyPressEvent = new KeyboardEvent('keyup');
var inputBox = document.querySelector('.Test');
inputBox.value = 'sugg';
var suggestions = document.querySelector('.autocomplete-suggestions')
// WHEN
inputBox.dispatchEvent(keyPressEvent);
jest.advanceTimersByTime(1);
// THEN
expect(removeSpaces(suggestions.innerHTML)).toBe(removeSpaces(`
<div class="autocomplete-suggestion" data-val="suggestion 1" data-index="0"><b>sugg</b>estion 1</div>
<div class="autocomplete-suggestion" data-val="suggestion 2" data-index="1"><b>sugg</b>estion 2</div>
`));
});
it('should merge local suggestions with the source suggestions if queryHistoryStorageName is not null', function () {
// GIVEN
autoComplete({
delay: 0,
selector: '.Test',
source: function (a, callback) {
callback([{ target: '<b>sugg</b>estion 1' }, { target: '<b>sugg</b>estion 2' }]);
},
queryHistoryStorageName: 'localTest',
renderItem: renderItem,
buildTerm: buildTerm,
target: 'target',
minChars: 3
});
window.localStorage.setItem('localTest', JSON.stringify([{ target: 'suggestion local 1' }, { target: 'suggestion local 2' }]))
var keyPressEvent = new KeyboardEvent('keyup');
var inputBox = document.querySelector('.Test');
inputBox.value = 'sugg';
var suggestions = document.querySelector('.autocomplete-suggestions')
// WHEN
inputBox.dispatchEvent(keyPressEvent);
jest.advanceTimersByTime(1);
// THEN
expect(removeSpaces(suggestions.innerHTML)).toBe(removeSpaces(`
<div class="autocomplete-suggestion" data-val="<b>sugg</b>estion local 2" data-index="0">
<b>sugg</b>estion local 2
</div>
<div class="autocomplete-suggestion" data-val="<b>sugg</b>estion local 1" data-index="1">
<b>sugg</b>estion local 1
</div>
<div class="autocomplete-suggestion" data-val="<b>sugg</b>estion 1" data-index="2">
<b>sugg</b>estion 1
</div>
<div class="autocomplete-suggestion" data-val="<b>sugg</b>estion 2" data-index="3">
<b>sugg</b>estion 2
</div>
`));
})
it('should set "selected" in class name when arrow down key is pressed', function () {
// GIVEN
autoComplete({
selector: '.Test',
source: function (a, callback) { callback([]) },
queryHistoryStorageName: 'localTest',
buildTerm: buildTerm,
renderItem: renderItem,
target: 'target'
});
window.event = 'true';
window.localStorage.setItem('localTest', JSON.stringify([{ target: '1' }, { target: '2' }]))
var clickEvent = new Event('click');
var arrowEvent = new KeyboardEvent('keydown', { keyCode: 40 });
var inputBox = document.querySelector('.Test');
inputBox.dispatchEvent(clickEvent);
// WHEN
inputBox.dispatchEvent(arrowEvent);
// THEN
var selectedSuggestion = document.querySelector('.autocomplete-suggestion.selected');
expect(selectedSuggestion).not.toBeNull()
});
it('should set display "none" and reset input value to last value when esc key is pressed', function () {
// GIVEN
autoComplete({
selector: '.Test',
source: function (a, callback) { callback([]) },
queryHistoryStorageName: 'localTest'
});
window.event = 'true';
window.localStorage.setItem('localTest', JSON.stringify(['1', '2']))
var clickEvent = new Event('click');
var escEvent = new KeyboardEvent('keydown', { keyCode: 27 });
var inputBox = document.querySelector('.Test');
var suggestions = document.querySelector('.autocomplete-suggestions');
inputBox.last_val = 'any';
inputBox.value = 'discarted'
inputBox.dispatchEvent(clickEvent);
suggestions.style.display = 'block';
// WHEN
inputBox.dispatchEvent(escEvent);
// THEN
expect(suggestions.style.display).toBe('none');
expect(inputBox.value).toBe('any');
});
it('should call o.onSelect function with the value of selected suggestion when enter key is pressed', function () {
// GIVEN
var selectFunction = jest.fn();
autoComplete({
delay: 0,
selector: '.Test',
source: function (a, callback) {
callback(['suggestion 1', 'suggestion 2']);
},
onSelect: selectFunction
});
window.event = 'true';
var keyPressEvent = new KeyboardEvent('keyup', { keyCode: 1 });
var inputBox = document.querySelector('.Test');
inputBox.value = 'suggw';
var suggestions = document.querySelector('.autocomplete-suggestions')
inputBox.dispatchEvent(keyPressEvent);
jest.advanceTimersByTime(1);
var arrowEvent = new KeyboardEvent('keydown', { keyCode: 40 });
var enterEvent = new KeyboardEvent('keydown', { keyCode: 13 });
inputBox.dispatchEvent(arrowEvent);
// WHEN
inputBox.dispatchEvent(enterEvent);
// THEN
expect(selectFunction).toHaveBeenCalled();
});
it('should add query to local storage if queryHistoryStorageName is not null and form is submitted', function () {
// GIVEN
autoComplete({
selector: '.Test',
source: function (a, callback) { callback([]) },
queryHistoryStorageName: 'localTest',
formSelector: '.TestForm',
buildTerm: function (term) { return term }
});
var submitEvent = new Event('submit');
var form = document.querySelector('.TestForm')
var inputBox = document.querySelector('.Test');
inputBox.value = 'new suggestion';
// WHEN
form.dispatchEvent(submitEvent);
var queries = window.localStorage.getItem('localTest');
// THEN
expect(queries).toBe('["new suggestion"]');
});
it('should not add query to local storage if queryHistoryStorageName or formSelector is null and form is submitted', function () {
// GIVEN
autoComplete({
selector: '.Test',
source: function (a, callback) { callback([]) },
buildTerm: function (term) { return term }
});
var submitEvent = new Event('submit');
var form = document.querySelector('.TestForm')
var inputBox = document.querySelector('.Test');
inputBox.value = 'new suggestion';
// WHEN
form.dispatchEvent(submitEvent);
var queries = window.localStorage.getItem('localTest');
// THEN
expect(queries).toBe(null);
});
})
function renderText(tooltip, text) {
return '<span class="autocomplete-chip chip chip--outline chip--sm tooltip-lg" data-toggle="tooltip" data-placement="bottom-right" data-tooltip ="' + tooltip +
'">' +
text +
'</span>';
}