From ea219f083e487f563c948609bbff4932e2bcc724 Mon Sep 17 00:00:00 2001 From: Wei Zhu Date: Sun, 30 Oct 2016 15:40:13 +0800 Subject: [PATCH] Add some tests --- src/Select.jsx | 1 + src/util.js | 14 +++++++++----- tests/util.spec.js | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 50 insertions(+), 5 deletions(-) create mode 100644 tests/util.spec.js diff --git a/src/Select.jsx b/src/Select.jsx index 8a5a9bd1a..b056e18cc 100644 --- a/src/Select.jsx +++ b/src/Select.jsx @@ -182,6 +182,7 @@ const Select = React.createClass({ }); this.fireChange(nextValue); this.setOpenState(false, true); + this.setInputValue('', false); return; } this.setInputValue(val); diff --git a/src/util.js b/src/util.js index 010de6898..57352574e 100644 --- a/src/util.js +++ b/src/util.js @@ -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; } } @@ -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; } diff --git a/tests/util.spec.js b/tests/util.spec.js new file mode 100644 index 000000000..73aa47721 --- /dev/null +++ b/tests/util.spec.js @@ -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([]); + }); +});