Skip to content

Commit 179febf

Browse files
cdtinneyHaroenv
authored andcommitted
feat: Throw err on update if suggestions are invalid type (#256)
* Throw err on update if suggestions are invalid type Resolves #131 * Check error type and message in spec
1 parent 0e65fee commit 179febf

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/autocomplete/dataset.js

+2
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@ _.mixin(Dataset.prototype, EventEmitter, {
110110
.html(getSuggestionsHtml.apply(this, renderArgs))
111111
.prepend(that.templates.header ? getHeaderHtml.apply(this, renderArgs) : null)
112112
.append(that.templates.footer ? getFooterHtml.apply(this, renderArgs) : null);
113+
} else if (suggestions && !Array.isArray(suggestions)) {
114+
throw new TypeError('suggestions must be an array');
113115
}
114116

115117
if (this.$menu) {

test/unit/dataset_spec.js

+10
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@ describe('Dataset', function() {
8080
expect(this.dataset.getRoot()).toContainText('empty');
8181
});
8282

83+
it('should throw an error if suggestions is not an array', function() {
84+
this.source.and.callFake(fakeGetWithSyncNonArrayResults);
85+
expect(this.dataset.update.bind(this.dataset, 'woah'))
86+
.toThrowError(TypeError, 'suggestions must be an array');
87+
});
88+
8389
it('should set the aa-without class when no suggestions are available', function() {
8490
var $menu = $('<div />');
8591
this.dataset = new Dataset({
@@ -500,6 +506,10 @@ describe('Dataset', function() {
500506
cb([{display: '4'}, {display: '5'}, {display: '6'}]);
501507
}
502508

509+
function fakeGetWithSyncNonArrayResults(query, cb) {
510+
cb({});
511+
}
512+
503513
function fakeGetWithSyncEmptyResults(query, cb) {
504514
cb();
505515
}

0 commit comments

Comments
 (0)