2012-12-18 22:07:02 +00:00
|
|
|
|
/*jslint vars: true*/
|
2013-01-21 00:51:44 +00:00
|
|
|
|
/*global describe, it, expect, waits, waitsFor, runs, afterEach, spyOn, $*/
|
2012-12-18 22:07:02 +00:00
|
|
|
|
|
|
|
|
|
describe('Autocomplete', function () {
|
|
|
|
|
'use strict';
|
|
|
|
|
|
2012-12-19 21:53:34 +00:00
|
|
|
|
afterEach(function () {
|
|
|
|
|
$('.autocomplete-suggestions').hide();
|
|
|
|
|
});
|
|
|
|
|
|
2012-12-18 22:07:02 +00:00
|
|
|
|
it('Should initialize autocomplete options', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
options = { serviceUrl: '/autocomplete/service/url' },
|
|
|
|
|
autocomplete = new $.Autocomplete(input, options);
|
|
|
|
|
|
|
|
|
|
expect(autocomplete.options.serviceUrl).toEqual(options.serviceUrl);
|
|
|
|
|
expect(autocomplete.suggestionsContainer).not.toBeNull();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Should set autocomplete attribute to "off"', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {});
|
|
|
|
|
|
|
|
|
|
expect(autocomplete).not.toBeNull();
|
|
|
|
|
expect(input.getAttribute('autocomplete')).toEqual('off');
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Should get current value', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
lookup: [{ value: 'Jamaica', data: 'B' }]
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
input.value = 'Jam';
|
|
|
|
|
autocomplete.onValueChange();
|
|
|
|
|
|
|
|
|
|
expect(autocomplete.visible).toBe(true);
|
|
|
|
|
expect(autocomplete.currentValue).toEqual('Jam');
|
|
|
|
|
});
|
|
|
|
|
|
2012-12-19 21:53:34 +00:00
|
|
|
|
it('Should call formatResult three times', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
counter = 0,
|
|
|
|
|
suggestion,
|
|
|
|
|
currentValue,
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
lookup: ['Jamaica', 'Jamaica', 'Jamaica'],
|
|
|
|
|
formatResult: function (s, v) {
|
|
|
|
|
suggestion = s;
|
|
|
|
|
currentValue = v;
|
|
|
|
|
counter += 1;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
input.value = 'Jam';
|
|
|
|
|
autocomplete.onValueChange();
|
|
|
|
|
|
|
|
|
|
expect(suggestion.value).toBe('Jamaica');
|
|
|
|
|
expect(suggestion.data).toBe(null);
|
|
|
|
|
expect(currentValue).toEqual('Jam');
|
|
|
|
|
expect(counter).toEqual(3);
|
|
|
|
|
});
|
|
|
|
|
|
2012-12-18 22:07:02 +00:00
|
|
|
|
it('Verify onSelect callback', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
context,
|
|
|
|
|
value,
|
|
|
|
|
data,
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
lookup: [{ value: 'A', data: 'B' }],
|
|
|
|
|
onSelect: function (suggestion) {
|
|
|
|
|
context = this;
|
|
|
|
|
value = suggestion.value;
|
|
|
|
|
data = suggestion.data;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
input.value = 'A';
|
|
|
|
|
autocomplete.onValueChange();
|
|
|
|
|
autocomplete.select(0);
|
|
|
|
|
|
|
|
|
|
expect(context).toEqual(input);
|
|
|
|
|
expect(value).toEqual('A');
|
|
|
|
|
expect(data).toEqual('B');
|
|
|
|
|
});
|
|
|
|
|
|
2012-12-19 20:44:48 +00:00
|
|
|
|
it('Should convert suggestions format', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
lookup: ['A', 'B']
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(autocomplete.options.lookup[0].value).toBe('A');
|
|
|
|
|
expect(autocomplete.options.lookup[1].value).toBe('B');
|
|
|
|
|
});
|
|
|
|
|
|
2013-02-21 17:34:47 +00:00
|
|
|
|
it('Should execute onSearchStart', function () {
|
2012-12-19 21:53:34 +00:00
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
startQuery,
|
|
|
|
|
ajaxExecuted = false,
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
serviceUrl: '/test',
|
2013-02-21 17:34:47 +00:00
|
|
|
|
onSearchStart: function (params) {
|
|
|
|
|
startQuery = params.query;
|
2012-12-19 21:53:34 +00:00
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$.mockjax({
|
|
|
|
|
url: '/test',
|
|
|
|
|
responseTime: 50,
|
|
|
|
|
response: function (settings) {
|
|
|
|
|
ajaxExecuted = true;
|
|
|
|
|
var query = settings.data.query,
|
|
|
|
|
response = {
|
|
|
|
|
query: query,
|
|
|
|
|
suggestions: []
|
|
|
|
|
};
|
|
|
|
|
this.responseText = JSON.stringify(response);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
input.value = 'A';
|
|
|
|
|
autocomplete.onValueChange();
|
|
|
|
|
|
|
|
|
|
waitsFor(function () {
|
|
|
|
|
return ajaxExecuted;
|
|
|
|
|
}, 'Ajax call never completed.', 100);
|
|
|
|
|
|
|
|
|
|
runs(function () {
|
|
|
|
|
expect(ajaxExecuted).toBe(true);
|
|
|
|
|
expect(startQuery).toBe('A');
|
2013-02-21 17:34:47 +00:00
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Should execute onSearchCompleted', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
completeQuery,
|
|
|
|
|
ajaxExecuted = false,
|
|
|
|
|
url = '/test-completed',
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
serviceUrl: url,
|
|
|
|
|
onSearchComplete: function (query) {
|
|
|
|
|
completeQuery = query;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$.mockjax({
|
|
|
|
|
url: url,
|
|
|
|
|
responseTime: 50,
|
|
|
|
|
response: function (settings) {
|
|
|
|
|
ajaxExecuted = true;
|
|
|
|
|
var query = settings.data.query,
|
|
|
|
|
response = {
|
|
|
|
|
query: query,
|
|
|
|
|
suggestions: []
|
|
|
|
|
};
|
|
|
|
|
this.responseText = JSON.stringify(response);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
input.value = 'A';
|
|
|
|
|
autocomplete.onValueChange();
|
|
|
|
|
|
|
|
|
|
waitsFor(function () {
|
|
|
|
|
return ajaxExecuted;
|
|
|
|
|
}, 'Ajax call never completed.', 100);
|
|
|
|
|
|
|
|
|
|
runs(function () {
|
|
|
|
|
expect(ajaxExecuted).toBe(true);
|
2012-12-19 21:53:34 +00:00
|
|
|
|
expect(completeQuery).toBe('A');
|
|
|
|
|
});
|
|
|
|
|
});
|
2013-01-05 00:58:49 +00:00
|
|
|
|
|
2013-02-21 22:25:07 +00:00
|
|
|
|
it('Should transform results', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
ajaxExecuted = false,
|
|
|
|
|
url = '/test-transform',
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
serviceUrl: url,
|
|
|
|
|
transformResult: function (result, query) {
|
|
|
|
|
return {
|
|
|
|
|
query: query,
|
|
|
|
|
suggestions: $.map(result.split(','), function (item) {
|
|
|
|
|
return { value: item, data: null };
|
|
|
|
|
})
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$.mockjax({
|
|
|
|
|
url: url,
|
|
|
|
|
responseTime: 50,
|
|
|
|
|
response: function () {
|
|
|
|
|
ajaxExecuted = true;
|
|
|
|
|
this.responseText = 'Andora,Angola,Argentina';
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
input.value = 'A';
|
|
|
|
|
autocomplete.onValueChange();
|
|
|
|
|
|
|
|
|
|
waitsFor(function () {
|
|
|
|
|
return ajaxExecuted;
|
|
|
|
|
}, 'Ajax call never completed.', 100);
|
|
|
|
|
|
|
|
|
|
runs(function () {
|
|
|
|
|
expect(ajaxExecuted).toBe(true);
|
|
|
|
|
expect(autocomplete.suggestions.length).toBe(3);
|
|
|
|
|
expect(autocomplete.suggestions[0].value).toBe('Andora');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2013-02-21 23:07:33 +00:00
|
|
|
|
it('Should not require orginal query value from the server', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
ajaxExecuted = false,
|
|
|
|
|
url = '/test-original-query',
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
serviceUrl: url
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$.mockjax({
|
|
|
|
|
url: url,
|
|
|
|
|
responseTime: 50,
|
|
|
|
|
response: function () {
|
|
|
|
|
ajaxExecuted = true;
|
|
|
|
|
var response = {
|
|
|
|
|
query: null,
|
|
|
|
|
suggestions: ['A', 'B', 'C']
|
|
|
|
|
};
|
|
|
|
|
this.responseText = JSON.stringify(response);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
input.value = 'A';
|
|
|
|
|
autocomplete.onValueChange();
|
|
|
|
|
|
|
|
|
|
waitsFor(function () {
|
|
|
|
|
return ajaxExecuted;
|
|
|
|
|
}, 'Ajax call never completed.', 100);
|
|
|
|
|
|
|
|
|
|
runs(function () {
|
|
|
|
|
expect(ajaxExecuted).toBe(true);
|
|
|
|
|
expect(autocomplete.suggestions.length).toBe(3);
|
|
|
|
|
expect(autocomplete.suggestions[0].value).toBe('A');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
2013-01-05 00:58:49 +00:00
|
|
|
|
it('Should should not preventDefault when tabDisabled is set to false', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
lookup: [{ value: 'Jamaica', data: 'B' }],
|
2013-01-20 20:26:30 +00:00
|
|
|
|
tabDisabled: false,
|
|
|
|
|
autoSelectFirst: true
|
2013-01-20 20:09:09 +00:00
|
|
|
|
});
|
2013-01-05 00:58:49 +00:00
|
|
|
|
input.value = 'Jam';
|
|
|
|
|
autocomplete.onValueChange();
|
|
|
|
|
|
|
|
|
|
var event = $.Event('keydown');
|
|
|
|
|
event.keyCode = 9; // the tab keycode
|
|
|
|
|
spyOn(event, 'stopImmediatePropagation');
|
|
|
|
|
spyOn(event, 'preventDefault');
|
|
|
|
|
spyOn(autocomplete, 'suggest');
|
|
|
|
|
|
|
|
|
|
expect(autocomplete.visible).toBe(true);
|
|
|
|
|
expect(autocomplete.disabled).toBe(undefined);
|
|
|
|
|
expect(autocomplete.selectedIndex).not.toBe(-1);
|
|
|
|
|
|
|
|
|
|
$(input).trigger(event);
|
|
|
|
|
|
|
|
|
|
expect(event.stopImmediatePropagation).not.toHaveBeenCalled();
|
|
|
|
|
expect(event.preventDefault).not.toHaveBeenCalled();
|
|
|
|
|
expect(autocomplete.suggest).not.toHaveBeenCalled();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Should should preventDefault when tabDisabled is set to true', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
lookup: [{ value: 'Jamaica', data: 'B' }],
|
2013-01-20 20:26:30 +00:00
|
|
|
|
tabDisabled: true,
|
|
|
|
|
autoSelectFirst: true
|
2013-01-20 20:09:09 +00:00
|
|
|
|
});
|
2013-01-05 00:58:49 +00:00
|
|
|
|
input.value = 'Jam';
|
|
|
|
|
autocomplete.onValueChange();
|
|
|
|
|
|
|
|
|
|
var event = $.Event('keydown');
|
|
|
|
|
event.keyCode = 9; // the tab keycode
|
|
|
|
|
spyOn(event, 'stopImmediatePropagation');
|
|
|
|
|
spyOn(event, 'preventDefault');
|
|
|
|
|
spyOn(autocomplete, 'suggest');
|
|
|
|
|
|
|
|
|
|
expect(autocomplete.visible).toBe(true);
|
|
|
|
|
expect(autocomplete.disabled).toBe(undefined);
|
|
|
|
|
expect(autocomplete.selectedIndex).not.toBe(-1);
|
|
|
|
|
|
|
|
|
|
$(input).trigger(event);
|
|
|
|
|
|
|
|
|
|
expect(event.stopImmediatePropagation).toHaveBeenCalled();
|
|
|
|
|
expect(event.preventDefault).toHaveBeenCalled();
|
|
|
|
|
expect(autocomplete.suggest).not.toHaveBeenCalled();
|
|
|
|
|
});
|
2013-01-20 20:26:30 +00:00
|
|
|
|
|
|
|
|
|
it('Should not autoselect first item by default', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
lookup: ['Jamaica', 'Jamaica', 'Jamaica']
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
input.value = 'Jam';
|
|
|
|
|
autocomplete.onValueChange();
|
|
|
|
|
|
|
|
|
|
expect(autocomplete.selectedIndex).toBe(-1);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
it('Should autoselect first item autoSelectFirst set to true', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
lookup: ['Jamaica', 'Jamaica', 'Jamaica'],
|
|
|
|
|
autoSelectFirst: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
input.value = 'Jam';
|
|
|
|
|
autocomplete.onValueChange();
|
|
|
|
|
|
|
|
|
|
expect(autocomplete.selectedIndex).toBe(0);
|
|
|
|
|
});
|
2013-01-21 00:51:44 +00:00
|
|
|
|
|
|
|
|
|
it('Should use custom query parameter name', function () {
|
|
|
|
|
var input = document.createElement('input'),
|
|
|
|
|
paramName = 'custom',
|
|
|
|
|
paramValue = null,
|
|
|
|
|
autocomplete = new $.Autocomplete(input, {
|
|
|
|
|
serviceUrl: '/test-query',
|
|
|
|
|
paramName: paramName
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$.mockjax({
|
|
|
|
|
url: '/test-query',
|
|
|
|
|
responseTime: 5,
|
|
|
|
|
response: function (settings) {
|
|
|
|
|
paramValue = settings.data[paramName];
|
|
|
|
|
var response = {
|
|
|
|
|
query: paramValue,
|
|
|
|
|
suggestions: []
|
|
|
|
|
};
|
|
|
|
|
this.responseText = JSON.stringify(response);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
input.value = 'Jam';
|
|
|
|
|
autocomplete.onValueChange();
|
|
|
|
|
|
|
|
|
|
waits(10);
|
|
|
|
|
|
|
|
|
|
runs(function () {
|
|
|
|
|
expect(paramValue).toBe('Jam');
|
|
|
|
|
});
|
|
|
|
|
});
|
2013-04-24 19:46:04 +00:00
|
|
|
|
|
|
|
|
|
it('Should destroy autocomplete instance', function () {
|
|
|
|
|
var input = $(document.createElement('input')),
|
|
|
|
|
div = $(document.createElement('div'));
|
|
|
|
|
|
|
|
|
|
input.autocomplete({
|
|
|
|
|
serviceUrl: '/test-dispose',
|
|
|
|
|
appendTo: div
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
expect(input.data('autocomplete')).toBeDefined();
|
|
|
|
|
expect(div.children().length).toBeGreaterThan(0);
|
|
|
|
|
|
|
|
|
|
input.autocomplete('dispose');
|
|
|
|
|
|
|
|
|
|
expect(input.data('autocomplete')).toBeUndefined();
|
|
|
|
|
expect(div.children().length).toBe(0);
|
|
|
|
|
});
|
2013-04-24 19:55:34 +00:00
|
|
|
|
|
|
|
|
|
it('Should return Autocomplete instance if called without arguments', function () {
|
|
|
|
|
var input = $(document.createElement('input'));
|
|
|
|
|
|
|
|
|
|
input.autocomplete({
|
|
|
|
|
serviceUrl: '/test-dispose'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var instance = input.autocomplete();
|
|
|
|
|
|
|
|
|
|
expect(instance instanceof $.Autocomplete).toBe(true);
|
|
|
|
|
});
|
2012-12-18 22:07:02 +00:00
|
|
|
|
});
|