Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clear method #10

Merged
merged 2 commits into from
Jan 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
### v1.1.3

- Passing tests

### v1.1.2

- [bug] Clear container when update is called [#9](https://github.com/tristen/suggestions/pull/9)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "suggestions",
"version": "1.1.2",
"version": "1.1.3",
"description": "A typeahead component for inputs",
"main": "index.js",
"scripts": {
Expand Down
8 changes: 8 additions & 0 deletions src/suggestions.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ Suggestions.prototype.handleFocus = function() {
*/
Suggestions.prototype.update = function(revisedData) {
this.data = revisedData;
this.list.draw();
};

/**
* Clears data
*/
Suggestions.prototype.clear = function() {
this.data = [];
this.list.clear();
};

Expand Down
48 changes: 21 additions & 27 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@
var test = require('tape');
var Suggestions = require('../');

var keyUpEvent = document.createEvent('HTMLEvents');
keyUpEvent.initEvent('keyup', true, false);
var focusEvent = document.createEvent('HTMLEvents');
focusEvent.initEvent('focus', true, false);
var blurEvent = document.createEvent('HTMLEvents');
blurEvent.initEvent('blur', true, false);

test('basics', function(t) {
var parent = document.createElement('div');
var input = document.createElement('input');
Expand All @@ -19,12 +26,6 @@ test('basics', function(t) {

input.value = 'ear';

var keyUpEvent = document.createEvent('HTMLEvents');
keyUpEvent.initEvent('keyup', true, false);

var focusEvent = document.createEvent('HTMLEvents');
focusEvent.initEvent('focus', true, false);

input.dispatchEvent(keyUpEvent);
input.dispatchEvent(focusEvent);

Expand All @@ -38,8 +39,6 @@ test('basics', function(t) {

t.deepEqual(suggestionResults, ['bear', 'bearing'], 'populate a correct results');

var blurEvent = document.createEvent('HTMLEvents');
blurEvent.initEvent('blur', true, false);
input.dispatchEvent(blurEvent);

t.equal(suggestionsContainer.style.display, 'none', 'suggestions container hidden on blur');
Expand Down Expand Up @@ -69,12 +68,6 @@ test('options', function(t) {

var suggestionsContainer = parent.querySelector('ul');

var keyUpEvent = document.createEvent('HTMLEvents');
keyUpEvent.initEvent('keyup', true, false);

var focusEvent = document.createEvent('HTMLEvents');
focusEvent.initEvent('focus', true, false);

input.value = 'be';
input.dispatchEvent(keyUpEvent);
input.dispatchEvent(focusEvent);
Expand Down Expand Up @@ -111,12 +104,6 @@ test('Suggestion.getItemValue', function(t) {
var typeahead = new Suggestions(input, data);
typeahead.getItemValue = function(item) { return item.name; };

var keyUpEvent = document.createEvent('HTMLEvents');
keyUpEvent.initEvent('keyup', true, false);

var focusEvent = document.createEvent('HTMLEvents');
focusEvent.initEvent('focus', true, false);

input.value = 'bear';
input.dispatchEvent(keyUpEvent);
input.dispatchEvent(focusEvent);
Expand All @@ -141,12 +128,6 @@ test('Suggestion.update', function(t) {
typeahead.update(['hear', 'hearing', 'har', 'hail']);
input.value = 'hear';

var keyUpEvent = document.createEvent('HTMLEvents');
keyUpEvent.initEvent('keyup', true, false);

var focusEvent = document.createEvent('HTMLEvents');
focusEvent.initEvent('focus', true, false);

input.dispatchEvent(keyUpEvent);
input.dispatchEvent(focusEvent);

Expand All @@ -157,8 +138,21 @@ test('Suggestion.update', function(t) {
});

t.deepEqual(suggestionResults, ['hear', 'hearing'], 'data array was revised');
t.end();
});

test('Suggestion.clear', function(t) {
var parent = document.createElement('div');
var input = document.createElement('input');
parent.appendChild(input);

// Initial array of data
var data = ['bear', 'bearing', 'bar', 'ball'];

var typeahead = new Suggestions(input, data);
var suggestionsContainer = parent.querySelector('ul');

typeahead.update([]);
typeahead.clear();
input.value = 'hear';

input.dispatchEvent(keyUpEvent);
Expand Down