Skip to content

Commit

Permalink
Clear the section/tags when empty
Browse files Browse the repository at this point in the history
When searching for an empty section or tag, revert to a latest search
  • Loading branch information
Fabio Crisci committed Jul 28, 2015
1 parent 43ef51d commit 83d5b5b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 15 deletions.
34 changes: 19 additions & 15 deletions facia-tool/public/js/widgets/autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,21 +80,25 @@ class AutoComplete extends BaseWidget {
}

type() {
this.alertMessage('searching for ' + this.filter() + '...');
this[typeBounceSym]().then(res => {
this.alertMessage(false);
if (res && res.results && res.results.length) {
this.currentPage(res.currentPage || 1);
this.totalPages(res.pages || 1);
this.suggestions(res.results);
} else {
this.alertMessage('...sorry, no ' + this.getPath() + ' found.');
}
})
.catch(ex => {
this.alertMessage(ex.message);
})
.then(() => this.emit('update'));
if (this.filter()) {
this.alertMessage('searching for ' + this.filter() + '...');
this[typeBounceSym]().then(res => {
this.alertMessage(false);
if (res && res.results && res.results.length) {
this.currentPage(res.currentPage || 1);
this.totalPages(res.pages || 1);
this.suggestions(res.results);
} else {
this.alertMessage('...sorry, no ' + this.getPath() + ' found.');
}
})
.catch(ex => {
this.alertMessage(ex.message);
})
.then(() => this.emit('update'));
} else {
this.select('');
}
}

nextPage() {
Expand Down
53 changes: 53 additions & 0 deletions facia-tool/test/public/spec/autocomplete.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,5 +273,58 @@ describe('Autocomplete', function () {
.then(done)
.catch(done.fail);
});

it('clears the search on empty filter', function (done) {
var counter = 0, widget;
this.scope({
url: CONST.apiSearchBase + '/sections?q=*',
response: function () {
counter += 1;
this.responseText = {
response: {
results: [{ id: 'one' }, {id: 'two' }]
}
};
}
});
this.ko.apply({}, true)
.then((autocompleteWidget) => {
widget = autocompleteWidget;
$('.search--filter').val('bill').change();
})
.then(() => wait.event('update', widget))
.then(() => {
expect(counter).toBe(1);
expect($('ul.suggestions li').length).toBe(2);

setTimeout(() => $('ul.suggestions li:nth(0)').click(), 10);
})
.then(() => wait.event('change', widget))
.then(evtArgument => {
expect(evtArgument).toEqual({
query: 'one',
path: 'sections',
param: 'section'
});
expect(counter).toBe(1);
expect($('.search--filter').val()).toBe('one');
})
.then(() => {
// clear the text input (it triggers a sync event)
setTimeout(() => $('.search--filter').val('').change());

return wait.event('change', widget);
})
.then(evtArgument => {
expect(counter).toBe(1);
expect(evtArgument).toEqual({
query: '',
path: 'sections',
param: 'section'
});
})
.then(done)
.catch(done.fail);
});
});
});

0 comments on commit 83d5b5b

Please sign in to comment.