Skip to content

Commit

Permalink
Merge branch 'master' into docs-clarify-api
Browse files Browse the repository at this point in the history
  • Loading branch information
cdtinney authored Oct 28, 2018
2 parents 5fde8d3 + 179febf commit 1a90da5
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ Datasets can be configured using the following options.
* `debounce` – If set, will postpone the source execution until after `debounce` milliseconds
have elapsed since the last time it was invoked.

* `cache` - If set to `false`, subsequent identical queries will always execute the source function for suggestions. Defaults to `true`.

## Sources

Expand Down
3 changes: 2 additions & 1 deletion scripts/netlify-deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ function logStdOut(opts) {
}

if (!process.env.NETLIFY_API_KEY || !process.env.NETLIFY_SITE_ID) {
throw new Error(
console.warn(
'Both NETLIFY_API_KEY and NETLIFY_SITE_ID are required. ' +
'They can be found on ' +
'https://app.netlify.com/sites/autocompletejs-playgrounds/settings/general' +
' and https://app.netlify.com/account/applications'
);
process.exit(0);
}

execa('yarn', ['build'])
Expand Down
9 changes: 8 additions & 1 deletion src/autocomplete/dataset.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ function Dataset(o) {

this.debounce = o.debounce;

this.cache = o.cache !== false;

this.templates = getTemplates(o.templates, this.displayFn);

this.css = _.mixin({}, css, o.appendTo ? css.appendTo : {});
Expand Down Expand Up @@ -108,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 Expand Up @@ -235,7 +239,10 @@ _.mixin(Dataset.prototype, EventEmitter, {
},

shouldFetchFromCache: function shouldFetchFromCache(query) {
return this.cachedQuery === query && this.cachedSuggestions && this.cachedSuggestions.length;
return this.cache &&
this.cachedQuery === query &&
this.cachedSuggestions &&
this.cachedSuggestions.length;
},

clearCachedSuggestions: function clearCachedSuggestions() {
Expand Down
35 changes: 34 additions & 1 deletion test/unit/dataset_spec.js
Original file line number Diff line number Diff line change
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 @@ -291,7 +297,7 @@ describe('Dataset', function() {
expect(this.dataset.cachedRenderExtraArgs).toEqual([42, true, false]);
});

it('should retrieved cached results for subsequent identical queries', function() {
it('should retrieve cached results for subsequent identical queries', function() {
this.source.and.callFake(fakeGetWithSyncResults);

this.dataset.update('woah');
Expand All @@ -308,6 +314,29 @@ describe('Dataset', function() {
expect(this.dataset.getRoot()).toContainText('three');
});

it('should not retrieve cached results for subsequent identical queries if cache is disabled', function() {
this.dataset = new Dataset({
name: 'test',
source: this.source = jasmine.createSpy('source'),
cache: false,
});

this.source.and.callFake(fakeGetWithSyncResultsAndExtraParams);

this.dataset.update('woah');
expect(this.source.calls.count()).toBe(1);
expect(this.dataset.getRoot()).toContainText('one');
expect(this.dataset.getRoot()).toContainText('two');
expect(this.dataset.getRoot()).toContainText('three');

this.dataset.clear();
this.dataset.update('woah');
expect(this.source.calls.count()).toBe(2);
expect(this.dataset.getRoot()).toContainText('one');
expect(this.dataset.getRoot()).toContainText('two');
expect(this.dataset.getRoot()).toContainText('three');
});

it('should reuse render function extra params for subsequent identical queries', function() {
var spy = spyOn(this.dataset, '_render');
this.source.and.callFake(fakeGetWithSyncResultsAndExtraParams);
Expand Down Expand Up @@ -477,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 1a90da5

Please sign in to comment.