Skip to content
This repository has been archived by the owner on Nov 22, 2021. It is now read-only.

Commit

Permalink
refactor(autoComplete): Rename autoSelectFirstSuggestion as selectFir…
Browse files Browse the repository at this point in the history
…stMatch
  • Loading branch information
mbenford committed Dec 2, 2014
1 parent 6c8d480 commit fbab7a0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
2 changes: 1 addition & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = function(config) {
// - Safari (only Mac)
// - PhantomJS
// - IE (only Windows)
browsers: ['Chrome', 'Firefox', 'Opera'],
browsers: ['Chrome'],

// If browser does not capture in given timeout [ms], kill it
captureTimeout: 60000,
Expand Down
11 changes: 6 additions & 5 deletions src/auto-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
* becomes empty. The $query variable will be passed to the expression as an empty string.
* @param {boolean=} {loadOnFocus=false} Flag indicating that the source option will be evaluated when the input element
* gains focus. The current input value is available as $query.
* @param {boolean=} [autoSelectFirstSuggestion=false] Flag indicating that the first suggestion will not be
* automatically selected once the suggestion box is shown.
* @param {boolean=} [selectFirstMatch=true] Flag indicating that the first match will be automatically selected once
* the suggestion list is shown.
*/
tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tagsInputConfig) {
function SuggestionList(loadFn, options) {
Expand All @@ -50,9 +50,10 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags
$timeout.cancel(debouncedLoadId);
};
self.show = function() {
if (self.items.length > 0 && options.autoSelectFirstSuggestion) {
if (options.selectFirstMatch) {
self.select(0);
} else {
}
else {
self.selected = null;
}
self.visible = true;
Expand Down Expand Up @@ -122,7 +123,7 @@ tagsInput.directive('autoComplete', function($document, $timeout, $sce, $q, tags
loadOnDownArrow: [Boolean, false],
loadOnEmpty: [Boolean, false],
loadOnFocus: [Boolean, false],
autoSelectFirstSuggestion: [Boolean, false]
selectFirstMatch: [Boolean, true]
});

options = scope.options;
Expand Down
42 changes: 27 additions & 15 deletions test/auto-complete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe('autoComplete directive', function() {
parentCtrl, element, isolateScope, suggestionList, deferred, tagsInput, eventHandlers;

beforeEach(function() {
jasmine.addMatchers(customMatchers);

module('ngTagsInput');

inject(function($rootScope, _$compile_, _$q_, _$timeout_) {
Expand Down Expand Up @@ -311,6 +313,7 @@ describe('autoComplete directive', function() {
it('does not change the input value when the enter key is pressed and there is nothing selected', function() {
// Arrange
loadSuggestions(2);
suggestionList.selected = null;

// Act
sendKeyDown(KEYS.enter);
Expand Down Expand Up @@ -357,14 +360,6 @@ describe('autoComplete directive', function() {
expect(getSuggestion(2).hasClass('selected')).toBe(false);
});

it('selects no suggestion after the suggestion box is shown', function() {
// Arrange/Act
loadSuggestions(2);

// Assert
expect(suggestionList.selected).toBeNull();
});

it('discards all load calls but the last one', function() {
// Arrange
var deferred1 = $q.defer(), deferred2 = $q.defer(), deferred3 = $q.defer();
Expand Down Expand Up @@ -1031,24 +1026,41 @@ describe('autoComplete directive', function() {
});
});

describe('auto-select-first-suggestion option', function() {
it('initializes the option to false', function() {
describe('select-first-match option', function() {
it('initializes the option to true', function() {
// Arrange/Act
compile();

// Assert
expect(isolateScope.options.autoSelectFirstSuggestion).toBe(false);
expect(isolateScope.options.selectFirstMatch).toBe(true);
});

it('selects the first suggestion after the suggestion box is shown if true', function() {
it('selects the first suggestion after the suggestion box is shown if the option is true', function() {
// Arrange
compile('auto-select-first-suggestion="true"');
compile('select-first-match="true"');

//Act
loadSuggestions(2);
loadSuggestions(3);

// Assert
expect(getSuggestion(0).hasClass('selected')).toBe(true);
expect(getSuggestion(0)).toHaveClass('selected');
expect(getSuggestion(1)).not.toHaveClass('selected');
expect(getSuggestion(2)).not.toHaveClass('selected');

});

it('doesn\'t select any suggestion after the suggestion box is shown if the option is false', function() {
// Arrange
compile('select-first-match="false"');

//Act
loadSuggestions(3);

// Assert
expect(getSuggestion(0)).not.toHaveClass('selected');
expect(getSuggestion(1)).not.toHaveClass('selected');
expect(getSuggestion(2)).not.toHaveClass('selected');

});
});
});
4 changes: 2 additions & 2 deletions test/tags-input.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ describe('tags-input directive', function() {
isolateScope, element;

beforeEach(function() {
jasmine.addMatchers(customMatchers);

module('ngTagsInput');

inject(function(_$compile_, _$rootScope_, _$document_, _$timeout_) {
Expand All @@ -13,8 +15,6 @@ describe('tags-input directive', function() {
$document = _$document_;
$timeout = _$timeout_;
});

jasmine.addMatchers(customMatchers);
});

function compile() {
Expand Down

0 comments on commit fbab7a0

Please sign in to comment.