diff --git a/package.json b/package.json index ad46841..0739813 100644 --- a/package.json +++ b/package.json @@ -35,7 +35,7 @@ "babel" ], "testPathIgnorePatterns": [ - "src/__tests__/utils" + "src/__tests__/test-utils" ] }, "lint-staged": { diff --git a/src/PowerSelect/__tests__/PowerSelect-test.js b/src/PowerSelect/__tests__/PowerSelect-test.js index de7045d..0f9b0e0 100644 --- a/src/PowerSelect/__tests__/PowerSelect-test.js +++ b/src/PowerSelect/__tests__/PowerSelect-test.js @@ -1,8 +1,8 @@ // /* global describe, it, expect */ import React from 'react'; import sinon from 'sinon'; -import PageObjectBase from '../../__tests__/utils/create-page-object'; -import { frameworks, countries, KEY_CODES } from '../../__tests__/utils/constants'; +import PageObjectBase from '../../__tests__/test-utils/create-page-object'; +import { frameworks, countries, KEY_CODES } from '../../__tests__/test-utils/constants'; import PowerSelect from '../index'; class PowerSelectPageObject extends PageObjectBase { diff --git a/src/PowerSelectMultiple/__tests__/PowerSelectMultiple-test.js b/src/PowerSelectMultiple/__tests__/PowerSelectMultiple-test.js index d34d540..dc2f7e7 100644 --- a/src/PowerSelectMultiple/__tests__/PowerSelectMultiple-test.js +++ b/src/PowerSelectMultiple/__tests__/PowerSelectMultiple-test.js @@ -1,8 +1,8 @@ // /* global describe, it, expect */ import React from 'react'; import sinon from 'sinon'; -import PageObjectBase from '../../__tests__/utils/create-page-object'; -import { frameworks, countries, KEY_CODES } from '../../__tests__/utils/constants'; +import PageObjectBase from '../../__tests__/test-utils/create-page-object'; +import { frameworks, countries, KEY_CODES } from '../../__tests__/test-utils/constants'; import PowerSelectMultiple from '../index'; class PowerSelectMultiplePageObject extends PageObjectBase { diff --git a/src/TypeAhead/__tests__/TypeAhead-test.js b/src/TypeAhead/__tests__/TypeAhead-test.js index be55908..58a11e2 100644 --- a/src/TypeAhead/__tests__/TypeAhead-test.js +++ b/src/TypeAhead/__tests__/TypeAhead-test.js @@ -1,8 +1,8 @@ // /* global describe, it, expect */ import React from 'react'; import sinon from 'sinon'; -import PageObjectBase from '../../__tests__/utils/create-page-object'; -import { frameworks, countries, KEY_CODES } from '../../__tests__/utils/constants'; +import PageObjectBase from '../../__tests__/test-utils/create-page-object'; +import { frameworks, countries, KEY_CODES } from '../../__tests__/test-utils/constants'; import TypeAhead from '../index'; class TypeAheadPageObject extends PageObjectBase { diff --git a/src/__tests__/utils/constants.js b/src/__tests__/test-utils/constants.js similarity index 100% rename from src/__tests__/utils/constants.js rename to src/__tests__/test-utils/constants.js diff --git a/src/__tests__/utils/create-page-object.js b/src/__tests__/test-utils/create-page-object.js similarity index 100% rename from src/__tests__/utils/create-page-object.js rename to src/__tests__/test-utils/create-page-object.js diff --git a/src/__tests__/utils/matcher-test.js b/src/__tests__/utils/matcher-test.js new file mode 100644 index 0000000..7bae4f1 --- /dev/null +++ b/src/__tests__/utils/matcher-test.js @@ -0,0 +1,56 @@ +// /* global describe, it, expect */ +import { matcher } from './../../utils'; + +describe('matcher()', () => { + it('should match string', () => { + const option = 'option'; + expect(matcher({ option, searchTerm: '' })).toBe(true); + expect(matcher({ option, searchTerm: 'opt' })).toBe(true); + expect(matcher({ option, searchTerm: 'opt ' })).toBe(true); + expect(matcher({ option, searchTerm: 'a' })).toBe(false); + }); + it('should match number', () => { + const option = 1234567; + expect(matcher({ option, searchTerm: '' })).toBe(true); + expect(matcher({ option, searchTerm: '12' })).toBe(true); + expect(matcher({ option, searchTerm: '12 ' })).toBe(true); + expect(matcher({ option, searchTerm: '9' })).toBe(false); + }); + it('should match not undefined', () => { + const option = undefined; + expect(matcher({ option, searchTerm: '' })).toBe(false); + expect(matcher({ option, searchTerm: 'opt' })).toBe(false); + expect(matcher({ option, searchTerm: '12' })).toBe(false); + }); + it('should match not null', () => { + const option = null; + expect(matcher({ option, searchTerm: '' })).toBe(false); + expect(matcher({ option, searchTerm: 'opt' })).toBe(false); + expect(matcher({ option, searchTerm: '12' })).toBe(false); + }); + + const option = { name: 'Matthew Stevens', age: 6, id: null }; + it('should match string in object', () => { + expect(matcher({ option, searchTerm: '', searchIndices: 'name' })).toBe(true); + expect(matcher({ option, searchTerm: 'mAt', searchIndices: 'name' })).toBe(true); + expect(matcher({ option, searchTerm: 'mat', searchIndices: 'name' })).toBe(true); + expect(matcher({ option, searchTerm: 'mat ', searchIndices: 'name' })).toBe(true); + expect(matcher({ option, searchTerm: 'b', searchIndices: 'name' })).toBe(false); + }); + it('should match number in object', () => { + expect(matcher({ option, searchTerm: '', searchIndices: 'age' })).toBe(true); + expect(matcher({ option, searchTerm: '6', searchIndices: 'age' })).toBe(true); + expect(matcher({ option, searchTerm: '6 ', searchIndices: 'age' })).toBe(true); + expect(matcher({ option, searchTerm: '1', searchIndices: 'age' })).toBe(false); + }); + it('should match not undefined in object', () => { + expect(matcher({ option, searchTerm: '', searchIndices: 'gender' })).toBe(false); + expect(matcher({ option, searchTerm: 'opt', searchIndices: 'gender' })).toBe(false); + expect(matcher({ option, searchTerm: '12', searchIndices: 'gender' })).toBe(false); + }); + it('should match not null in object', () => { + expect(matcher({ option, searchTerm: '', searchIndices: 'id' })).toBe(false); + expect(matcher({ option, searchTerm: 'opt', searchIndices: 'id' })).toBe(false); + expect(matcher({ option, searchTerm: '12', searchIndices: 'id' })).toBe(false); + }); +}); diff --git a/src/utils.js b/src/utils.js index e2454b1..376784b 100644 --- a/src/utils.js +++ b/src/utils.js @@ -2,17 +2,31 @@ import React, { isValidElement, cloneElement } from 'react'; export const matcher = ({ option, searchTerm = '', searchIndices }) => { searchTerm = searchTerm.trim().toLowerCase(); - if (typeof option === 'string') { - return option.toLowerCase().indexOf(searchTerm) !== -1; - } + if (searchIndices) { return makeArray(searchIndices).some(index => { - return (option[index] || '').toLowerCase().indexOf(searchTerm) !== -1; + let value = option[index]; + return ( + !isNone(value) && + String(value) + .toLowerCase() + .indexOf(searchTerm) !== -1 + ); }); } + + return ( + !isNone(option) && + String(option) + .toLowerCase() + .indexOf(searchTerm) !== -1 + ); + return true; }; +export const isNone = value => value === null || value === undefined; + export const makeArray = obj => { if (obj === null || obj === undefined) { return [];