From d56232af4cab214f713738ba0449350395e0763f Mon Sep 17 00:00:00 2001 From: Grant Klinsing Date: Wed, 21 Jun 2017 23:47:15 -0500 Subject: [PATCH 1/2] add defaultFilterFn tests --- tests/util.spec.js | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/tests/util.spec.js b/tests/util.spec.js index 946468fcb..a1a8a1b12 100644 --- a/tests/util.spec.js +++ b/tests/util.spec.js @@ -1,5 +1,10 @@ /* eslint-disable no-undef */ -import { includesSeparators, splitBySeparators, getValuePropValue } from '../src/util'; +import { + includesSeparators, + splitBySeparators, + getValuePropValue, + defaultFilterFn, +} from '../src/util'; describe('includesSeparators', () => { const separators = [' ', ',']; @@ -67,3 +72,25 @@ describe('getValuePropValue', () => { expect(errorSpy).not.toHaveBeenCalled(); }); }); + +describe('defaultFilterFn', () => { + function TesterClass() { + this.props = { + optionFilterProp: 'label', + }; + } + const testerInstance = new TesterClass(); + const child = { + props: { + label: 'my-val', + }, + }; + + it('returns true when input matches option value', () => { + expect(defaultFilterFn.call(testerInstance, 'my-val', child)).toBe(true); + }); + + it('returns false when input does NOT match option value', () => { + expect(defaultFilterFn.call(testerInstance, 'wrong-val', child)).toBe(false); + }); +}); From 566ef7b56e787d52848e440c05713c8a3fae0b9c Mon Sep 17 00:00:00 2001 From: Grant Klinsing Date: Wed, 21 Jun 2017 23:51:03 -0500 Subject: [PATCH 2/2] Passing bool values to filterOption should behave the same as if no values are passed --- src/FilterMixin.js | 17 ++++++++---- tests/FilterMixin.spec.js | 13 +++++++++- tests/Select.spec.js | 27 +++++++++++++++++++- tests/__snapshots__/FilterMixin.spec.js.snap | 13 ++++++++-- 4 files changed, 61 insertions(+), 9 deletions(-) diff --git a/src/FilterMixin.js b/src/FilterMixin.js index 4f038aab7..4d667dbbe 100644 --- a/src/FilterMixin.js +++ b/src/FilterMixin.js @@ -13,14 +13,21 @@ export default { if (!input) { return true; } - const filterOption = ('filterOption' in this.props) ? - this.props.filterOption : defaultFilter; - if (!filterOption) { + let filterFn = this.props.filterOption; + if ('filterOption' in this.props) { + if (this.props.filterOption === true) { + filterFn = defaultFilter; + } + } else { + filterFn = defaultFilter; + } + + if (!filterFn) { return true; } else if (child.props.disabled) { return false; - } else if (typeof filterOption === 'function') { - return filterOption.call(this, input, child); + } else if (typeof filterFn === 'function') { + return filterFn.call(this, input, child); } return true; }, diff --git a/tests/FilterMixin.spec.js b/tests/FilterMixin.spec.js index af6667922..e6ddf82ec 100644 --- a/tests/FilterMixin.spec.js +++ b/tests/FilterMixin.spec.js @@ -125,6 +125,17 @@ describe('FilterMixin', () => { expect(wrapper).toMatchSnapshot(); }); + it('does not filter when filterOption value is false', () => { + const wrapper = render( + + ); + + expect(wrapper).toMatchSnapshot(); + }); + describe('tag mode', () => { it('renders unlisted item in value', () => { const wrapper = render( @@ -137,7 +148,7 @@ describe('FilterMixin', () => { expect(wrapper).toMatchSnapshot(); }); - it('renders search value when not fount', () => { + it('renders search value when not found', () => { const wrapper = render( @@ -139,6 +139,31 @@ describe('Select', () => { expect(wrapper.find('MenuItem').props().value).toBe('1'); }); + it('should filter options when filterOption is true', () => { + const wrapper = mount( + + ); + + wrapper.find('input').simulate('change', { target: { value: '2' } }); + expect(wrapper.find('MenuItem').length).toBe(1); + expect(wrapper.find('MenuItem').props().value).toBe('2'); + }); + + it('should not filter options when filterOption is false', () => { + const wrapper = mount( + + ); + + wrapper.find('input').simulate('change', { target: { value: '1' } }); + expect(wrapper.find('MenuItem').length).toBe(2); + }); + it('specify which prop to filter', () => { const wrapper = mount(