From 7b4918e28c5c40096bfef6c5b70cb4af3519c505 Mon Sep 17 00:00:00 2001 From: Neto Chaves Date: Sat, 11 Apr 2020 16:44:38 -0300 Subject: [PATCH 1/2] test: add test cases for createFilterOptions --- .../useAutocomplete/useAutocomplete.test.js | 127 ++++++++++++++++++ 1 file changed, 127 insertions(+) diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.test.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.test.js index 3718b6b421192a..7594412425ea6b 100644 --- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.test.js +++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.test.js @@ -51,4 +51,131 @@ describe('createFilterOptions', () => { ]); }); }); + + describe('option: limit', () => { + it('limits the number of suggested options to be shown', () => { + const filterOptions = createFilterOptions({ limit: 2 }); + + const getOptionLabel = (option) => option.name; + const options = [ + { + id: '1234', + name: 'a1', + }, + { + id: '5678', + name: 'a2', + }, + { + id: '9abc', + name: 'a3', + }, + { + id: '9abc', + name: 'a4', + }, + ]; + + expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal([ + options[0], + options[1], + ]); + }); + }); + + describe('option: matchFrom', () => { + let filterOptions; + let getOptionLabel; + let options; + beforeEach(() => { + filterOptions = createFilterOptions({ matchFrom: 'any' }); + getOptionLabel = (option) => option.name; + options = [ + { + id: '1234', + name: 'ab', + }, + { + id: '5678', + name: 'ba', + }, + { + id: '9abc', + name: 'ca', + }, + ]; + }); + describe('any', () => { + it('show all results that match', () => { + expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal(options); + }); + }); + describe('start', () => { + it('show only results that start with search', () => { + expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal(options); + }); + }); + }); + + describe('option: ignoreAccents', () => { + it('does not ignore accents', () => { + const filterOptions = createFilterOptions({ ignoreAccents: false }); + + const getOptionLabel = (option) => option.name; + const options = [ + { + id: '1234', + name: 'áb', + }, + { + id: '5678', + name: 'ab', + }, + { + id: '9abc', + name: 'áe', + }, + { + id: '9abc', + name: 'ae', + }, + ]; + + expect(filterOptions(options, { inputValue: 'á', getOptionLabel })).to.deep.equal([ + options[0], + options[2], + ]); + }); + }); + + describe('option: ignoreCase', () => { + it('matches results with case insensitive', () => { + const filterOptions = createFilterOptions({ ignoreCase: false }); + + const getOptionLabel = (option) => option.name; + const options = [ + { + id: '1234', + name: 'Ab', + }, + { + id: '5678', + name: 'ab', + }, + { + id: '9abc', + name: 'Ae', + }, + { + id: '9abc', + name: 'ae', + }, + ]; + + expect(filterOptions(options, { inputValue: 'A', getOptionLabel })).to.deep.equal([ + options[0], + options[2], + ]); + }); + }); }); From 6afe84d534dcef829fa5812cc771ede99e58aa79 Mon Sep 17 00:00:00 2001 From: Olivier Tassinari Date: Sat, 11 Apr 2020 23:00:17 +0200 Subject: [PATCH 2/2] Update useAutocomplete.test.js --- .../material-ui-lab/src/useAutocomplete/useAutocomplete.test.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.test.js b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.test.js index 7594412425ea6b..3c973a8e7260da 100644 --- a/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.test.js +++ b/packages/material-ui-lab/src/useAutocomplete/useAutocomplete.test.js @@ -105,11 +105,13 @@ describe('createFilterOptions', () => { }, ]; }); + describe('any', () => { it('show all results that match', () => { expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal(options); }); }); + describe('start', () => { it('show only results that start with search', () => { expect(filterOptions(options, { inputValue: 'a', getOptionLabel })).to.deep.equal(options);