Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix #424 groupOrder not working properly when maxItemPerGroup is …
…not set
  • Loading branch information
running-coder committed Apr 16, 2018
1 parent b0e985b commit ba694cd
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 4 deletions.
4 changes: 2 additions & 2 deletions dist/jquery.typeahead.min.js

Large diffs are not rendered by default.

3 changes: 1 addition & 2 deletions src/jquery.typeahead.js
Expand Up @@ -4,7 +4,7 @@
* Licensed under the MIT license
*
* @author Tom Bertrand
* @version 2.10.4 (2018-3-30)
* @version 2.10.4 (2018-4-16)
* @link http://www.runningcoder.org/jquerytypeahead/
*/
(function (factory) {
Expand Down Expand Up @@ -409,7 +409,6 @@
}

if (
this.options.maxItemPerGroup &&
this.options.group &&
this.options.group.key
) {
Expand Down
114 changes: 114 additions & 0 deletions test/option/groupOrder.test.js
@@ -0,0 +1,114 @@
const $ = require('jquery');
const Typeahead = require('../../src/jquery.typeahead');

describe('Typeahead groupOrder option Tests', () => {
'use strict';

let myTypeahead;

describe('groupOrder defined as desc', () => {
beforeAll(() => {
document.body.innerHTML = '<input class="js-typeahead">';

myTypeahead = $.typeahead({
input: '.js-typeahead',
minLength: 0,
group: 'category',
groupOrder: 'desc',
display: 'name',
source: {
myGroup: {
data: [{ category: 'Arizona', id: '1', name: 'Phil' }, { category: 'Indiana', id: '3', name: 'George' }],
},
},
});
});

it('Should have 1 group and contain 2 items ordered DESC', () => {
expect(myTypeahead.groupBy).toEqual('category');

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

expect(myTypeahead.groups).toEqual(['Indiana', 'Arizona']);
expect(myTypeahead.resultCountPerGroup).toEqual({ Indiana: 1, Arizona: 1 });

expect(myTypeahead.result[0]).toEqual({
matchedKey: 'name',
category: 'Indiana',
id: '3',
name: 'George',
group: 'myGroup',
});
});
});

describe('groupOrder defined as a boolean', () => {
beforeAll(() => {
document.body.innerHTML = '<input class="js-typeahead">';

myTypeahead = $.typeahead({
input: '.js-typeahead',
minLength: 0,
group: 'category',
groupOrder: true,
display: 'name',
source: {
myGroup: {
data: [{ category: 'Arizona', id: '1', name: 'Phil' }, { category: 'Indiana', id: '3', name: 'George' }],
},
},
});
});

it('Should have 1 group and contain 2 items ordered ASC', () => {
expect(myTypeahead.groupBy).toEqual('category');

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

expect(myTypeahead.groups).toEqual(['Arizona', 'Indiana']);
expect(myTypeahead.resultCountPerGroup).toEqual({ Arizona: 1, Indiana: 1 });
expect(myTypeahead.result[0]).toEqual({
matchedKey: 'name',
category: 'Arizona',
id: '1',
name: 'Phil',
group: 'myGroup',
});
});
});

describe('groupOrder defined as an Array', () => {
beforeAll(() => {
document.body.innerHTML = '<input class="js-typeahead">';

myTypeahead = $.typeahead({
input: '.js-typeahead',
minLength: 0,
group: 'category',
groupOrder: ['Indiana', 'Arizona'],
display: 'name',
source: {
myGroup: {
data: [{ category: 'Arizona', id: '1', name: 'Phil' }, { category: 'Indiana', id: '3', name: 'George' }],
},
},
});
});

it('Should have 1 group and contain 2 items', () => {
expect(myTypeahead.groupBy).toEqual('category');

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

expect(myTypeahead.groups).toEqual(['Indiana', 'Arizona']);
expect(myTypeahead.resultCountPerGroup).toEqual({ Indiana: 1, Arizona: 1 });
expect(myTypeahead.result[0]).toEqual({
matchedKey: 'name',
category: 'Indiana',
id: '3',
name: 'George',
group: 'myGroup',
});
});
});
});

0 comments on commit ba694cd

Please sign in to comment.