From 179febff0170dc568750ca4273dd50165b183267 Mon Sep 17 00:00:00 2001 From: Colin Tinney Date: Mon, 15 Oct 2018 03:53:36 -0400 Subject: [PATCH] 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 --- src/autocomplete/dataset.js | 2 ++ test/unit/dataset_spec.js | 10 ++++++++++ 2 files changed, 12 insertions(+) diff --git a/src/autocomplete/dataset.js b/src/autocomplete/dataset.js index bd1f56f3f..6d809c90c 100644 --- a/src/autocomplete/dataset.js +++ b/src/autocomplete/dataset.js @@ -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) { diff --git a/test/unit/dataset_spec.js b/test/unit/dataset_spec.js index 1a4cb0ae3..0e853a30d 100644 --- a/test/unit/dataset_spec.js +++ b/test/unit/dataset_spec.js @@ -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 = $('
'); this.dataset = new Dataset({ @@ -500,6 +506,10 @@ describe('Dataset', function() { cb([{display: '4'}, {display: '5'}, {display: '6'}]); } + function fakeGetWithSyncNonArrayResults(query, cb) { + cb({}); + } + function fakeGetWithSyncEmptyResults(query, cb) { cb(); }