Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pixelastic committed Nov 12, 2018
1 parent 82a6bd0 commit 9335d10
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 7 deletions.
8 changes: 4 additions & 4 deletions src/autocomplete/typeahead.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ _.mixin(Typeahead.prototype, {

_onSuggestionClicked: function onSuggestionClicked(type, $el) {
var datum;
var context = { selectionMethod: 'click' }
var context = {selectionMethod: 'click'};

if (datum = this.dropdown.getDatumForSuggestion($el)) {
this._select(datum, context);
Expand Down Expand Up @@ -256,7 +256,7 @@ _.mixin(Typeahead.prototype, {

cursorDatum = this.dropdown.getDatumForCursor();
topSuggestionDatum = this.dropdown.getDatumForTopSuggestion();
var context = { selectionMethod: 'blur' }
var context = {selectionMethod: 'blur'};

if (!this.debug) {
if (this.autoselectOnBlur && cursorDatum) {
Expand All @@ -277,7 +277,7 @@ _.mixin(Typeahead.prototype, {

cursorDatum = this.dropdown.getDatumForCursor();
topSuggestionDatum = this.dropdown.getDatumForTopSuggestion();
var context = { selectionMethod: 'enterKey' }
var context = {selectionMethod: 'enterKey'};

if (cursorDatum) {
this._select(cursorDatum, context);
Expand All @@ -296,7 +296,7 @@ _.mixin(Typeahead.prototype, {
}

var datum;
var context = { selectionMethod: 'tabKey' }
var context = {selectionMethod: 'tabKey'};

if (datum = this.dropdown.getDatumForCursor()) {
this._select(datum, context);
Expand Down
78 changes: 75 additions & 3 deletions test/unit/typeahead_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,23 @@ describe('Typeahead', function() {
var that = this;
waitsForAndRuns(function() { return that.dropdown.close.calls.count(); }, done, 100);
});

it('should pass the selection method as part of the context ', function(done) {
var spy;

this.$input.on('autocomplete:selected', spy = jasmine.createSpy());
this.dropdown.trigger('suggestionClicked');

expect(spy).toHaveBeenCalledWith(
jasmine.any(Object),
undefined,
undefined,
{ selectionMethod: 'click' }
);

var that = this;
waitsForAndRuns(function() { return that.dropdown.close.calls.count(); }, done, 100);
});
});

describe('when dropdown triggers suggestionClicked with undefined displayKey', function() {
Expand Down Expand Up @@ -339,6 +356,23 @@ describe('Typeahead', function() {
expect(this.input.setQuery).toHaveBeenCalledWith(fixtures.data.simple[1].value);
expect(this.input.setInputValue).toHaveBeenCalledWith(fixtures.data.simple[1].value, true);
});

it('should pass the selectionMethod as part of the context', function() {
this.view.autoselectOnBlur = true;
this.dropdown.getDatumForTopSuggestion.and.returnValue(testDatum);

var spy;

this.$input.on('autocomplete:selected', spy = jasmine.createSpy());
this.input.trigger('blurred');

expect(spy).toHaveBeenCalledWith(
jasmine.any(Object),
undefined,
undefined,
{ selectionMethod: 'blur' }
);
});
});

describe('when debug flag is set', function() {
Expand Down Expand Up @@ -392,7 +426,7 @@ describe('Typeahead', function() {
handleObj: jasmine.objectContaining({
type: 'autocomplete:selected'
})
}), undefined, undefined);
}), undefined, undefined, jasmine.any(Object));
expect(view.input.setQuery).toHaveBeenCalledWith('');
expect(view.input.setInputValue).toHaveBeenCalledWith('', true);
});
Expand Down Expand Up @@ -424,9 +458,28 @@ describe('Typeahead', function() {
var $e;

$e = jasmine.createSpyObj('event', ['preventDefault']);
this.input.trigger(enterKeyed', $e);
expect($e.prventDefault).toHaveBeenCalled();
});

it('should pass the selection method as part of the context ', function(done) {
var $e;
var spy;
var anything = jasmine.any(Object);

$e = jasmine.createSpyObj('event', ['preventDefault']);
this.$input.on('autocomplete:selected', spy = jasmine.createSpy());
this.input.trigger('enterKeyed', $e);

expect($e.preventDefault).toHaveBeenCalled();
expect(spy).toHaveBeenCalledWith(
anything,
undefined,
undefined,
{ selectionMethod: 'enterKey' }
);

var that = this;
waitsForAndRuns(function() { return that.dropdown.close.calls.count(); }, done, 100);
});
});

Expand Down Expand Up @@ -461,14 +514,33 @@ describe('Typeahead', function() {

expect($e.preventDefault).toHaveBeenCalled();
});

it('should pass the selectionMethod as part of the context', function(done) {
var $e;
var spy;

$e = jasmine.createSpyObj('event', ['preventDefault']);
this.$input.on('autocomplete:selected', spy = jasmine.createSpy());
this.input.trigger('tabKeyed', $e);

expect(spy).toHaveBeenCalledWith(
jasmine.any(Object),
undefined,
undefined,
{ selectionMethod: 'tabKey' }
);

var that = this;
waitsForAndRuns(function() { return that.dropdown.close.calls.count(); }, done, 100);
});
});

describe('when cursor is not in use', function() {
it('should autocomplete if tabAutocomplete is true', function() {
var spy;

this.input.getQuery.and.returnValue('bi');
this.input.getHint.and.returnValue(testDatum.value);
th.getHint.and.returnValue(testDatum.value);
this.input.isCursorAtEnd.and.returnValue(true);
this.dropdown.getDatumForTopSuggestion.and.returnValue(testDatum);
this.$input.on('autocomplete:autocompleted', spy = jasmine.createSpy());
Expand Down

0 comments on commit 9335d10

Please sign in to comment.