Navigation Menu

Skip to content

Commit

Permalink
Add #280 suggestions, if group is set to maxLength: 0 and global is…
Browse files Browse the repository at this point in the history
… set to `maxLength: 1`

Add `option.maxLength` to not search inside a group if above X characters
Add group option to override the global config for `minLength`, `maxLength` and `dynamic`
Add Deferred object for searching dynamic groups `this.node.triggerHandler('input').then(() => {})`
Changed `dynamic` input event to `search`
Fix issue when `maxItem` and `maxGroupItem` would send inaccurate results
  • Loading branch information
running-coder committed Feb 5, 2017
1 parent 30a4e1f commit 7ff0a45
Show file tree
Hide file tree
Showing 15 changed files with 507 additions and 294 deletions.
6 changes: 3 additions & 3 deletions dist/jquery.typeahead.min.js

Large diffs are not rendered by default.

195 changes: 100 additions & 95 deletions src/jquery.typeahead.js

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions test/integration/anyValueType.test.js
Expand Up @@ -21,7 +21,7 @@ describe('Typeahead can not display null or boolean if the item is a string', ()
});

it('Should display any value types', () => {
myTypeahead.node.trigger('input.typeahead');
myTypeahead.node.trigger('input');

expect(myTypeahead.result.length).toEqual(3);
});
Expand Down Expand Up @@ -58,21 +58,21 @@ describe('Typeahead can display any value type Tests from inside an object', ()
});

it('Should display any value types', () => {
myTypeahead.node.trigger('input.typeahead');
myTypeahead.node.trigger('input');

expect(myTypeahead.resultHtml.find('span').text()).toEqual('string 12345 true false 42');
});

it('Should display a boolean "false" search', () => {
myTypeahead.node.val('false');
myTypeahead.node.trigger('input.typeahead');
myTypeahead.node.trigger('input');

expect(myTypeahead.result.length).toEqual(1);
});

it('Should display a numeric value', () => {
myTypeahead.node.val('345');
myTypeahead.node.trigger('input.typeahead');
myTypeahead.node.trigger('input');

expect(myTypeahead.result.length).toEqual(1);
});
Expand Down
@@ -1,7 +1,7 @@
const $ = require("jquery");
const Typeahead = require('../../src/jquery.typeahead');

describe('Typeahead Callback Tests', () => {
describe('Typeahead onCancel Callback Tests', () => {
'use strict';

let myTypeahead,
Expand Down Expand Up @@ -42,26 +42,26 @@ describe('Typeahead Callback Tests', () => {

it('Should call onCancel callback when ESC is pressed', () => {
myTypeahead.node.val('test');
myTypeahead.node.trigger('input.typeahead');
myTypeahead.node.trigger('input');

myTypeahead.node.trigger($.Event("keydown", { keyCode: 27 }));
expect(onCancel).toBeTruthy();
});

it('Should call onCancel callback if cancel button is clicked', () => {
myTypeahead.node.val('test');
myTypeahead.node.trigger('input.typeahead');
myTypeahead.node.trigger('input');

myTypeahead.node.parent().find('.typeahead__cancel-button').trigger('mousedown')
expect(onCancel).toBeTruthy();
});

it('Should call onCancel callback if a character is deleted and the input is empty', () => {
myTypeahead.node.val('test');
myTypeahead.node.trigger('input.typeahead');
myTypeahead.node.trigger('input');

myTypeahead.node.val('');
myTypeahead.node.trigger('input.typeahead');
myTypeahead.node.trigger('input');

expect(onCancel).toBeTruthy();
});
Expand Down
45 changes: 45 additions & 0 deletions test/integration/callback.onsendrequest.test.js
@@ -0,0 +1,45 @@
const $ = require("jquery");
const Typeahead = require('../../src/jquery.typeahead');

describe('Typeahead onSendRequest Callback Tests', () => {
'use strict';

let myTypeahead;

beforeAll(() => {

document.body.innerHTML = '<input class="js-typeahead">';

myTypeahead = $.typeahead({
input: '.js-typeahead',
minLength: 0,
generateOnLoad: true,
display: ['name'],
emptyTemplate: "no result for {{query}}",
source: {
group1: {
ajax: {
url: ''
}
},
group2: {
ajax: {
url: ''
}
}
},
callback: {
onSendRequest: function (node, event) {

// Return false will prevent from sending Ajax request(s)
return !!this.query.length;
}
}
});
});

it('Should not display any data because the requests were canceled', () => {
// @TODO Add the test
});

});
92 changes: 92 additions & 0 deletions test/integration/length.test.js
@@ -0,0 +1,92 @@
const $ = require("jquery");
const Typeahead = require('../../src/jquery.typeahead');

describe('Typeahead displays result according to input length per group', () => {

let myTypeahead;

beforeAll(() => {

document.body.innerHTML = '<input class="js-typeahead">';

myTypeahead = $.typeahead({
input: '.js-typeahead',
template: function () {
return "{{display}}"
},
source: {
group1: {
minLength: 0,
maxLength: 2,
data: [
'group1-data1',
'group1-data2',
'group1-data3'
]
},
group2: {
minLength: 1,
data: [
'group2-data1',
'group2-data2',
'group2-data3'
]
},
group3: {
minLength: 0,
data: [
'group3-data1',
'group3-data2',
'group3-data3'
]
},
group4: {
maxLength: 3,
data: [
'group4-data1',
'group4-data2',
'group4-data3'
]
},
group5: {
minLength: 0,
maxLength: 3,
data: [
'group5-data1',
'group5-data2',
'group5-data3'
]
}
}
});
});

it('Should assign the proper Typeahead selector', () => {

myTypeahead.node.val('');
myTypeahead.node.trigger('input');

expect(myTypeahead.searchGroups).toEqual(['group1', 'group3', 'group5']);

myTypeahead.node.val('g');
myTypeahead.node.trigger('input');

expect(myTypeahead.searchGroups).toEqual(['group1', 'group2', 'group3', 'group5']);

myTypeahead.node.val('gr');
myTypeahead.node.trigger('input');

expect(myTypeahead.searchGroups).toEqual(['group1', 'group2', 'group3', 'group4', 'group5']);

myTypeahead.node.val('group');
myTypeahead.node.trigger('input');

expect(myTypeahead.searchGroups).toEqual(['group2', 'group3']);

});

afterAll(() => {
delete window.Typeahead['.js-typeahead'];
});

});

0 comments on commit 7ff0a45

Please sign in to comment.