Skip to content

Commit

Permalink
Added more tests + fixed an edge case where updateOption would not pr…
Browse files Browse the repository at this point in the history
…operly re-render items (only if the value changed as part of the update).
  • Loading branch information
brianreavis committed Aug 9, 2013
1 parent da0793d commit 98d8376
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 12 deletions.
12 changes: 2 additions & 10 deletions src/selectize.js
Original file line number Diff line number Diff line change
Expand Up @@ -1212,7 +1212,7 @@ $.extend(Selectize.prototype, {
*/
getOption: function(value) {
value = hash_key(value);
return value ? this.$dropdown_content.find('[data-selectable]').filter('[data-value="' + value.replace(/(['"])/g, '\\$1') + '"]:first') : $();
return value ? this.$dropdown_content.find('[data-selectable]').filter('[data-value="' + escape_quotes(value) + '"]:first') : $();
},

/**
Expand All @@ -1238,15 +1238,7 @@ $.extend(Selectize.prototype, {
* @returns {object}
*/
getItem: function(value) {
var i = this.items.indexOf(value);
if (i !== -1) {
if (i >= this.caretPos) i++;
var $el = $(this.$control[0].childNodes[i]);
if ($el.attr('data-value') === value) {
return $el;
}
}
return $();
return this.$control.children('[data-value="' + escape_quotes(hash_key(value)) + '"]');
},

/**
Expand Down
11 changes: 11 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ var escape_regex = function(str) {
return (str + '').replace(/([.?*+^$[\]\\(){}|-])/g, '\\$1');
};

/**
* Escapes quotation marks with backslashes. Useful
* for escaping values for use in CSS attribute selectors.
*
* @param {string} str
* @return {string}
*/
var escape_quotes = function(str) {
return str.replace(/(['"])/g, '\\$1');
};

var hook = {};

/**
Expand Down
105 changes: 103 additions & 2 deletions tests/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@
{value: 'null'},
{value: 'a'},
{value: 'b'},
{value: 'c'},
{value: '\''},
{value: '"'},
{value: '\\\''},
Expand Down Expand Up @@ -175,11 +176,15 @@
test.selectize.addItem(0);
expect(test.selectize.items.indexOf('0')).to.not.be.equal(-1);
});
it('should update DOM', function() {
test.selectize.addItem('c');
expect(test.selectize.$control.find('[data-value=c]').length).to.be.equal(1);
});
});

describe('updateOption()', function() {
before(function() {
test = setup_test('<select>', {
test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
Expand All @@ -190,14 +195,15 @@
{value: 'c'},
{value: 'd'},
{value: 'e'},
{value: 'f'},
{value: 'null'},
{value: 'undefined'},
{value: '\''},
{value: '"'},
{value: '\\\''},
{value: '\\"'},
],
items: ['e']
items: ['e','f']
});
});
after(function() {
Expand Down Expand Up @@ -235,6 +241,11 @@
expect(test.selectize.options['undefined']).to.not.have.property('test');
expect(test.selectize.options['null']).to.not.have.property('test');
});
it('should update DOM', function() {
test.selectize.updateOption('f', {value: 'f_updated'});
expect(test.selectize.$control.find('[data-value=f]').length).to.be.equal(0);
expect(test.selectize.$control.find('[data-value=f_updated]').length).to.be.equal(1);
});
});

describe('getOption()', function() {
Expand Down Expand Up @@ -289,6 +300,96 @@
expect(test.selectize.getOption(undefined).length).to.be.equal(0);
});
});

describe('getItem()', function() {
before(function() {
test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 0},
{value: 1},
{value: 'a'},
{value: 'b'},
{value: '\''},
{value: '"'},
{value: '\\\''},
{value: '\\"'},
],
items: ['0','1','a','b','\'','"','\\\'','\\"']
});
});
after(function() {
test.teardown();
});
it('should allow string values', function() {
expect(test.selectize.getItem('a')).to.be.ok;
expect(test.selectize.getItem('a').length).to.be.equal(1);
expect(test.selectize.getItem('b')).to.be.ok;
expect(test.selectize.getItem('b').length).to.be.equal(1);
});
it('should allow integer values', function() {
expect(test.selectize.getItem(0)).to.be.ok;
expect(test.selectize.getItem(0).length).to.be.equal(1);
expect(test.selectize.getItem(1)).to.be.ok;
expect(test.selectize.getItem(1).length).to.be.equal(1);
});
it('should allow values with quotation marks', function() {
expect(test.selectize.getItem('\'')).to.be.ok;
expect(test.selectize.getItem('\'').length).to.be.equal(1);
expect(test.selectize.getItem('"')).to.be.ok;
expect(test.selectize.getItem('"').length).to.be.equal(1);
});
it('should allow values with backslashes', function() {
expect(test.selectize.getItem('\\\'')).to.be.ok;
expect(test.selectize.getItem('\\\'').length).to.be.equal(1);
expect(test.selectize.getItem('\\"')).to.be.ok;
expect(test.selectize.getItem('\\"').length).to.be.equal(1);
});
it('should not allow undefined / null values', function() {
expect(test.selectize.getItem(null)).to.be.ok;
expect(test.selectize.getItem(null).length).to.be.equal(0);
expect(test.selectize.getItem(undefined)).to.be.ok;
expect(test.selectize.getItem(undefined).length).to.be.equal(0);
});
});

describe('clear()', function() {
before(function() {
test = setup_test('<select multiple>', {
valueField: 'value',
labelField: 'value',
options: [
{value: 0},
{value: 1},
{value: 2},
{value: 3},
],
items: ['1','2','3']
});
});
after(function() {
test.teardown();
});
it('should empty "items" array', function() {
test.selectize.clear();
expect(test.selectize.items.length).to.be.equal(0);
});
it('should update DOM', function() {
test.selectize.clear();
expect(test.selectize.$control.find('[data-value=1]').length).to.be.equal(0);
expect(test.selectize.$control.find('[data-value=2]').length).to.be.equal(0);
expect(test.selectize.$control.find('[data-value=3]').length).to.be.equal(0);
});
it('should not give control focus', function(done) {
test.selectize.clear();
window.setTimeout(function() {
expect(test.selectize.isFocused).to.be.equal(false);
done();
}, 0);
});
});

});

})();

0 comments on commit 98d8376

Please sign in to comment.