From 9335d107e2aaf86f6733be7f62bfcc7ac8822b1c Mon Sep 17 00:00:00 2001 From: Pixelastic Date: Mon, 12 Nov 2018 11:23:22 +0100 Subject: [PATCH] Adding tests --- src/autocomplete/typeahead.js | 8 ++-- test/unit/typeahead_spec.js | 78 +++++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 7 deletions(-) diff --git a/src/autocomplete/typeahead.js b/src/autocomplete/typeahead.js index 24636c8321..73d256aef6 100644 --- a/src/autocomplete/typeahead.js +++ b/src/autocomplete/typeahead.js @@ -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); @@ -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) { @@ -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); @@ -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); diff --git a/test/unit/typeahead_spec.js b/test/unit/typeahead_spec.js index 621bed15ad..770d4d2f0b 100644 --- a/test/unit/typeahead_spec.js +++ b/test/unit/typeahead_spec.js @@ -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() { @@ -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() { @@ -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); }); @@ -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); }); }); @@ -461,6 +514,25 @@ 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() { @@ -468,7 +540,7 @@ describe('Typeahead', 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());