Skip to content

Commit

Permalink
Add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
yesmeck committed Oct 30, 2016
1 parent 0461293 commit ea219f0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Select.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ const Select = React.createClass({
});
this.fireChange(nextValue);
this.setOpenState(false, true);
this.setInputValue('', false);
return;
}
this.setInputValue(val);
Expand Down
14 changes: 9 additions & 5 deletions src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,8 @@ export function findFirstMenuItem(children) {
}

export function includesSeparators(string, separators) {
if (~separators.indexOf(string)) {
return false;
}
for (let i = 0; i < separators.length; ++i) {
if (~string.indexOf(separators[i])) {
if (string.lastIndexOf(separators[i]) > 0) {
return true;
}
}
Expand All @@ -118,5 +115,12 @@ export function includesSeparators(string, separators) {

export function splitBySeparators(string, separators) {
const reg = new RegExp(`[${separators.join()}]`);
return string.split(reg);
const array = string.split(reg);
if (array[0] === '') {
array.shift();
}
if (array[array.length - 1] === '') {
array.pop();
}
return array;
}
40 changes: 40 additions & 0 deletions tests/util.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import expect from 'expect.js';
import { includesSeparators, splitBySeparators } from '../src/util';

describe('includesSeparators', () => {
const separators = [' ', ','];
it('return true when given includes separators', () => {
expect(includesSeparators(',foo,bar', separators)).to.be(true);
});

it('return false when given do not include separators', () => {
expect(includesSeparators('foobar', separators)).to.be(false);
});

it('return false when string only has a leading separator', () => {
expect(includesSeparators(',foobar', separators)).to.be(false);
});
});

describe('splitBySeparators', () => {
const separators = [' ', ','];
it('split given string by separators', () => {
const string = 'foo bar,baz';
expect(splitBySeparators(string, separators)).to.eql(['foo', 'bar', 'baz']);
});

it('split string with leading separator ', () => {
const string = ',foo';
expect(splitBySeparators(string, separators)).to.eql(['foo']);
});

it('split string with trailling separator', () => {
const string = 'foo,';
expect(splitBySeparators(string, separators)).to.eql(['foo']);
});

it('split a separator', () => {
const string = ',';
expect(splitBySeparators(string, separators)).to.eql([]);
});
});

0 comments on commit ea219f0

Please sign in to comment.