Skip to content

Commit

Permalink
feat: Throw err on update if suggestions are invalid type (#256)
Browse files Browse the repository at this point in the history
* Throw err on update if suggestions are invalid type

Resolves #131

* Check error type and message in spec
  • Loading branch information
cdtinney authored and Haroenv committed Oct 15, 2018
1 parent 0e65fee commit 179febf
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/autocomplete/dataset.js
Expand Up @@ -110,6 +110,8 @@ _.mixin(Dataset.prototype, EventEmitter, {
.html(getSuggestionsHtml.apply(this, renderArgs))
.prepend(that.templates.header ? getHeaderHtml.apply(this, renderArgs) : null)
.append(that.templates.footer ? getFooterHtml.apply(this, renderArgs) : null);
} else if (suggestions && !Array.isArray(suggestions)) {
throw new TypeError('suggestions must be an array');
}

if (this.$menu) {
Expand Down
10 changes: 10 additions & 0 deletions test/unit/dataset_spec.js
Expand Up @@ -80,6 +80,12 @@ describe('Dataset', function() {
expect(this.dataset.getRoot()).toContainText('empty');
});

it('should throw an error if suggestions is not an array', function() {
this.source.and.callFake(fakeGetWithSyncNonArrayResults);
expect(this.dataset.update.bind(this.dataset, 'woah'))
.toThrowError(TypeError, 'suggestions must be an array');
});

it('should set the aa-without class when no suggestions are available', function() {
var $menu = $('<div />');
this.dataset = new Dataset({
Expand Down Expand Up @@ -500,6 +506,10 @@ describe('Dataset', function() {
cb([{display: '4'}, {display: '5'}, {display: '6'}]);
}

function fakeGetWithSyncNonArrayResults(query, cb) {
cb({});
}

function fakeGetWithSyncEmptyResults(query, cb) {
cb();
}
Expand Down

0 comments on commit 179febf

Please sign in to comment.